-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSF-Code.cpp
498 lines (441 loc) · 16.2 KB
/
BSF-Code.cpp
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
/*==============================================================================
Project: CoFePro
Theme: Projection Algorithm for Solving Convex Feasibility Problems
Module: BSF-Code.cpp (Problem Independent Code)
Prefix: BC
Author: Leonid B. Sokolinsky
This source code is a part of BSF Skeleton (https://github.com/leonid-sokolinsky/BSF-skeleton)
==============================================================================*/
#include "BSF-Data.h" // Problem Independent Variables & Data Structures
#include "BSF-Forwards.h" // Problem Independent Function Forwards
#include "BSF-ProblemFunctions.h" // Predefined Problem Function Forwards
using namespace std;
//======================================== Problem-independent codes (don't modify them) ====================================
int main(int argc, char* argv[]) {
char emptystring[] = "";
char* message = emptystring;
unsigned success;
BC_MpiRun();
BD_success = true;
PC_bsf_Init(&BD_success);
MPI_Allreduce(&BD_success, &success, 1, MPI_UNSIGNED, MPI_LAND, MPI_COMM_WORLD);
if (!success) {
if (BD_rank == BD_masterRank)
cout << "Error: PC_bsf_Init failed!" << endl;
MPI_Finalize();
exit(1);
};
BD_success = true;
BC_Init(&BD_success);
MPI_Allreduce(&BD_success, &success, 1, MPI_UNSIGNED, MPI_LAND, MPI_COMM_WORLD);
if (!success) {
if (BD_rank == BD_masterRank) cout << "Error: BC_Init failed (not enough memory)!" << endl;
MPI_Finalize();
exit(1);
};
if (BD_rank == BD_masterRank)
BC_Master();
else
BC_Worker();
MPI_Finalize();
return 0;
};
static void BC_Master() {// The head function of the master process.
PC_bsf_ParametersOutput(BD_order.parameter);
BD_iterCounter = 0;
BD_t = -MPI_Wtime();
do {
PC_bsf_JobDispatcher(&(BD_order.parameter), &BD_newJobCase, &BD_exit);
if (BD_exit) break;
BD_jobCase = BD_newJobCase;
if (BD_jobCase > PP_BSF_MAX_JOB_CASE) {
cout << "BC_Master:Error: Job Case = " << BD_jobCase << " > PP_BSF_MAX_JOB_CASE = " << PP_BSF_MAX_JOB_CASE << endl;
BD_exit = BD_EXIT;
break;
};
BC_MasterMap(!BD_EXIT);
BC_MasterReduce();
switch (BD_jobCase) {
case 0:
PC_bsf_ProcessResults(
&BD_extendedReduceResult_P->elem,
BD_extendedReduceResult_P->reduceCounter,
&(BD_order.parameter),
&BD_newJobCase, &BD_exit);
#ifdef PP_BSF_ITER_OUTPUT
if (BD_iterCounter % PP_BSF_TRACE_COUNT == 0)
PC_bsf_IterOutput(
&BD_extendedReduceResult_P->elem,
BD_extendedReduceResult_P->reduceCounter,
BD_order.parameter,
BD_t + MPI_Wtime(),
BD_newJobCase);
#endif // PP_BSF_ITER_OUTPUT
break;
case 1:
PC_bsf_ProcessResults_1(
&BD_extendedReduceResult_P_1->elem,
BD_extendedReduceResult_P_1->reduceCounter,
&(BD_order.parameter),
&BD_newJobCase, &BD_exit);
#ifdef PP_BSF_ITER_OUTPUT
if (BD_iterCounter % PP_BSF_TRACE_COUNT == 0)
PC_bsf_IterOutput_1(
&BD_extendedReduceResult_P_1->elem,
BD_extendedReduceResult_P_1->reduceCounter,
BD_order.parameter,
BD_t + MPI_Wtime(),
BD_newJobCase);
#endif // PP_BSF_ITER_OUTPUT
break;
case 2:
PC_bsf_ProcessResults_2(
&BD_extendedReduceResult_P_2->elem,
BD_extendedReduceResult_P_2->reduceCounter,
&(BD_order.parameter),
&BD_newJobCase, &BD_exit);
#ifdef PP_BSF_ITER_OUTPUT
if (BD_iterCounter % PP_BSF_TRACE_COUNT == 0)
PC_bsf_IterOutput_2(
&BD_extendedReduceResult_P_2->elem,
BD_extendedReduceResult_P_2->reduceCounter,
BD_order.parameter,
BD_t + MPI_Wtime(),
BD_newJobCase);
#endif // PP_BSF_ITER_OUTPUT
break;
case 3:
PC_bsf_ProcessResults_3(
&BD_extendedReduceResult_P_3->elem,
BD_extendedReduceResult_P_3->reduceCounter,
&(BD_order.parameter),
&BD_newJobCase, &BD_exit);
#ifdef PP_BSF_ITER_OUTPUT
if (BD_iterCounter % PP_BSF_TRACE_COUNT == 0)
PC_bsf_IterOutput_3(
&BD_extendedReduceResult_P_3->elem,
BD_extendedReduceResult_P_3->reduceCounter,
BD_order.parameter,
BD_t + MPI_Wtime(),
BD_newJobCase);
#endif // PP_BSF_ITER_OUTPUT
break;
default:
cout << "BC_Master: Undefined job type!" << endl;
break;
};
BD_iterCounter++;
} while (!BD_exit);
BD_t += MPI_Wtime();
BC_MasterMap(BD_EXIT);
switch (BD_jobCase) {
case 0:
PC_bsf_ProblemOutput(&BD_extendedReduceResult_P->elem, BD_extendedReduceResult_P->reduceCounter, BD_order.parameter, BD_t);
break;
case 1:
PC_bsf_ProblemOutput_1(&BD_extendedReduceResult_P_1->elem, BD_extendedReduceResult_P_1->reduceCounter, BD_order.parameter, BD_t);
break;
case 2:
PC_bsf_ProblemOutput_2(&BD_extendedReduceResult_P_2->elem, BD_extendedReduceResult_P_2->reduceCounter, BD_order.parameter, BD_t);
break;
case 3:
PC_bsf_ProblemOutput_3(&BD_extendedReduceResult_P_3->elem, BD_extendedReduceResult_P_3->reduceCounter, BD_order.parameter, BD_t);
break;
default:
cout << "BC_Master: Undefined job type!" << endl;
break;
};
};
static void BC_Worker() {// The head function of a worker process.
bool exit;
while (true) {
exit = BC_WorkerMap();
if (exit) break;
BC_WorkerReduce();
};
};
static void BC_MasterMap(bool exit) { // Forms an order and sends it to the worker processes to perform the Map function in the current iteration.
PC_bsfAssignJobCase(BD_jobCase);
PC_bsfAssignIterCounter(BD_iterCounter);
BD_order.exit = exit;
BD_order.jobCase = BD_jobCase;
BD_order.iterCounter = BD_iterCounter;
for (int rank = 0; rank < BD_numOfWorkers; rank++) {
MPI_Isend(
&BD_order,
sizeof(BT_order_T),
MPI_BYTE,
rank,
0,
MPI_COMM_WORLD,
&BD_request[rank]);
};
MPI_Waitall(BD_numOfWorkers, BD_request, BD_status);
};
static void BC_MasterReduce() {
for (int rank = 0; rank < BD_numOfWorkers; rank++)
switch (BD_jobCase) {
case 0:
MPI_Irecv(&BD_extendedReduceList[rank], sizeof(BT_extendedReduceElem_T), MPI_BYTE, rank, 0, MPI_COMM_WORLD, &BD_request[rank]);
break;
case 1:
MPI_Irecv(&BD_extendedReduceList_1[rank], sizeof(BT_extendedReduceElem_T_1), MPI_BYTE, rank, 0, MPI_COMM_WORLD, &BD_request[rank]);
break;
case 2:
MPI_Irecv(&BD_extendedReduceList_2[rank], sizeof(BT_extendedReduceElem_T_2), MPI_BYTE, rank, 0, MPI_COMM_WORLD, &BD_request[rank]);
break;
case 3:
MPI_Irecv(&BD_extendedReduceList_3[rank], sizeof(BT_extendedReduceElem_T_3), MPI_BYTE, rank, 0, MPI_COMM_WORLD, &BD_request[rank]);
break;
default:
cout << "BC_MasterReduce Error: Undefined job type!" << endl;
break;
};
MPI_Waitall(BD_numOfWorkers, BD_request, BD_status);
switch (BD_jobCase) {
case 0:
BC_ProcessExtendedReduceList(BD_extendedReduceList, BD_numOfWorkers, &BD_extendedReduceResult_P);
break;
case 1:
BC_ProcessExtendedReduceList_1(BD_extendedReduceList_1, BD_numOfWorkers, &BD_extendedReduceResult_P_1);
break;
case 2:
BC_ProcessExtendedReduceList_2(BD_extendedReduceList_2, BD_numOfWorkers, &BD_extendedReduceResult_P_2);
break;
case 3:
BC_ProcessExtendedReduceList_3(BD_extendedReduceList_3, BD_numOfWorkers, &BD_extendedReduceResult_P_3);
break;
default:
cout << "BC_MasterReduce Error: Undefined job type!" << endl;
break;
};
};
static bool BC_WorkerMap() { // Performs the Map function
MPI_Recv(
&BD_order,
sizeof(BT_order_T),
MPI_BYTE,
BD_masterRank,
0,
MPI_COMM_WORLD,
&BD_status[BD_rank]);
if (BD_order.exit)
return BD_EXIT;
PC_bsfAssignJobCase(BD_order.jobCase);
PC_bsfAssignIterCounter(BD_order.iterCounter);
PC_bsfAssignSublistLength(BD_sublistSize[BD_rank]);
PC_bsfAssignAddressOffset(BD_offset[BD_rank]);
PC_bsfAssignParameter(BD_order.parameter);
#ifdef PP_BSF_OMP
#ifdef PP_BSF_NUM_THREADS
#pragma omp parallel for num_threads(PP_BSF_NUM_THREADS)
#else
#pragma omp parallel for
#endif // PP_BSF_NUM_THREADS
#endif // PP_BSF_OMP
for (int i = BD_offset[BD_rank]; i < BD_offset[BD_rank] + BD_sublistSize[BD_rank]; i++) {
int index;
index = i - BD_offset[BD_rank];
PC_bsfAssignNumberInSublist(index);
switch (BD_order.jobCase) {
case 0:
BD_extendedReduceList[index].reduceCounter = 1;
PC_bsf_MapF(&BD_mapSubList[index], &BD_extendedReduceList[index].elem,
&BD_extendedReduceList[index].reduceCounter);
break;
case 1:
BD_extendedReduceList_1[index].reduceCounter = 1;
PC_bsf_MapF_1(&BD_mapSubList[index], &BD_extendedReduceList_1[index].elem,
&BD_extendedReduceList_1[index].reduceCounter);
break;
case 2:
BD_extendedReduceList_2[index].reduceCounter = 1;
PC_bsf_MapF_2(&BD_mapSubList[index], &BD_extendedReduceList_2[index].elem,
&BD_extendedReduceList_2[index].reduceCounter);
break;
case 3:
BD_extendedReduceList_3[index].reduceCounter = 1;
PC_bsf_MapF_3(&BD_mapSubList[index], &BD_extendedReduceList_3[index].elem,
&BD_extendedReduceList_3[index].reduceCounter);
break;
default:
cout << "BC_WorkerReduce Error: Undefined job type!" << endl;
break;
};
};
/* Time measurement *///t_Map += MPI_Wtime();
/* Time measurement *///cout << "t_Map = " << t_Map << "\t";
return !BD_EXIT;
};
static void BC_WorkerReduce() {
switch (BD_order.jobCase) {
case 0:
BC_ProcessExtendedReduceList(BD_extendedReduceList, BD_sublistSize[BD_rank], &BD_extendedReduceResult_P);
MPI_Send(BD_extendedReduceResult_P, sizeof(BT_extendedReduceElem_T), MPI_BYTE, BD_masterRank, 0, MPI_COMM_WORLD);
break;
case 1:
BC_ProcessExtendedReduceList_1(BD_extendedReduceList_1, BD_sublistSize[BD_rank], &BD_extendedReduceResult_P_1);
MPI_Send(BD_extendedReduceResult_P_1, sizeof(BT_extendedReduceElem_T_1), MPI_BYTE, BD_masterRank, 0, MPI_COMM_WORLD);
break;
case 2:
BC_ProcessExtendedReduceList_2(BD_extendedReduceList_2, BD_sublistSize[BD_rank], &BD_extendedReduceResult_P_2);
MPI_Send(BD_extendedReduceResult_P_2, sizeof(BT_extendedReduceElem_T_2), MPI_BYTE, BD_masterRank, 0, MPI_COMM_WORLD);
break;
case 3:
BC_ProcessExtendedReduceList_3(BD_extendedReduceList_3, BD_sublistSize[BD_rank], &BD_extendedReduceResult_P_3);
MPI_Send(BD_extendedReduceResult_P_3, sizeof(BT_extendedReduceElem_T_3), MPI_BYTE, BD_masterRank, 0, MPI_COMM_WORLD);
break;
default:
cout << "BC_WorkerReduce Error: Undefined job type!" << endl;
break;
};
};
static void BC_ProcessExtendedReduceList(BT_extendedReduceElem_T* reduceList, int length, BT_extendedReduceElem_T** extendedReduceResult_P) {
int firstSuccessIndex = -1;
*extendedReduceResult_P = &reduceList[0];
for (int i = 0; i < length; i++) {
if (reduceList[i].reduceCounter > 0) {
*extendedReduceResult_P = &reduceList[i];
firstSuccessIndex = i;
break;
};
};
if (firstSuccessIndex >= 0) {
for (int i = firstSuccessIndex + 1; i < length; i++)
if (BD_extendedReduceList[i].reduceCounter > 0) {
PC_bsf_ReduceF(&(*extendedReduceResult_P)->elem, &BD_extendedReduceList[i].elem,
&(*extendedReduceResult_P)->elem);
(*extendedReduceResult_P)->reduceCounter += BD_extendedReduceList[i].reduceCounter;
};
};
};
static void BC_ProcessExtendedReduceList_1(BT_extendedReduceElem_T_1* reduceList, int length, BT_extendedReduceElem_T_1** extendedReduceResult_P) {
int firstSuccessIndex = -1;
*extendedReduceResult_P = &reduceList[0];
for (int i = 0; i < length; i++) {
if (reduceList[i].reduceCounter > 0) {
*extendedReduceResult_P = &reduceList[i];
firstSuccessIndex = i;
break;
};
};
if (firstSuccessIndex >= 0) {
for (int i = firstSuccessIndex + 1; i < length; i++)
if (BD_extendedReduceList_1[i].reduceCounter > 0) {
PC_bsf_ReduceF_1(&(*extendedReduceResult_P)->elem, &BD_extendedReduceList_1[i].elem,
&(*extendedReduceResult_P)->elem);
(*extendedReduceResult_P)->reduceCounter += BD_extendedReduceList_1[i].reduceCounter;
};
};
};
static void BC_ProcessExtendedReduceList_2(BT_extendedReduceElem_T_2* reduceList, int length, BT_extendedReduceElem_T_2** extendedReduceResult_P) {
int firstSuccessIndex = -1;
*extendedReduceResult_P = &reduceList[0];
for (int i = 0; i < length; i++) {
if (reduceList[i].reduceCounter > 0) {
*extendedReduceResult_P = &reduceList[i];
firstSuccessIndex = i;
break;
};
};
if (firstSuccessIndex >= 0) {
for (int i = firstSuccessIndex + 1; i < length; i++)
if (BD_extendedReduceList_2[i].reduceCounter > 0) {
PC_bsf_ReduceF_2(&(*extendedReduceResult_P)->elem, &BD_extendedReduceList_2[i].elem,
&(*extendedReduceResult_P)->elem);
(*extendedReduceResult_P)->reduceCounter += BD_extendedReduceList_2[i].reduceCounter;
};
};
};
static void BC_ProcessExtendedReduceList_3(BT_extendedReduceElem_T_3* reduceList, int length, BT_extendedReduceElem_T_3** extendedReduceResult_P) {
int firstSuccessIndex = -1;
*extendedReduceResult_P = &reduceList[0];
for (int i = 0; i < length; i++) {
if (reduceList[i].reduceCounter > 0) {
*extendedReduceResult_P = &reduceList[i];
firstSuccessIndex = i;
break;
};
};
if (firstSuccessIndex >= 0) {
for (int i = firstSuccessIndex + 1; i < length; i++)
if (BD_extendedReduceList_3[i].reduceCounter > 0) {
PC_bsf_ReduceF_3(&(*extendedReduceResult_P)->elem, &BD_extendedReduceList_3[i].elem,
&(*extendedReduceResult_P)->elem);
(*extendedReduceResult_P)->reduceCounter += BD_extendedReduceList_3[i].reduceCounter;
};
};
};
static void BC_Init(bool* success) {// Performs the memory allocation and the initialization of the skeleton data structures and variables.
cout << setprecision(PP_BSF_PRECISION);
int offset, first, last, subListSize;
PC_bsf_SetListSize(&BD_listSize);
if (BD_size > BD_listSize + 1) {
if (BD_rank == 0) cout << "Error: MPI_SIZE must be < Map List Size + 2 =" << BD_listSize + 2 << endl;
MPI_Finalize();
exit(1);
};
BD_masterRank = BD_size - 1;
BD_numOfWorkers = BD_size - 1;
PC_bsf_SetInitParameter(&(BD_order.parameter));
if (BD_rank == BD_masterRank) {
subListSize = BD_numOfWorkers;
}
else {
BD_elemsPerWorker = BD_listSize / BD_numOfWorkers;
BD_tailLength = BD_listSize - BD_elemsPerWorker * BD_numOfWorkers;
offset = 0;
for (int rank = 0; rank < BD_numOfWorkers; rank++) {
BD_sublistSize[rank] = (int)(BD_elemsPerWorker + (rank < BD_tailLength ? 1 : 0));
BD_offset[rank] = offset;
offset += BD_sublistSize[rank];
}
subListSize = BD_sublistSize[BD_rank];
BD_mapSubList = (PT_bsf_mapElem_T*)calloc(subListSize, sizeof(PT_bsf_mapElem_T));
if (BD_mapSubList == NULL) {
*success = false;
return;
}
first = BD_offset[BD_rank];
last = BD_offset[BD_rank] + BD_sublistSize[BD_rank] - 1;
for (int i = first; i <= last; i++)
PC_bsf_SetMapListElem(&BD_mapSubList[i - first], i);
}
BD_extendedReduceList = (BT_extendedReduceElem_T*)calloc(subListSize, sizeof(BT_extendedReduceElem_T));
if (BD_extendedReduceList == NULL) {*success = false;return;};
if (PP_BSF_MAX_JOB_CASE > 0) {
BD_extendedReduceList_1 = (BT_extendedReduceElem_T_1*)calloc(subListSize, sizeof(BT_extendedReduceElem_T_1));
if (BD_extendedReduceList_1 == NULL) { *success = false; return; };
}
if (PP_BSF_MAX_JOB_CASE > 1) {
BD_extendedReduceList_2 = (BT_extendedReduceElem_T_2*)calloc(subListSize, sizeof(BT_extendedReduceElem_T_2));
if (BD_extendedReduceList_2 == NULL) { *success = false; return; };
}
if (PP_BSF_MAX_JOB_CASE > 2) {
BD_extendedReduceList_3 = (BT_extendedReduceElem_T_3*)calloc(subListSize, sizeof(BT_extendedReduceElem_T_3));
if (BD_extendedReduceList_3 == NULL) { *success = false; return; };
}
}
static void BC_MpiRun() {
int rc;
rc = MPI_Init(NULL, NULL); // Starting MPI
if (rc != MPI_SUCCESS) {
printf("Error starting MPI program. Terminating! \n");
MPI_Abort(MPI_COMM_WORLD, rc);
};
MPI_Comm_rank(MPI_COMM_WORLD, &BD_rank);
PC_bsfAssignMpiRank(BD_rank);
MPI_Comm_size(MPI_COMM_WORLD, &BD_size);
PC_bsfAssignMpiMaster(BD_size - 1);
if (BD_size > PP_BSF_MAX_MPI_SIZE) {
if (BD_rank == 0) cout << "Error: MPI_SIZE exceeded the maximum allowed value PP_BSF_MAX_MPI_SIZE = " << PP_BSF_MAX_MPI_SIZE << endl;
MPI_Finalize();
exit(1);
}
PC_bsfAssignNumOfWorkers(BD_size - 1);
if (BD_size < 2) {
if (BD_rank == 0) cout << "Error: MPI_SIZE must be > 1" << endl;
MPI_Finalize();
exit(1);
};
};