-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathapply.cpp
More file actions
402 lines (345 loc) · 11.8 KB
/
apply.cpp
File metadata and controls
402 lines (345 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "Parameters.h"
#include "DBReader.h"
#include "DBWriter.h"
#include "Util.h"
#include "Debug.h"
#if defined(__CYGWIN__) || defined(__EMSCRIPTEN__)
int apply(int, const char **, const Command&) {
Debug(Debug::ERROR) << "\"apply\" is not supported on this platform\n";
EXIT(EXIT_FAILURE);
}
#else
#include <climits>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <spawn.h>
#include <poll.h>
#include <sys/wait.h>
#include <sys/mman.h>
#ifdef OPENMP
#include <omp.h>
#endif
pid_t create_pipe(
const char* prog_path,
char** prog_argv,
char** local_environ,
int fd[2]) {
if (prog_path == NULL) {
return -1;
}
if (prog_argv == NULL) {
return -1;
}
int pipe1Ids[2];
int res = pipe(pipe1Ids);
if (res != 0) {
perror("pipe failed");
return -1;
}
if (fcntl(pipe1Ids[0], F_SETFD, FD_CLOEXEC) == -1 || fcntl(pipe1Ids[1], F_SETFD, FD_CLOEXEC) == -1) {
perror("fcntl failed");
close(pipe1Ids[0]);
close(pipe1Ids[1]);
return -1;
}
int pipe2Ids[2];
res = pipe(pipe2Ids);
if (res != 0) {
perror("pipe failed");
close(pipe1Ids[0]);
close(pipe1Ids[1]);
return -1;
}
if (fcntl(pipe2Ids[0], F_SETFD, FD_CLOEXEC) == -1 || fcntl(pipe2Ids[1], F_SETFD, FD_CLOEXEC) == -1) {
perror("fcntl failed");
close(pipe1Ids[0]);
close(pipe1Ids[1]);
close(pipe2Ids[0]);
close(pipe2Ids[1]);
return -1;
}
posix_spawn_file_actions_t actions;
posix_spawn_file_actions_init(&actions);
if (posix_spawn_file_actions_adddup2(&actions, pipe2Ids[0], STDIN_FILENO) != 0
|| posix_spawn_file_actions_adddup2(&actions, pipe1Ids[1], STDOUT_FILENO) != 0) {
perror("posix_spawn_file_actions failed");
posix_spawn_file_actions_destroy(&actions);
close(pipe1Ids[0]);
close(pipe1Ids[1]);
close(pipe2Ids[0]);
close(pipe2Ids[1]);
return -1;
}
int pid;
res = posix_spawnp(&pid, prog_path, &actions, NULL, prog_argv, local_environ);
if (res != 0) {
perror("posix_spawn failed");
close(pipe1Ids[0]);
close(pipe1Ids[1]);
close(pipe2Ids[0]);
close(pipe2Ids[1]);
return -1;
}
posix_spawn_file_actions_destroy(&actions);
if (close(pipe2Ids[0]) != 0 || close(pipe1Ids[1]) != 0) {
perror("close");
return -1;
}
fd[0] = pipe1Ids[0];
fd[1] = pipe2Ids[1];
return pid;
}
int apply_by_entry(char* data, size_t size, unsigned int key, DBWriter& writer,
const char* program_name, char ** program_argv, char **environ, unsigned int proc_idx) {
// only works with the environ we construct ourselves
// local_environment() leaves the first element free to use for ourselves
snprintf(environ[0], 64, "MMSEQS_ENTRY_NAME=%d", key);
bool write_closed = false;
int fd[2];
pid_t child_pid;
if ((child_pid = create_pipe(program_name, program_argv, environ, fd)) == -1) {
perror("create_pipe");
return -1;
}
size_t written = 0;
int error = 0;
char buffer[PIPE_BUF];
writer.writeStart(proc_idx);
struct pollfd plist[2];
for (;;) {
size_t rest = size - written;
size_t batch_size = PIPE_BUF;
if (rest < PIPE_BUF) {
batch_size = rest;
}
plist[0].fd = write_closed == false ? fd[1] : fd[1] * -1;
plist[0].events = POLLOUT;
plist[0].revents = 0;
plist[1].fd = fd[0];
plist[1].events = POLLIN;
plist[1].revents = 0;
if (poll(plist, 2, -1) == -1) {
if (errno == EAGAIN) {
perror("again");
continue;
}
perror("poll");
error = errno;
break;
}
if (plist[0].revents & POLLOUT) {
ssize_t write_size = batch_size;
if (size > written) {
for (;;) {
ssize_t w = write(fd[1], data + written, write_size);
if (w < 0) {
if (errno != EAGAIN) {
perror("write stdin1");
error = errno;
goto end;
} else {
write_size = write_size / 2;
if (write_size == 0) {
break;
}
}
} else {
written += w;
break;
}
}
} else {
if (close(fd[1]) == -1) {
perror("close error");
error = errno;
break;
}
write_closed = true;
}
} else if (plist[1].revents & POLLIN) {
ssize_t bytes_read = read(plist[1].fd, &buffer, sizeof(buffer));
if (bytes_read > 0) {
writer.writeAdd(buffer, bytes_read, proc_idx);
} else if (bytes_read < 0) {
if (errno != EAGAIN) {
perror("read stdout0");
error = errno;
break;
}
} else if (bytes_read == 0 && write_closed == true) {
break;
}
} else {
// nothing left to read or write
break;
}
}
end:
writer.writeEnd(key, proc_idx, true);
if (write_closed == true) {
close(fd[1]);
}
if (close(fd[0]) == -1) {
perror("close stdout");
error = errno;
}
int status = 0;
while (waitpid(child_pid, &status, 0) == -1) {
if (errno == EINTR) {
continue;
}
perror("waitpid");
error = errno;
}
errno = error;
return WEXITSTATUS(status);
}
void ignore_signal(int signal) {
struct sigaction handler;
handler.sa_handler = SIG_IGN;
sigemptyset(&handler.sa_mask);
handler.sa_flags = 0;
sigaction(signal, &handler, NULL);
}
extern char **environ;
int prefix(const char *pre, const char *str) {
return strncmp(pre, str, strlen(pre)) == 0;
}
char** local_environment() {
size_t env_size = 0;
for (size_t i = 0; environ[i] != NULL; ++i) {
env_size++;
}
char** local_environ = (char**)malloc(sizeof(char*) * (env_size + 2));
size_t j = 0;
local_environ[j++] = (char*)malloc(sizeof(char) * 64);
for (size_t i = 0; environ[i] != NULL; ++i) {
if (prefix("MMSEQS_ENTRY_NAME=", environ[i]) == 0) {
local_environ[j++] = environ[i];
}
}
local_environ[j] = NULL;
return local_environ;
}
void free_local_environment(char** local_environ) {
free(local_environ[0]);
free(local_environ);
}
int apply(int argc, const char **argv, const Command& command) {
MMseqsMPI::init(argc, argv);
Parameters& par = Parameters::getInstance();
par.parseParameters(argc, argv, command, true, Parameters::PARSE_REST, 0);
#ifdef OPENMP
// forking does not play well with OpenMP threads
omp_set_num_threads(1);
#endif
DBReader<unsigned int> reader(par.db1.c_str(), par.db1Index.c_str(), par.threads, DBReader<unsigned int>::USE_DATA|DBReader<unsigned int>::USE_INDEX);
reader.open(DBReader<unsigned int>::SORT_BY_LENGTH);
Debug::Progress progress(reader.getSize());
#ifdef HAVE_MPI
struct worker_s {
int ready;
int mpiRank;
int mpiProc;
};
worker_s* shared_memory = (worker_s*)mmap(NULL, sizeof(worker_s), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
memset(shared_memory, 0, sizeof(worker_s));
#endif
Debug(Debug::INFO) << "Start applying.\n";
for (int thread = 0; thread < par.threads; ++thread) {
switch (fork()) {
default:
break;
case -1:
Debug(Debug::ERROR) << "Could not fork worker process!\n";
EXIT(EXIT_FAILURE);
case 0: {
int mpiRank = 0;
int mpiProcs = 1;
// get progress only from first thread on master
if (thread != 0 || mpiRank != MMseqsMPI::MASTER) {
Debug::setDebugLevel(0);
}
#ifdef HAVE_MPI
while (shared_memory->ready == 0) {
usleep(10);
}
mpiRank = shared_memory->mpiRank;
mpiProcs = shared_memory->mpiProc;
std::pair<std::string, std::string> mpiDb = Util::createTmpFileNames(par.db2, par.db2Index, shared_memory->mpiRank);
std::pair<std::string, std::string> outDb = Util::createTmpFileNames(mpiDb.first, mpiDb.second, thread);
#else
std::pair<std::string, std::string> outDb = Util::createTmpFileNames(par.db2, par.db2Index, thread);
#endif
DBWriter writer(outDb.first.c_str(), outDb.second.c_str(), 1, par.compressed, Parameters::DBTYPE_GENERIC_DB);
writer.open();
char **local_environ = local_environment();
ignore_signal(SIGPIPE);
for (size_t i = 0; i < reader.getSize(); ++i) {
progress.updateProgress();
if (static_cast<ssize_t>(i) % (mpiProcs * par.threads) != (thread * mpiProcs + mpiRank)) {
continue;
}
unsigned int key = reader.getDbKey(i);
char *data = reader.getData(i, thread);
if (*data == '\0') {
writer.writeData(NULL, 0, key, 0);
continue;
}
size_t size = reader.getEntryLen(i) - 1;
int status = apply_by_entry(data, size, key, writer, par.restArgv[0], const_cast<char**>(par.restArgv), local_environ, 0);
if (status == -1) {
Debug(Debug::WARNING) << "Entry " << key << " system error number " << errno << "!\n";
continue;
}
if (status > 0) {
Debug(Debug::WARNING) << "Entry " << key << " exited with error code " << status << "!\n";
continue;
}
}
writer.close(true);
reader.close();
free_local_environment(local_environ);
_Exit(0);
}
}
}
#ifdef HAVE_MPI
shared_memory->mpiRank = MMseqsMPI::rank;
shared_memory->mpiProc = MMseqsMPI::numProc;
__sync_fetch_and_add(&(shared_memory->ready), 1);
#endif
for (int proc_idx = 0; proc_idx < par.threads; ++proc_idx) {
int status = 0;
while (waitpid(-1, &status, 0) == -1) {
if (errno == EINTR) {
continue;
}
}
}
reader.close();
#ifdef HAVE_MPI
std::pair<std::string, std::string> outDb = Util::createTmpFileNames(par.db2, par.db2Index, MMseqsMPI::rank);
#else
std::pair<std::string, std::string> outDb = std::make_pair(par.db2, par.db2Index);
#endif
std::vector<std::pair<std::string, std::string>> splitFiles;
for (int proc_idx = 0; proc_idx < par.threads; ++proc_idx) {
splitFiles.emplace_back(Util::createTmpFileNames(outDb.first, outDb.second, proc_idx));
}
DBWriter::mergeResults(outDb.first, outDb.second, splitFiles);
#ifdef HAVE_MPI
MPI_Barrier(MPI_COMM_WORLD);
if (MMseqsMPI::rank == MMseqsMPI::MASTER) {
// master reduces results
std::vector<std::pair<std::string, std::string>> splitFiles;
for (int proc = 0; proc < MMseqsMPI::numProc; ++proc) {
splitFiles.emplace_back(Util::createTmpFileNames(par.db2, par.db2Index, proc));
}
DBWriter::mergeResults(par.db2, par.db2Index, splitFiles);
}
#endif
return EXIT_SUCCESS;
}
#endif