-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanwork.c
189 lines (157 loc) · 4.76 KB
/
manwork.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <mpi.h>
#include <pthread.h>
#include "lqueue.h"
#include "manwork.h"
#define mpisend(MSG, PID) {\
MPI_Send(MSG, sizeof(buf), MPI_CHAR, PID, 26, MPI_COMM_WORLD);\
}
#define mpirecv(buf, source) {\
MPI_Recv(buf, sizeof(buf), MPI_CHAR, source, MPI_ANY_TAG, MPI_COMM_WORLD, &status);\
}
struct request {
uint8_t done;
char payload[511];
};
struct response {
uint8_t terminated;
uint8_t hasResult;
char result[511];
};
struct generatorPackage {
void (*generator)(void *payload, lqueue_t *qjobs);
void *payload;
lqueue_t *qjobs;
int *finished;
};
void manager(int np, void (*generator)(void *payload, lqueue_t *qjobs),
void *payload, lqueue_t *qresult);
void worker(void (*process)(void *payload, void *result));
void *
generatorWrapper(void *argument)
{
struct generatorPackage *genPac = (struct generatorPackage *)argument;
genPac->generator(genPac->payload, genPac->qjobs);
*genPac->finished = 1;
return NULL;
}
void
mpigo(int argc, char** argv,
void (*generator)(void *payload, lqueue_t *qjobs), void *payload,
void (*process)(void *payload, void *result),
void (*display)(lqueue_t *qresult, int np))
{
int np;
int pid;
lqueue_t *qresult;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &np);
MPI_Comm_rank(MPI_COMM_WORLD, &pid);
if (pid == 0) {
qresult = lqopen();
manager(np, generator, payload, qresult);
display(qresult, np);
}
else
worker(process);
MPI_Finalize();
}
void
manager(int np, void (*generator)(void *payload, lqueue_t *qjobs),
void *payload, lqueue_t *qresult)
{
MPI_Status status;
lqueue_t *qjobs = lqopen();
pthread_t *gen = calloc(1, sizeof(pthread_t));
// generate jobs
int finished = 0;
struct generatorPackage *genPac = calloc(1, sizeof(struct generatorPackage));
genPac->generator = generator;
genPac->payload = payload;
genPac->qjobs = qjobs;
genPac->finished = &finished;
pthread_create(gen, NULL, &generatorWrapper, (void *)genPac);
char buf[512];
struct request *req;
struct report *rep;
int numProcessDone = 0;
void *job;
int checkGen = 1;
do {
job = lqget(qjobs);
if (checkGen && job == NULL) {
if (finished) {
pthread_join(*gen, NULL);
checkGen = 0;
}
continue;
}
req = calloc(1, sizeof(struct request));
memset(buf, 0, sizeof(buf));
mpirecv(buf, MPI_ANY_SOURCE);
if (((struct response *)buf)->terminated)
numProcessDone++; // count how many have finished
if (((struct response *)buf)->hasResult) {
rep = calloc(1, sizeof(struct report));
rep->pid = status.MPI_SOURCE;
memcpy(rep->result, ((struct response *)buf)->result, sizeof(rep->result));
lqput(qresult, rep);
}
if (job != NULL) { // not done
req->done = 0;
memcpy(req->payload, job, sizeof(req->payload));
mpisend(req, status.MPI_SOURCE);
}
else { // all done
req->done = 1;
mpisend(req, status.MPI_SOURCE);
}
} while (numProcessDone < np - 1);
}
void
worker(void (*process)(void *payload, void *result)) {
char buf[512];
char temp[512];
MPI_Status status;
struct response *resp = calloc(1, sizeof(struct response));
void *result;
int hasResult = 0;
int shouldExit = 0;
((struct response *)buf)->hasResult = 0; // first ever
((struct response *)buf)->terminated = 0;
mpisend(buf, 0);
do {
mpirecv(buf, 0);
if (((struct request *)buf)->done) {
if (!hasResult) shouldExit = 1;
else {
mpisend(resp, 0);
hasResult = 0;
}
}
else {
if (hasResult) {
mpisend(resp, 0);
hasResult = 0;
}
else {
((struct response *)temp)->hasResult = 0;
((struct response *)temp)->terminated = 0;
mpisend(temp, 0);
}
result = calloc(1, sizeof(resp->result));
memset(result, 0, sizeof(resp->result));
process(((struct request *)buf)->payload, result);
resp = calloc(1, sizeof(struct response));
resp->hasResult = hasResult = 1;
memcpy(resp->result, result, sizeof(resp->result));
}
} while (!shouldExit);
((struct response *)buf)->hasResult = 0;
((struct response *)buf)->terminated = 1;
mpisend(buf, 0);
}