-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgr_cuda_kernel.cu
1766 lines (1511 loc) · 66.2 KB
/
gr_cuda_kernel.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
#ifndef _GR_CUDA_KERNEL_H_
#define _GR_CUDA_KERNEL_H_
#include <stdio.h>
#include <mpi.h>
#include "H5Cpp.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <helper_functions.h>
using namespace std;
/*
TODO: cuda_run is a beast, so split up into multiple functions to e.g. keep MPI stuff away from other stuff
TODO: at the moment, every processor and every kernel has all the data. Change this so that processors only have the data they need.
TODO: Change bcs etc so less copying of data to CPU - instead can use e.g. cudaMemcpyPeerAsync to copy between GPUs on the same node.
NOTE: debug memory leaks using
mpirun -np 4 valgrind --leak-check=full ./gr_cuda
NOTE: run using
mpirun -np 4 ./gr_cuda
*/
unsigned int nextPow2(unsigned int x)
{
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
void check_mpi_error(int mpi_err) {
/*
Checks to see if the integer returned by an mpi function, mpi_err, is an MPI error. If so, it prints out some useful stuff to screen.
*/
int errclass, resultlen;
char err_buffer[MPI_MAX_ERROR_STRING];
if (mpi_err != MPI_SUCCESS) {
MPI_Error_class(mpi_err, &errclass);
if (errclass == MPI_ERR_RANK) {
fprintf(stderr,"Invalid rank used in MPI send call\n");
MPI_Error_string(mpi_err,err_buffer,&resultlen);
fprintf(stderr,err_buffer);
MPI_Finalize();
} else {
fprintf(stderr, "Other MPI error\n");
MPI_Error_string(mpi_err,err_buffer,&resultlen);
fprintf(stderr,err_buffer);
MPI_Finalize();
}
}
}
void getNumKernels(int nx, int ny, int nlayers, int ng, int n_processes, int *maxBlocks, int *maxThreads, dim3 * kernels, int * cumulative_kernels) {
/*
Return the number of kernels needed to run the problem given its size and the constraints of the GPU.
Parameters
----------
nx, ny, nlayers : int
dimensions of problem
ng : int
number of ghost cells
maxBlocks, maxThreads : int
maximum number of blocks and threads possible for device(s)
n_processes : int
number of MPI processes
kernels : dim3 *
number of kernels per process
cumulative_kernels : int *
cumulative total of kernels per process
*/
// won't actually use maxThreads - fix to account for the fact we want something square
*maxThreads = nlayers * int(sqrt(float(*maxThreads)/nlayers)) * int(sqrt(*maxThreads/nlayers));
*maxBlocks = int(sqrt(float(*maxBlocks))) * int(sqrt(float(*maxBlocks)));
// calculate number of kernels needed
if (nx*ny*nlayers > *maxBlocks * *maxThreads) {
int kernels_x = int(ceil(float(nx-2*ng) / (sqrt(float(*maxThreads * *maxBlocks)/nlayers) - 2.0*ng)));
int kernels_y = int(ceil(float(ny-2*ng) / (sqrt(float(*maxThreads * *maxBlocks)/nlayers) - 2.0*ng)));
// easiest (but maybe not most balanced way) is to split kernels into strips
// not enough kernels to fill all processes. This would be inefficient if ny > nx, so need to fix this to look in the y direction if this is the case.
if (kernels_y < n_processes) {
for (int i = 0; i < kernels_y; i++) {
kernels[i].x = kernels_x;
kernels[i].y = 1;
}
// blank out the other ones
for (int i = kernels_y; i < n_processes; i++) {
kernels[i].x = 0;
kernels[i].y = 0;
}
} else {
// split up in the y direction to keep stuff contiguous in memory
int strip_width = int(floor(float(kernels_y) / float(n_processes)));
for (int i = 0; i < n_processes; i++) {
kernels[i].y = strip_width;
kernels[i].x = kernels_x;
}
// give the last one the remainders
kernels[n_processes-1].y += kernels_y - n_processes * strip_width;
}
} else {
kernels[0].x = 1;
kernels[0].y = 1;
for (int i = 1; i < n_processes; i++) {
kernels[i].x = 0;
kernels[i].y = 0;
}
}
cumulative_kernels[0] = kernels[0].x * kernels[0].y;
for (int i = 1; i < n_processes; i++) {
cumulative_kernels[i] = cumulative_kernels[i-1] + kernels[i].x * kernels[i].y;
}
}
void getNumBlocksAndThreads(int nx, int ny, int nlayers, int ng, int maxBlocks, int maxThreads, int n_processes, dim3 *kernels, dim3 *blocks, dim3 *threads)
{
/*
Returns the number of blocks and threads required for each kernel given the size of the problem and the constraints of the device.
Parameters
----------
nx, ny, nlayers : int
dimensions of problem
ng : int
number of ghost cells
maxBlocks, maxThreads : int
maximum number of blocks and threads possible for device(s)
n_processes : int
number of MPI processes
kernels, blocks, threads : dim3 *
number of kernels, blocks and threads per process / kernel
*/
//get device capability, to avoid block/grid size exceed the upper bound
cudaDeviceProp prop;
int device;
cudaGetDevice(&device);
cudaGetDeviceProperties(&prop, device);
int total = (nx - 2*ng) * (ny - 2*ng) * nlayers;
int kernels_x = 0;
int kernels_y = 0;
for (int i = 0; i < n_processes; i++) {
kernels_y += kernels[i].y;
}
kernels_x = kernels[0].x;
if ((kernels_x > 1) || (kernels_y > 1)) {
// initialise
threads[0].x = 0;
threads[0].y = 0;
blocks[0].x = 0;
blocks[0].y = 0;
for (int j = 0; j < (kernels_y-1); j++) {
for (int i = 0; i < (kernels_x-1); i++) {
threads[j*kernels_x + i].x = int(sqrt(float(maxThreads)/nlayers));
threads[j*kernels_x + i].y = int(sqrt(float(maxThreads)/nlayers));
threads[j*kernels_x + i].z = nlayers;
blocks[j*kernels_x + i].x = int(sqrt(float(maxBlocks)));
blocks[j*kernels_x + i].y = int(sqrt(float(maxBlocks)));
blocks[j*kernels_x + i].z = 1;
}
}
// kernels_x-1
int nx_remaining = nx - (threads[0].x * blocks[0].x - 2*ng) * (kernels_x - 1);
//printf("nx_remaining: %i\n", nx_remaining);
for (int j = 0; j < (kernels_y-1); j++) {
threads[j*kernels_x + kernels_x-1].y =
int(sqrt(float(maxThreads)/nlayers));
threads[j*kernels_x + kernels_x-1].z = nlayers;
threads[j*kernels_x + kernels_x-1].x =
(nx_remaining < threads[j*kernels_x + kernels_x-1].y) ? nx_remaining : threads[j*kernels_x + kernels_x-1].y;
blocks[j*kernels_x + kernels_x-1].x = int(ceil(float(nx_remaining) /
float(threads[j*kernels_x + kernels_x-1].x)));
blocks[j*kernels_x + kernels_x-1].y = int(sqrt(float(maxBlocks)));
blocks[j*kernels_x + kernels_x-1].z = 1;
}
// kernels_y-1
int ny_remaining = ny - (threads[0].y * blocks[0].y - 2*ng) * (kernels_y - 1);
//printf("ny_remaining: %i\n", ny_remaining);
for (int i = 0; i < (kernels_x-1); i++) {
threads[(kernels_y-1)*kernels_x + i].x =
int(sqrt(float(maxThreads)/nlayers));
threads[(kernels_y-1)*kernels_x + i].y =
(ny_remaining < threads[(kernels_y-1)*kernels_x + i].x) ? ny_remaining : threads[(kernels_y-1)*kernels_x + i].x;
threads[(kernels_y-1)*kernels_x + i].z = nlayers;
blocks[(kernels_y-1)*kernels_x + i].x = int(sqrt(float(maxBlocks)));
blocks[(kernels_y-1)*kernels_x + i].y = int(ceil(float(ny_remaining) /
float(threads[(kernels_y-1)*kernels_x + i].y)));
blocks[(kernels_y-1)*kernels_x + i].z = 1;
}
// recalculate
nx_remaining = nx - (threads[0].x * blocks[0].x - 2*ng) * (kernels_x - 1);
ny_remaining = ny - (threads[0].y * blocks[0].y - 2*ng) * (kernels_y - 1);
//printf("nx_remaining: %i\n", nx_remaining);
//printf("ny_remaining: %i\n", ny_remaining);
// (kernels_x-1, kernels_y-1)
threads[(kernels_y-1)*kernels_x + kernels_x-1].x =
(nx_remaining < int(sqrt(float(maxThreads)/nlayers))) ? nx_remaining : int(sqrt(float(maxThreads/nlayers)));
threads[(kernels_y-1)*kernels_x + kernels_x-1].y =
(ny_remaining < int(sqrt(float(maxThreads)/nlayers))) ? ny_remaining : int(sqrt(float(maxThreads)/nlayers));
threads[(kernels_y-1)*kernels_x + kernels_x-1].z = nlayers;
blocks[(kernels_y-1)*kernels_x + kernels_x-1].x =
int(ceil(float(nx_remaining) /
float(threads[(kernels_y-1)*kernels_x + kernels_x-1].x)));
blocks[(kernels_y-1)*kernels_x + kernels_x-1].y =
int(ceil(float(ny_remaining) /
float(threads[(kernels_y-1)*kernels_x + kernels_x-1].y)));
blocks[(kernels_y-1)*kernels_x + kernels_x-1].z = 1;
} else {
int total_threads = (total < maxThreads*2) ? nextPow2((total + 1)/ 2) : maxThreads;
threads[0].x = int(floor(sqrt(float(total_threads)/float(nlayers))));
threads[0].y = int(floor(sqrt(float(total_threads)/float(nlayers))));
threads[0].z = nlayers;
total_threads = threads[0].x * threads[0].y * threads[0].z;
int total_blocks = int(ceil(float(total) / float(total_threads)));
blocks[0].x = int(ceil(sqrt(float(total_blocks)/float(nx*ny))*nx));
blocks[0].y = int(ceil(sqrt(float(total_blocks)/float(nx*ny))*ny));
blocks[0].z = 1;
total_blocks = blocks[0].x * blocks[0].y;
if ((float)total_threads*total_blocks > (float)prop.maxGridSize[0] * prop.maxThreadsPerBlock) {
printf("n is too large, please choose a smaller number!\n");
}
if (total_blocks > prop.maxGridSize[0]) {
printf("Grid size <%d> exceeds the device capability <%d>, set block size as %d (original %d)\n",
total_blocks, prop.maxGridSize[0], total_threads*2, total_threads);
blocks[0].x /= 2;
blocks[0].y /= 2;
threads[0].x *= 2;
threads[0].y *= 2;
}
}
}
__device__ void bcs(float * grid, int nx, int ny, int nlayers, int kx_offset, int ky_offset) {
/*
Enforce boundary conditions on section of grid.
*/
// outflow
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
if ((l < nlayers) && (y < ny) && (x < nx) ) {
for (int i = 0; i < 4; i++) {
if (x == 0) {
grid[((y * nx) * nlayers + l)*4+i] = grid[((y * nx + 1) * nlayers + l)*4+i];
} else if (x == (nx-1)) {
grid[((y * nx + (nx-1)) * nlayers + l)*4+i] = grid[((y * nx + (nx-2)) * nlayers + l)*4+i];
} else if (y == 0) {
grid[(x * nlayers + l)*4+i] = grid[((nx + x) * nlayers + l)*4+i];
} else if (y == (ny-1)) {
grid[(((ny-1) * nx + x) * nlayers + l)*4+i] = grid[(((ny-2) * nx + x) * nlayers + l)*4+i];
}
}
}
}
__device__ void bcs_fv(float * grid, int nx, int ny, int nlayers, int ng, int kx_offset, int ky_offset) {
/*
Enforce boundary conditions on section of grid.
*/
// outflow
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
if ((l < nlayers) && (y < ny) && (x < nx) ) {
for (int i = 0; i < 4; i++) {
if (x < ng) {
grid[((y * nx + x) * nlayers + l)*4+i] = grid[((y * nx + ng) * nlayers + l)*4+i];
} else if (x > (nx-ng-1)) {
grid[((y * nx + x) * nlayers + l)*4+i] = grid[((y * nx + (nx-ng-1)) * nlayers + l)*4+i];
} else if (y < ng) {
grid[((y * nx + x) * nlayers + l)*4+i] = grid[(((ng * nx + x) * + x) * nlayers + l)*4+i];
} else if (y > (ny-ng-1)) {
grid[((y * nx + x) * nlayers + l)*4+i] = grid[(((ny-ng-1) * nx + x) * nlayers + l)*4+i];
}
}
}
}
void bcs_fv(float * grid, int nx, int ny, int nlayers, int ng) {
/*
Enforce boundary conditions on section of grid.
*/
// outflow
for (int l = 0; l < nlayers; l++) {
for (int y = 0; y < ny; y++){
for (int i = 0; i < 4; i++) {
for (int g = 0; g < ng; g++) {
grid[((y * nx + g) * nlayers + l)*4+i] = grid[((y * nx + ng) * nlayers + l)*4+i];
grid[((y * nx + (nx-1-g)) * nlayers + l)*4+i] = grid[((y * nx + (nx-1-ng)) * nlayers + l)*4+i];
}
}
}
for (int x = 0; x < nx; x++){
for (int i = 0; i < 4; i++) {
for (int g = 0; g < ng; g++) {
grid[((g * nx + x) * nlayers + l)*4+i] = grid[((ng * nx + x) * nlayers + l)*4+i];
grid[(((ny-1-g) * nx + x) * nlayers + l)*4+i] = grid[(((ny-1-ng) * nx + x) * nlayers + l)*4+i];
}
}
}
}
}
void bcs_mpi(float * grid, int nx, int ny, int nlayers, int ng, MPI_Comm comm, MPI_Status status, int rank, int n_processes, int y_size) {
/*
Enforce boundary conditions across processes / at edges of grid.
Loops have been ordered in a way so as to try and keep memory accesses as contiguous as possible.
Need to do non-blocking send, blocking receive then wait.
Parameters
----------
grid : float *
grid of data
nx, ny, nlayers : int
dimensions of grid
ng : int
number of ghost cells
comm : MPI_Comm
MPI communicator
status : MPI_Status
status of MPI processes
rank, n_processes : int
rank of MPI process and total number of MPI processes
y_size : int
size of grid in y direction running on each process (except the last one)
*/
// x boundaries
for (int y = 0; y < ny; y++) {
for (int g = 0; g < ng; g++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[((y * nx + g) * nlayers + l)*4+i] = grid[((y * nx + ng) * nlayers + l)*4+i];
grid[((y * nx + (nx-1-g)) * nlayers + l)*4+i] = grid[((y * nx + (nx-1-ng)) * nlayers + l)*4+i];
}
}
}
}
// interior cells between processes
// make some buffers for sending and receiving
float * ysbuf = new float[nlayers*nx*ng*4];
float * yrbuf = new float[nlayers*nx*ng*4];
int tag = 1;
int mpi_err;
MPI_Request request;
// if there are process above and below, send/receive
if ((rank > 0) && (rank < n_processes-1)) {
// send to below, receive from above
// copy stuff to buffer
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
ysbuf[((g * nx + x) * nlayers + l)*4+i] = grid[(((y_size*rank+ng+g) * nx + x) * nlayers + l)*4+i];
}
}
}
}
mpi_err = MPI_Issend(ysbuf, nlayers*nx*ng*4, MPI_FLOAT, rank-1, tag, comm, &request);
check_mpi_error(mpi_err);
mpi_err = MPI_Recv(yrbuf, nlayers*nx*ng*4, MPI_FLOAT, rank+1, tag, comm, &status);
check_mpi_error(mpi_err);
MPI_Wait(&request, &status);
// copy received data back to grid
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[(((y_size*rank+ny-ng+g) * nx + x) * nlayers + l)*4+i] = yrbuf[((g * nx + x) * nlayers + l)*4+i];
}
}
}
}
// send to above, receive from below
// copy stuff to buffer
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
ysbuf[((g * nx + x) * nlayers + l)*4+i] = grid[(((y_size*rank+ny-2*ng+g) * nx + x) * nlayers + l)*4+i];
}
}
}
}
MPI_Issend(ysbuf, nlayers*nx*ng*4, MPI_FLOAT, rank+1, tag, comm, &request);
MPI_Recv(yrbuf, nlayers*nx*ng*4, MPI_FLOAT, rank-1, tag, comm, &status);
MPI_Wait(&request, &status);
// copy received data back to grid
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[(((y_size*rank+g) * nx + x) * nlayers + l)*4+i] = yrbuf[((g * nx + x) * nlayers + l)*4+i];
}
}
}
}
} else if (rank == 0) {
// do outflow for top boundary
// copy stuff to buffer
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
ysbuf[((g * nx + x) * nlayers + l)*4+i] = grid[(((ny-2*ng+g) * nx + x) * nlayers + l)*4+i];
}
}
}
}
MPI_Issend(ysbuf, nlayers*nx*ng*4, MPI_FLOAT, 1, tag, comm, &request);
MPI_Recv(yrbuf, nlayers*nx*ng*4, MPI_FLOAT, 1, tag, comm, &status);
MPI_Wait(&request, &status);
// copy received data back to grid
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[(((ny-ng+g) * nx + x) * nlayers + l)*4+i] = yrbuf[((g * nx + x) * nlayers + l)*4+i];
}
}
}
}
// outflow stuff on top boundary
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[((g * nx + x) * nlayers + l)*4+i] = grid[((ng * nx + x) * nlayers + l)*4+i];
}
}
}
}
} else {
// copy stuff to buffer
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
ysbuf[((g * nx + x) * nlayers + l)*4+i] = grid[(((y_size*rank+ng+g) * nx + x) * nlayers + l)*4+i];
}
}
}
}
// bottom-most process
MPI_Issend(ysbuf, nlayers*nx*ng*4, MPI_FLOAT, rank-1, tag, comm, &request);
MPI_Recv(yrbuf, nlayers*nx*ng*4, MPI_FLOAT, rank-1, tag, comm, &status);
MPI_Wait(&request, &status);
// copy received data back to grid
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[(((y_size*rank+g) * nx + x) * nlayers + l)*4+i] = yrbuf[((g * nx + x) * nlayers + l)*4+i];
}
}
}
}
// outflow for bottom boundary
for (int g = 0; g < ng; g++) {
for (int x = 0; x < nx; x++) {
for (int l = 0; l < nlayers; l++) {
for (int i = 0; i < 4; i++) {
grid[(((ny-1-g) * nx + x) * nlayers + l)*4+i] = grid[(((ny-1-ng) * nx + x) * nlayers + l)*4+i];
}
}
}
}
}
delete[] ysbuf;
delete[] yrbuf;
}
__device__ void calc_Q(float * U, float * rho_d, float * Q_d,
int nx, int ny, int nlayers,
int kx_offset, int ky_offset, bool burning) {
/*
Calculate heating rate using equation 64 of Spitkovsky+ 2002.
Parameters
----------
U : float *
grid of state vectors
rho_d : float *
list of densities for different layers
Q_d : float *
grid of heating rate for each point in each layer
nx, ny, nlayers : int
dimensions of grids
kx_offset, ky_offset : int
x, y offset for current kernel
burning: bool
Do we have burning? If false, do nothing.
*/
if (burning) {
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
// set some constants
//float kappa = 0.03; // opacity, constant
//float column_depth = 5.4; // y
float Y = 1.0; // for simplicity as they do just have eps_3alpha = 0 so that helium abundance remains constant.
// in this model the scale height represents the temperature
if ((x > 0) && (x < (nx-1)) && (y > 0) && (y < (ny-1)) && (l < nlayers)) {
// changed to e^-35 to try and help GPU
Q_d[(y * nx + x) * nlayers + l] = 3.0e13 * rho_d[l]*rho_d[l] * pow(Y, 3) * exp(-35.0/U[((y * nx + x) * nlayers + l)*4]) / pow(U[((y * nx + x) * nlayers + l)*4], 3); //- 0.4622811 * pow(U[((y * nx + x) * nlayers + l)*4], 4) / (3.0 * kappa * column_depth * column_depth);
}
}
}
__global__ void evolve_fv(float * beta_d, float * gamma_up_d,
float * Un_d,
float * qx_plus_half, float * qx_minus_half,
float * qy_plus_half, float * qy_minus_half,
float * fx_plus_half, float * fx_minus_half,
float * fy_plus_half, float * fy_minus_half,
int nx, int ny, int nlayers, float alpha,
float dx, float dy, float dt,
int kx_offset, int ky_offset) {
/*
First part of evolution through one timestep using finite volume methods.
Reconstructs state vector to cell boundaries using slope limiter
and calculates fluxes there.
NOTE: we assume that beta is smooth so can get value at cell boundaries with simple averaging
Parameters
----------
beta_d : float *
shift vector at each grid point.
gamma_up_d : float *
gamma matrix at each grid point
Un_d : float *
state vector at each grid point in each layer
qx_plus_half, qx_minus_half : float *
state vector reconstructed at right and left boundaries
qy_plus_half, qy_minus_half : float *
state vector reconstructed at top and bottom boundaries
fx_plus_half, fx_minus_half : float *
flux vector at right and left boundaries
fy_plus_half, fy_minus_half : float *
flux vector at top and bottom boundaries
nx, ny, nlayers : int
dimensions of grid
alpha : float
lapse function
kx_offset, ky_offset : int
x, y offset for current kernel
*/
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
int offset = ((y * nx + x) * nlayers + l) * 4;
if ((x > 0) && (x < (nx-1)) && (y > 0) && (y < (ny-1)) && (l < nlayers)) {
// x-direction
for (int i = 0; i < 4; i++) {
float S_upwind = (Un_d[((y * nx + x+1) * nlayers + l) * 4 + i] -
Un_d[((y * nx + x) * nlayers + l) * 4 + i]) / dx;
float S_downwind = (Un_d[((y * nx + x) * nlayers + l) * 4 + i] -
Un_d[((y * nx + x-1) * nlayers + l) * 4 + i]) / dx;
float S = 0.5 * (S_upwind + S_downwind); // S_av
float r = 1.0e5;
// make sure don't divide by zero
if (abs(S_downwind) > 1.0e-5) {
r = S_upwind / S_downwind;
}
// MC
//float phi = max(float(0.0), min(float((2.0 * r) / (1.0 + r)), float(2.0 / (1.0 + r))));
// superbee
float phi = 0.0;
if (r >= 1.0) {
phi = min(float(2.0), min(r, float(2.0 / (1.0 + r))));
} else if (r >= 0.5) {
phi = 1.0;
} else if (r > 0.0) {
phi = 2.0 * r;
}
S *= phi;
qx_plus_half[offset + i] = Un_d[offset + i] + S * 0.5 * dx;
qx_minus_half[offset + i] = Un_d[offset + i] - S * 0.5 * dx;
// initialise
fx_plus_half[offset + i] = 0.0;
fx_minus_half[offset + i] = 0.0;
}
// plus half stuff
float W = sqrt(
float(qx_plus_half[offset + 1] * qx_plus_half[offset + 1] *
gamma_up_d[0] +
2.0 * qx_plus_half[offset + 1] * qx_plus_half[offset + 2] *
gamma_up_d[1] +
qx_plus_half[offset + 2] * qx_plus_half[offset + 2] *
gamma_up_d[3]) /
(qx_plus_half[offset] * qx_plus_half[offset]) + 1.0);
float u = qx_plus_half[offset + 1] / (qx_plus_half[offset] * W);
float v = qx_plus_half[offset + 2] / (qx_plus_half[offset] * W);
// beta[0] at i+1/2, j
float beta = 0.5 * (beta_d[(y * nx + x) * 2] + beta_d[(y * nx + x+1) * 2]);
float qx = u * gamma_up_d[0] + v * gamma_up_d[1] - beta / alpha;
fx_plus_half[offset] = qx_plus_half[offset] * qx;
fx_plus_half[offset + 1] = qx_plus_half[offset + 1] * qx +
0.5 * qx_plus_half[offset] * qx_plus_half[offset] / (W*W);
fx_plus_half[offset + 2] = qx_plus_half[offset + 2] * qx;
fx_plus_half[offset + 3] = qx_plus_half[offset + 3] * qx;
// minus half stuff
W = sqrt(
float(qx_minus_half[offset + 1] * qx_minus_half[offset + 1] *
gamma_up_d[0] +
2.0 * qx_minus_half[offset + 1] * qx_minus_half[offset + 2] *
gamma_up_d[1] +
qx_minus_half[offset + 2] * qx_minus_half[offset + 2] *
gamma_up_d[3]) /
(qx_minus_half[offset] * qx_minus_half[offset]) + 1.0);
u = qx_minus_half[offset + 1] / (qx_minus_half[offset] * W);
v = qx_minus_half[offset + 2] / (qx_minus_half[offset] * W);
// beta[0] at i-1/2, j
beta = 0.5 * (beta_d[(y * nx + x-1) * 2] + beta_d[(y * nx + x) * 2]);
qx = u * gamma_up_d[0] + v * gamma_up_d[1] - beta / alpha;
fx_minus_half[offset] = qx_minus_half[offset] * qx;
fx_minus_half[offset + 1] = qx_minus_half[offset + 1] * qx +
0.5 * qx_minus_half[offset] * qx_minus_half[offset] / (W*W);
fx_minus_half[offset + 2] = qx_minus_half[offset + 2] * qx;
fx_minus_half[offset + 3] = qx_minus_half[offset + 3] * qx;
// y-direction
for (int i = 0; i < 4; i++) {
float S_upwind = (Un_d[(((y+1) * nx + x) * nlayers + l) * 4 + i] -
Un_d[((y * nx + x) * nlayers + l) * 4 + i]) / dy;
float S_downwind = (Un_d[((y * nx + x) * nlayers + l) * 4 + i] -
Un_d[(((y-1) * nx + x) * nlayers + l) * 4 + i]) / dy;
float S = 0.5 * (S_upwind + S_downwind); // S_av
float r = 1.0e5;
// make sure don't divide by zero
if (abs(S_downwind) > 1.0e-5) {
r = S_upwind / S_downwind;
}
// MC
//float phi = max(float(0.0), min(float((2.0 * r) / (1.0 + r)), float(2.0 / (1.0 + r))));
// superbee
float phi = 0.0;
if (r >= 1.0) {
phi = min(float(2.0), min(r, float(2.0 / (1.0 + r))));
} else if (r >= 0.5) {
phi = 1.0;
} else if (r > 0.0) {
phi = 2.0 * r;
}
S *= phi;
qy_plus_half[offset + i] = Un_d[offset + i] + S * 0.5 * dy;
qy_minus_half[offset + i] = Un_d[offset + i] - S * 0.5 * dy;
// initialise
fy_plus_half[offset + i] = 0.0;
fy_minus_half[offset + i] = 0.0;
}
// plus half stuff
W = sqrt(
float(qy_plus_half[offset + 1] * qy_plus_half[offset + 1] *
gamma_up_d[0] +
2.0 * qy_plus_half[offset + 1] * qy_plus_half[offset + 2] *
gamma_up_d[1] +
qy_plus_half[offset + 2] * qy_plus_half[offset + 2] *
gamma_up_d[3]) /
(qy_plus_half[offset] * qy_plus_half[offset]) + 1.0);
u = qy_plus_half[offset + 1] / (qy_plus_half[offset] * W);
v = qy_plus_half[offset + 2] / (qy_plus_half[offset] * W);
// beta[1] at i, j+1/2
beta = 0.5 * (beta_d[((y+1) * nx + x) * 2 + 1] + beta_d[(y * nx + x) * 2 + 1]);
float qy = v * gamma_up_d[3] + u * gamma_up_d[1] - beta / alpha;
fy_plus_half[offset] = qy_plus_half[offset] * qy;
fy_plus_half[offset + 1] = qy_plus_half[offset + 1] * qy;
fy_plus_half[offset + 2] = qy_plus_half[offset + 2] * qy +
0.5 * qy_plus_half[offset] * qy_plus_half[offset] / (W*W);
fy_plus_half[offset + 3] = qy_plus_half[offset + 3] * qy;
// minus half stuff
W = sqrt(
float(qy_minus_half[offset+1] * qy_minus_half[offset+1] *
gamma_up_d[0] +
2.0 * qy_minus_half[offset + 1] * qy_minus_half[offset + 2] *
gamma_up_d[1] +
qy_minus_half[offset+2] * qy_minus_half[offset + 2] *
gamma_up_d[3]) /
(qy_minus_half[offset]*qy_minus_half[offset]) + 1.0);
u = qy_minus_half[offset + 1] / (qy_minus_half[offset] * W);
v = qy_minus_half[offset + 2] / (qy_minus_half[offset] * W);
// beta[1] at i, j-1/2
beta = 0.5 * (beta_d[((y-1) * nx + x) * 2 + 1] + beta_d[(y * nx + x) * 2 + 1]);
qy = v * gamma_up_d[3] + u * gamma_up_d[1] - beta / alpha;
fy_minus_half[offset] = qy_minus_half[offset] * qy;
fy_minus_half[offset + 1] = qy_minus_half[offset + 1] * qy;
fy_minus_half[offset + 2] = qy_minus_half[offset + 2] * qy +
0.5 * qy_minus_half[offset] * qy_minus_half[offset] / (W*W);
fy_minus_half[offset + 3] = qy_minus_half[offset + 3] * qy;
}
}
__global__ void evolve_fv_fluxes(float * F,
float * qx_plus_half, float * qx_minus_half,
float * qy_plus_half, float * qy_minus_half,
float * fx_plus_half, float * fx_minus_half,
float * fy_plus_half, float * fy_minus_half,
int nx, int ny, int nlayers, float alpha,
float dx, float dy, float dt,
int kx_offset, int ky_offset) {
/*
Calculates fluxes in finite volume evolution by solving the Riemann
problem at the cell boundaries.
Parameters
----------
F : float *
flux vector at each point in grid and each layer
qx_plus_half, qx_minus_half : float *
state vector reconstructed at right and left boundaries
qy_plus_half, qy_minus_half : float *
state vector reconstructed at top and bottom boundaries
fx_plus_half, fx_minus_half : float *
flux vector at right and left boundaries
fy_plus_half, fy_minus_half : float *
flux vector at top and bottom boundaries
nx, ny, nlayers : int
dimensions of grid
alpha : float
lapse function
dx, dy, dt : float
gridpoint spacing and timestep spacing
kx_offset, ky_offset : int
x, y offset for current kernel
*/
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
// do fluxes
if ((x > 0) && (x < (nx-1)) && (y > 0) && (y < (ny-1)) && (l < nlayers)) {
for (int i = 0; i < 4; i++) {
// x-boundary
// from i-1
float fx_m = 0.5 * (
fx_plus_half[((y * nx + x-1) * nlayers + l) * 4 + i] +
fx_minus_half[((y * nx + x) * nlayers + l) * 4 + i] +
qx_plus_half[((y * nx + x-1) * nlayers + l) * 4 + i] -
qx_minus_half[((y * nx + x) * nlayers + l) * 4 + i]);
// from i+1
float fx_p = 0.5 * (
fx_plus_half[((y * nx + x) * nlayers + l) * 4 + i] +
fx_minus_half[((y * nx + x+1) * nlayers + l) * 4 + i] +
qx_plus_half[((y * nx + x) * nlayers + l) * 4 + i] -
qx_minus_half[((y * nx + x+1) * nlayers + l) * 4 + i]);
// y-boundary
// from j-1
float fy_m = 0.5 * (
fy_plus_half[(((y-1) * nx + x) * nlayers + l) * 4 + i] +
fy_minus_half[((y * nx + x) * nlayers + l) * 4 + i] +
qy_plus_half[(((y-1) * nx + x) * nlayers + l) * 4 + i] -
qy_minus_half[((y * nx + x) * nlayers + l) * 4 + i]);
// from j+1
float fy_p = 0.5 * (
fy_plus_half[((y * nx + x) * nlayers + l) * 4 + i] +
fy_minus_half[(((y+1) * nx + x) * nlayers + l) * 4 + i] +
qy_plus_half[((y * nx + x) * nlayers + l) * 4 + i] -
qy_minus_half[(((y+1) * nx + x) * nlayers + l) * 4 + i]);
F[((y * nx + x) * nlayers + l)*4 + i] =
-((1.0/dx) * alpha * (fx_p - fx_m) +
(1.0/dy) * alpha * (fy_p - fy_m));
}
}
}
__global__ void evolve_fv_heating(float * gamma_up_d,
float * Un_d, float * Up, float * U_half,
float * qx_plus_half, float * qx_minus_half,
float * qy_plus_half, float * qy_minus_half,
float * fx_plus_half, float * fx_minus_half,
float * fy_plus_half, float * fy_minus_half,
float * sum_phs, float * rho_d, float * Q_d,
float mu,
int nx, int ny, int nlayers, float alpha,
float dx, float dy, float dt,
bool burning,
int kx_offset, int ky_offset) {
/*
Does the heating part of the evolution.
Parameters
----------
gamma_up_d : float *
gamma matrix at each grid point
Un_d : float *
state vector at each grid point in each layer at current timestep
Up : float *
state vector at next timestep
U_half : float *
state vector at half timestep
qx_plus_half, qx_minus_half : float *
state vector reconstructed at right and left boundaries
qy_plus_half, qy_minus_half : float *
state vector reconstructed at top and bottom boundaries
fx_plus_half, fx_minus_half : float *
flux vector at right and left boundaries
fy_plus_half, fy_minus_half : float *
flux vector at top and bottom boundaries
sum_phs : float *
sum of Phi in different layers
rho_d : float *
list of densities in different layers
Q_d : float *
heating rate at each grid point in each layer
mu : float
friction
nx, ny, nlayers : int
dimensions of grid
alpha : float
lapse function
dx, dy, dt : float
gridpoint spacing and timestep spacing
burning : bool
is burning present in this system?
kx_offset, ky_offset : int
x, y offset for current kernel
*/
int x = kx_offset + blockIdx.x * blockDim.x + threadIdx.x;
int y = ky_offset + blockIdx.y * blockDim.y + threadIdx.y;
int l = threadIdx.z;
// copy to U_half
if ((x < nx) && (y < ny) && (l < nlayers)) {
for (int i = 0; i < 4; i++) {
U_half[((y * nx + x) * nlayers + l)*4+i] =
Up[((y * nx + x) * nlayers + l)*4+i];
}
}
// calculate Q
calc_Q(Up, rho_d, Q_d, nx, ny, nlayers, kx_offset, ky_offset, burning);
float W = 1.0;
// do source terms
if ((x < nx) && (y < ny) && (l < nlayers)) {
W = sqrt(float((U_half[((y * nx + x) * nlayers + l)*4+1] *
U_half[((y * nx + x) * nlayers + l)*4+1] * gamma_up_d[0] +
2.0 * U_half[((y * nx + x) * nlayers + l)*4+1] *
U_half[((y * nx + x) * nlayers + l)*4+2] *
gamma_up_d[1] +
U_half[((y * nx + x) * nlayers + l)*4+2] *
U_half[((y * nx + x) * nlayers + l)*4+2] *
gamma_up_d[3]) /
(U_half[((y * nx + x) * nlayers + l)*4] *
U_half[((y * nx + x) * nlayers + l)*4]) + 1.0));
U_half[((y * nx + x) * nlayers + l)*4] /= W;
}
__syncthreads();
if ((x < nx) && (y < ny) && (l < nlayers)) {
sum_phs[(y * nx + x) * nlayers + l] = 0.0;
float sum_qs = 0.0;
float deltaQx = 0.0;
float deltaQy = 0.0;
if (l < (nlayers - 1)) {
sum_qs += (Q_d[(y * nx + x) * nlayers + l+1] - Q_d[(y * nx + x) * nlayers + l]);
deltaQx = (Q_d[(y * nx + x) * nlayers + l] + mu) *
(U_half[((y * nx + x) * nlayers + l)*4+1] -
U_half[((y * nx + x) * nlayers + (l+1))*4+1]) /
(W*U_half[((y * nx + x) * nlayers + l)*4]);
deltaQy = (Q_d[(y * nx + x) * nlayers + l] + mu) *
(U_half[((y * nx + x) * nlayers + l)*4+2] -
U_half[((y * nx + x) * nlayers + (l+1))*4+2]) /
(W*U_half[((y * nx + x) * nlayers + l)*4]);
}
if (l > 0) {
sum_qs += -rho_d[l-1] / rho_d[l] * (Q_d[(y * nx + x) * nlayers + l] - Q_d[(y * nx + x) * nlayers + l-1]);
deltaQx = rho_d[l-1] / rho_d[l] *
(Q_d[(y * nx + x) * nlayers + l] + mu) *
(U_half[((y * nx + x) * nlayers + l)*4+1] -
U_half[((y * nx + x) * nlayers + l-1)*4+1]) /
(W*U_half[((y * nx + x) * nlayers + l)*4]);
deltaQy = rho_d[l-1] / rho_d[l] *
(Q_d[(y * nx + x) * nlayers + l] + mu) *
(U_half[((y * nx + x) * nlayers + l)*4+2] -
U_half[((y * nx + x) * nlayers + l-1)*4+2]) /
(W*U_half[((y * nx + x) * nlayers + l)*4]);
}
for (int j = 0; j < l; j++) {
sum_phs[(y * nx + x) * nlayers + l] += rho_d[j] / rho_d[l] *
U_half[((y * nx + x) * nlayers + j)*4];