forked from N-BodyShop/changa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compute.cpp
2741 lines (2420 loc) · 89.6 KB
/
Compute.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
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 "ParallelGravity.h"
#include "GenericTreeNode.h"
//#include "codes.h"
#include "Opt.h"
#include "Compute.h"
#include "TreeWalk.h"
#include "State.h"
#include "Space.h"
#include "gravity.h"
int decodeReqID(int reqID);
void Compute::setOpt(Opt *_opt){
opt = _opt;
}
OptType Compute::getOptType(){
return opt->getSelfType();
}
void Compute::init(void *buck, int ar, Opt *o){
computeEntity = buck;
activeRung = ar;
opt = o;
}
State *Compute::getNewState(int dim1, int dim2){
State *s = new State();
// 2 arrays of counters
// 0. numAdditionalRequests[] - sized numBuckets, init to numChunks
// 1. remaining Chunk[] - sized numChunks
s->counterArrays[0] = new int [dim1];
s->counterArrays[1] = new int [dim2];
s->currentBucket = 0;
s->bWalkDonePending = 0;
// this variable shouldn't be used at all in the remote walk
s->myNumParticlesPending = -1;
return s;
}
State *Compute::getNewState(int dim1){
// 0. local component of numAdditionalRequests, init to 1
State *s = new State();
s->counterArrays[0] = new int [dim1];
s->counterArrays[1] = 0;
s->currentBucket = 0;
s->bWalkDonePending = 0;
// this is used by local walks
// not prefetch ones, even though
// prefetch computes use this version
// of gtNewState
s->myNumParticlesPending = dim1;
return s;
}
State *Compute::getNewState(){
return 0;
}
void Compute::freeState(State *s){
if(s->counterArrays[0]){
delete [] s->counterArrays[0];
s->counterArrays[0] = 0;
}
if(s->counterArrays[1]){
delete [] s->counterArrays[1];
s->counterArrays[1] = 0;
}
delete s;
}
#if INTERLIST_VER > 0
/// @brief Version that frees a DoubleWalkState
void ListCompute::freeState(State *s){
freeDoubleWalkState((DoubleWalkState *)s);
Compute::freeState(s);
}
/// @brief Free up all the lists.
void ListCompute::freeDoubleWalkState(DoubleWalkState *state){
for(int i = 0; i < INTERLIST_LEVELS; i++){
state->undlists[i].free();
state->clists[i].free();
}
delete [] state->chklists;
state->undlists.free();
state->clists.free();
if(state->rplists.length() > 0){
for(int i = 0; i < INTERLIST_LEVELS; i++){
state->rplists[i].free();
}
state->rplists.free();
}
else if(state->lplists.length() > 0){
for(int i = 0; i < INTERLIST_LEVELS; i++){
state->lplists[i].free();
}
state->lplists.free();
}
#ifdef CUDA
if (!bUseCpu) {
state->nodeLists.free();
state->particleLists.free();
}
#endif
if(state->placedRoots){
delete [] state->placedRoots;
state->placedRoots = 0;
}
}
DoubleWalkState *ListCompute::allocDoubleWalkState(){
DoubleWalkState *s = new DoubleWalkState;
s->level = 0;
s->chklists = new CheckList[INTERLIST_LEVELS];
s->undlists.resize(INTERLIST_LEVELS);
s->clists.resize(INTERLIST_LEVELS);
if(getOptType() == Remote){
s->rplists.resize(INTERLIST_LEVELS);
}
else if(getOptType() == Local){
s->lplists.resize(INTERLIST_LEVELS);
}
return s;
}
/// @brief Version that allocates a DoubleWalkState
State *ListCompute::getNewState(int d1, int d2){
DoubleWalkState *s = allocDoubleWalkState();
s->counterArrays[0] = new int [d1];
s->counterArrays[1] = new int [d2];
// one boolean for each chunk
s->placedRoots = new bool [d2];
s->currentBucket = 0;
s->bWalkDonePending = 0;
s->myNumParticlesPending = -1;
return s;
}
/// @brief Version that allocates a DoubleWalkState
State *ListCompute::getNewState(int d1){
DoubleWalkState *s = allocDoubleWalkState();
s->counterArrays[0] = new int [d1];
s->counterArrays[1] = 0;
// no concept of chunks in local computation
s->placedRoots = new bool [1];
s->currentBucket = 0;
s->bWalkDonePending = 0;
s->myNumParticlesPending = d1;
return s;
}
/// @brief Version that allocates a DoubleWalkState
State *ListCompute::getNewState(){
DoubleWalkState *s = allocDoubleWalkState();
s->counterArrays[0] = 0;
s->counterArrays[1] = 0;
// this function used for remote-resume states,
// no placedRoots vars required
s->placedRoots = 0;
s->currentBucket = 0;
s->bWalkDonePending = 0;
s->myNumParticlesPending = -1;
return s;
}
/// @brief Clear lists.
void ListCompute::initState(State *state){
DoubleWalkState *s = (DoubleWalkState *)state;
int level = s->level;
UndecidedList &myUndlist = s->undlists[level];
// *my* undecided list:
myUndlist.length() = 0;
// interaction lists:
s->clists[level].length() = 0;
// s->clists[level].reserve(1000);
if(getOptType() == Local){
s->lplists[level].length() = 0;
// s->lplists[level].reserve(100);
}
else if(getOptType() == Remote){
s->rplists[level].length() = 0;
// s->rplists[level].reserve(100);
}
else{
CkAbort("Invalid Opt type for ListCompute");
}
}
#endif
void GravityCompute::reassoc(void *ce, int ar, Opt *o){
computeEntity = ce;
activeRung = ar;
opt = o;
}
#if INTERLIST_VER > 0
/// @brief Reassociate the target node.
void ListCompute::reassoc(void *ce, int ar, Opt *o){
computeEntity = ce;
activeRung = ar;
opt = o;
}
#endif
void GravityCompute::nodeMissedEvent(int reqID, int chunk, State *state, TreePiece *tp){
if(getOptType() == Remote){
state->counterArrays[0][decodeReqID(reqID)]++;
state->counterArrays[1][chunk]++;
}
}
void PrefetchCompute::startNodeProcessEvent(State *state){
//return owner->incPrefetchWaiting();
state->counterArrays[0][0]++;
//return state->counterArrays[0][0];
}
void PrefetchCompute::finishNodeProcessEvent(TreePiece *owner, State *state){
//int save = owner->decPrefetchWaiting();
int save = --state->counterArrays[0][0];
if(save == 0){
owner->startRemoteChunk();
}
}
#if INTERLIST_VER > 0
/// @brief Update state on a node miss.
/// @param reqID unused.
void ListCompute::nodeMissedEvent(int reqID, int chunk, State *state, TreePiece *tp){
CkAssert(getOptType() == Remote);
#ifdef CHANGA_REFACTOR_MEMCHECK
CkPrintf("memcheck before nodemissed\n");
CmiMemoryCheck();
#endif
int startBucket;
int end;
GenericTreeNode *source = (GenericTreeNode *)computeEntity;
tp->getBucketsBeneathBounds(source, startBucket, end);
tp->updateUnfinishedBucketState(startBucket, end, 1, chunk, state);
#ifdef CHANGA_REFACTOR_MEMCHECK
CkPrintf("memcheck after nodemissed\n");
CmiMemoryCheck();
#endif
}
#endif
int GravityCompute::openCriterion(TreePiece *ownerTP,
GenericTreeNode *node, int reqID, State *state){
return
openCriterionBucket(node,(GenericTreeNode *)computeEntity,ownerTP->decodeOffset(reqID));
}
void GravityCompute::recvdParticles(ExternalGravityParticle *part,int num,int chunk,int reqID,State *state,TreePiece *tp, Tree::NodeKey &remoteBucket){
//TreePiece *tp = tw->getOwnerTP();
Vector3D<cosmoType> offset = tp->decodeOffset(reqID);
int reqIDlist = decodeReqID(reqID);
CkAssert(num > 0);
state->counterArrays[0][reqIDlist] -= 1;
state->counterArrays[1][chunk] -= 1;
GenericTreeNode* reqnode = tp->bucketList[reqIDlist];
int computed = 0;
#ifdef BENCHMARK_TIME_COMPUTE
double startTime = CmiWallTimer();
#endif
for(int i=0;i<num;i++){
#if COSMO_STATS > 1
for(int j = reqnode->firstParticle; j <= reqnode->lastParticle; ++j) {
tp->myParticles[j].extpartmass += part[i].mass;
}
#endif
#ifdef COSMO_EVENTS
double startTimer = CmiWallTimer();
#endif
#ifdef HPM_COUNTER
hpmStart(2,"particle force");
#endif
computed = partBucketForce(&part[i], reqnode, tp->myParticles, offset, activeRung);
#ifdef HPM_COUNTER
hpmStop(2);
#endif
#ifdef COSMO_EVENTS
traceUserBracketEvent(partForceUE, startTimer, CmiWallTimer());
#endif
}
#ifdef BENCHMARK_TIME_COMPUTE
computeTimePart += CmiWallTimer() - startTime;
#endif
tp->particleInterRemote[chunk] += computed * num;
tp->finishBucket(reqIDlist);
CkAssert(state->counterArrays[1][chunk] >= 0);
if (state->counterArrays[1][chunk] == 0) {
cacheGravPart[CkMyPe()].finishedChunk(chunk, tp->particleInterRemote[chunk]);
#ifdef CHECK_WALK_COMPLETIONS
CkPrintf("[%d] finishedChunk %d GravityCompute::recvdParticles\n", tp->getIndex(), chunk);
#endif
tp->finishedChunk(chunk);
}
}
void PrefetchCompute::recvdParticles(ExternalGravityParticle *egp,int num,int chunk,int reqID,State *state, TreePiece *tp, Tree::NodeKey &remoteBucket){
//#ifdef CUDA
// wait for prefetched particles as well
// this way, all nodes/parts not missed will be handled by RNR
// and all those missed, by RR
// if we didn't wait for particles
// it could transpire that we don't miss on these particles during RNR, but they aren't present on the gpu
// and so we'd have to have a separate array of missed particles for the RNR (in much the same way that the RR has
// separate arrays for missed nodes and particles)
finishNodeProcessEvent(tp, state);
//#endif
}
void GravityCompute::nodeRecvdEvent(TreePiece *owner, int chunk, State *state, int reqIDlist){
state->counterArrays[0][reqIDlist]--;
owner->finishBucket(reqIDlist);
CkAssert(chunk >= 0);
state->counterArrays[1][chunk] --;
CkAssert(state->counterArrays[1][chunk] >= 0);
if (state->counterArrays[1][chunk] == 0) {
cacheGravPart[CkMyPe()].finishedChunk(chunk, owner->particleInterRemote[chunk]);
#ifdef CHECK_WALK_COMPLETIONS
CkPrintf("[%d] finishedChunk %d GravityCompute::nodeRecvdEvent\n", owner->getIndex(), chunk);
#endif
owner->finishedChunk(chunk);
}// end if finished with chunk
}
#if INTERLIST_VER > 0
/// @brief Update state upon receiving a remote node.
///
/// Calls TreePiece::finishedChunk() if all outstanding requests are satisfied.
void ListCompute::nodeRecvdEvent(TreePiece *owner, int chunk, State *state, int reqIDlist){
int start, end;
GenericTreeNode *source = (GenericTreeNode *)computeEntity;
owner->getBucketsBeneathBounds(source, start, end);
owner->updateBucketState(start, end, 1, chunk, state);
CkAssert(chunk >= 0);
int remainingChunk;
remainingChunk = state->counterArrays[1][chunk];
#ifdef CHANGA_REFACTOR_MEMCHECK
CkPrintf("memcheck after noderecvd\n");
CmiMemoryCheck();
#endif
CkAssert(remainingChunk >= 0);
#if COSMO_PRINT_BK > 1
CkPrintf("[%d] nodeRecvdEvent chunk: %d remainingChunk: %d\n", owner->getIndex(), chunk, remainingChunk);
#endif
if (remainingChunk == 0) {
#ifdef CUDA
if (!bUseCpu) {
// no more nodes/particles are going to be delivered by the cache
// flush the interactions remaining in the state
DoubleWalkState *ds = (DoubleWalkState *)state;
if(ds->nodeLists.totalNumInteractions > 0){
sendNodeInteractionsToGpu(ds, owner);
resetCudaNodeState(ds);
}
if(ds->particleLists.totalNumInteractions > 0){
sendPartInteractionsToGpu(ds, owner);
resetCudaPartState(ds);
}
}
#endif
#if COSMO_PRINT_BK > 1
CkPrintf("[%d] FINISHED CHUNK %d from nodeRecvdEvent\n", owner->getIndex(), chunk);
#endif
cacheGravPart[CkMyPe()].finishedChunk(chunk, owner->particleInterRemote[chunk]);
#ifdef CHECK_WALK_COMPLETIONS
CkPrintf("[%d] finishedChunk %d ListCompute::nodeRecvdEvent\n", owner->getIndex(), chunk);
#endif
owner->finishedChunk(chunk);
}// end if finished with chunk
}
#endif
#include "TreeNode.h"
using namespace TreeStuff;
int GravityCompute::doWork(GenericTreeNode *node, TreeWalk *tw,
State *state, int chunk, int reqID, bool isRoot, bool &didcomp, int awi){
// ignores state
TreePiece *tp = tw->getOwnerTP();
if(node->getType() == Empty || node->getType() == CachedEmpty){
#ifdef CHANGA_REFACTOR_WALKCHECK
if(node->parent->getType() != Boundary || getOptType() == Local){
int bucketIndex = decodeReqID(reqID);
tp->addToBucketChecklist(bucketIndex, node->getKey());
tp->combineKeys(node->getKey(), bucketIndex);
}
#endif
return DUMP;
}
int open;
open = openCriterion(tp, node, reqID, state);
CkAssert(opt != NULL);
int action = opt->action(open, node);
if(action == KEEP){ // keep node
return KEEP;
}
else if(action == COMPUTE){
//CkPrintf("GravityCompute %d bucket %llu node %llu\n", tp->getIndex(), ((GenericTreeNode*)computeEntity)->getKey(), node->getKey());
//CkPrintf("GravityCompute %d bucket %llu node %llu\n", tp->getIndex(), keyBits(((GenericTreeNode*)computeEntity)->getKey(),63).c_str(), keyBits(node->getKey(),63).c_str());
didcomp = true;
#ifdef BENCHMARK_TIME_COMPUTE
double startTime = CmiWallTimer();
#endif
int computed = nodeBucketForce(node,
(GenericTreeNode *)computeEntity,
tp->getParticles(),
tp->decodeOffset(reqID),
activeRung);
GenericTreeNode *b = (GenericTreeNode *)computeEntity;
updateInterMass(b->particlePointer,b->firstParticle,b->lastParticle,node->moments.totalMass);
#ifdef BENCHMARK_TIME_COMPUTE
computeTimeNode += CmiWallTimer() - startTime;
#endif
if(getOptType() == Remote){
tp->addToNodeInterRemote(chunk, computed);
}
else if(getOptType() == Local){
tp->addToNodeInterLocal(computed);
}
#ifdef CHANGA_REFACTOR_WALKCHECK
int bucketIndex = decodeReqID(reqID);
tp->addToBucketChecklist(bucketIndex, node->getKey());
tp->combineKeys(node->getKey(), bucketIndex);
#endif
return DUMP;
}
else if(action == KEEP_LOCAL_BUCKET){
didcomp = true;
//CkPrintf("GravityCompute %d bucket %llu local bucket %llu\n", tp->getIndex(), ((GenericTreeNode*)computeEntity)->getKey(), node->getKey());
//CkPrintf("GravityCompute %d bucket %s local bucket %s\n", tp->getIndex(), keyBits(((GenericTreeNode*)computeEntity)->getKey(),63).c_str(), keyBits(node->getKey(),63).c_str());
#if CHANGA_REFACTOR_DEBUG > 2
CkAssert(node->getType() == Bucket);
CkPrintf("[%d] GravityCompute told to KEEP_LOCAL_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
// since this is a local bucket, we should have the particles at hand
GravityParticle *part = node->particlePointer;
CkAssert(part);
//ckerr << "(keep_local_bucket) particlePointer[0] - mass: " << part[0].mass << endl;
int computed = 0;
#ifdef BENCHMARK_TIME_COMPUTE
double startTime = CmiWallTimer();
#endif
Vector3D<cosmoType> offset = tp->decodeOffset(reqID);
for(int i = node->firstParticle; i <= node->lastParticle; i++){
computed += partBucketForce(
&part[i-node->firstParticle],
(GenericTreeNode *)computeEntity,
tp->getParticles(),
offset,
activeRung);
GenericTreeNode *b = (GenericTreeNode *)computeEntity;
updateInterMass(b->particlePointer,b->firstParticle,b->lastParticle,&part[i-node->firstParticle],offset);
}
#ifdef BENCHMARK_TIME_COMPUTE
computeTimePart += CmiWallTimer() - startTime;
#endif
// we could have done the following, because this is a KEEP_LOCAL_BUCKET
//
// tp->addToParticleInterLocal(computed);
//
// to be sure, though, we do instead:
if(getOptType() == Remote){
tp->addToParticleInterRemote(chunk, computed);
}
else if(getOptType() == Local){
tp->addToParticleInterLocal(computed);
}
#ifdef CHANGA_REFACTOR_WALKCHECK
int bucketIndex = decodeReqID(reqID);
tp->addToBucketChecklist(bucketIndex, node->getKey());
tp->combineKeys(node->getKey(), bucketIndex);
#endif
return DUMP;
}
else if(action == KEEP_REMOTE_BUCKET){
didcomp = true;
// fetch particles and compute.
//CkPrintf("GravityCompute %d bucket %s remote bucket %s\n", tp->getIndex(), ((GenericTreeNode*)computeEntity)->getKey(), node->getKey());
//CkPrintf("GravityCompute %d bucket %s remote bucket %s\n", tp->getIndex(), keyBits(((GenericTreeNode*)computeEntity)->getKey(),63).c_str(), keyBits(node->getKey(),63).c_str());
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] GravityCompute told to KEEP_REMOTE_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
Tree::NodeKey keyref = node->getKey();
ExternalGravityParticle *part;
part = tp->requestParticles(keyref, chunk, node->remoteIndex,
node->firstParticle, node->lastParticle,
reqID, awi, computeEntity);
if(part){
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("Particles found in cache\n");
#endif
int computed = computeParticleForces(tp, node, part, reqID);
// jetley
//CkAssert(chunk >= 0);
//tp->addToParticleInterRemote(chunk, computed);
if(getOptType() == Remote){
tp->addToParticleInterRemote(chunk, computed);
}
else if(getOptType() == Local){
tp->addToParticleInterLocal(computed);
}
}
else{
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("Particles not found in cache\n");
#endif
if(getOptType() == Remote){
state->counterArrays[0][decodeReqID(reqID)] += 1;
state->counterArrays[1][chunk] += 1;
}
}
return DUMP;
}
else if(action == DUMP || action == NOP){
return DUMP;
}
CkAbort("bad walk state");
return -1;
}
void GravityCompute::updateInterMass(GravityParticle *p, int start, int end, double totalMass){
for(int j = start; j <= end; j++){
p[j-start].interMass += totalMass;
}
}
void GravityCompute::updateInterMass(GravityParticle *p, int start, int end, GravityParticle *s, Vector3D<cosmoType> &offset){
Vector3D<cosmoType> r;
for(int j = start; j <= end; j++){
r = offset + s->position - p[j-start].position;
if(r.lengthSquared() == 0) continue;
p[j-start].interMass += s->mass;
}
}
///
/// @brief Calculate forces due to particles in a node
/// @param ownerTP TreePiece of target particles
/// @param node GenericTreeNode containing source particles
/// @param part array of source particles
/// @param reqID bucket number of target particles and offset
///
int GravityCompute::computeParticleForces(TreePiece *ownerTP, GenericTreeNode *node, ExternalGravityParticle *part, int reqID){
int computed = 0;
#ifdef BENCHMARK_TIME_COMPUTE
double startTime = CmiWallTimer();
#endif
for(int i = node->firstParticle; i <= node->lastParticle; i++){
computed += partBucketForce(
&part[i-node->firstParticle],
(GenericTreeNode *)computeEntity,
ownerTP->getParticles(),
ownerTP->decodeOffset(reqID),
activeRung);
}
#ifdef BENCHMARK_TIME_COMPUTE
computeTimePart += CmiWallTimer() - startTime;
#endif
#ifdef CHANGA_REFACTOR_WALKCHECK
int bucketIndex = decodeReqID(reqID);
ownerTP->addToBucketChecklist(bucketIndex, node->getKey());
ownerTP->combineKeys(node->getKey(), bucketIndex);
#endif
return computed;
}
int PrefetchCompute::openCriterion(TreePiece *ownerTP,
GenericTreeNode *node, int reqID, State *state){
Vector3D<cosmoType> offset = ownerTP->decodeOffset(reqID);
BinaryTreeNode testNode;
testNode.boundingBox = ownerTP->prefetchReq;
if(openCriterionBucket(node, &testNode, offset))
return 1;
return 0;
}
int PrefetchCompute::doWork(GenericTreeNode *node, TreeWalk *tw, State *state, int chunk, int reqID, bool isRoot, bool &didcomp, int awi){
TreePiece *tp = tw->getOwnerTP();
// ignores state
CkMustAssert(!(node == NULL), "PrefetchComputedoWork() given NULL node");
int open = 0;
open = openCriterion(tp, node, reqID, state);
int decision = opt->action(open, node);
if (decision == DUMP || decision == KEEP){
return decision;
}
else if(decision == KEEP_REMOTE_BUCKET){
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] PrefetchCompute told to KEEP_REMOTE_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
Tree::NodeKey keyref = node->getKey();
ExternalGravityParticle *part;
part = tp->requestParticles(keyref, chunk, node->remoteIndex,
node->firstParticle, node->lastParticle, reqID,
awi, (void *)0);
if(part == NULL){
//#ifdef CUDA
// waiting for particles for reasons discussed
// in comments within recvdParticles
state->counterArrays[0][0]++;
//#endif
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] Particles not found in cache\n", tp->getIndex());
#endif
}
else{
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] Particles found in cache\n", tp->getIndex());
#endif
}
return DUMP;
}
else if(decision == KEEP_LOCAL_BUCKET){
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] PrefetchCompute told to KEEP_LOCAL_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
CkAbort("PrefetchOpt told PrefetchCompute to KEEP_LOCAL_BUCKET. This shouldn't happen.\n");
}
CkAbort("PrefetchCompute: bad option");
return -1;
}
#if INTERLIST_VER > 0
/// @brief Process a node.
/// @param node is the global node being processed.
/// @param state contains the lists to be checked.
/// @param chunk chunk we are walking; used in case of a miss
/// @param reqID Encodes offset and bucket
/// @param awi Active walk index; used in case of a miss
/// @return KEEP if we descend further down the tree
int ListCompute::doWork(GenericTreeNode *node, TreeWalk *tw, State *state, int chunk, int reqID, bool isRoot, bool &didcomp, int awi){
DoubleWalkState *s = (DoubleWalkState *)state;
int level = s->level;
CheckList &chklist = s->chklists[level];
UndecidedList &undlist = s->undlists[level];
TreePiece *tp = tw->getOwnerTP();
Vector3D<cosmoType> offset = tp->decodeOffset(reqID);
if(node->getType() == Empty || node->getType() == CachedEmpty){
#ifdef CHANGA_REFACTOR_WALKCHECK_INTERLIST
if(node->parent->getType() != Boundary || getOptType() == Local){
addNodeToInt(node, reqID, s);
}
#endif
return DUMP;
}
// check opening criterion
int open;
open = openCriterion(tp, node, reqID, state);
//CkPrintf("[%d] open: %d\n", tp->getIndex(), open);
int fakeOpen;
// in the interlist version, there are three possible return
// values for the opencriterin function, whereas the Opt object
// only knows about two (true, open and false, no need to open).
// convert -1 (ancestor fully contained) to true (open)
if(open == INTERSECT || open == CONTAIN){
fakeOpen = 1;
}
else{
fakeOpen = 0;
}
int action = opt->action(fakeOpen, node);
if(action == KEEP){
if(open == CONTAIN)
{
// ancestor is a node and is contained
// only enqueue nodes if they are available
addChildrenToCheckList(node, reqID, chunk, awi, s, chklist, tp);
// DUMP, because
// we've just added the children of the glbl node to the chklist,
// so it won't be empty when dowork returns to dft
// if the local node needs to be opened by someone in the chklist,
// it will be, eventually. We should return KEEP only when we have
// added things to the undecided list,
// i.e. when we are emptying the chklist and not adding nodes to it
// Technically, this should be a NOP
return DUMP;
}// end if contain
else if(open == INTERSECT)
{
GenericTreeNode *localNode = (GenericTreeNode *)computeEntity;
if(localNode->getType() == Bucket){
// if the local node is a bucket, we shouldn't descend further locally
// instead, add the children of the glblnode to its checklist
addChildrenToCheckList(node, reqID, chunk, awi, s, chklist, tp);
//Vector3D<double> vec = tp->decodeOffset(reqID);
//CkPrintf("level %d: %d (%f, %f, %f)\n", s->level, node->getKey(), vec.x, vec.y, vec.z);
return DUMP;
}
else{
// ancestor is a node but only intersects,
// modify undecided list and send KEEP to tw:
OffsetNode on;
on.node = node;
on.offsetID = reqID;
undlist.push_back(on);
return KEEP;
}
}
}
else if(action == COMPUTE){
// only nodes can be COMPUTEd
// particles must be KEEP_*_BUCKETed
// so we only need to add node to clist here
didcomp = true;
// add to list
addNodeToInt(node, reqID, s);
// all particles beneath this node have been
// scheduled for computation
// int computed;
// computed = node->lastParticle-node->firstParticle+1;
return DUMP;
}
else if(action == KEEP_LOCAL_BUCKET){
didcomp = true;
#if CHANGA_REFACTOR_DEBUG > 2
CkAssert(node->getType() == Bucket);
CkPrintf("[%d] ListCompute told to KEEP_LOCAL_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
// since this is a local bucket, we should have the particles at hand
GravityParticle *part = node->particlePointer;
CkAssert(part);
int computed = node->lastParticle-node->firstParticle+1;
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
if (!bUseCpu) {
NodeKey key = node->getKey();
addLocalParticlesToInt(part, computed, offset, s, key, node);
//addLocalParticlesToInt(part, computed, offset, s, key);
} else {
addLocalParticlesToInt(part, computed, offset, s, 0, 0);
}
#else
addLocalParticlesToInt(part, computed, offset, s);
#endif
/*
if(getOptType() == Remote){
CkPrintf("[%d] adding %d to partinterremote\n", computed);
tp->addToParticleInterRemote(chunk, computed);
}
else if(getOptType() == Local){
CkPrintf("[%d] adding %d to partinterlocal\n", computed);
tp->addToParticleInterLocal(computed);
}
*/
return DUMP;
}
else if(action == KEEP_REMOTE_BUCKET){
didcomp = true;
// fetch particles and compute.
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("[%d] ListCompute told to KEEP_REMOTE_BUCKET, chunk=%d, remoteIndex=%d, first=%d, last=%d, reqID=%d\n", tp->getIndex(),
chunk, node->remoteIndex,
node->firstParticle,
node->lastParticle,
reqID);
#endif
Tree::NodeKey keyref = node->getKey();
ExternalGravityParticle *part;
part = tp->requestParticles(keyref, chunk, node->remoteIndex,
node->firstParticle, node->lastParticle,
reqID, awi, computeEntity);
if(part){
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("Particles found in cache\n");
#endif
int computed = node->lastParticle-node->firstParticle+1;
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
if (!bUseCpu) {
NodeKey key = node->getKey();
addRemoteParticlesToInt(part, computed, offset, s, key);
} else {
addRemoteParticlesToInt(part, computed, offset, s, 0);
}
#else
addRemoteParticlesToInt(part, computed, offset, s);
#endif
/*
if(getOptType() == Remote){
CkPrintf("[%d] adding %d to partinterremote\n", computed);
tp->addToParticleInterRemote(chunk, computed);
}
else if(getOptType() == Local){
CkPrintf("[%d] adding %d to partinterlocal\n", computed);
tp->addToParticleInterLocal(computed);
}
*/
}
else{
#if CHANGA_REFACTOR_DEBUG > 2
CkPrintf("Particles not found in cache\n");
#endif
CkAssert(getOptType() == Remote);
// particles missed
int start, end;
GenericTreeNode *source = (GenericTreeNode *)computeEntity;
tp->getBucketsBeneathBounds(source, start, end);
#if COSMO_PRINT_BK > 1
CkPrintf("[%d] missed parts %ld (chunk %d)\n", tp->getIndex(), keyref << 1, chunk);
#endif
tp->updateUnfinishedBucketState(start, end, 1, chunk, state);
#ifdef CHANGA_REFACTOR_MEMCHECK
CkPrintf("memcheck after particlesmissed (%ld)\n", keyref);
CmiMemoryCheck();
#endif
}
return DUMP;
}
else if(action == DUMP || action == NOP){
return DUMP;
}
CkAbort("ListCompute: bad walk state");
return -1;
}
/// @brief Process received remote particles
/// @param part Array of particles received.
/// @param num Number of particles
///
/// Update the state bookkeeping, add the particles to the interaction
/// list and call stateReady() to compute their interactions. Call
/// TreePiece::finishedChunk() if all outstanding requests are satisfied.
void ListCompute::recvdParticles(ExternalGravityParticle *part,int num,int chunk,int reqID,State *state_,TreePiece *tp, Tree::NodeKey &remoteBucket){
Vector3D<cosmoType> offset = tp->decodeOffset(reqID);
CkAssert(num > 0);
GenericTreeNode *source = (GenericTreeNode *)computeEntity;
int startBucket;
int end;
DoubleWalkState *state = (DoubleWalkState *)state_;
tp->getBucketsBeneathBounds(source, startBucket, end);
// init state
bool remoteLists = state->rplists.length() > 0;
int level = source->getLevel(source->getKey());
for(int i = 0; i <= level; i++){
state->clists[i].length() = 0;
}
if(remoteLists)
for(int i = 0; i <= level; i++){
state->rplists[i].length() = 0;
}
// put particles in list at correct level
// (key) here.
state->level = level;
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
if (!bUseCpu) {
NodeKey key = remoteBucket >> 1;
addRemoteParticlesToInt(part, num, offset, state, key);
} else {
addRemoteParticlesToInt(part, num, offset, state, 0);
}
#else
addRemoteParticlesToInt(part, num, offset, state);
#endif
state->lowestNode = source;
stateReady(state, tp, chunk, startBucket, end);
tp->updateBucketState(startBucket, end, 1, chunk, state);
int remainingChunk;
#ifdef CHANGA_REFACTOR_MEMCHECK
CkPrintf("memcheck after particlesrecvd\n");
CmiMemoryCheck();
#endif
remainingChunk = state->counterArrays[1][chunk];
#if COSMO_PRINT_BK > 1
CkPrintf("[%d] recvdParticles chunk: %d remainingChunk: %d\n", tp->getIndex(), chunk, remainingChunk);
#endif
CkAssert(remainingChunk >= 0);
if (remainingChunk == 0) {
#ifdef CUDA
if (!bUseCpu) {
if(state->nodeLists.totalNumInteractions > 0){
sendNodeInteractionsToGpu(state, tp);
resetCudaNodeState(state);
}
if(state->particleLists.totalNumInteractions > 0){
sendPartInteractionsToGpu(state, tp);
resetCudaPartState(state);
}
}
#endif
#if COSMO_PRINT_BK > 1
CkPrintf("[%d] FINISHED CHUNK %d from recvdParticles\n", tp->getIndex(), chunk);
#endif
cacheGravPart[CkMyPe()].finishedChunk(chunk, tp->particleInterRemote[chunk]);
#ifdef CHECK_WALK_COMPLETIONS
CkPrintf("[%d] finishedChunk %d ListCompute::recvdParticles\n", tp->getIndex(), chunk);
#endif
tp->finishedChunk(chunk);
}
}
/// @brief apply node opening criterion to this node.
int ListCompute::openCriterion(TreePiece *ownerTP,
GenericTreeNode *node, int reqID, State *state){
return
openCriterionNode(node,(GenericTreeNode *)computeEntity, ownerTP->decodeOffset(reqID));
}
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
void ListCompute::addRemoteParticlesToInt(ExternalGravityParticle *parts, int n, Vector3D<cosmoType> &offset, DoubleWalkState *s, NodeKey key){
#else
void ListCompute::addRemoteParticlesToInt(ExternalGravityParticle *parts, int n, Vector3D<cosmoType> &offset, DoubleWalkState *s){
#endif
RemotePartInfo rpi;
int level = s->level;
rpi.particles = parts;
rpi.numParticles = n;
rpi.offset = offset;
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
if (!bUseCpu) rpi.key = key;
#endif
s->rplists[level].push_back(rpi);
}
#if defined CHANGA_REFACTOR_PRINT_INTERACTIONS || defined CHANGA_REFACTOR_WALKCHECK_INTERLIST || defined CUDA
void ListCompute::addLocalParticlesToInt(GravityParticle *parts, int n, Vector3D<cosmoType> &offset, DoubleWalkState *s, NodeKey key, GenericTreeNode *gtn){
#else
void ListCompute::addLocalParticlesToInt(GravityParticle *parts, int n, Vector3D<cosmoType> &offset, DoubleWalkState *s){