-
Notifications
You must be signed in to change notification settings - Fork 2
/
WorkerUtil.c
526 lines (392 loc) · 14.9 KB
/
WorkerUtil.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "WorkerUtil.h"
/**
* This is a set of helper functions used by the WORKER.
*/
#define MAX_LENGTH 500
extern int debug_level;
/**
* Parses the final_keys received from the PROXY
*/
void parseFinalKeysMessage(char *message, short *final_num_keys, char **final_keys[])
{
int offset;
char *p = message;
short *a;
*final_num_keys = parseShort(message);
p += 2;
if(!(*final_keys = (char**) malloc(sizeof(char*) * (*final_num_keys)))) {
systemError("Could not allocate ranks in parseFinalKeysMessage");
debug_print(debug_level, ERROR_LOG, "%s", "Could not allocate ranks in parseFinalKeysMessage");
}
for(int i = 0; i < *final_num_keys; i++)
{
if(sscanf(p, " %m[?a-zA-Z0-9_]%n", &((*final_keys)[i]), &offset) != 1)
break;
p += offset;
}
}
// parse the sub-query message received from the proxy..all parameters are populated with
// information about the current worker.
void parseSubQMessage(char *message, short *in_degree, short *out_degree, int **ranks,
short *num_keys, char **keys[], char **subQ)
{
char *p = message;
*in_degree = parseShort(p); p += 2;
*out_degree = parseShort(p); p += 2;
if(!(*ranks = (int *) malloc(sizeof(int) * (*out_degree))))
systemError("Could not allocate ranks in parseSubQMessage");
for(int i = 0; i < *out_degree; i++)
{
(*ranks)[i] = parseInt(p);
p += 4;
}
*num_keys = parseShort(p); p += 2;
*subQ = p;
if(!(*keys = (char **) malloc(sizeof(char *) * (*num_keys))))
systemError("Could not allocate results in parseSubQMessage");
p += strlen("select ");
for(int i = 0; i < *num_keys; i++)
{
int offset;
if(sscanf(p, " %m[?a-zA-Z0-9_]%n", &((*keys)[i]), &offset) != 1)
break;
p += offset;
}
}
int createMetadataMessage(char **message, char *keys[], short num_keys)
{
int message_len = 0;
for(int i = 0; i < num_keys; i++){
message_len += (sizeof(int8_t) * (strlen(keys[i]) + 1));
}
message_len += sizeof(int16_t);
message_len += sizeof(int8_t);
if(!(*message = (int8_t *) malloc(sizeof(int8_t) * (message_len)))) {
systemError("Could not allocate message in createMetadataMessage");
debug_print(debug_level, ERROR_LOG, "%s\n", "Could not allocate message in createMetadataMessage");
}
char *m = *message;
num_keys = htons(num_keys);
memcpy(m, (int8_t *)&num_keys, 2);
m += 2;
for(int i = 0; i < ntohs(num_keys); i++)
{
int len = strlen(keys[i]);
memcpy(m, (int8_t *)keys[i], len);
m += len;
memcpy(m, (int8_t *)" ", 1);
m += 1;
}
memcpy(m, (int8_t *)"\0", 1);
return message_len;
}
int createResultMessage(char **message, int worker_id)
{
int message_len = 0;
char filename[20];
char ch;
sprintf(filename, "result_%d.txt", worker_id);
FILE *stream = fopen(filename, "r");
fseek(stream, 0L, SEEK_END);
int fsize = ftell(stream);
fseek(stream, 0L, SEEK_SET);
message_len += (sizeof(int8_t) * fsize);
//message_len += sizeof(int8_t); //aisha
//if(!(*message = (int8_t *) malloc(sizeof(int8_t) * (message_len)))) { //- AISHA
if(!(*message = (int8_t *) malloc(message_len))) {
systemError("Could not allocate message in createResultMessage");
debug_print(debug_level, ERROR_LOG, "%s\n", "Could not allocate message in createResultMessage");
}
char *m = *message;
fread(m, sizeof(int8_t), fsize, stream);
fclose(stream);
m += fsize;
//memcpy(m, (int8_t *)"\0", 1); aisha
return message_len;
}
void parseMetadataMessage(char *message, short *num_keys, char **keys[])
{
char *p = message;
int offset;
*num_keys = parseShort(p); p += 2;
if(!(*keys = (char **)(malloc(sizeof(char *) * (*num_keys))))) {
systemError("Could not allocate results in parseMetadataMessage");
debug_print(debug_level, ERROR_LOG, "%s\n", "Could not allocate results in parseMetadataMessage");
}
for(int i = 0; i < *num_keys; i++)
{
if(sscanf(p, " %ms%n", &((*keys)[i]), &offset) != 1)
break;
p += offset;
}
}
void parseResultMessage(char *message, char **result_set)
{
*result_set = message;
}
// parse the result message received from the neighbor and populate the parameters with
// information about the neigbhor's query.
/*void parseResultMessage(char *message, short *num_results, char **results[], char **result_set)
{
char *p = message;
int offset;
*num_results = parseShort(p); p += 2;
if(!(*results = (char **)(malloc(sizeof(char *) * (*num_results)))))
systemError("Could not allocate results in parseResultMessage");
for(int i = 0; i < *num_results; i++)
{
if(sscanf(p, " %ms%n", &((*results)[i]), &offset) != 1)
break;
p += offset;
}
*result_set = p + 1;
}*/
/**
* The following create the communicators to send neighbor results effectively
*/
void createCommunicatorRanksArray(int my_rank, int *my_ngbrs_ranks, int my_num_ngbrs,
int *comm_size, int **comm_ranks)
{
*comm_size = my_num_ngbrs + 1; //because need to include self
*comm_ranks = (int *) malloc(sizeof(int) * (*comm_size));
memcpy(*comm_ranks, &my_rank, sizeof(int));
memcpy(*comm_ranks + 1, my_ngbrs_ranks, my_num_ngbrs * sizeof(int));
}
void sendCreateCommunicatorRequest(int num_ngbrs, int *ngbrs_ranks, int comm_size, int *comm_ranks)
{
for(int i = 0; i < num_ngbrs; i++)
{
MPI_Send(&comm_size, 1, MPI_INT, ngbrs_ranks[i], NUM_NGBRS_TAG, MPI_COMM_WORLD);
MPI_Send(comm_ranks, comm_size, MPI_INT, ngbrs_ranks[i], NGBRS_RANKS_TAG, MPI_COMM_WORLD);
//debug_print(debug_level, COMM_LOG, "%s\n", "sent CreateCommunicatorRequest");aisha
}
}
void sendMyMetadata(char *my_metadata, int len, int source, int num_ngbrs, int *ngbrs_ranks)
{
MPI_Status status;
MPI_Request request;
for(int i = 0; i < num_ngbrs; i++)
{
MPI_Send(my_metadata, len, MPI_BYTE, ngbrs_ranks[i], METADATA_TAG, MPI_COMM_WORLD);
//debug_print(debug_level, COMM_LOG, "%s\n", "sendMyMetadata");aisha
}
}
void sendMyResult(char *my_result_set, int len, int source, int num_ngbrs, int *ngbrs_ranks)
{
MPI_Status status;
MPI_Request request;
if(num_ngbrs != 0)
{
/*MPI_Bcast(my_subQ_result, len, MPI_CHAR, source, NEIGHBORS_WORLD);*/
for(int i = 0; i < num_ngbrs; i++)
{
//time_t startTime = time(NULL);
// printf("Worker %d started sending its sub-query result of length %d to Worker %d\n", source, len, ngbrs_ranks[i]);
fflush(stdout);
//debug_print(debug_level, COMM_LOG, "Worker %d started sending its \
sub-query result of length %d to Worker %d\n",
//source, len, ngbrs_ranks[i]); aisha
MPI_Send(my_result_set, len, MPI_CHAR, ngbrs_ranks[i], RESULT_SET_TO_JOIN_TAG, MPI_COMM_WORLD);
//debug_print(debug_level, COMM_LOG, "%s\n", "sendMyResult");aisha
//time_t endTime = time(NULL);
//fflush(stdout);
}
//printf("Worker %d sent its traffic volume to Proxy", source);
//MPI_Isend(&len, 1, MPI_INT, PROXY, VOLUME_TAG, MPI_COMM_WORLD, &request);
}
else
{
/***** send sub-query result (to be unioned) to the Proxy *****/
//debug_print(debug_level, COMM_LOG, "Worker %d sent its sub-query result of length %d to Proxy\n", source, len);aisha
MPI_Send(my_result_set, len, MPI_CHAR, PROXY, RESULT_SET_TO_UNION_TAG, MPI_COMM_WORLD);
}
}
int recvCreateCommunicatorRequest(int *num_ngbrs, int **ngbrs_ranks)
{
MPI_Status status;
MPI_Recv(num_ngbrs, 1, MPI_INT, MPI_ANY_SOURCE, NUM_NGBRS_TAG, MPI_COMM_WORLD, &status);
//debug_print(debug_level, COMM_LOG, "recvCreateCommunicatorRequest\n", "");
if((*ngbrs_ranks = (int *) malloc(sizeof(int) * (*num_ngbrs))) == NULL) {
systemError("Could not malloc ngbrs_ranks");
//aisha debug_print(debug_level, ERROR_LOG, "%s\n", "Could not malloc ngbrs_ranks");
}
MPI_Recv(*ngbrs_ranks, *num_ngbrs, MPI_INT, status.MPI_SOURCE, NGBRS_RANKS_TAG, MPI_COMM_WORLD, &status);
//aisha debug_print(debug_level, COMM_LOG, "recvCreateCommunicatorRequest\n", "");
// printf("\tneed to create COMM for %d ngbrs\n", *num_ngbrs); //aisha
return status.MPI_SOURCE;
}
int sendCreateCommunicatorAck(int *num_ngbrs, int **ngbrs_ranks)
{
// Sending acknowledgement for creating new comm
MPI_Status status;
char* ackMsg="ACK";
MPI_Send(ackMsg, strlen(ackMsg), MPI_CHAR, status.MPI_SOURCE, ACK_TAG, MPI_COMM_WORLD);
debug_print(debug_level, COMM_LOG, "sendCreateCommunicatorAck\n", "");
return status.MPI_SOURCE;
}
void recieveCommunicatorCreationAck(int num_ngbrs, int *ngbrs_ranks, int comm_size, int *comm_ranks)
{
for(int i = 0; i < num_ngbrs; i++)
{
// Receiving acknowledgement for creating new comm
MPI_Status status;
int message_len=3;
char* buf;
if((buf = (char *)malloc(message_len * sizeof(char))) == NULL) {
systemError("Can not allocate buffer in URecv");
debug_print(debug_level, ERROR_LOG, "%s\n", "Can not allocate buffer in URecv");
}
MPI_Recv(buf, message_len, MPI_CHAR, ngbrs_ranks[i], ACK_TAG, MPI_COMM_WORLD, &status);
debug_print(debug_level, COMM_LOG, "recieveCommunicatorCreationAck\n", "");
}
}
/**
* This receives the neighbour results using the above created communicators
* via a MPI_Bcast
*/
void *receiveNgbrResult(void *arg)
{
int my_rank1;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank1);
fflush(stdout);
//debug_print(debug_level, COMM_LOG, "Worker %d in receiveNgbrResult\n", my_rank1);
MPI_Status status;
char error_msg[MPI_MAX_ERROR_STRING];
int *error_msglen;
ResultInfo info = *((ResultInfo *) arg);
char **ngbr_result_set = info.ngbr_result_set;
int *msg_len = info.msg_len;
MPI_Comm NGBRS_COMM = *((MPI_Comm *) info.NGBRS_COMM);
int ngbr_rank = info.ngbr_rank;
int my_rank = info.my_rank;
time_t startTime = time(NULL);
int comm_size = 0;
MPI_Comm_size(NGBRS_COMM, &comm_size);
//debug_print(debug_level, COMM_LOG, "---->THE COMM SIZE IS %d\n", comm_size);
MPI_Bcast(msg_len, 1, MPI_INT, 0, NGBRS_COMM);
//debug_print(debug_level, COMM_LOG, "***** Worker %d recvd that the length of the message is %d from Worker %d\n", my_rank, *msg_len, ngbr_rank);
*ngbr_result_set = (char*) malloc(sizeof(char) * (*msg_len));
char* new_nr = malloc(sizeof(char)*(*msg_len));
char buffer[*msg_len]; //this seems unnecessary
MPI_Bcast(new_nr, *msg_len, MPI_CHAR, 0, NGBRS_COMM);
//debug_print(debug_level, COMM_LOG, "***** Worker %d recvd the message of length %d from Worker %d\n", my_rank, *msg_len, ngbr_rank);
time_t endTime = time(NULL);
//debug_print(debug_level, COMM_LOG, ">>>>>>>>>>>>>>>>>>>>> EXITED\n", "");
return (void*) new_nr;
}
void createCommunicator(MPI_Comm *NGBRS_COMM, int comm_size, int *comm_ranks)
{
MPI_Group NGBRS_GROUP, MPI_COMM_GROUP;
MPI_Comm_group(MPI_COMM_WORLD, &MPI_COMM_GROUP);
MPI_Group_incl(MPI_COMM_GROUP, comm_size, comm_ranks, &NGBRS_GROUP);
MPI_Comm_create_group(MPI_COMM_WORLD, NGBRS_GROUP, NGBRS_COMM_TAG, NGBRS_COMM);
}
int hasJoinNeighbor(Node *node)
{
Neighbor *ngbr;
for(ngbr = node->out_neighbors; ngbr != NULL; ngbr = ngbr->next)
{
if(ngbr->node->isJoinNode)
//&& !ngbr->node->is_deleted)
{
return 1;
}
}
return 0;
}
void *recievePartialResultsT(void *arg)
{
PartialResultsStruct pr = *((PartialResultsStruct *)arg);
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Status status;
recvCreateCommunicatorRequest(pr.comm_size, pr.comm_ranks);
createCommunicator(pr.NGBRS_COMM,(*pr.comm_size), (*pr.comm_ranks));
sendCreateCommunicatorAck(pr.comm_size, pr.comm_ranks);
sendCreateCommunicatorAck(pr.comm_size, pr.comm_ranks);
MPI_URecv(pr.ngbr_metadata, MPI_CHAR, MPI_ANY_SOURCE, METADATA_TAG, MPI_COMM_WORLD, &status);
parseMetadataMessage(*pr.ngbr_metadata, pr.ngbr_num_keys, pr.ngbr_keys);
debug_print(debug_level, COMM_LOG, "Worker %d recvd metadata from Worker %d\n", my_rank, status.MPI_SOURCE);
free(*pr.ngbr_metadata);
// void *exit_status;
pr.tid2_arg->NGBRS_COMM = pr.NGBRS_COMM;
pr.tid2_arg->ngbr_rank = status.MPI_SOURCE;
pr.tid2_arg->ngbr_result_set = pr.my_result_set;
pr.tid2_arg->msg_len = pr.msg_len;
pthread_create(pr.ngbr_tid, NULL, receiveNgbrResult, pr.tid2_arg);
if(pr.num_results_recvd == (pr.my_in_degree - 1))
{
joinHelper(my_rank, pr.my_num_keys, pr.my_keys, *pr.ngbr_num_keys, *pr.ngbr_keys,
*pr.ngbr_result_set, *pr.final_num_keys, *pr.final_keys, *pr.subQ_tid, *pr.ngbr_tid);
}
else
{
joinHelper(my_rank, pr.my_num_keys, pr.my_keys, *pr.ngbr_num_keys, *pr.ngbr_keys,
*pr.ngbr_result_set, 0, NULL, *pr.subQ_tid, *pr.ngbr_tid);
}
}
void recievePartialResults(int *comm_size, int **comm_ranks, MPI_Comm *NGBRS_COMM,
ResultInfo *tid2_arg, char **ngbr_metadata, short *ngbr_num_keys, char ***ngbr_keys,
char **my_result_set, pthread_t *ngbr_tid, int *msg_len, int num_results_recvd, int my_in_degree,
short *my_num_keys, char ***my_keys, char **ngbr_result_set, short *final_num_keys,
char ***final_keys, pthread_t *subQ_tid)
{
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Status status;
recvCreateCommunicatorRequest(comm_size, comm_ranks);
createCommunicator(NGBRS_COMM,(*comm_size), (*comm_ranks));
sendCreateCommunicatorAck(comm_size, comm_ranks);
sendCreateCommunicatorAck(comm_size, comm_ranks);
MPI_URecv(ngbr_metadata, MPI_CHAR, MPI_ANY_SOURCE, METADATA_TAG, MPI_COMM_WORLD, &status);
parseMetadataMessage(*ngbr_metadata, ngbr_num_keys, ngbr_keys);
debug_print(debug_level, COMM_LOG, "Worker %d recvd metadata from Worker %d\n", my_rank, status.MPI_SOURCE);
free(*ngbr_metadata);
// void *exit_status;
tid2_arg->NGBRS_COMM = NGBRS_COMM;
tid2_arg->ngbr_rank = status.MPI_SOURCE;
tid2_arg->ngbr_result_set = my_result_set;
tid2_arg->msg_len = msg_len;
pthread_create(ngbr_tid, NULL, receiveNgbrResult, tid2_arg);
if(num_results_recvd == (my_in_degree - 1))
{
joinHelper(my_rank, my_num_keys, my_keys, *ngbr_num_keys, *ngbr_keys,
*ngbr_result_set, *final_num_keys, *final_keys, *subQ_tid, *ngbr_tid);
}
else
{
joinHelper(my_rank, my_num_keys, my_keys, *ngbr_num_keys, *ngbr_keys,
*ngbr_result_set, 0, NULL, *subQ_tid, *ngbr_tid);
}
}
void *recievePartialResultsT2(void *arg)
{
PartialResultsStruct pr = *((PartialResultsStruct *)arg);
int my_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Status status;
recvCreateCommunicatorRequest(pr.comm_size, pr.comm_ranks);
createCommunicator(pr.NGBRS_COMM,(*pr.comm_size), (*pr.comm_ranks));
sendCreateCommunicatorAck(pr.comm_size, pr.comm_ranks);
sendCreateCommunicatorAck(pr.comm_size, pr.comm_ranks);
MPI_URecv(pr.ngbr_metadata, MPI_CHAR, MPI_ANY_SOURCE, METADATA_TAG, MPI_COMM_WORLD, &status);
parseMetadataMessage(*pr.ngbr_metadata, pr.ngbr_num_keys, pr.ngbr_keys);
debug_print(debug_level, COMM_LOG, "Worker %d recvd metadata from Worker %d\n", my_rank, status.MPI_SOURCE);
free(*pr.ngbr_metadata);
// void *exit_status;
pr.tid2_arg->NGBRS_COMM = pr.NGBRS_COMM;
pr.tid2_arg->ngbr_rank = status.MPI_SOURCE;
pr.tid2_arg->ngbr_result_set = pr.my_result_set;
pr.tid2_arg->msg_len = pr.msg_len;
pthread_create(pr.ngbr_tid, NULL, receiveNgbrResult, pr.tid2_arg);
if(pr.num_results_recvd == (pr.my_in_degree - 1))
{
joinHelper(my_rank, pr.my_num_keys, pr.my_keys, *pr.ngbr_num_keys, *pr.ngbr_keys,
*pr.ngbr_result_set, *pr.final_num_keys, *pr.final_keys, *pr.subQ_tid, *pr.ngbr_tid);
}
else
{
joinHelper(my_rank, pr.my_num_keys, pr.my_keys, *pr.ngbr_num_keys, *pr.ngbr_keys,
*pr.ngbr_result_set, 0, NULL, *pr.subQ_tid, *pr.ngbr_tid);
}
}