-
Notifications
You must be signed in to change notification settings - Fork 2
/
domain.c
1165 lines (938 loc) · 31.6 KB
/
domain.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include "allvars.h"
#include "proto.h"
/*! \file domain.c
* \brief code for domain decomposition
*
* This file contains the code for the domain decomposition of the
* simulation volume. The domains are constructed from disjoint subsets
* of the leaves of a fiducial top-level tree that covers the full
* simulation volume. Domain boundaries hence run along tree-node
* divisions of a fiducial global BH tree. As a result of this method, the
* tree force are in principle strictly independent of the way the domains
* are cut. The domain decomposition can be carried out for an arbitrary
* number of CPUs. Individual domains are not cubical, but spatially
* coherent since the leaves are traversed in a Peano-Hilbert order and
* individual domains form segments along this order. This also ensures
* that each domain has a small surface to volume ratio, which minimizes
* communication.
*/
#define TOPNODEFACTOR 20.0
#define REDUC_FAC 0.98
/*! toGo[task*NTask + partner] gives the number of particles in task 'task'
* that have to go to task 'partner'
*/
static int *toGo, *toGoSph;
static int *local_toGo, *local_toGoSph;
static int *list_NumPart;
static int *list_N_gas;
static int *list_load;
static int *list_loadsph;
static double *list_work;
static long long maxload, maxloadsph;
static struct topnode_exchange
{
peanokey Startkey;
int Count;
}
*toplist, *toplist_local;
/*! This is the main routine for the domain decomposition. It acts as a
* driver routine that allocates various temporary buffers, maps the
* particles back onto the periodic box if needed, and then does the
* domain decomposition, and a final Peano-Hilbert order of all particles
* as a tuning measure.
*/
void domain_Decomposition(void)
{
double t0, t1;
#ifdef PMGRID
if(All.PM_Ti_endstep == All.Ti_Current)
{
All.NumForcesSinceLastDomainDecomp = 1 + All.TotNumPart * All.TreeDomainUpdateFrequency;
/* to make sure that we do a domain decomposition before the PM-force is evaluated.
this is needed to make sure that the particles are wrapped into the box */
}
#endif
/* Check whether it is really time for a new domain decomposition */
if(All.NumForcesSinceLastDomainDecomp > All.TotNumPart * All.TreeDomainUpdateFrequency)
{
t0 = second();
#ifdef PERIODIC
do_box_wrapping(); /* map the particles back onto the box */
#endif
All.NumForcesSinceLastDomainDecomp = 0;
TreeReconstructFlag = 1; /* ensures that new tree will be constructed */
if(ThisTask == 0)
{
printf("domain decomposition... \n");
fflush(stdout);
}
Key = malloc(sizeof(peanokey) * All.MaxPart);
KeySorted = malloc(sizeof(peanokey) * All.MaxPart);
toGo = malloc(sizeof(int) * NTask * NTask);
toGoSph = malloc(sizeof(int) * NTask * NTask);
local_toGo = malloc(sizeof(int) * NTask);
local_toGoSph = malloc(sizeof(int) * NTask);
list_NumPart = malloc(sizeof(int) * NTask);
list_N_gas = malloc(sizeof(int) * NTask);
list_load = malloc(sizeof(int) * NTask);
list_loadsph = malloc(sizeof(int) * NTask);
list_work = malloc(sizeof(double) * NTask);
MPI_Allgather(&NumPart, 1, MPI_INT, list_NumPart, 1, MPI_INT, MPI_COMM_WORLD);
MPI_Allgather(&N_gas, 1, MPI_INT, list_N_gas, 1, MPI_INT, MPI_COMM_WORLD);
maxload = All.MaxPart * REDUC_FAC;
maxloadsph = All.MaxPartSph * REDUC_FAC;
domain_decompose();
free(list_work);
free(list_loadsph);
free(list_load);
free(list_N_gas);
free(list_NumPart);
free(local_toGoSph);
free(local_toGo);
free(toGoSph);
free(toGo);
if(ThisTask == 0)
{
printf("domain decomposition done. \n");
fflush(stdout);
}
t1 = second();
All.CPU_Domain += timediff(t0, t1);
#ifdef N_GRAVS
#ifdef PMGRID
#ifndef PEANOHILBERT
printf("ngravs: Type ordered Peano-Hilbert subordering required for N_GRAVS combined with PMGRID. Enable in Makefile and recompile.\n");
fflush(stdout);
endrun(1003);
#endif
#endif
#endif
#ifdef PEANOHILBERT
t0 = second();
peano_hilbert_order();
t1 = second();
All.CPU_Peano += timediff(t0, t1);
#endif
free(KeySorted);
free(Key);
}
}
/*! This function carries out the actual domain decomposition for all
* particle types. It will try to balance the work-load for each domain,
* as estimated based on the P[i]-GravCost values. The decomposition will
* respect the maximum allowed memory-imbalance given by the value of
* PartAllocFactor.
*/
void domain_decompose(void)
{
int i, j, status;
int ngrp, task, partner, sendcount, recvcount;
long long sumtogo, sumload;
int maxload, *temp;
double sumwork, maxwork;
for(i = 0; i < 6; i++)
NtypeLocal[i] = 0;
for(i = 0; i < N_GRAVS; i++)
NgravLocal[i] = 0;
for(i = 0; i < NumPart; i++) {
NtypeLocal[P[i].Type]++;
NgravLocal[TypeToGrav[P[i].Type]]++;
}
/* because Ntype[] is of type `long long', we cannot do a simple
* MPI_Allreduce() to sum the total particle numbers
*/
temp = malloc(NTask * 6 * sizeof(int));
MPI_Allgather(NtypeLocal, 6, MPI_INT, temp, 6, MPI_INT, MPI_COMM_WORLD);
for(i = 0; i < 6; i++)
{
Ntype[i] = 0;
for(j = 0; j < NTask; j++)
Ntype[i] += temp[j * 6 + i];
}
free(temp);
// KC 2/7/16
// Note that this only checks the active softenings,
// the only ones that affect particles present in the
// any particular initial condition.
#ifndef UNEQUALSOFTENINGS
for(i = 0; i < 6; i++)
if(Ntype[i] > 0)
break;
for(ngrp = i + 1; ngrp < 6; ngrp++)
{
if(Ntype[ngrp] > 0)
if(All.SofteningTable[ngrp] != All.SofteningTable[i])
{
if(ThisTask == 0)
{
fprintf(stdout, "Code was not compiled with UNEQUALSOFTENINGS, but some of the\n");
fprintf(stdout, "softening lengths are unequal nevertheless.\n");
fprintf(stdout, "This is not allowed.\n");
}
endrun(0);
}
}
#endif
/* determine global dimensions of domain grid */
domain_findExtent();
domain_determineTopTree();
/* determine cost distribution in domain grid */
domain_sumCost();
/* find the split of the domain grid recursively */
status = domain_findSplit(0, NTask, 0, NTopleaves - 1);
if(status != 0)
{
if(ThisTask == 0)
printf("\nNo domain decomposition that stays within memory bounds is possible.\n");
endrun(0);
}
/* now try to improve the work-load balance of the split */
domain_shiftSplit();
DomainMyStart = DomainStartList[ThisTask];
DomainMyLast = DomainEndList[ThisTask];
if(ThisTask == 0)
{
sumload = maxload = 0;
sumwork = maxwork = 0;
for(i = 0; i < NTask; i++)
{
sumload += list_load[i];
sumwork += list_work[i];
if(list_load[i] > maxload)
maxload = list_load[i];
if(list_work[i] > maxwork)
maxwork = list_work[i];
}
printf("work-load balance=%g memory-balance=%g\n",
maxwork / (sumwork / NTask), maxload / (((double) sumload) / NTask));
}
/* determine for each cpu how many particles have to be shifted to other cpus */
domain_countToGo();
for(i = 0, sumtogo = 0; i < NTask * NTask; i++)
sumtogo += toGo[i];
while(sumtogo > 0)
{
if(ThisTask == 0)
{
printf("exchange of %d%09d particles\n", (int) (sumtogo / 1000000000),
(int) (sumtogo % 1000000000));
fflush(stdout);
}
for(ngrp = 1; ngrp < (1 << PTask); ngrp++)
{
for(task = 0; task < NTask; task++)
{
partner = task ^ ngrp;
if(partner < NTask && task < partner)
{
/* treat SPH separately */
if(All.TotN_gas > 0)
{
domain_findExchangeNumbers(task, partner, 1, &sendcount, &recvcount);
list_NumPart[task] += recvcount - sendcount;
list_NumPart[partner] -= recvcount - sendcount;
list_N_gas[task] += recvcount - sendcount;
list_N_gas[partner] -= recvcount - sendcount;
toGo[task * NTask + partner] -= sendcount;
toGo[partner * NTask + task] -= recvcount;
toGoSph[task * NTask + partner] -= sendcount;
toGoSph[partner * NTask + task] -= recvcount;
if(task == ThisTask) /* actually carry out the exchange */
domain_exchangeParticles(partner, 1, sendcount, recvcount);
if(partner == ThisTask)
domain_exchangeParticles(task, 1, recvcount, sendcount);
}
domain_findExchangeNumbers(task, partner, 0, &sendcount, &recvcount);
list_NumPart[task] += recvcount - sendcount;
list_NumPart[partner] -= recvcount - sendcount;
toGo[task * NTask + partner] -= sendcount;
toGo[partner * NTask + task] -= recvcount;
if(task == ThisTask) /* actually carry out the exchange */
domain_exchangeParticles(partner, 0, sendcount, recvcount);
if(partner == ThisTask)
domain_exchangeParticles(task, 0, recvcount, sendcount);
}
}
}
for(i = 0, sumtogo = 0; i < NTask * NTask; i++)
sumtogo += toGo[i];
}
}
/*! This function tries to find a split point in a range of cells in the
* domain-grid. The range of cells starts at 'first', and ends at 'last'
* (inclusively). The number of cpus that holds the range is 'ncpu', with
* the first cpu given by 'cpustart'. If more than 2 cpus are to be split,
* the function calls itself recursively. The division tries to achieve a
* best particle-load balance under the constraint that 'maxload' and
* 'maxloadsph' may not be exceeded, and that each cpu holds at least one
* cell from the domaingrid. If such a decomposition cannot be achieved, a
* non-zero error code is returned.
*
* After successful completion, DomainMyStart[] and DomainMyLast[] contain
* the first and last cell of the domaingrid assigned to the local task
* for the given type. Also, DomainTask[] contains for each cell the task
* it was assigned to.
*/
int domain_findSplit(int cpustart, int ncpu, int first, int last)
{
int i, split, ok_left, ok_right;
long long load, sphload, load_leftOfSplit, sphload_leftOfSplit;
int ncpu_leftOfSplit;
double maxAvgLoad_CurrentSplit, maxAvgLoad_NewSplit;
ncpu_leftOfSplit = ncpu / 2;
for(i = first, load = 0, sphload = 0; i <= last; i++)
{
load += DomainCount[i];
sphload += DomainCountSph[i];
}
split = first + ncpu_leftOfSplit;
for(i = first, load_leftOfSplit = sphload_leftOfSplit = 0; i < split; i++)
{
load_leftOfSplit += DomainCount[i];
sphload_leftOfSplit += DomainCountSph[i];
}
/* find the best split point in terms of work-load balance */
while(split < last - (ncpu - ncpu_leftOfSplit - 1) && split > 0)
{
maxAvgLoad_CurrentSplit =
dmax(load_leftOfSplit / ncpu_leftOfSplit, (load - load_leftOfSplit) / (ncpu - ncpu_leftOfSplit));
maxAvgLoad_NewSplit =
dmax((load_leftOfSplit + DomainCount[split]) / ncpu_leftOfSplit,
(load - load_leftOfSplit - DomainCount[split]) / (ncpu - ncpu_leftOfSplit));
if(maxAvgLoad_NewSplit <= maxAvgLoad_CurrentSplit)
{
load_leftOfSplit += DomainCount[split];
sphload_leftOfSplit += DomainCountSph[split];
split++;
}
else
break;
}
/* we will now have to check whether this solution is possible given the restrictions on the maximum load */
for(i = first, load_leftOfSplit = 0, sphload_leftOfSplit = 0; i < split; i++)
{
load_leftOfSplit += DomainCount[i];
sphload_leftOfSplit += DomainCountSph[i];
}
if(load_leftOfSplit > maxload * ncpu_leftOfSplit ||
(load - load_leftOfSplit) > maxload * (ncpu - ncpu_leftOfSplit))
{
/* we did not find a viable split */
return -1;
}
if(sphload_leftOfSplit > maxloadsph * ncpu_leftOfSplit ||
(sphload - sphload_leftOfSplit) > maxloadsph * (ncpu - ncpu_leftOfSplit))
{
/* we did not find a viable split */
return -1;
}
if(ncpu_leftOfSplit >= 2)
ok_left = domain_findSplit(cpustart, ncpu_leftOfSplit, first, split - 1);
else
ok_left = 0;
if((ncpu - ncpu_leftOfSplit) >= 2)
ok_right = domain_findSplit(cpustart + ncpu_leftOfSplit, ncpu - ncpu_leftOfSplit, split, last);
else
ok_right = 0;
if(ok_left == 0 && ok_right == 0)
{
/* found a viable split */
if(ncpu_leftOfSplit == 1)
{
for(i = first; i < split; i++)
DomainTask[i] = cpustart;
list_load[cpustart] = load_leftOfSplit;
list_loadsph[cpustart] = sphload_leftOfSplit;
DomainStartList[cpustart] = first;
DomainEndList[cpustart] = split - 1;
}
if((ncpu - ncpu_leftOfSplit) == 1)
{
for(i = split; i <= last; i++)
DomainTask[i] = cpustart + ncpu_leftOfSplit;
list_load[cpustart + ncpu_leftOfSplit] = load - load_leftOfSplit;
list_loadsph[cpustart + ncpu_leftOfSplit] = sphload - sphload_leftOfSplit;
DomainStartList[cpustart + ncpu_leftOfSplit] = split;
DomainEndList[cpustart + ncpu_leftOfSplit] = last;
}
return 0;
}
/* we did not find a viable split */
return -1;
}
/*! This function tries to improve the domain decomposition found by
* domain_findSplit() with respect to work-load balance. To this end, the
* boundaries in the existing domain-split solution (which was found by
* trying to balance the particle load) are shifted as long as this leads
* to better work-load while still remaining within the allowed
* memory-imbalance constraints.
*/
void domain_shiftSplit(void)
{
int i, task, iter = 0, moved;
double maxw, newmaxw;
for(task = 0; task < NTask; task++)
list_work[task] = 0;
for(i = 0; i < NTopleaves; i++)
list_work[DomainTask[i]] += DomainWork[i];
do
{
for(task = 0, moved = 0; task < NTask - 1; task++)
{
maxw = dmax(list_work[task], list_work[task + 1]);
if(list_work[task] < list_work[task + 1])
{
newmaxw = dmax(list_work[task] + DomainWork[DomainStartList[task + 1]],
list_work[task + 1] - DomainWork[DomainStartList[task + 1]]);
if(newmaxw <= maxw)
{
if(list_load[task] + DomainCount[DomainStartList[task + 1]] <= maxload)
{
if(list_loadsph[task] + DomainCountSph[DomainStartList[task + 1]] > maxloadsph)
continue;
/* ok, we can move one domain cell from right to left */
list_work[task] += DomainWork[DomainStartList[task + 1]];
list_load[task] += DomainCount[DomainStartList[task + 1]];
list_loadsph[task] += DomainCountSph[DomainStartList[task + 1]];
list_work[task + 1] -= DomainWork[DomainStartList[task + 1]];
list_load[task + 1] -= DomainCount[DomainStartList[task + 1]];
list_loadsph[task + 1] -= DomainCountSph[DomainStartList[task + 1]];
DomainTask[DomainStartList[task + 1]] = task;
DomainStartList[task + 1] += 1;
DomainEndList[task] += 1;
moved++;
}
}
}
else
{
newmaxw = dmax(list_work[task] - DomainWork[DomainEndList[task]],
list_work[task + 1] + DomainWork[DomainEndList[task]]);
if(newmaxw <= maxw)
{
if(list_load[task + 1] + DomainCount[DomainEndList[task]] <= maxload)
{
if(list_loadsph[task + 1] + DomainCountSph[DomainEndList[task]] > maxloadsph)
continue;
/* ok, we can move one domain cell from left to right */
list_work[task] -= DomainWork[DomainEndList[task]];
list_load[task] -= DomainCount[DomainEndList[task]];
list_loadsph[task] -= DomainCountSph[DomainEndList[task]];
list_work[task + 1] += DomainWork[DomainEndList[task]];
list_load[task + 1] += DomainCount[DomainEndList[task]];
list_loadsph[task + 1] += DomainCountSph[DomainEndList[task]];
DomainTask[DomainEndList[task]] = task + 1;
DomainEndList[task] -= 1;
DomainStartList[task + 1] -= 1;
moved++;
}
}
}
}
iter++;
}
while(moved > 0 && iter < 10 * NTopleaves);
}
/*! This function counts how many particles have to be exchanged between
* two CPUs according to the domain split. If the CPUs are already quite
* full and hold data from other CPUs as well, not all the particles may
* be exchanged at once. In this case the communication phase has to be
* repeated, until enough of the third-party particles have been moved
* away such that the decomposition can be completed.
*/
void domain_findExchangeNumbers(int task, int partner, int sphflag, int *send, int *recv)
{
int numpartA, numpartsphA, ntobesentA, maxsendA, maxsendA_old;
int numpartB, numpartsphB, ntobesentB, maxsendB, maxsendB_old;
numpartA = list_NumPart[task];
numpartsphA = list_N_gas[task];
numpartB = list_NumPart[partner];
numpartsphB = list_N_gas[partner];
if(sphflag == 1)
{
ntobesentA = toGoSph[task * NTask + partner];
ntobesentB = toGoSph[partner * NTask + task];
}
else
{
ntobesentA = toGo[task * NTask + partner] - toGoSph[task * NTask + partner];
ntobesentB = toGo[partner * NTask + task] - toGoSph[partner * NTask + task];
}
maxsendA = imin(ntobesentA, All.BunchSizeDomain);
maxsendB = imin(ntobesentB, All.BunchSizeDomain);
do
{
maxsendA_old = maxsendA;
maxsendB_old = maxsendB;
maxsendA = imin(All.MaxPart - numpartB + maxsendB, maxsendA);
maxsendB = imin(All.MaxPart - numpartA + maxsendA, maxsendB);
}
while((maxsendA != maxsendA_old) || (maxsendB != maxsendB_old));
/* now make also sure that there is enough space for SPH particeles */
if(sphflag == 1)
{
do
{
maxsendA_old = maxsendA;
maxsendB_old = maxsendB;
maxsendA = imin(All.MaxPartSph - numpartsphB + maxsendB, maxsendA);
maxsendB = imin(All.MaxPartSph - numpartsphA + maxsendA, maxsendB);
}
while((maxsendA != maxsendA_old) || (maxsendB != maxsendB_old));
}
*send = maxsendA;
*recv = maxsendB;
}
/*! This function exchanges particles between two CPUs according to the
* domain split. In doing this, the memory boundaries which may restrict
* the exhange process are observed.
*/
void domain_exchangeParticles(int partner, int sphflag, int send_count, int recv_count)
{
int i, no, n, count, rep;
MPI_Status status;
for(n = 0, count = 0; count < send_count && n < NumPart; n++)
{
if(sphflag)
{
if(P[n].Type != 0)
continue;
}
else
{
if(P[n].Type == 0)
continue;
}
no = 0;
while(TopNodes[no].Daughter >= 0)
no = TopNodes[no].Daughter + (Key[n] - TopNodes[no].StartKey) / (TopNodes[no].Size / 8);
no = TopNodes[no].Leaf;
if(DomainTask[no] == partner)
{
if(sphflag) /* special reorder routine for SPH particles (need to stay at beginning) */
{
DomainPartBuf[count] = P[n]; /* copy particle and collect in contiguous memory */
DomainKeyBuf[count] = Key[n];
DomainSphBuf[count] = SphP[n];
P[n] = P[N_gas - 1];
P[N_gas - 1] = P[NumPart - 1];
Key[n] = Key[N_gas - 1];
Key[N_gas - 1] = Key[NumPart - 1];
SphP[n] = SphP[N_gas - 1];
N_gas--;
/* update local counts to reflect the departure */
NtypeLocal[0]--;
NgravLocal[0]--;
}
else
{
/* update local counts to reflect the departure */
NtypeLocal[P[n].Type]--;
NgravLocal[TypeToGrav[P[n].Type]]--;
DomainPartBuf[count] = P[n]; /* copy particle and collect in contiguous memory */
DomainKeyBuf[count] = Key[n];
P[n] = P[NumPart - 1];
Key[n] = Key[NumPart - 1];
}
count++;
NumPart--;
n--;
}
}
if(count != send_count)
{
printf("Houston, we got a problem...\n");
printf("ThisTask=%d count=%d send_count=%d\n", ThisTask, count, send_count);
endrun(88);
}
/* transmit */
for(rep = 0; rep < 2; rep++)
{
if((rep == 0 && ThisTask < partner) || (rep == 1 && ThisTask > partner))
{
if(send_count > 0)
{
MPI_Ssend(&DomainPartBuf[0], send_count * sizeof(struct particle_data), MPI_BYTE, partner,
TAG_PDATA, MPI_COMM_WORLD);
MPI_Ssend(&DomainKeyBuf[0], send_count * sizeof(peanokey), MPI_BYTE, partner, TAG_KEY,
MPI_COMM_WORLD);
if(sphflag)
MPI_Ssend(&DomainSphBuf[0], send_count * sizeof(struct sph_particle_data), MPI_BYTE, partner,
TAG_SPHDATA, MPI_COMM_WORLD);
}
}
if((rep == 1 && ThisTask < partner) || (rep == 0 && ThisTask > partner))
{
if(recv_count > 0)
{
if(sphflag)
{
if((NumPart - N_gas) > recv_count)
{
for(i = 0; i < recv_count; i++)
{
P[NumPart + i] = P[N_gas + i];
Key[NumPart + i] = Key[N_gas + i];
}
}
else
{
for(i = NumPart - 1; i >= N_gas; i--)
{
P[i + recv_count] = P[i];
Key[i + recv_count] = Key[i];
}
}
MPI_Recv(&P[N_gas], recv_count * sizeof(struct particle_data), MPI_BYTE, partner, TAG_PDATA,
MPI_COMM_WORLD, &status);
MPI_Recv(&Key[N_gas], recv_count * sizeof(peanokey), MPI_BYTE, partner, TAG_KEY,
MPI_COMM_WORLD, &status);
MPI_Recv(&SphP[N_gas], recv_count * sizeof(struct sph_particle_data), MPI_BYTE, partner,
TAG_SPHDATA, MPI_COMM_WORLD, &status);
/* Update local counts to reflect receipt */
NtypeLocal[0] += recv_count;
NgravLocal[0] += recv_count;
N_gas += recv_count;
}
else
{
MPI_Recv(&P[NumPart], recv_count * sizeof(struct particle_data), MPI_BYTE, partner,
TAG_PDATA, MPI_COMM_WORLD, &status);
MPI_Recv(&Key[NumPart], recv_count * sizeof(peanokey), MPI_BYTE, partner,
TAG_KEY, MPI_COMM_WORLD, &status);
/* Update the NtypeLocal[] counts to reflect the receipt */
for(i = NumPart; i < NumPart + recv_count; ++i) {
NtypeLocal[P[i].Type]++;
NgravLocal[TypeToGrav[P[i].Type]]++;
}
}
NumPart += recv_count;
}
}
}
}
/*! This function determines how many particles that are currently stored
* on the local CPU have to be moved off according to the domain
* decomposition.
*/
void domain_countToGo(void)
{
int n, no;
for(n = 0; n < NTask; n++)
{
local_toGo[n] = 0;
local_toGoSph[n] = 0;
}
for(n = 0; n < NumPart; n++)
{
no = 0;
while(TopNodes[no].Daughter >= 0)
no = TopNodes[no].Daughter + (Key[n] - TopNodes[no].StartKey) / (TopNodes[no].Size / 8);
no = TopNodes[no].Leaf;
if(DomainTask[no] != ThisTask)
{
local_toGo[DomainTask[no]] += 1;
if(P[n].Type == 0)
local_toGoSph[DomainTask[no]] += 1;
}
}
MPI_Allgather(local_toGo, NTask, MPI_INT, toGo, NTask, MPI_INT, MPI_COMM_WORLD);
MPI_Allgather(local_toGoSph, NTask, MPI_INT, toGoSph, NTask, MPI_INT, MPI_COMM_WORLD);
}
/*! This function walks the global top tree in order to establish the
* number of leaves it has. These leaves are distributed to different
* processors.
*/
void domain_walktoptree(int no)
{
int i;
if(TopNodes[no].Daughter == -1)
{
TopNodes[no].Leaf = NTopleaves;
NTopleaves++;
}
else
{
for(i = 0; i < 8; i++)
domain_walktoptree(TopNodes[no].Daughter + i);
}
}
/*! This routine bins the particles onto the domain-grid, i.e. it sums up the
* total number of particles and the total amount of work in each of the
* domain-cells. This information forms the basis for the actual decision on
* the adopted domain decomposition.
*/
void domain_sumCost(void)
{
int i, n, no;
double *local_DomainWork;
int *local_DomainCount;
int *local_DomainCountSph;
local_DomainWork = malloc(NTopnodes * sizeof(double));
local_DomainCount = malloc(NTopnodes * sizeof(int));
local_DomainCountSph = malloc(NTopnodes * sizeof(int));
NTopleaves = 0;
domain_walktoptree(0);
for(i = 0; i < NTopleaves; i++)
{
local_DomainWork[i] = 0;
local_DomainCount[i] = 0;
local_DomainCountSph[i] = 0;
}
if(ThisTask == 0)
printf("NTopleaves= %d\n", NTopleaves);
for(n = 0; n < NumPart; n++)
{
no = 0;
while(TopNodes[no].Daughter >= 0)
no = TopNodes[no].Daughter + (Key[n] - TopNodes[no].StartKey) / (TopNodes[no].Size / 8);
no = TopNodes[no].Leaf;
if(P[n].Ti_endstep > P[n].Ti_begstep)
local_DomainWork[no] += (1.0 + P[n].GravCost) / (P[n].Ti_endstep - P[n].Ti_begstep);
else
local_DomainWork[no] += (1.0 + P[n].GravCost);
local_DomainCount[no] += 1;
if(P[n].Type == 0)
local_DomainCountSph[no] += 1;
}
MPI_Allreduce(local_DomainWork, DomainWork, NTopleaves, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(local_DomainCount, DomainCount, NTopleaves, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(local_DomainCountSph, DomainCountSph, NTopleaves, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
free(local_DomainCountSph);
free(local_DomainCount);
free(local_DomainWork);
}
/*! This routine finds the extent of the global domain grid.
*/
void domain_findExtent(void)
{
int i, j;
double len, xmin[3], xmax[3], xmin_glob[3], xmax_glob[3];
/* determine local extension */
for(j = 0; j < 3; j++)
{
xmin[j] = MAX_REAL_NUMBER;
xmax[j] = -MAX_REAL_NUMBER;
}
for(i = 0; i < NumPart; i++)
{
for(j = 0; j < 3; j++)
{
if(xmin[j] > P[i].Pos[j])
xmin[j] = P[i].Pos[j];
if(xmax[j] < P[i].Pos[j])
xmax[j] = P[i].Pos[j];
}
}
MPI_Allreduce(xmin, xmin_glob, 3, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce(xmax, xmax_glob, 3, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
len = 0;
for(j = 0; j < 3; j++)
if(xmax_glob[j] - xmin_glob[j] > len)
len = xmax_glob[j] - xmin_glob[j];
len *= 1.001;
for(j = 0; j < 3; j++)
{
DomainCenter[j] = 0.5 * (xmin_glob[j] + xmax_glob[j]);
DomainCorner[j] = 0.5 * (xmin_glob[j] + xmax_glob[j]) - 0.5 * len;
}
DomainLen = len;
DomainFac = 1.0 / len * (((peanokey) 1) << (BITS_PER_DIMENSION));
}
/*! This function constructs the global top-level tree node that is used
* for the domain decomposition. This is done by considering the string of
* Peano-Hilbert keys for all particles, which is recursively chopped off
* in pieces of eight segments until each segment holds at most a certain
* number of particles.
*/
void domain_determineTopTree(void)
{
int i, ntop_local, ntop;
int *ntopnodelist, *ntopoffset;
for(i = 0; i < NumPart; i++)
{
KeySorted[i] = Key[i] = peano_hilbert_key((P[i].Pos[0] - DomainCorner[0]) * DomainFac,
(P[i].Pos[1] - DomainCorner[1]) * DomainFac,
(P[i].Pos[2] - DomainCorner[2]) * DomainFac,
BITS_PER_DIMENSION);
}
qsort(KeySorted, NumPart, sizeof(peanokey), domain_compare_key);
NTopnodes = 1;
TopNodes[0].Daughter = -1;
TopNodes[0].Size = PEANOCELLS;
TopNodes[0].StartKey = 0;
TopNodes[0].Count = NumPart;
TopNodes[0].Pstart = 0;
domain_topsplit_local(0, 0);
toplist_local = malloc(NTopnodes * sizeof(struct topnode_exchange));
for(i = 0, ntop_local = 0; i < NTopnodes; i++)
{
if(TopNodes[i].Daughter == -1) /* only use leaves */
{
toplist_local[ntop_local].Startkey = TopNodes[i].StartKey;
toplist_local[ntop_local].Count = TopNodes[i].Count;
ntop_local++;
}
}
ntopnodelist = malloc(sizeof(int) * NTask);
ntopoffset = malloc(sizeof(int) * NTask);
MPI_Allgather(&ntop_local, 1, MPI_INT, ntopnodelist, 1, MPI_INT, MPI_COMM_WORLD);
for(i = 0, ntop = 0, ntopoffset[0] = 0; i < NTask; i++)
{
ntop += ntopnodelist[i];
if(i > 0)
ntopoffset[i] = ntopoffset[i - 1] + ntopnodelist[i - 1];
}
toplist = malloc(ntop * sizeof(struct topnode_exchange));
for(i = 0; i < NTask; i++)
{
ntopnodelist[i] *= sizeof(struct topnode_exchange);
ntopoffset[i] *= sizeof(struct topnode_exchange);
}
MPI_Allgatherv(toplist_local, ntop_local * sizeof(struct topnode_exchange), MPI_BYTE,
toplist, ntopnodelist, ntopoffset, MPI_BYTE, MPI_COMM_WORLD);
qsort(toplist, ntop, sizeof(struct topnode_exchange), domain_compare_toplist);
NTopnodes = 1;
TopNodes[0].Daughter = -1;
TopNodes[0].Size = PEANOCELLS;
TopNodes[0].StartKey = 0;
TopNodes[0].Count = All.TotNumPart;
TopNodes[0].Pstart = 0;