forked from N-BodyShop/changa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HostCUDA.cu
2205 lines (1932 loc) · 74.4 KB
/
HostCUDA.cu
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
#ifdef _WIN32
#define NOMINMAX
#endif
#ifdef HAPI_MEMPOOL
#define GPU_MEMPOOL
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
// #include <cutil.h>
#include <assert.h>
#include "CudaFunctions.h"
#include "CUDAMoments.cu"
#include "HostCUDA.h"
#include "EwaldCUDA.h"
#include "hapi.h"
#include "cuda_typedef.h"
#include "cuda/intrinsics/voting.hu"
#include "cuda/intrinsics/shfl.hu"
#ifdef GPU_LOCAL_TREE_WALK
#include "codes.h"
#endif //GPU_LOCAL_TREE_WALK
#ifdef HAPI_TRACE
# define HAPI_TRACE_BEGIN() double trace_start_time = CmiWallTimer()
# define HAPI_TRACE_END(ID) traceUserBracketEvent(ID, trace_start_time, CmiWallTimer())
#else
# define HAPI_TRACE_BEGIN() /* */
# define HAPI_TRACE_END(ID) /* */
#endif
#define cudaChk(code) cudaErrorDie(code, #code, __FILE__, __LINE__)
inline void cudaErrorDie(cudaError_t retCode, const char* code,
const char* file, int line) {
if (retCode != cudaSuccess) {
fprintf(stderr, "Fatal CUDA Error %s at %s:%d.\nReturn value %d from '%s'.",
cudaGetErrorString(retCode), file, line, retCode, code);
abort();
}
}
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
#include "converse.h"
#endif
__device__ __constant__ EwaldReadOnlyData cachedData[1];
__device__ __constant__ EwtData ewt[NEWH];
//__constant__ constantData[88];
//
//
#ifdef HAPI_TRACE
extern "C" void traceUserBracketEvent(int e, double beginT, double endT);
extern "C" double CmiWallTimer();
#endif
void allocatePinnedHostMemory(void **ptr, size_t size){
if(size <= 0){
*((char **)ptr) = NULL;
#ifdef CUDA_PRINT_ERRORS
printf("allocatePinnedHostMemory: 0 size!\n");
#endif
assert(0);
return;
}
#ifdef HAPI_MEMPOOL
hapiMallocHost(ptr, size, true);
#else
hapiMallocHost(ptr, size, false);
#endif
#ifdef CUDA_PRINT_ERRORS
printf("allocatePinnedHostMemory: %s size: %zu\n", cudaGetErrorString( cudaGetLastError() ), size);
#endif
}
void freePinnedHostMemory(void *ptr){
if(ptr == NULL){
#ifdef CUDA_PRINT_ERRORS
printf("freePinnedHostMemory: NULL ptr!\n");
#endif
assert(0);
return;
}
#ifdef HAPI_MEMPOOL
hapiFreeHost(ptr, true);
#else
hapiFreeHost(ptr, false);
#endif
#ifdef CUDA_PRINT_ERRORS
printf("freePinnedHostMemory: %s\n", cudaGetErrorString( cudaGetLastError() ));
#endif
}
/// @brief Transfer local moments, particle data and acceleration fields to GPU memory
/// @param moments Array of moments
/// @param sMoments Size of moments array
/// @param compactParts Array of particles
/// @param sCompactParts Size of particle array
/// @param varParts Zeroed-out particle acceleration fields
/// @param sVarParts Size of acceleration array
/// @param d_localMoments Uninitalized pointer to moments on GPU
/// @param d_compactParts Uninitalized pointer to particles on GPU
/// @param d_varParts Uninitalized pointer to accelerations on GPU
/// @param stream CUDA stream to handle the memory transfer
/// @param numParticles Total number of particle accelerations to initalize
void DataManagerTransferLocalTree(void *moments, size_t sMoments,
void *compactParts, size_t sCompactParts,
void *varParts, size_t sVarParts,
void **d_localMoments, void **d_compactParts, void **d_varParts,
cudaStream_t stream, int numParticles,
void *callback) {
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) DM LOCAL TREE moments %zu partcores %zu partvars %zu\n",
CmiMyPe(),
sMoments,
sCompactParts,
sVarParts
);
#endif
HAPI_TRACE_BEGIN();
cudaChk(cudaMalloc(d_localMoments, sMoments));
cudaChk(cudaMalloc(d_compactParts, sCompactParts));
cudaChk(cudaMalloc(d_varParts, sVarParts));
cudaChk(cudaMemcpyAsync(*d_localMoments, moments, sMoments, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(*d_compactParts, compactParts, sCompactParts, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(*d_varParts, varParts, sVarParts, cudaMemcpyHostToDevice, stream));
ZeroVars<<<numParticles / THREADS_PER_BLOCK + 1, dim3(THREADS_PER_BLOCK), 0, stream>>>(
(VariablePartData *) *d_varParts,
numParticles);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_XFER_LOCAL);
hapiAddCallback(stream, callback);
}
/// @brief Transfer remote moments and particle data to GPU memory
/// @param moments Array of remote moments
/// @param sMoments Size of remote moments array
/// @param remoteParts Array of remote particles
/// @param sRemoteParts Size of remote particle array
/// @param d_remoteMoments Uninitalized pointer to remote moments on GPU
/// @param d_remoteParts Uninitalized pointer to remote particles on GPU
/// @param stream CUDA stream to handle the memory transfer
void DataManagerTransferRemoteChunk(void *moments, size_t sMoments,
void *remoteParts, size_t sRemoteParts,
void **d_remoteMoments, void **d_remoteParts,
cudaStream_t stream,
void *callback) {
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) DM REMOTE CHUNK moments %zu partcores %zu\n",
CmiMyPe(),
sMoments,
sRemoteParts
);
#endif
HAPI_TRACE_BEGIN();
cudaChk(cudaMalloc(d_remoteMoments, sMoments));
cudaChk(cudaMalloc(d_remoteParts, sRemoteParts));
cudaChk(cudaMemcpyAsync(*d_remoteMoments, moments, sMoments, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(*d_remoteParts, remoteParts, sRemoteParts, cudaMemcpyHostToDevice, stream));
HAPI_TRACE_END(CUDA_XFER_REMOTE);
hapiAddCallback(stream, callback);
}
/************** Gravity *****************/
/// @brief Initiate a local gravity calculation on the GPU, via an interaction
/// list calculation between nodes, or do a local tree walk
/// @param data CudaRequest object containing parameters for the calculation
void TreePieceCellListDataTransferLocal(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER LOCAL CELL\n", CmiMyPe());
#endif
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TRANSFER LOCAL CELL KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nremote_moments: (0x%x)\nil_cell: (0x%x)\n",
data->d_localParts,
data->d_localVars,
data->d_localMoments,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
#ifdef GPU_LOCAL_TREE_WALK
gpuLocalTreeWalk<<<(data->lastParticle - data->firstParticle + 1)
/ THREADS_PER_BLOCK + 1, dim3(THREADS_PER_BLOCK), 0, stream>>> (
data->d_localMoments,
data->d_localParts,
data->d_localVars,
data->firstParticle,
data->lastParticle,
data->rootIdx,
data->theta,
data->thetaMono,
data->nReplicas,
data->fperiod,
data->fperiodY,
data->fperiodZ
);
#else
dim3 dimensions = THREADS_PER_BLOCK;
#ifdef CUDA_2D_TB_KERNEL
dimensions = dim3(NODES_PER_BLOCK, PARTS_PER_BLOCK);
#endif
nodeGravityComputation<<<dim3(data->numBucketsPlusOne-1), dimensions, 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_localMoments,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_GRAV_LOCAL);
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a remote gravity calculation on the GPU between tree nodes
/// @param data CudaRequest object containing parameters for the calculation
void TreePieceCellListDataTransferRemote(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER REMOTE CELL\n", CmiMyPe());
#endif
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TRANSFER REMOTE CELL KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nremote_moments: (0x%x)\nil_cell: (0x%x)\n",
data->d_localParts,
data->d_localVars,
data->d_remoteMoments,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
dim3 dimensions = THREADS_PER_BLOCK;
#ifdef CUDA_2D_TB_KERNEL
dimensions = dim3(NODES_PER_BLOCK, PARTS_PER_BLOCK);
#endif
nodeGravityComputation<<<data->numBucketsPlusOne-1, dimensions, 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_remoteMoments,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_GRAV_REMOTE);
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a remote resume gravity calculation on the GPU between tree nodes
/// @param data CudaRequest object containing parameters for the calculation
void TreePieceCellListDataTransferRemoteResume(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
void *d_missedNodes;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER REMOTE RESUME CELL\n", CmiMyPe());
#endif
cudaChk(cudaMalloc(&d_missedNodes, data->sMissed));
cudaChk(cudaMemcpyAsync(d_missedNodes, data->missedNodes, data->sMissed, cudaMemcpyHostToDevice, stream));
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TRANSFER REMOTE RESUME CELL KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nmissed_moments (0x%x)\nil_cell (0x%x)\n",
data->d_localParts,
data->d_localVars,
d_missedNodes,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
dim3 dimensions = THREADS_PER_BLOCK;
#ifdef CUDA_2D_TB_KERNEL
dimensions = dim3(NODES_PER_BLOCK, PARTS_PER_BLOCK);
#endif
nodeGravityComputation<<<data->numBucketsPlusOne-1, dimensions, 0, stream>>> (
data->d_localParts,
data->d_localVars,
(CudaMultipoleMoments *)d_missedNodes,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaFree(d_missedNodes));
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_REMOTE_RESUME);
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a small phase local gravity calculation on the GPU between particles
/// @param data CudaRequest object containing parameters for the calculation
void TreePiecePartListDataTransferLocalSmallPhase(CudaRequest *data, CompactPartData *particles, int len){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
TreePieceDataTransferBasic(data, &devPtr);
size_t size = (len) * sizeof(CompactPartData);
void* bufferHostBuffer;
void* d_smallParts;
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TreePiecePartListDataTransferLocalSmallPhase KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nil_cell: (0x%x)\n",
data->d_localParts,
data->d_localVars,
devPtr.d_list
);
#endif
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER LOCAL SMALL PHASE %zu\n",
CmiMyPe(),
size
);
#endif
HAPI_TRACE_BEGIN();
allocatePinnedHostMemory(&bufferHostBuffer, size);
#ifdef CUDA_PRINT_ERRORS
printf("TPPartSmallPhase 0: %s\n", cudaGetErrorString( cudaGetLastError() ) );
#endif
memcpy(bufferHostBuffer, particles, size);
cudaChk(cudaMalloc(&d_smallParts, size));
cudaChk(cudaMemcpyAsync(d_smallParts, bufferHostBuffer, size, cudaMemcpyHostToDevice, stream));
#ifndef CUDA_NO_KERNELS
#ifdef CUDA_2D_TB_KERNEL
particleGravityComputation<<<data->numBucketsPlusOne-1, dim3(NODES_PER_BLOCK_PART, PARTS_PER_BLOCK_PART), 0, stream>>> (
data->d_localParts,
data->d_localVars,
(CompactPartData *)d_smallParts,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#else
particleGravityComputation<<<data->numBucketsPlusOne-1, THREADS_PER_BLOCK, 0, stream>>> (
data->d_localParts,
data->d_localVars,
(CompactPartData *)d_smallParts,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_PART_GRAV_LOCAL_SMALL);
cudaChk(cudaFree(d_smallParts));
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a local gravity calculation on the GPU between particles
/// @param data CudaRequest object containing parameters for the calculation
void TreePiecePartListDataTransferLocal(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER LOCAL LARGEPHASE PART\n", CmiMyPe());
#endif
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TreePiecePartListDataTransferLocal buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nil_cell: (0x%x)\n",
data->d_localParts,
data->d_localVars,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
#ifdef CUDA_2D_TB_KERNEL
particleGravityComputation<<<data->numBucketsPlusOne-1, dim3(NODES_PER_BLOCK_PART, PARTS_PER_BLOCK_PART), 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_localParts,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#else
particleGravityComputation<<<data->numBucketsPlusOne-1, THREADS_PER_BLOCK, 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_localParts,
(ILPart *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_PART_GRAV_LOCAL);
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a remote gravity calculation on the GPU between particles
/// @param data CudaRequest object containing parameters for the calculation
void TreePiecePartListDataTransferRemote(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER REMOTE PART\n", CmiMyPe());
#endif
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TreePiecePartListDataTransferRemote KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nil_cell: (0x%x) (0x%x)\n",
data->d_localParts,
data->d_localVars,
data->list,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
#ifdef CUDA_2D_TB_KERNEL
particleGravityComputation<<<data->numBucketsPlusOne-1, dim3(NODES_PER_BLOCK_PART, PARTS_PER_BLOCK_PART), 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_remoteParts,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#else
particleGravityComputation<<<data->numBucketsPlusOne-1, THREADS_PER_BLOCK, 0, stream>>> (
data->d_localParts,
data->d_localVars,
data->d_remoteParts,
(ILPart *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_PART_GRAV_REMOTE);
hapiAddCallback(stream, data->cb);
}
/// @brief Initiate a remote gravity calculation on the GPU between particles
/// @param data CudaRequest object containing parameters for the calculation
void TreePiecePartListDataTransferRemoteResume(CudaRequest *data){
cudaStream_t stream = data->stream;
CudaDevPtr devPtr;
void* d_missedParts;
TreePieceDataTransferBasic(data, &devPtr);
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER REMOTE RESUME PART\n", CmiMyPe());
#endif
cudaChk(cudaMalloc(&d_missedParts, data->sMissed));
cudaChk(cudaMemcpyAsync(d_missedParts, data->missedParts, data->sMissed, cudaMemcpyHostToDevice, stream));
#ifdef CUDA_NOTIFY_DATA_TRANSFER_DONE
printf("TreePiecePartListDataTransferRemoteResume KERNELSELECT buffers:\nlocal_particles: (0x%x)\nlocal_particle_vars: (0x%x)\nmissed_parts (0x%x)\nil_cell: (0x%x) (0x%x)\n",
data->d_localParts,
data->d_localVars,
(CompactPartData *)d_missedParts,
data->list,
devPtr.d_list
);
#endif
HAPI_TRACE_BEGIN();
#ifndef CUDA_NO_KERNELS
#ifdef CUDA_2D_TB_KERNEL
particleGravityComputation<<<data->numBucketsPlusOne-1, dim3(NODES_PER_BLOCK_PART, PARTS_PER_BLOCK_PART), 0, stream>>> (
data->d_localParts,
data->d_localVars,
(CompactPartData *)d_missedParts,
(ILCell *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#else
particleGravityComputation<<<data->numBucketsPlusOne-1, THREADS_PER_BLOCK, 0, stream>>> (
data->d_localParts,
data->d_localVars,
(CompactPartData *)d_missedParts,
(ILPart *)devPtr.d_list,
devPtr.d_bucketMarkers,
devPtr.d_bucketStarts,
devPtr.d_bucketSizes,
data->fperiod
);
#endif
#endif
TreePieceDataTransferBasicCleanup(&devPtr);
cudaChk(cudaFree(d_missedParts));
cudaChk(cudaPeekAtLastError());
HAPI_TRACE_END(CUDA_PART_GRAV_REMOTE);
hapiAddCallback(stream, data->cb);
}
/// @brief Allocate space and copy bucket and interaction list data to
/// device memory
/// @param data CudaRequest object containing parameters for the calculation
/// @param ptr CudaDevPtr object that stores handles to device memory
void TreePieceDataTransferBasic(CudaRequest *data, CudaDevPtr *ptr){
cudaStream_t stream = data->stream;
int numBucketsPlusOne = data->numBucketsPlusOne;
int numBuckets = numBucketsPlusOne-1;
size_t listSize = (data->numInteractions) * sizeof(ILCell);
size_t markerSize = (numBucketsPlusOne) * sizeof(int);
size_t startSize = (numBuckets) * sizeof(int);
cudaChk(cudaMalloc(&ptr->d_list, listSize));
cudaChk(cudaMalloc(&ptr->d_bucketMarkers, markerSize));
cudaChk(cudaMalloc(&ptr->d_bucketStarts, startSize));
cudaChk(cudaMalloc(&ptr->d_bucketSizes, startSize));
cudaChk(cudaMemcpyAsync(ptr->d_list, data->list, listSize, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(ptr->d_bucketMarkers, data->bucketMarkers, markerSize, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(ptr->d_bucketStarts, data->bucketStarts, startSize, cudaMemcpyHostToDevice, stream));
cudaChk(cudaMemcpyAsync(ptr->d_bucketSizes, data->bucketSizes, startSize, cudaMemcpyHostToDevice, stream));
#ifdef CUDA_VERBOSE_KERNEL_ENQUEUE
printf("(%d) TRANSFER BASIC %zu bucket_markers %zu bucket_starts %zu\n",
CmiMyPe(),
listSize,
markerSize,
startSize
);
#endif
}
/// @brief Free device memory used for interaction list and bucket data
/// @param ptr CudaDevPtr object that stores handles to device memory
void TreePieceDataTransferBasicCleanup(CudaDevPtr *ptr){
cudaChk(cudaFree(ptr->d_list));
cudaChk(cudaFree(ptr->d_bucketMarkers));
cudaChk(cudaFree(ptr->d_bucketStarts));
cudaChk(cudaFree(ptr->d_bucketSizes));
}
/** @brief Transfer forces from the GPU back to the host. Also schedules
* the freeing of the device buffers used for the force calculation.
* @param hostBuffer Buffer to store results.
* @param size hostBuffer size.
* @param d_varParts Pointer to finalized accelerations on GPU
* @param stream CUDA stream to handle the memory transfer
* @param cb Callback when transfer is done.
*/
void TransferParticleVarsBack(VariablePartData *hostBuffer, size_t size, void *d_varParts,
cudaStream_t stream, void *cb){
HAPI_TRACE_BEGIN();
cudaChk(cudaMemcpyAsync(hostBuffer, d_varParts, size, cudaMemcpyDeviceToHost, stream));
HAPI_TRACE_END(CUDA_XFER_BACK);
hapiAddCallback(stream, cb);
}
/*
void DummyKernel(void *cb){
hapiWorkRequest* dummy = hapiCreateWorkRequest();
dummy->setExecParams(1, THREADS_PER_BLOCK);
dummy->setDeviceToHostCallback(cb);
#ifdef HAPI_TRACE
dummy->setTraceName("dummyRun");
#endif
dummy->setRunKernel(run_kernel_DUMMY);
hapiEnqueue(dummy);
}
*/
/*
* Kernels
*/
/****
* GPU Local tree walk (computation integrated)
****/
#ifdef GPU_LOCAL_TREE_WALK
__device__ __forceinline__ void
ldgTreeNode(CUDATreeNode &m, CudaMultipoleMoments *ptr) {
m.radius = __ldg(&(ptr->radius));
m.soft = __ldg(&(ptr->soft));
m.totalMass = __ldg(&(ptr->totalMass));
m.cm.x = __ldg(&(ptr->cm.x));
m.cm.y = __ldg(&(ptr->cm.y));
m.cm.z = __ldg(&(ptr->cm.z));
m.bucketStart = __ldg(&(ptr->bucketStart));
m.bucketSize = __ldg(&(ptr->bucketSize));
m.particleCount = __ldg(&(ptr->particleCount));
m.children[0] = __ldg(&(ptr->children[0]));
m.children[1] = __ldg(&(ptr->children[1]));
m.type = __ldg(&(ptr->type));
};
__device__ __forceinline__ void
ldgBucketNode(CUDABucketNode &m, CompactPartData *ptr) {
m.soft = __ldg(&(ptr->soft));
m.totalMass = __ldg(&(ptr->mass));
m.cm.x = __ldg(&(ptr->position.x));
m.cm.y = __ldg(&(ptr->position.y));
m.cm.z = __ldg(&(ptr->position.z));
};
__device__ __forceinline__ void
ldgBucketNode(CUDABucketNode &m, CudaMultipoleMoments *ptr) {
m.radius = __ldg(&(ptr->radius));
m.soft = __ldg(&(ptr->soft));
m.totalMass = __ldg(&(ptr->totalMass));
m.cm.x = __ldg(&(ptr->cm.x));
m.cm.y = __ldg(&(ptr->cm.y));
m.cm.z = __ldg(&(ptr->cm.z));
m.lesser_corner.x = __ldg(&(ptr->lesser_corner.x));
m.lesser_corner.y = __ldg(&(ptr->lesser_corner.y));
m.lesser_corner.z = __ldg(&(ptr->lesser_corner.z));
m.greater_corner.x = __ldg(&(ptr->greater_corner.x));
m.greater_corner.y = __ldg(&(ptr->greater_corner.y));
m.greater_corner.z = __ldg(&(ptr->greater_corner.z));
};
__device__ __forceinline__ void
ldgParticle(CompactPartData &m, CompactPartData *ptr) {
m.mass = __ldg(&(ptr->mass));
m.soft = __ldg(&(ptr->soft));
m.position.x = __ldg(&(ptr->position.x));
m.position.y = __ldg(&(ptr->position.y));
m.position.z = __ldg(&(ptr->position.z));
}
__device__ __forceinline__ void stackInit(int &sp, int* stk, int rootIdx) {
sp = 0;
stk[sp] = rootIdx;
}
__device__ __forceinline__ void stackPush(int &sp) {
++sp;
}
__device__ __forceinline__ void stackPop(int &sp) {
--sp;
}
const int stackDepth = 64;
//__launch_bounds__(1024,1)
__global__ void gpuLocalTreeWalk(
CudaMultipoleMoments *moments,
CompactPartData *particleCores,
VariablePartData *particleVars,
int firstParticle,
int lastParticle,
int rootIdx,
cudatype theta,
cudatype thetaMono,
int nReplicas,
cudatype fperiod,
cudatype fperiodY,
cudatype fperiodZ) {
CUDABucketNode myNode;
CompactPartData myParticle;
#if __CUDA_ARCH__ >= 700
// Non-lockstepping code for Volta GPUs
int sp;
int stk[stackDepth];
CUDATreeNode targetNode;
#define SP sp
#define STACK_TOP_INDEX stk[SP]
#define TARGET_NODE targetNode
#else
// Default lockstepping code
__shared__ int sp[WARPS_PER_BLOCK];
__shared__ int stk[WARPS_PER_BLOCK][stackDepth];
__shared__ CUDATreeNode targetNode[WARPS_PER_BLOCK];
#define SP sp[WARP_INDEX]
#define STACK_TOP_INDEX stk[WARP_INDEX][SP]
#define TARGET_NODE targetNode[WARP_INDEX]
#endif
CudaVector3D acc = {0,0,0};
cudatype pot = 0;
cudatype idt2 = 0;
CudaVector3D offset = {0,0,0};
int targetIndex = -1;
// variables for CUDA_momEvalFmomrcm
CudaVector3D r;
cudatype rsq = 0;
cudatype twoh = 0;
int flag = 1;
int critical = stackDepth;
int cond = 1;
for(int pidx = blockIdx.x*blockDim.x + threadIdx.x + firstParticle;
pidx <= lastParticle; pidx += gridDim.x*blockDim.x) {
// initialize the variables belonging to current thread
int nodePointer = particleCores[pidx].nodeId;
ldgParticle(myParticle, &particleCores[pidx]);
ldgBucketNode(myNode, &moments[nodePointer]);
for(int x = -nReplicas; x <= nReplicas; x++) {
for(int y = -nReplicas; y <= nReplicas; y++) {
for(int z = -nReplicas; z <= nReplicas; z++) {
// generate the offset for the periodic boundary conditions
offset.x = x*fperiod;
offset.y = y*fperiodY;
offset.z = z*fperiodZ;
flag = 1;
critical = stackDepth;
cond = 1;
#if __CUDA_ARCH__ >= 700
stackInit(SP, stk, rootIdx);
#else
stackInit(SP, stk[WARP_INDEX], rootIdx);
#endif
while(SP >= 0) {
if (flag == 0 && critical >= SP) {
flag = 1;
}
targetIndex = STACK_TOP_INDEX;
stackPop(SP);
if (flag) {
ldgTreeNode(TARGET_NODE, &moments[targetIndex]);
// Each warp increases its own TARGET_NODE in the shared memory,
// so there is no actual data racing here.
addCudaVector3D(TARGET_NODE.cm, offset, TARGET_NODE.cm);
int open = CUDA_openCriterionNode(TARGET_NODE, myNode, -1, theta,
thetaMono);
int action = CUDA_OptAction(open, TARGET_NODE.type);
critical = SP;
cond = ((action == KEEP) && (open == CONTAIN || open == INTERSECT));
if (action == COMPUTE) {
if (CUDA_openSoftening(TARGET_NODE, myNode)) {
r.x = TARGET_NODE.cm.x - myParticle.position.x;
r.y = TARGET_NODE.cm.y - myParticle.position.y;
r.z = TARGET_NODE.cm.z - myParticle.position.z;
rsq = r.x*r.x + r.y*r.y + r.z*r.z;
twoh = TARGET_NODE.soft + myParticle.soft;
cudatype a, b;
if (rsq != 0) {
CUDA_SPLINE(rsq, twoh, a, b);
idt2 = fmax(idt2, (myParticle.mass + TARGET_NODE.totalMass) * b);
pot -= TARGET_NODE.totalMass * a;
acc.x += r.x*b*TARGET_NODE.totalMass;
acc.y += r.y*b*TARGET_NODE.totalMass;
acc.z += r.z*b*TARGET_NODE.totalMass;
}
} else {
// compute with the node targetnode
r.x = myParticle.position.x - TARGET_NODE.cm.x;
r.y = myParticle.position.y - TARGET_NODE.cm.y;
r.z = myParticle.position.z - TARGET_NODE.cm.z;
rsq = r.x*r.x + r.y*r.y + r.z*r.z;
if (rsq != 0) {
cudatype dir = rsqrt(rsq);
#if defined (HEXADECAPOLE)
CUDA_momEvalFmomrcm(&moments[targetIndex], &r, dir, &acc, &pot);
idt2 = fmax(idt2, (myParticle.mass +
moments[targetIndex].totalMass)*dir*dir*dir);
#else
cudatype a, b, c, d;
twoh = moments[targetIndex].soft + myParticle.soft;
CUDA_SPLINEQ(dir, rsq, twoh, a, b, c, d);
cudatype qirx = moments[targetIndex].xx*r.x +
moments[targetIndex].xy*r.y +
moments[targetIndex].xz*r.z;
cudatype qiry = moments[targetIndex].xy*r.x +
moments[targetIndex].yy*r.y +
moments[targetIndex].yz*r.z;
cudatype qirz = moments[targetIndex].xz*r.x +
moments[targetIndex].yz*r.y +
moments[targetIndex].zz*r.z;
cudatype qir = 0.5 * (qirx*r.x + qiry*r.y + qirz*r.z);
cudatype tr = 0.5 * (moments[targetIndex].xx +
moments[targetIndex].yy +
moments[targetIndex].zz);
cudatype qir3 = b*moments[targetIndex].totalMass + d*qir - c*tr;
pot -= moments[targetIndex].totalMass * a + c*qir - b*tr;
acc.x -= qir3*r.x - c*qirx;
acc.y -= qir3*r.y - c*qiry;
acc.z -= qir3*r.z - c*qirz;
idt2 = fmax(idt2, (myParticle.mass +
moments[targetIndex].totalMass) * b);
#endif //HEXADECAPOLE
}
}
} else if (action == KEEP_LOCAL_BUCKET) {
// compute with each particle contained by node targetnode
int target_firstparticle = TARGET_NODE.bucketStart;
int target_lastparticle = TARGET_NODE.bucketStart +
TARGET_NODE.bucketSize;
cudatype a, b;
for (int i = target_firstparticle; i < target_lastparticle; ++i) {
CompactPartData targetParticle = particleCores[i];
addCudaVector3D(targetParticle.position, offset, targetParticle.position);
r.x = targetParticle.position.x - myParticle.position.x;
r.y = targetParticle.position.y - myParticle.position.y;
r.z = targetParticle.position.z - myParticle.position.z;
rsq = r.x*r.x + r.y*r.y + r.z*r.z;
twoh = targetParticle.soft + myParticle.soft;
if (rsq != 0) {
CUDA_SPLINE(rsq, twoh, a, b);
pot -= targetParticle.mass * a;
acc.x += r.x*b*targetParticle.mass;
acc.y += r.y*b*targetParticle.mass;
acc.z += r.z*b*targetParticle.mass;
idt2 = fmax(idt2, (myParticle.mass + targetParticle.mass) * b);
}
}
}
if (!any(cond)) {
continue;
}
if (!cond) {
flag = 0;
} else {
if (TARGET_NODE.children[1] != -1) {
stackPush(SP);
STACK_TOP_INDEX = TARGET_NODE.children[1];
}
if (TARGET_NODE.children[0] != -1) {
stackPush(SP);
STACK_TOP_INDEX = TARGET_NODE.children[0];
}
}
}
#if __CUDA_ARCH__ >= 700
__syncwarp();
#endif
}
} // z replicas
} // y replicas
} // x replicas
particleVars[pidx].a.x += acc.x;
particleVars[pidx].a.y += acc.y;
particleVars[pidx].a.z += acc.z;
particleVars[pidx].potential += pot;
particleVars[pidx].dtGrav = fmax(idt2, particleVars[pidx].dtGrav);
}
}
#endif //GPU_LOCAL_TREE_WALK
/**
* @brief interaction between multipole moments and buckets of particles.
* @param particleCores Read-only properties of particles.
* @param particleVars Accumulators of accelerations etc. of particles.
* @param moments Multipole moments from which to calculate forces.
* @param ils Cells on the interaction list. Each Cell has an index into
* moments.
* @param ilmarks Indices into ils for each block.
* @param bucketStarts Indices into particleCores and particleVars
* for each block
* @param bucketSizes Size of the bucket for each block
* @param fPeriod Size of periodic boundary condition.
*/
// 2d thread blocks
#ifdef CUDA_2D_TB_KERNEL
#define TRANSLATE(x,y) (y*NODES_PER_BLOCK+x)
#ifndef CUDA_2D_FLAT
__device__ __forceinline__ void ldg_moments(CudaMultipoleMoments &m, CudaMultipoleMoments *ptr)
{
m.radius = __ldg(&(ptr->radius));
m.soft = __ldg(&(ptr->soft));
m.totalMass = __ldg(&(ptr->totalMass));
m.cm.x = __ldg(&(ptr->cm.x));
m.cm.y = __ldg(&(ptr->cm.y));
m.cm.z = __ldg(&(ptr->cm.z));
#ifdef HEXADECAPOLE
m.xx = __ldg(&(ptr->xx));
m.xy = __ldg(&(ptr->xy));
m.xz = __ldg(&(ptr->xz));
m.yy = __ldg(&(ptr->yy));
m.yz = __ldg(&(ptr->yz));
m.xxx = __ldg(&(ptr->xxx));
m.xyy = __ldg(&(ptr->xyy));
m.xxy = __ldg(&(ptr->xxy));
m.yyy = __ldg(&(ptr->yyy));
m.xxz = __ldg(&(ptr->xxz));
m.yyz = __ldg(&(ptr->yyz));
m.xyz = __ldg(&(ptr->xyz));
m.xxxx = __ldg(&(ptr->xxxx));
m.xyyy = __ldg(&(ptr->xyyy));
m.xxxy = __ldg(&(ptr->xxxy));
m.yyyy = __ldg(&(ptr->yyyy));
m.xxxz = __ldg(&(ptr->xxxz));
m.yyyz = __ldg(&(ptr->yyyz));
m.xxyy = __ldg(&(ptr->xxyy));
m.xxyz = __ldg(&(ptr->xxyz));
m.xyyz = __ldg(&(ptr->xyyz));
#else
m.xx = __ldg(&(ptr->xx));
m.xy = __ldg(&(ptr->xy));
m.xz = __ldg(&(ptr->xz));
m.yy = __ldg(&(ptr->yy));
m.yz = __ldg(&(ptr->yz));