-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpiengine.c
113 lines (96 loc) · 2.98 KB
/
mpiengine.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
#include "jobfork.h"
#include <mpi.h>
void mpi_manager(const int nworker, int *nsent) {
int i, n, numsent, numrecv, sender, irecv, job_status;
char *cmd;
MPI_Status status;
/* send one job to each worker */
n = (nworker < cstat.num) ? nworker : cstat.num;
numsent = numrecv = 0;
for (i = 0; i < n; i++) {
cmd = cstat.cmd + numsent * cstat.len;
printf("-> Allocating command to task %d (job index: %d):\n %s\n",
i + 1, nsent[i], cmd);
#ifdef FLUSH_STDOUT
fflush(stdout);
#endif
MPI_Send(nsent + i, 1, MPI_INT, i + 1, 0, MPI_COMM_WORLD);
MPI_Send(cmd, cstat.len, MPI_CHAR, i + 1, numsent, MPI_COMM_WORLD);
numsent++;
nsent[i] += 1;
}
/* send jobs to free workers */
for (i = numsent; i < cstat.num; i++) {
MPI_Recv(&job_status, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
sender = status.MPI_SOURCE;
irecv = status.MPI_TAG;
cstat.status[irecv] = job_status;
numrecv++;
cmd = cstat.cmd + i * cstat.len;
printf("-> Allocating command to task %d (job index: %d):\n %s\n",
sender, nsent[sender - 1], cmd);
#ifdef FLUSH_STDOUT
fflush(stdout);
#endif
MPI_Send(nsent + sender - 1, 1, MPI_INT, sender, 0, MPI_COMM_WORLD);
MPI_Send(cmd, cstat.len, MPI_CHAR, sender, numsent, MPI_COMM_WORLD);
numsent++;
nsent[sender - 1] += 1;
}
/* no more jobs to be sent */
i = -1;
while (numrecv < cstat.num) {
MPI_Recv(&job_status, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
sender = status.MPI_SOURCE;
irecv = status.MPI_TAG;
cstat.status[irecv] = job_status;
numrecv++;
MPI_Send(&i, 1, MPI_INT, sender, 0, MPI_COMM_WORLD);
}
}
void mpi_worker(char *cmd, CHILD_INFO *ci) {
int idx = 0, cnt, job_status, ret, myrank;
char line[CMD_BUF];
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Recv(&cnt, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
while (cnt >= 0) {
MPI_Recv(cmd, cstat.len, MPI_CHAR, 0, MPI_ANY_TAG,
MPI_COMM_WORLD, &status);
idx = status.MPI_TAG;
job_status = JOB_START;
if ((ret = create_child(cmd, ci))) {
MSG_ERR("failed to execute command `%s'.\n", cmd);
job_status = JOB_FAIL;
}
else {
memset(line, 0, CMD_BUF);
while (fgets(line, CMD_BUF, ci->out) != NULL) {
MSG_CHILD_STDOUT(myrank, cnt, line);
#ifdef FLUSH_STDOUT
fflush(stdout);
#endif
memset(line, 0, CMD_BUF);
}
while (fgets(line, CMD_BUF, ci->err) != NULL) {
MSG_CHILD_STDERR(myrank, cnt, line);
#ifdef FLUSH_STDOUT
fflush(stderr);
#endif
memset(line, 0, CMD_BUF);
}
if ((ret = close_child(ci))) {
MSG_ERR("unable to finish command `%s'.\n", cmd);
#ifdef FLUSH_STDOUT
fflush(stdout);
#endif
job_status = JOB_FAIL;
}
else job_status = JOB_DONE;
}
MPI_Send(&job_status, 1, MPI_INT, 0, idx, MPI_COMM_WORLD);
MPI_Recv(&cnt, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
}
}