-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathloadbalance.c
More file actions
602 lines (508 loc) · 22.5 KB
/
loadbalance.c
File metadata and controls
602 lines (508 loc) · 22.5 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <fftw3.h>
#include <mpi.h>
#include <hdf5.h>
#include <gsl/gsl_math.h>
#include <string.h>
#include "raytrace.h"
//define to never change domain decomp
#define STATIC_DOMAINDECOMP
//define this to always use equal area domain decomp
#define EQUALAREA_DOMAINDECOMP
//take care of cases where equal area always wins
#ifdef SHTONLY
#define EQUALAREA_DOMAINDECOMP
#endif
#if (defined POINTMASSTEST || defined NFWHALOTEST)
#define EQUALAREA_DOMAINDECOMP
#endif
static void loadBalanceBundleCellsPerCPU(void);
static void divide_tasks_domaindecomp(int firstTask, int lastTask, long firstPCell, long lastPCell, double *totCPUPerBundleCell);
static int mightNeedToSendBuffCellsRPI(long sendTask, long recvTask, long *minRPITasks, long *maxRPITasks, long *firstRPITasks, long *lastRPITasks);
void load_balance_tasks(void)
{
double time;
long i;
//////////////////////////////
//do the load balancing //
//////////////////////////////
#ifndef STATIC_DOMAINDECOMP
loadBalanceBundleCellsPerCPU();
#endif
//////////////////////////////
//now set part buffer cells //
//////////////////////////////
time = -MPI_Wtime();
//NOT NEEDED ANY MORE (ray buffer cell flag is set by buffer ray routine) - mark_bundlecells(rayTraceData.galImageSearchRayBufferRad,PRIMARY_BUNDLECELL,RAYBUFF_BUNDLECELL);
mark_bundlecells(rayTraceData.partBuffRad,PRIMARY_BUNDLECELL,PARTBUFF_BUNDLECELL);
time += MPI_Wtime();
//if(ThisTask == 0)
//fprintf(stderr,"marking part buffer regions took %lg seconds.\n",time);
//////////////////////////////
//reset the cpu times //
//////////////////////////////
for(i=0;i<NbundleCells;++i)
bundleCells[i].cpuTime = 0.0;
}
void getDomainDecompPerCPU(int report)
{
long i,j,k;
double *totCPUPerBundleCell,tot;
double *cpuPerBundleCell;
long setRestByHand;
long maxNumCellsPerTask = ((long) ((1.0 + rayTraceData.maxRayMemImbalance)*((double) NrestrictedPeanoInd)/((double) NTasks)));
double memPerBundleCell = 1.0/((double) NrestrictedPeanoInd);
long NumBundleCellsPerTask = NrestrictedPeanoInd/NTasks;
/*
get the actual domain decomp
- first divide up tasks by cost
- then check domain decomp for correctness
- if not correct, revert to equal area domain decomp
*/
//if(ThisTask == 0)
//fprintf(stderr,"%d: maxNumCellsPerTask = %ld, memfac = %lf, num per task = %lf\n",ThisTask,maxNumCellsPerTask,
// 1.0 + rayTraceData.maxRayMemImbalance,((double) NrestrictedPeanoInd)/((double) NTasks));
// do all reduce for CPU load balance
totCPUPerBundleCell = (double*)malloc(sizeof(double)*NbundleCells);
assert(totCPUPerBundleCell != NULL);
cpuPerBundleCell = (double*)malloc(sizeof(double)*NbundleCells);
assert(cpuPerBundleCell != NULL);
for(i=0;i<NbundleCells;++i)
cpuPerBundleCell[i] = 0.0;
for(i=0;i<NbundleCells;++i)
cpuPerBundleCell[i] = bundleCells[i].cpuTime;
long sec,Nsec,dsec,lsec;
dsec = 512;
Nsec = NbundleCells/dsec;
if(dsec*Nsec < NbundleCells)
++Nsec;
for(sec=0;sec<Nsec;++sec)
{
lsec = dsec;
if(lsec + sec*dsec > NbundleCells)
lsec = NbundleCells - sec*dsec;
MPI_Allreduce(cpuPerBundleCell + sec*dsec,totCPUPerBundleCell + sec*dsec,(int) lsec,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
}
//MPI_Allreduce(cpuPerBundleCell,totCPUPerBundleCell,(int) NbundleCells,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
//get first and last peano ind for each task
for(i=0;i<NbundleCells;++i)
if(bundleCellsNest2RestrictedPeanoInd[i] == -1)
totCPUPerBundleCell[i] = 0.0;
tot = 0.0;
for(i=0;i<NbundleCells;++i)
tot += totCPUPerBundleCell[i];
for(i=0;i<NbundleCells;++i)
totCPUPerBundleCell[i] /= tot;
//does a recursive binary split of tasks along cost curve
divide_tasks_domaindecomp(0,NTasks-1,0l,NrestrictedPeanoInd-1,totCPUPerBundleCell);
//error check
setRestByHand = 0;
for(i=0;i<NTasks;++i)
{
if(!(firstRestrictedPeanoIndTasks[i] >= 0 && firstRestrictedPeanoIndTasks[i] < NrestrictedPeanoInd))
setRestByHand = 1;
if(!(lastRestrictedPeanoIndTasks[i] >= 0 && lastRestrictedPeanoIndTasks[i] < NrestrictedPeanoInd))
setRestByHand = 1;
if(!(lastRestrictedPeanoIndTasks[i] >= firstRestrictedPeanoIndTasks[i]))
setRestByHand = 1;
if(i < NTasks-1)
{
if(!(lastRestrictedPeanoIndTasks[i] + 1 == firstRestrictedPeanoIndTasks[i+1]))
setRestByHand = 1;
}
else
{
if(!(lastRestrictedPeanoIndTasks[i] == NrestrictedPeanoInd-1))
setRestByHand = 1;
}
if(lastRestrictedPeanoIndTasks[i] - firstRestrictedPeanoIndTasks[i] + 1 > maxNumCellsPerTask)
setRestByHand = 1;
if(setRestByHand)
break;
}
#ifdef EQUALAREA_DOMAINDECOMP
setRestByHand = 1;
#endif
if(setRestByHand)
{
if(ThisTask == 0)
{
/*fprintf(stderr,"doman decomp for ray load balance failed! reverting to equal area domain decomp!\n");
fprintf(stderr,"NrestrictedPeanoInd (total # of active cells) = %ld\n",NrestrictedPeanoInd);
for(i=0;i<NTasks;++i)
fprintf(stderr,"%ld: firstRestrictedPeanoInd|lastRestrictedPeanoInd = %ld|%ld|%ld|%lf|%ld\n",i,
firstRestrictedPeanoIndTasks[i],lastRestrictedPeanoIndTasks[i],
lastRestrictedPeanoIndTasks[i] - firstRestrictedPeanoIndTasks[i] + 1,
((double) (lastRestrictedPeanoIndTasks[i] - firstRestrictedPeanoIndTasks[i] + 1))/((double) NrestrictedPeanoInd),
((long) ((1.0 + rayTraceData.maxRayMemImbalance)*NrestrictedPeanoInd/NTasks)));
*/
}
j = NrestrictedPeanoInd - NTasks*NumBundleCellsPerTask;
k = 0;
for(i=0;i<NTasks;++i)
{
firstRestrictedPeanoIndTasks[i] = k;
if(i < j)
lastRestrictedPeanoIndTasks[i] = k + NumBundleCellsPerTask;
else
lastRestrictedPeanoIndTasks[i] = k + NumBundleCellsPerTask - 1;
k = lastRestrictedPeanoIndTasks[i] + 1;
}
lastRestrictedPeanoIndTasks[NTasks-1] = NrestrictedPeanoInd - 1;
}
/* mark domain of each node */
for(i=0;i<NbundleCells;++i)
CLEARBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL);
for(i=firstRestrictedPeanoIndTasks[ThisTask];i<=lastRestrictedPeanoIndTasks[ThisTask];++i)
if(bundleCellsRestrictedPeanoInd2Nest[i] != -1)
SETBITFLAG(bundleCells[bundleCellsRestrictedPeanoInd2Nest[i]].active,PRIMARY_BUNDLECELL);
//print some stats
double tmem,maxMem = -1.0;
double tcpu,maxCPU = -1.0;
for(i=0;i<NTasks;++i)
{
tmem = 0.0;
tcpu = 0.0;
for(j=firstRestrictedPeanoIndTasks[i];j<=lastRestrictedPeanoIndTasks[i];++j)
{
tmem += memPerBundleCell;
tcpu += totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[j]];
}
if(tmem > maxMem)
maxMem = tmem;
if(tcpu > maxCPU)
maxCPU = tcpu;
}
if(ThisTask == 0 && report)
{
fprintf(stderr,"max mem,cpu for domain decomp = %lf|%lf of 1.0 (%lf per task).\n",maxMem,maxCPU,1.0/NTasks);
fflush(stderr);
}
free(totCPUPerBundleCell);
free(cpuPerBundleCell);
#ifdef DEBUG
#if DEBUG_LEVEL > 0
if(ThisTask == 0)
{
fprintf(stderr,"NrestrictedPeanoInd (total # of active cells) = %ld\n",NrestrictedPeanoInd);
for(i=0;i<NTasks;++i)
fprintf(stderr,"%ld: firstRestrictedPeanoInd|lastRestrictedPeanoInd = %ld|%ld|%ld|%lf|%ld\n",i,
firstRestrictedPeanoIndTasks[i],lastRestrictedPeanoIndTasks[i],
lastRestrictedPeanoIndTasks[i] - firstRestrictedPeanoIndTasks[i] + 1,
((double) (lastRestrictedPeanoIndTasks[i] - firstRestrictedPeanoIndTasks[i] + 1))/((double) NrestrictedPeanoInd),
((long) ((1.0 + rayTraceData.maxRayMemImbalance)*NrestrictedPeanoInd/NTasks)));
}
#endif
#endif
}
void loadBalanceBundleCellsPerCPU(void)
{
long log2NTasks;
long level,sendTask,recvTask;
long i;
long *firstRPITasks,*lastRPITasks;
long *bundleCellHasRays;
long NumSendThisTask,GlobalNumSend;
long NumRecvThisTask,GlobalNumRecv;
long maxNumCellsToSendRecv;
long Nsend,Nrecv;
long didSend,didRecv;
MPI_Request requestSend,requestRecv;
MPI_Status Stat;
long bundleNestToRecv;
long NumRaysPerBundleCell,shift,round;
long bnestOfRaysToMove,rayStartOfRaysToMove;
HEALPixRay *raysToMove;
long *maxNumCellsToSend;
if(ThisTask == 0)
fprintf(stderr,"load balancing nodes.\n");
//set up for loop
log2NTasks = 0;
while(NTasks > (1 << log2NTasks))
++log2NTasks;
shift = rayTraceData.rayOrder - rayTraceData.bundleOrder;
shift = 2*shift;
NumRaysPerBundleCell = 1;
NumRaysPerBundleCell = (NumRaysPerBundleCell << shift);
maxNumCellsToSend = (long*)malloc(sizeof(long)*NTasks);
assert(maxNumCellsToSend != NULL);
//record information about old bundle cells and rays
firstRPITasks = (long*)malloc(sizeof(long)*NTasks);
assert(firstRPITasks != NULL);
lastRPITasks = (long*)malloc(sizeof(long)*NTasks);
assert(lastRPITasks != NULL);
for(i=0;i<NTasks;++i)
{
firstRPITasks[i] = firstRestrictedPeanoIndTasks[i];
lastRPITasks[i] = lastRestrictedPeanoIndTasks[i];
}
bundleCellHasRays = (long*)malloc(sizeof(long)*NbundleCells);
assert(bundleCellHasRays != NULL);
for(i=0;i<NbundleCells;++i)
{
if(ISSETBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL))
bundleCellHasRays[i] = 1;
else
bundleCellHasRays[i] = 0;
}
//get actual domain decomp
getDomainDecompPerCPU(1);
//get number of cells each task needs to send and recv total
NumSendThisTask = 0;
NumRecvThisTask = 0;
for(i=0;i<NbundleCells;++i)
{
if(bundleCellHasRays[i] && !(ISSETBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL)))
++NumSendThisTask;
if(!(bundleCellHasRays[i]) && ISSETBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL))
++NumRecvThisTask;
}
MPI_Allreduce(&NumSendThisTask,&GlobalNumSend,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
MPI_Allreduce(&NumRecvThisTask,&GlobalNumRecv,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
round = 0;
while(GlobalNumSend > 0 || GlobalNumRecv > 0)
{
if(ThisTask == 0)
fprintf(stderr,"round %ld: # of bundle cells left to send,recv = %ld|%ld\n",round,GlobalNumSend,GlobalNumRecv);
++round;
/*algorithm to loop through pairs of tasks linearly
-lifted from Gadget-2 under GPL (http://www.gnu.org/copyleft/gpl.html)
-see pm_periodic.c from Gadget-2 at http://www.mpa-garching.mpg.de/gadget/
*/
for(level = 0; level < (1 << log2NTasks); level++) /* note: for level=0, target is the same task */
{
maxNumCellsToSendRecv = (MaxNumAllRaysGlobal - NumAllRaysGlobal)/NumRaysPerBundleCell;
MPI_Allgather(&maxNumCellsToSendRecv,1,MPI_LONG,maxNumCellsToSend,1,MPI_LONG,MPI_COMM_WORLD);
sendTask = ThisTask;
recvTask = ThisTask ^ level;
if(recvTask < NTasks && sendTask != recvTask)
{
if(mightNeedToSendBuffCellsRPI(sendTask,recvTask,firstRestrictedPeanoIndTasks,lastRestrictedPeanoIndTasks,firstRPITasks,lastRPITasks) ||
mightNeedToSendBuffCellsRPI(recvTask,sendTask,firstRestrictedPeanoIndTasks,lastRestrictedPeanoIndTasks,firstRPITasks,lastRPITasks))
{
Nsend = 0;
for(i=firstRPITasks[sendTask];i<=lastRPITasks[sendTask];++i)
{
if(bundleCellHasRays[bundleCellsRestrictedPeanoInd2Nest[i]] &&
(firstRestrictedPeanoIndTasks[recvTask] <= i && i <= lastRestrictedPeanoIndTasks[recvTask]))
++Nsend;
}
if(Nsend > maxNumCellsToSend[recvTask])
Nsend = maxNumCellsToSend[recvTask];
MPI_Sendrecv(&Nsend,1,MPI_LONG,(int) recvTask,TAG_NUMBUFF_LOADBAL,
&Nrecv,1,MPI_LONG,(int) recvTask,TAG_NUMBUFF_LOADBAL,
MPI_COMM_WORLD,&Stat);
i = firstRPITasks[sendTask];
while(Nsend > 0 || Nrecv > 0)
{
//first get bundle index to recv
//then send cells
//get bundle ind to recv
if(Nrecv > 0)
{
MPI_Irecv(&bundleNestToRecv,1,MPI_LONG,(int) recvTask,TAG_BUFFIND_LOADBAL,MPI_COMM_WORLD,&requestRecv);
didRecv = 1;
}
else
didRecv = 0;
if(Nsend > 0)
{
while(i <= lastRPITasks[sendTask])
{
if(firstRestrictedPeanoIndTasks[recvTask] <= i && i <= lastRestrictedPeanoIndTasks[recvTask] &&
bundleCellHasRays[bundleCellsRestrictedPeanoInd2Nest[i]])
break;
++i;
}
if(i > lastRPITasks[sendTask])
{
fprintf(stderr,"%d: out of restricted peano ind bundle cells in while Nsend, Nrecv loop\n",ThisTask);
MPI_Abort(MPI_COMM_WORLD,456);
}
MPI_Issend(&(bundleCellsRestrictedPeanoInd2Nest[i]),1,MPI_LONG,(int) recvTask,TAG_BUFFIND_LOADBAL,MPI_COMM_WORLD,&requestSend);
didSend = 1;
}
else
didSend = 0;
if(didRecv)
MPI_Wait(&requestRecv,&Stat);
if(didSend)
MPI_Wait(&requestSend,&Stat);
//now get rays for the bundle cell
if(Nrecv > 0)
{
if(NumAllRaysGlobal >= MaxNumAllRaysGlobal)
{
fprintf(stderr,"%d: out of memory for rays during load balance! MaxNumAllRaysGlobal = %ld, NumAllRaysGlobal = %ld\n",
ThisTask,MaxNumAllRaysGlobal,NumAllRaysGlobal);
MPI_Abort(MPI_COMM_WORLD,112);
}
bundleCells[bundleNestToRecv].rays = AllRaysGlobal + NumAllRaysGlobal;
bundleCells[bundleNestToRecv].Nrays = NumRaysPerBundleCell;
NumAllRaysGlobal += NumRaysPerBundleCell;
MPI_Irecv(bundleCells[bundleNestToRecv].rays,
(int) (sizeof(HEALPixRay)*NumRaysPerBundleCell),MPI_BYTE,
(int) recvTask,TAG_BUFF_LOADBAL,MPI_COMM_WORLD,&requestRecv);
didRecv = 1;
}
else
didRecv = 0;
if(Nsend > 0)
{
MPI_Issend(bundleCells[bundleCellsRestrictedPeanoInd2Nest[i]].rays,
(int) (sizeof(HEALPixRay)*NumRaysPerBundleCell),MPI_BYTE,
(int) recvTask,TAG_BUFF_LOADBAL,MPI_COMM_WORLD,&requestSend);
didSend = 1;
}
else
didSend = 0;
if(didRecv)
{
MPI_Wait(&requestRecv,&Stat);
--Nrecv;
bundleCellHasRays[bundleNestToRecv] = 1;
}
if(didSend)
{
MPI_Wait(&requestSend,&Stat);
--Nsend;
//move rays at end of vector to the old spot freed by bundleCellsRestrictedPeanoInd2Nest[i]
rayStartOfRaysToMove = NumAllRaysGlobal - NumRaysPerBundleCell;
bnestOfRaysToMove = (AllRaysGlobal[rayStartOfRaysToMove].nest >> shift);
if(bnestOfRaysToMove != bundleCellsRestrictedPeanoInd2Nest[i])
{
raysToMove = bundleCells[bnestOfRaysToMove].rays;
bundleCells[bnestOfRaysToMove].rays = bundleCells[bundleCellsRestrictedPeanoInd2Nest[i]].rays;
memcpy(bundleCells[bnestOfRaysToMove].rays,raysToMove,sizeof(HEALPixRay)*NumRaysPerBundleCell);
}
bundleCellHasRays[bundleCellsRestrictedPeanoInd2Nest[i]] = 0;
bundleCells[bundleCellsRestrictedPeanoInd2Nest[i]].rays = NULL;
bundleCells[bundleCellsRestrictedPeanoInd2Nest[i]].Nrays = 0;
NumAllRaysGlobal -= NumRaysPerBundleCell;
++i;
}
}
}
}
}
//recompute # of cells to send and recv
NumSendThisTask = 0;
NumRecvThisTask = 0;
for(i=0;i<NbundleCells;++i)
{
if(bundleCellHasRays[i] && !(ISSETBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL)))
++NumSendThisTask;
if(!(bundleCellHasRays[i]) && ISSETBITFLAG(bundleCells[i].active,PRIMARY_BUNDLECELL))
++NumRecvThisTask;
}
MPI_Allreduce(&NumSendThisTask,&GlobalNumSend,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
MPI_Allreduce(&NumRecvThisTask,&GlobalNumRecv,1,MPI_LONG,MPI_SUM,MPI_COMM_WORLD);
}
//clean it all up
free(firstRPITasks);
free(lastRPITasks);
free(bundleCellHasRays);
free(maxNumCellsToSend);
}
static void divide_tasks_domaindecomp(int firstTask, int lastTask, long firstPCell, long lastPCell, double *totCPUPerBundleCell)
{
long i,Nt,ind;
int splitTask;
long splitPCell;
long memUse,maxMemUse,maxMemUsePerTask;
double maxCpuUse,cpuUse;
/* - split array down the middle by cost
- set split task so that [firstTask,splitTask] and (splitTask,lastTask]
form a group of tasks
- call recursively unit all tasks are split
*/
Nt = lastTask - firstTask + 1;
if(Nt == 1)
{
firstRestrictedPeanoIndTasks[firstTask] = firstPCell;
lastRestrictedPeanoIndTasks[lastTask] = lastPCell;
return;
}
else if((Nt & (Nt - 1)) != 0) //not a power of 2 so divide by hand
{
maxMemUsePerTask = ((long) ((1.0 + rayTraceData.maxRayMemImbalance)*((double) NrestrictedPeanoInd)/((double) NTasks)));
maxCpuUse = 0.0;
for(i=firstPCell;i<=lastPCell;++i)
maxCpuUse += totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[i]];
maxCpuUse /= Nt;
ind = firstPCell;
firstRestrictedPeanoIndTasks[firstTask] = firstPCell;
memUse = 1.0;
cpuUse = totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[ind]];
++ind;
for(i=firstTask;i<lastTask;++i)
{
while(cpuUse < maxCpuUse && memUse < maxMemUsePerTask && (lastPCell - ind) > lastTask-i)
{
++ind;
memUse += 1.0;
cpuUse += totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[ind]];
}
lastRestrictedPeanoIndTasks[i] = ind;
firstRestrictedPeanoIndTasks[i+1] = ind + 1;
memUse = 1.0;
cpuUse = totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[ind+1]];
++ind;
}
lastRestrictedPeanoIndTasks[lastTask] = lastPCell;
return;
}
else //recursive subdiv
{
maxMemUsePerTask = ((long) ((1.0 + rayTraceData.maxRayMemImbalance)*((double) NrestrictedPeanoInd)/((double) NTasks)));
splitTask = (lastTask - firstTask)/2 + firstTask;
maxMemUse = (splitTask - firstTask + 1)*maxMemUsePerTask;
maxCpuUse = 0.0;
for(i=firstPCell;i<=lastPCell;++i)
maxCpuUse += totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[i]];
maxCpuUse /= 2.0;
splitPCell = firstPCell + 1;
memUse = 0.0;
cpuUse = 0.0;
for(i=firstPCell;i<=lastPCell;++i)
{
if(memUse + 1 == maxMemUse ||
(cpuUse + totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[i]] >= maxCpuUse && lastPCell - i <= (lastTask - splitTask)*maxMemUsePerTask))
{
splitPCell = i;
break;
}
memUse += 1;
cpuUse += totCPUPerBundleCell[bundleCellsRestrictedPeanoInd2Nest[i]];
}
//if(ThisTask == 0)
//fprintf(stderr,"%d: f,s,l task = %d|%d|%d, f,s,l pcell = %ld|%ld|%ld, memUse,cpuUse = %ld,%lf (%ld,%lf)\n",
// ThisTask,firstTask,splitTask,lastTask,firstPCell,splitPCell,lastPCell,memUse,cpuUse,maxMemUse,maxCpuUse);
assert(firstTask < NTasks && firstTask >= 0);
assert(lastTask < NTasks && lastTask >= 0);
assert(splitTask < NTasks-1 && splitTask >= 0);
assert(splitPCell < NrestrictedPeanoInd-1 && splitPCell >= 0);
firstRestrictedPeanoIndTasks[firstTask] = firstPCell;
lastRestrictedPeanoIndTasks[splitTask] = splitPCell;
firstRestrictedPeanoIndTasks[splitTask+1] = splitPCell+1;
lastRestrictedPeanoIndTasks[lastTask] = lastPCell;
//recursive calls here
divide_tasks_domaindecomp(firstTask,splitTask,firstPCell,splitPCell,totCPUPerBundleCell);
divide_tasks_domaindecomp(splitTask+1,lastTask,splitPCell+1,lastPCell,totCPUPerBundleCell);
return;
}
}
static int mightNeedToSendBuffCellsRPI(long sendTask, long recvTask, long *minRPITasks, long *maxRPITasks, long *firstRPITasks, long *lastRPITasks)
{
long needToSend = 0;
if((firstRPITasks[recvTask] <= minRPITasks[sendTask] && minRPITasks[sendTask] <= lastRPITasks[recvTask]) ||
(firstRPITasks[recvTask] <= maxRPITasks[sendTask] && maxRPITasks[sendTask] <= lastRPITasks[recvTask]) ||
(minRPITasks[sendTask] <= firstRPITasks[recvTask] && firstRPITasks[recvTask] <= maxRPITasks[sendTask]) ||
(minRPITasks[sendTask] <= lastRPITasks[recvTask] && lastRPITasks[recvTask] <= maxRPITasks[sendTask]) )
needToSend = 1;
return needToSend;
}