-
Notifications
You must be signed in to change notification settings - Fork 0
/
non-mpi-cmd.c
314 lines (251 loc) · 6.63 KB
/
non-mpi-cmd.c
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
/*
Copyright 2010 Marcus Furlong <[email protected]>
This file is part of non-mpi-cmd.
non-mpi-cmd is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 only.
non-mpi-cmd is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with non-mpi-cmd. If not, see <http://www.gnu.org/licenses/>
*/
#define _SVID_SOURCE
#include <malloc.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <dirent.h>
#include <ctype.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <mpi.h>
#define MAX_BUF 1024
#define WORK_TAG 1
#define CWD_TAG 2
#define DIE_TAG 3
extern int getopt(int argc, char* const argv[], const char *optstring);
extern char *optarg;
extern int optind, opterr, optopt;
extern int gethostname(char *name, size_t len);
static void master (char*);
static void slave (void);
void construct_cmdline (char*, char*, char*);
int change_dir (char*, struct stat);
void scan_dir (char*);
void usage (char*);
void print_output(char *);
struct file {
char* name;
LIST_ENTRY(file) neighbours;
};
LIST_HEAD(files, file) files_head;
int main (int argc, char **argv) {
int rank = 0;
int dir_flag = 0, cmd_flag = 0;
char *dir = NULL, *cmd = NULL;
int c = 0;
int nfiles = 0;
struct stat buf;
struct file *f;
opterr = 0;
LIST_INIT(&files_head);
while ((c = getopt (argc, argv, "c:d:r")) != -1)
switch (c) {
case 'd':
dir = optarg;
dir_flag = 1;
break;
case 'c':
cmd = optarg;
cmd_flag = 1;
break;
case '?':
usage (argv[0]);
}
if ((dir_flag == 0) || (optind < argc) || (cmd_flag == 0))
usage (argv[0]);
if (stat(dir, &buf) == -1) {
perror(dir);
return 1;
}
if (!S_ISDIR(buf.st_mode)) {
perror(dir);
return 1;
}
if ( chdir(dir)<0 ) {
perror(dir);
return 1;
}
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
char *exe;
char *sp;
char cmdcpy[MAX_BUF];
bzero(cmdcpy, MAX_BUF);
strcat(cmdcpy, cmd);
exe = strtok_r(cmdcpy, " ", &sp);
if (exe == NULL) {
fprintf (stderr, "[MASTER] Error: %s is not parsable.\n", cmd);
usage (argv[0]);
}
stat(exe, &buf);
if ( (S_ISREG (buf.st_mode)) && (buf.st_mode & S_IRUSR) && (buf.st_mode & S_IXUSR) ) {
fprintf (stdout, "[MASTER] Running command: %s %s\n", exe, sp);
} else {
fprintf (stderr, "[MASTER] Error: %s is not an executable file.\n", exe);
usage (argv[0]);
}
fflush(stdout);
fflush(stderr);
scan_dir (dir);
f = LIST_FIRST(&files_head);
LIST_FOREACH(f, &files_head, neighbours) {
nfiles++;
}
fprintf (stdout, "[MASTER] Processing %d files\n", nfiles);
master(cmd);
fprintf (stdout, "[MASTER] Done.\n");
fflush(stdout);
}else{
slave();
}
MPI_Finalize();
return 0;
}
void print_output(char *output) {
fprintf(stdout, "%s", output);
fflush(stdout);
}
static void master (char* cmd) {
char fullcmd[MAX_BUF];
char hostname[HOST_NAME_MAX];
int size, rank;
MPI_Status status;
char result[MAX_BUF];
struct file* f = LIST_FIRST(&files_head);;
bzero(hostname,HOST_NAME_MAX-1);
gethostname(hostname,HOST_NAME_MAX-1);
MPI_Comm_size(MPI_COMM_WORLD, &size);
fprintf(stdout, "[MASTER] Running with %d processes on %s\n", size, hostname);
if (size == 1) {
LIST_FOREACH(f, &files_head, neighbours) {
construct_cmdline(cmd, f->name, fullcmd);
FILE *output = popen(fullcmd, "r");
char buffer[MAX_BUF];
fgets(buffer, sizeof(buffer), output);
pclose(output);
bzero(result, MAX_BUF);
strcat(result, "[MASTER] ");
strcat(result, hostname);
strcat(result, ": ");
strcat(result, buffer);
print_output(result);
}
}else{
for (rank = 1; rank < size; ++rank, f=LIST_NEXT(f, neighbours)) {
construct_cmdline(cmd, f->name, fullcmd);
MPI_Send(fullcmd, strlen(fullcmd)+1, MPI_CHAR, rank, WORK_TAG, MPI_COMM_WORLD);
}
LIST_FOREACH(f, &files_head, neighbours) {
if (rank == 1) {
MPI_Recv(&result, MAX_BUF+1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
print_output(result);
construct_cmdline(cmd, f->name, fullcmd);
MPI_Send(fullcmd, strlen(fullcmd)+1, MPI_CHAR, status.MPI_SOURCE, WORK_TAG, MPI_COMM_WORLD);
}else{
rank--;
}
}
for (rank = 1; rank < size; ++rank) {
MPI_Recv(&result, MAX_BUF+1, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
print_output(result);
}
for (rank = 1; rank < size; ++rank)
MPI_Send(0, 0, MPI_INT, rank, DIE_TAG, MPI_COMM_WORLD);
}
}
static void slave (void) {
char fullcmd[MAX_BUF];
MPI_Status status;
char result[MAX_BUF];
char hostname[HOST_NAME_MAX];
bzero(hostname,HOST_NAME_MAX-1);
gethostname(hostname,HOST_NAME_MAX-1);
while(1){
MPI_Recv(fullcmd, sizeof(fullcmd), MPI_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
if (status.MPI_TAG == DIE_TAG)
return;
FILE *output = popen(fullcmd, "r");
char buffer[MAX_BUF];
fgets(buffer, sizeof(buffer), output);
pclose(output);
bzero(result, MAX_BUF);
strcat(result, "[SLAVE] ");
strcat(result, hostname);
strcat(result, ": ");
strcat(result, buffer);
MPI_Send(&result, strlen(result)+1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
}
}
void construct_cmdline (char* cmd, char *filename, char* fullcmd) {
bzero(fullcmd, MAX_BUF);
strcat(fullcmd, cmd);
strcat(fullcmd, " ");
strcat(fullcmd, filename);
}
int change_dir (char *dir, struct stat buf) {
if (stat(dir, &buf) == -1) {
perror(dir);
return 1;
}
if (!S_ISDIR(buf.st_mode)) {
perror(dir);
return 1;
}
if ( chdir(dir)<0 ) {
perror(dir);
return 1;
}
return 0;
}
void scan_dir (char *dir) {
int i, n;
char tempfile[MAX_BUF];
struct dirent **namelist;
struct stat buf;
n = scandir (dir, &namelist, 0, alphasort);
if (n <= 0)
perror ("scandir");
else {
i = 2;
strcat (dir, "/");
while (i < n) {
strcpy (tempfile, dir);
strcat (tempfile, namelist[i]->d_name);
stat (tempfile, &buf);
if (S_ISREG (buf.st_mode)){
struct file *f;
f = malloc(sizeof(*f));
f->name = malloc(sizeof(char[MAX_BUF]));
bzero(f->name, MAX_BUF);
strcat(f->name, namelist[i]->d_name);
// fprintf(stdout, "[SCAN_DIR] %s\n", f->name);
LIST_INSERT_HEAD(&files_head, f, neighbours);
}
i++;
}
while (n>0)
free (namelist[--n]);
free (namelist);
}
}
void usage (char *progname) {
fprintf (stderr, "Usage: %s -c COMMAND -d DIRECTORY\n", progname );
exit (1);
}