Debian Codegraph @

src/ffindex_apply_mpi.c

raw
1 /*
2 * FFindex
3 * written by Andreas Hauser <[email protected]>.
4 * Please add your name here if you distribute modified versions.
5 *
6 * FFindex is provided under the Create Commons license "Attribution-ShareAlike
7 * 4.0", which basically captures the spirit of the Gnu Public License (GPL).
8 *
9 * See:
10 * http://creativecommons.org/licenses/by-sa/4.0/
11 *
12 * ffindex_apply
13 * apply a program to each FFindex entry
14 */
15
16 #define _GNU_SOURCE 1
17 #define _LARGEFILE64_SOURCE 1
18 #define _FILE_OFFSET_BITS 64
19
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <signal.h>
29
30 #ifdef HAVE_MPI
31 # include <mpi.h>
32 #endif
33
34
35 #include "ffindex.h"
36 #include "ffutil.h"
37
38 char* read_buffer;
39
40 int ffindex_apply_by_entry(char *data, ffindex_index_t* index, ffindex_entry_t* entry, char* program_name, char** program_argv, FILE* data_file_out, FILE* index_file_out, size_t *offset, FILE* log_file)
41 {
42 int ret = 0;
43 int capture_stdout = (data_file_out != NULL);
44
45 int pipefd_stdin[2];
46 int pipefd_stdout[2];
47
48 ret = pipe(pipefd_stdin);
49 if(ret != 0) { fprintf(stderr, "ERROR in pipe stdin!\n"); perror(entry->name); return errno; }
50
51 if(capture_stdout)
52 {
53 ret = pipe(pipefd_stdout);
54 if(ret != 0) { fprintf(stderr, "ERROR in pipe stdout!\n"); perror(entry->name); return errno; }
55 }
56
57 // Flush so child doesn't copy and also flushes, leading to duplicate output
58 fflush(data_file_out);
59 fflush(index_file_out);
60
61 pid_t child_pid = fork();
62
63 if(child_pid == 0)
64 {
65 close(pipefd_stdin[1]);
66 if(capture_stdout)
67 {
68 fclose(data_file_out);
69 fclose(index_file_out);
70 close(pipefd_stdout[0]);
71 }
72
73 // Make pipe from parent our new stdin
74 int newfd_in = dup2(pipefd_stdin[0], fileno(stdin));
75 if(newfd_in < 0) { fprintf(stderr, "ERROR in dup2 in %d %d\n", pipefd_stdin[0], newfd_in); perror(entry->name); }
76 close(pipefd_stdin[0]);
77
78 if(capture_stdout)
79 {
80 int newfd_out = dup2(pipefd_stdout[1], fileno(stdout));
81 if(newfd_out < 0) { fprintf(stderr, "ERROR in dup2 out %d %d\n", pipefd_stdout[1], newfd_out); perror(entry->name); }
82 close(pipefd_stdout[1]);
83 }
84
85 setenv("FFINDEX_ENTRY_NAME", entry->name, 1);
86
87 // exec program with the pipe as stdin
88 execvp(program_name, program_argv);
89 // never reached
90 }
91 else if(child_pid > 0)
92 {
93 // parent writes to and possible reads from child
94
95 int flags = 0;
96
97 // Read end is for child only
98 close(pipefd_stdin[0]);
99
100 if(capture_stdout)
101 close(pipefd_stdout[1]);
102
103 char *filedata = ffindex_get_data_by_entry(data, entry);
104
105 if(capture_stdout)
106 {
107 flags = fcntl(pipefd_stdout[0], F_GETFL, 0);
108 fcntl(pipefd_stdout[0], F_SETFL, flags | O_NONBLOCK);
109 }
110
111 // Write file data to child's stdin.
112 ssize_t written = 0;
113 size_t to_write = entry->length - 1; // Don't write ffindex trailing '\0'
114 char* b = read_buffer;
115 while(written < to_write)
116 {
117 size_t rest = to_write - written;
118 int batch_size = PIPE_BUF;
119 if(rest < PIPE_BUF)
120 batch_size = rest;
121
122 ssize_t w = write(pipefd_stdin[1], filedata + written, batch_size);
123 if(w < 0 && errno != EPIPE)
124 { fprintf(stderr, "ERROR in child!\n"); perror(entry->name); break; }
125 else
126 written += w;
127
128 if(capture_stdout)
129 {
130 // To avoid blocking try to read some data
131 ssize_t r = read(pipefd_stdout[0], b, PIPE_BUF);
132 if(r > 0)
133 b += r;
134 }
135 }
136 close(pipefd_stdin[1]); // child gets EOF
137
138 if(capture_stdout)
139 {
140 // Read rest
141 fcntl(pipefd_stdout[0], F_SETFL, flags); // Remove O_NONBLOCK
142 ssize_t r;
143 while((r = read(pipefd_stdout[0], b, PIPE_BUF)) > 0)
144 b += r;
145 close(pipefd_stdout[0]);
146
147 ffindex_insert_memory(data_file_out, index_file_out, offset, read_buffer, b - read_buffer, entry->name);
148 }
149
150 int status;
151 waitpid(child_pid, &status, 0);
152 if(log_file != NULL)
153 fprintf(log_file, "%s\t%ld\t%ld\t%d\n", entry->name, entry->offset, entry->length, WEXITSTATUS(status));
154 }
155 else
156 {
157 fprintf(stderr, "ERROR in fork()\n");
158 perror(entry->name);
159 return errno;
160 }
161 return EXIT_SUCCESS;
162 }
163
164 int main(int argn, char **argv)
165 {
166 int mpi_rank,
167 mpi_num_procs;
168
169 #ifdef HAVE_MPI
170 int mpi_error;
171 mpi_error = MPI_Init(&argn, &argv); if(mpi_error) goto EXCEPTION;
172 mpi_error = MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); if(mpi_error) goto EXCEPTION;
173 mpi_error = MPI_Comm_size(MPI_COMM_WORLD, &mpi_num_procs); if(mpi_error) goto EXCEPTION;
174 #else
175 mpi_rank = 0;
176 mpi_num_procs = 1;
177 #endif
178
179 int opt, merge = 1;
180 char *data_filename_out = NULL,
181 *index_filename_out = NULL,
182 *log_filename = NULL;
183
184 while ((opt = getopt(argn, argv, "d:i:mql:")) != -1)
185 {
186 switch (opt)
187 {
188 case 'l':
189 log_filename = optarg;
190 break;
191 case 'm':
192 merge = 0;
193 break;
194 case 'd':
195 data_filename_out = optarg;
196 break;
197 case 'i':
198 index_filename_out = optarg;
199 break;
200 }
201 }
202
203 if(argn - optind < 3)
204 {
205 fprintf(stderr, "ERROR Not enough arguments.\n\n");
206
207 fprintf(stderr, "USAGE: %s [-m] [-q] [-l LOG_FILE] -d DATA_FILENAME_OUT -i INDEX_FILENAME_OUT DATA_FILENAME INDEX_FILENAME -- PROGRAM [PROGRAM_ARGS]*\n"
208 "\t-l\tLOG_FILE like an FFindex file but with the return value of apply added.\n"
209 "\t\tThis file can be processed and used as FFindex file.\n"
210 "\t\tE.g. grep \"\t1$\" foo.log | cut -f 1-3 > foo-special.ffindex.\n"
211 "\t\twhere maybe the %s ran with -- perl -e 'return 1 if(/^SPECIAL/)'\n"
212 "\t-m\tDo not merge the FFindex parts generated by the different MPI processes\n"
213 "\t\tThis is useful for large MPI Jobs where merge time might be accounted.\n"
214 "\t-d\tFFDATA output file, stdout of program will be captured\n"
215 "\t-i\tFFINDEX output file, entries get the same name as input entry\n"
216 FFINDEX_COPYRIGHT,
217 basename(argv[0]),
218 basename(argv[0]));
219 errno = EINVAL;
220 goto EXCEPTION;
221 }
222 read_buffer = malloc(400 * 1024 * 1024);
223 char *data_filename = argv[optind++];
224 char *index_filename = argv[optind++];
225 char *program_name = argv[optind];
226 char **program_argv = argv + optind;
227
228 FILE *data_file = fopen(data_filename, "r");
229 FILE *index_file = fopen(index_filename, "r");
230
231 if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); }
232 if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); }
233
234 // Setup one output FFindex for each MPI process
235 FILE *data_file_out = NULL,
236 *index_file_out = NULL;
237 if(data_filename_out != NULL && index_filename_out != NULL)
238 {
239 char* data_filename_out_rank = malloc(FILENAME_MAX);
240 char* index_filename_out_rank = malloc(FILENAME_MAX);
241 snprintf( data_filename_out_rank, FILENAME_MAX, "%s.%d", data_filename_out, mpi_rank);
242 snprintf(index_filename_out_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank);
243 data_file_out = fopen(data_filename_out_rank, "w+");
244 index_file_out = fopen(index_filename_out_rank, "w+");
245 if( data_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename_out); exit(EXIT_FAILURE); }
246 if(index_file_out == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename_out); exit(EXIT_FAILURE); }
247 }
248
249 FILE *log_file = NULL;
250 // Setup log file
251 if(log_filename)
252 {
253 char* log_filename_rank = malloc(FILENAME_MAX);
254 snprintf(log_filename_rank, FILENAME_MAX, "%s.%d", index_filename_out, mpi_rank);
255 log_file = fopen(log_filename_rank, "w+");
256 if( log_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], log_filename_rank); exit(EXIT_FAILURE); }
257 }
258
259 int capture_stdout = (data_file_out != NULL);
260
261 size_t data_size;
262 char *data = ffindex_mmap_data(data_file, &data_size);
263
264 ffindex_index_t* index = ffindex_index_parse(index_file, 0);
265 if(index == NULL)
266 {
267 fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename);
268 #ifdef HAVE_MPI
269 MPI_Finalize();
270 #endif
271 exit(EXIT_FAILURE);
272 }
273
274 // Ignore SIGPIPE
275 struct sigaction handler;
276 handler.sa_handler = SIG_IGN;
277 sigemptyset(&handler.sa_mask);
278 handler.sa_flags = 0;
279 sigaction(SIGPIPE, &handler, NULL);
280
281 size_t batch_size, range_start, range_end;
282
283 if(index->n_entries >= mpi_num_procs)
284 batch_size = index->n_entries / mpi_num_procs;
285 else
286 batch_size = 0;
287 range_start = mpi_rank * batch_size;
288 range_end = range_start + batch_size;
289
290
291 size_t offset = 0;
292 // Foreach entry
293 if(batch_size > 0)
294 for(size_t entry_index = range_start; entry_index < range_end; entry_index++)
295 {
296 ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index);
297 if(entry == NULL) { perror(entry->name); goto EXCEPTION; }
298 int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file);
299 if(error != 0)
300 { perror(entry->name); break; }
301 }
302 ssize_t left_over = index->n_entries - (batch_size * mpi_num_procs);
303 if(mpi_rank < left_over)
304 {
305 size_t left_over_entry_index = (batch_size * mpi_num_procs) + mpi_rank;
306 ffindex_entry_t* entry = ffindex_get_entry_by_index(index, left_over_entry_index);
307 if(entry == NULL) { perror(entry->name); return errno; }
308 //fprintf(stderr, "handling left over: %ld\n", left_over_entry_index);
309 int error = ffindex_apply_by_entry(data, index, entry, program_name, program_argv, data_file_out, index_file_out, &offset, log_file);
310 if(error != 0)
311 perror(entry->name);
312 }
313
314 if(capture_stdout)
315 fclose(data_file_out);
316 if(index_file_out != NULL)
317 fclose(index_file_out);
318
319 #ifdef HAVE_MPI
320 MPI_Barrier(MPI_COMM_WORLD);
321 #endif
322
323
324 // merge FFindexes in master
325 if(mpi_rank == 0 && merge && data_filename_out != NULL)
326 {
327 char* merge_command = malloc(FILENAME_MAX * 5);
328 for(int i = 0; i < mpi_num_procs; i++)
329 {
330 snprintf( merge_command, FILENAME_MAX, "ffindex_build -as %s %s -d %s.%d -i %s.%d",
331 data_filename_out, index_filename_out, data_filename_out, i, index_filename_out, i);
332 //puts(merge_command);
333 if(system(merge_command) == 0)
334 {
335 snprintf(merge_command, FILENAME_MAX, "%s.%d", data_filename_out, i);
336 unlink(merge_command);
337 snprintf(merge_command, FILENAME_MAX, "%s.%d", index_filename_out, i);
338 unlink(merge_command);
339 }
340 else
341 {
342 fprintf(stderr, "merge command failed: %s\n", merge_command);
343 goto EXCEPTION;
344 }
345 }
346 }
347
348 #ifdef HAVE_MPI
349 MPI_Finalize();
350 #endif
351
352 return EXIT_SUCCESS;
353
354
355 EXCEPTION:
356
357 perror(argv[0]);
358 fprintf(stderr, "\nEXCEPTION ffindex_apply_mpi in rank: %d\n", mpi_rank);
359
360 #ifdef HAVE_MPI
361 MPI_Finalize();
362 #endif
363
364 return EXIT_FAILURE;
365 }
366
367 /* vim: ts=2 sw=2 et
368 */