-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathm_mpi_common.fpp
More file actions
1907 lines (1618 loc) · 87.5 KB
/
Copy pathm_mpi_common.fpp
File metadata and controls
1907 lines (1618 loc) · 87.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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
!>
!! @file
!! @brief Contains module m_mpi_common
#:include 'case.fpp'
#:include 'macros.fpp'
!> @brief MPI communication layer: domain decomposition, halo exchange, reductions, and parallel I/O setup
module m_mpi_common
#ifdef MFC_MPI
use mpi !< Message passing interface (MPI) module
#endif
use m_derived_types
use m_global_parameters
use m_helper
use ieee_arithmetic
use m_nvtx
use m_constants, only: recon_type_weno
implicit none
private :: s_apply_decomposition_policies
integer, private :: v_size
$:GPU_DECLARE(create='[v_size]')
real(wp), private, allocatable, dimension(:) :: buff_send !< Primitive variable send buffer for halo exchange
!> Primitive variable receive buffer for halo exchange Variables for EL bubbles communication
real(wp), private, allocatable, dimension(:) :: buff_recv
type(int_bounds_info) :: comm_coords(3)
integer :: comm_size(3)
!> q_beta indices to communicate: 1=void fraction, 2=d(beta)/dt, 5=energy source
integer :: beta_vars(1:3) = [1, 2, 5]
$:GPU_DECLARE(create='[comm_coords, comm_size, beta_vars]')
#ifndef __NVCOMPILER_GPU_UNIFIED_MEM
$:GPU_DECLARE(create='[buff_send, buff_recv]')
#endif
integer(kind=8) :: halo_size
$:GPU_DECLARE(create='[halo_size]')
logical, private :: exchange_all_chemistry_temperatures = .false.
logical, private :: use_rdma_transport = .false.
contains
!> Initialize the module.
impure subroutine s_initialize_mpi_common_module(exchange_all_chemistry_temperatures_in, use_rdma_transport_in)
logical, intent(in) :: exchange_all_chemistry_temperatures_in
logical, intent(in) :: use_rdma_transport_in
exchange_all_chemistry_temperatures = exchange_all_chemistry_temperatures_in
use_rdma_transport = use_rdma_transport_in
#ifdef MFC_MPI
! Allocating buff_send/recv and. Please note that for the sake of simplicity, both variables are provided sufficient storage
! to hold the largest buffer in the computational domain.
if (qbmm .and. .not. polytropic) then
v_size = sys_size + 2*nb*nnode
else if (chemistry .and. (chem_params%diffusion .or. exchange_all_chemistry_temperatures)) then
v_size = sys_size + 1
else
v_size = sys_size
end if
if (n > 0) then
if (p > 0) then
halo_size = nint(-1._wp + 1._wp*buff_size*(v_size)*(m + 2*buff_size + 1)*(n + 2*buff_size + 1)*(p + 2*buff_size &
& + 1)/(cells_bounds%mnp_min + 2*buff_size + 1))
else
halo_size = -1 + buff_size*(v_size)*(cells_bounds%mn_max + 2*buff_size + 1)
end if
else
halo_size = -1 + buff_size*(v_size)
end if
$:GPU_UPDATE(device='[halo_size, v_size]')
#ifndef __NVCOMPILER_GPU_UNIFIED_MEM
@:ALLOCATE(buff_send(0:halo_size), buff_recv(0:halo_size))
#else
allocate (buff_send(0:halo_size), buff_recv(0:halo_size))
$:GPU_ENTER_DATA(create='[capture:buff_send]')
$:GPU_ENTER_DATA(create='[capture:buff_recv]')
#endif
#endif
$:GPU_UPDATE(device='[beta_vars]')
end subroutine s_initialize_mpi_common_module
!> Initialize the MPI execution environment and query the number of processors and local rank.
impure subroutine s_mpi_initialize
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_INIT(ierr)
if (ierr /= MPI_SUCCESS) then
print '(A)', 'Unable to initialize MPI environment. Exiting.'
call MPI_ABORT(MPI_COMM_WORLD, 1, ierr)
end if
call MPI_COMM_SIZE(MPI_COMM_WORLD, num_procs, ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD, proc_rank, ierr)
#else
num_procs = 1
proc_rank = 0
#endif
$:GPU_UPDATE(device='[num_procs, proc_rank]')
end subroutine s_mpi_initialize
!> Set up MPI I/O data views and variable pointers for parallel file output.
impure subroutine s_initialize_mpi_data(q_cons_vf, ib_markers, ib_mpi_data, beta, qbmm_pb, qbmm_mv)
type(scalar_field), dimension(sys_size), intent(in) :: q_cons_vf
type(integer_field), optional, intent(in) :: ib_markers
type(mpi_io_ib_var), optional, intent(inout) :: ib_mpi_data
type(scalar_field), intent(in), optional :: beta
type(pres_field), intent(in), optional :: qbmm_pb, qbmm_mv
integer, dimension(num_dims) :: sizes_glb, sizes_loc
#ifdef MFC_MPI
integer :: i, j
integer :: ierr !< Generic flag used to identify and report MPI errors
integer :: alt_sys
logical :: bind_qbmm_fields
if (present(qbmm_pb) .neqv. present(qbmm_mv)) then
call s_mpi_abort('QBMM MPI I/O requires both pressure and moment fields.')
end if
bind_qbmm_fields = qbmm .and. .not. polytropic .and. present(qbmm_pb) .and. present(qbmm_mv)
if (present(beta)) then
alt_sys = sys_size + 1
else
alt_sys = sys_size
end if
do i = 1, sys_size
MPI_IO_DATA%var(i)%sf => q_cons_vf(i)%sf(0:m,0:n,0:p)
end do
if (present(beta)) then
MPI_IO_DATA%var(alt_sys)%sf => beta%sf(0:m,0:n,0:p)
end if
! Additional variables pb and mv for non-polytropic qbmm
if (bind_qbmm_fields) then
do i = 1, nb
do j = 1, nnode
MPI_IO_DATA%var(sys_size + (i - 1)*nnode + j)%sf => qbmm_pb%sf(0:m,0:n,0:p,j, i)
MPI_IO_DATA%var(sys_size + (i - 1)*nnode + j + nb*nnode)%sf => qbmm_mv%sf(0:m,0:n,0:p,j, i)
end do
end do
end if
! Define global(g) and local(l) sizes for flow variables
sizes_glb(1) = m_glb + 1; sizes_loc(1) = m + 1
if (n > 0) then
sizes_glb(2) = n_glb + 1; sizes_loc(2) = n + 1
if (p > 0) then
sizes_glb(num_dims) = p_glb + 1; sizes_loc(num_dims) = p + 1
end if
end if
! Define the view for each variable
do i = 1, alt_sys
call MPI_TYPE_CREATE_SUBARRAY(num_dims, sizes_glb, sizes_loc, start_idx, MPI_ORDER_FORTRAN, mpi_p, &
& MPI_IO_DATA%view(i), ierr)
call MPI_TYPE_COMMIT(MPI_IO_DATA%view(i), ierr)
end do
if (bind_qbmm_fields) then
do i = sys_size + 1, sys_size + 2*nb*nnode
call MPI_TYPE_CREATE_SUBARRAY(num_dims, sizes_glb, sizes_loc, start_idx, MPI_ORDER_FORTRAN, mpi_p, &
& MPI_IO_DATA%view(i), ierr)
call MPI_TYPE_COMMIT(MPI_IO_DATA%view(i), ierr)
end do
end if
if (present(ib_markers) .neqv. present(ib_mpi_data)) then
call s_mpi_abort('Immersed-boundary MPI I/O requires both marker and descriptor fields.')
end if
if (present(ib_markers)) then
ib_mpi_data%var%sf => ib_markers%sf(0:m,0:n,0:p)
call MPI_TYPE_CREATE_SUBARRAY(num_dims, sizes_glb, sizes_loc, start_idx, MPI_ORDER_FORTRAN, MPI_INTEGER, &
& ib_mpi_data%view, ierr)
call MPI_TYPE_COMMIT(ib_mpi_data%view, ierr)
end if
#endif
end subroutine s_initialize_mpi_data
!> Set up MPI I/O data views for downsampled (coarsened) parallel file output.
subroutine s_initialize_mpi_data_ds(m_ds, n_ds, p_ds, q_cons_vf)
integer, intent(in) :: m_ds, n_ds, p_ds
type(scalar_field), dimension(sys_size), intent(in), optional :: q_cons_vf
integer, dimension(num_dims) :: sizes_loc
integer, dimension(3) :: sf_start_idx
#ifdef MFC_MPI
integer :: i, ierr
sf_start_idx = (/0, 0, 0/)
if (present(q_cons_vf)) then
do i = 1, sys_size
MPI_IO_DATA%var(i)%sf => q_cons_vf(i)%sf(-1:m_ds + 1,-1:n_ds + 1,-1:p_ds + 1)
end do
end if
! Define global(g) and local(l) sizes for flow variables
sizes_loc(1) = m_ds + 3
if (n > 0) then
sizes_loc(2) = n_ds + 3
if (p > 0) then
sizes_loc(num_dims) = p_ds + 3
end if
end if
! Define the view for each variable
do i = 1, sys_size
call MPI_TYPE_CREATE_SUBARRAY(num_dims, sizes_loc, sizes_loc, sf_start_idx, MPI_ORDER_FORTRAN, mpi_p, &
& MPI_IO_DATA%view(i), ierr)
call MPI_TYPE_COMMIT(MPI_IO_DATA%view(i), ierr)
end do
#endif
end subroutine s_initialize_mpi_data_ds
!> Gather variable-length real vectors from all MPI ranks onto the root process.
impure subroutine s_mpi_gather_data(my_vector, counts, gathered_vector, root)
integer, intent(in) :: counts !< Array of vector lengths for each process
real(wp), intent(in), dimension(counts) :: my_vector !< Input vector on each process
integer, intent(in) :: root !< Rank of the root process
real(wp), allocatable, intent(out) :: gathered_vector(:) !< Gathered vector on the root process
integer :: i
integer :: ierr !< Generic flag used to identify and report MPI errors
integer, allocatable :: recounts(:), displs(:)
#ifdef MFC_MPI
allocate (recounts(num_procs))
call MPI_GATHER(counts, 1, MPI_INTEGER, recounts, 1, MPI_INTEGER, root, MPI_COMM_WORLD, ierr)
allocate (displs(size(recounts)))
displs(1) = 0
do i = 2, size(recounts)
displs(i) = displs(i - 1) + recounts(i - 1)
end do
allocate (gathered_vector(sum(recounts)))
call MPI_GATHERV(my_vector, counts, mpi_p, gathered_vector, recounts, displs, mpi_p, root, MPI_COMM_WORLD, ierr)
#endif
end subroutine s_mpi_gather_data
!> Gather per-rank time step wall-clock times onto rank 0 for performance reporting.
impure subroutine mpi_bcast_time_step_values(proc_time, time_avg)
real(wp), dimension(0:num_procs - 1), intent(inout) :: proc_time
real(wp), intent(inout) :: time_avg
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_GATHER(time_avg, 1, mpi_p, proc_time(0), 1, mpi_p, 0, MPI_COMM_WORLD, ierr)
#endif
end subroutine mpi_bcast_time_step_values
!> Print a case file error with the prohibited condition and message, then abort execution.
impure subroutine s_prohibit_abort(condition, message)
character(len=*), intent(in) :: condition, message
print *, ""
print *, "CASE FILE ERROR"
print *, " - Prohibited condition: ", trim(condition)
if (len_trim(message) > 0) then
print *, " - Note: ", trim(message)
end if
print *, ""
call s_mpi_abort(code=CASE_FILE_ERROR_CODE)
end subroutine s_prohibit_abort
!> The goal of this subroutine is to determine the global extrema of the stability criteria in the computational domain. This is
!! performed by sifting through the local extrema of each stability criterion. Note that each of the local extrema is from a
!! single process, within its assigned section of the computational domain. Finally, note that the global extrema values are
!! only bookkeept on the rank 0 processor.
impure subroutine s_mpi_reduce_stability_criteria_extrema(icfl_max_loc, vcfl_max_loc, Rc_min_loc, bubs_loc, icfl_max_glb, &
& vcfl_max_glb, Rc_min_glb, bubs_glb, ccfl_max_loc, ccfl_max_glb)
real(wp), intent(in) :: icfl_max_loc
real(wp), intent(in) :: vcfl_max_loc
real(wp), intent(in) :: Rc_min_loc
integer, intent(in) :: bubs_loc
real(wp), intent(out) :: icfl_max_glb
real(wp), intent(out) :: vcfl_max_glb
real(wp), intent(out) :: Rc_min_glb
integer, intent(out) :: bubs_glb
real(wp), intent(in) :: ccfl_max_loc
real(wp), intent(out) :: ccfl_max_glb
icfl_max_glb = icfl_max_loc
vcfl_max_glb = vcfl_max_loc
Rc_min_glb = Rc_min_loc
ccfl_max_glb = ccfl_max_loc
#ifdef MFC_MPI
block
integer :: ierr
bubs_glb = 0
call MPI_REDUCE(icfl_max_loc, icfl_max_glb, 1, mpi_p, MPI_MAX, 0, MPI_COMM_WORLD, ierr)
if (viscous) then
call MPI_REDUCE(vcfl_max_loc, vcfl_max_glb, 1, mpi_p, MPI_MAX, 0, MPI_COMM_WORLD, ierr)
call MPI_REDUCE(Rc_min_loc, Rc_min_glb, 1, mpi_p, MPI_MIN, 0, MPI_COMM_WORLD, ierr)
end if
if (surface_tension) then
call MPI_REDUCE(ccfl_max_loc, ccfl_max_glb, 1, mpi_p, MPI_MAX, 0, MPI_COMM_WORLD, ierr)
end if
if (bubbles_lagrange) then
call MPI_REDUCE(bubs_loc, bubs_glb, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
end if
end block
#else
icfl_max_glb = icfl_max_loc
bubs_glb = 0
if (viscous) then
vcfl_max_glb = vcfl_max_loc
Rc_min_glb = Rc_min_loc
end if
if (surface_tension) then
ccfl_max_glb = ccfl_max_loc
end if
if (bubbles_lagrange) bubs_glb = bubs_loc
#endif
end subroutine s_mpi_reduce_stability_criteria_extrema
!> Reduce a local integer value to its global sum across all MPI ranks.
subroutine s_mpi_reduce_int_sum(var_loc, sum)
integer, intent(in) :: var_loc
integer, intent(out) :: sum
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_REDUCE(var_loc, sum, 1, MPI_INTEGER, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
#else
sum = var_loc
#endif
end subroutine s_mpi_reduce_int_sum
!> Reduce a local real value to its global sum across all MPI ranks.
impure subroutine s_mpi_allreduce_sum(var_loc, var_glb)
real(wp), intent(in) :: var_loc
real(wp), intent(out) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_ALLREDUCE(var_loc, var_glb, 1, mpi_p, MPI_SUM, MPI_COMM_WORLD, ierr)
#endif
end subroutine s_mpi_allreduce_sum
!> Reduce an array of vectors to their global sums across all MPI ranks.
impure subroutine s_mpi_allreduce_vectors_sum(var_loc, var_glb, num_vectors, vector_length)
integer, intent(in) :: num_vectors, vector_length
real(wp), dimension(:,:), intent(in) :: var_loc
real(wp), dimension(:,:), intent(inout) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
if (loc(var_loc) == loc(var_glb)) then
call MPI_Allreduce(MPI_IN_PLACE, var_glb, num_vectors*vector_length, mpi_p, MPI_SUM, MPI_COMM_WORLD, ierr)
else
call MPI_Allreduce(var_loc, var_glb, num_vectors*vector_length, mpi_p, MPI_SUM, MPI_COMM_WORLD, ierr)
end if
#else
var_glb(1:num_vectors,1:vector_length) = var_loc(1:num_vectors,1:vector_length)
#endif
end subroutine s_mpi_allreduce_vectors_sum
!> Reduce a local integer value to its global sum across all MPI ranks.
impure subroutine s_mpi_allreduce_integer_sum(var_loc, var_glb)
integer(kind=8), intent(in) :: var_loc
integer(kind=8), intent(out) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_ALLREDUCE(var_loc, var_glb, 1, MPI_INTEGER8, MPI_SUM, MPI_COMM_WORLD, ierr)
#else
var_glb = var_loc
#endif
end subroutine s_mpi_allreduce_integer_sum
!> Reduce a local real value to its global minimum across all MPI ranks.
impure subroutine s_mpi_allreduce_min(var_loc, var_glb)
real(wp), intent(in) :: var_loc
real(wp), intent(out) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_ALLREDUCE(var_loc, var_glb, 1, mpi_p, MPI_MIN, MPI_COMM_WORLD, ierr)
#endif
end subroutine s_mpi_allreduce_min
!> Reduce a local real vector to its elementwise global minimum across all MPI ranks.
impure subroutine s_mpi_allreduce_min_vec(var_loc, var_glb)
real(wp), dimension(:), intent(in) :: var_loc
real(wp), dimension(:), intent(out) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_ALLREDUCE(var_loc, var_glb, size(var_loc), mpi_p, MPI_MIN, MPI_COMM_WORLD, ierr)
#else
var_glb = var_loc
#endif
end subroutine s_mpi_allreduce_min_vec
!> Reduce a local real value to its global maximum across all MPI ranks.
impure subroutine s_mpi_allreduce_max(var_loc, var_glb)
real(wp), intent(in) :: var_loc
real(wp), intent(out) :: var_glb
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_ALLREDUCE(var_loc, var_glb, 1, mpi_p, MPI_MAX, MPI_COMM_WORLD, ierr)
#endif
end subroutine s_mpi_allreduce_max
!> Reduce a local real value to its global minimum across all ranks
impure subroutine s_mpi_reduce_min(var_loc)
real(wp), intent(inout) :: var_loc
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
real(wp) :: var_glb
call MPI_REDUCE(var_loc, var_glb, 1, mpi_p, MPI_MIN, 0, MPI_COMM_WORLD, ierr)
call MPI_BCAST(var_glb, 1, mpi_p, 0, MPI_COMM_WORLD, ierr)
var_loc = var_glb
#endif
end subroutine s_mpi_reduce_min
!> Reduce a 2-element variable to its global maximum value with the owning processor rank (MPI_MAXLOC).
!> Reduce a local value to its global maximum with location (rank) across all ranks
impure subroutine s_mpi_reduce_maxloc(var_loc)
real(wp), dimension(2), intent(inout) :: var_loc
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
real(wp), dimension(2) :: var_glb !< Reduced (max value, rank) pair
call MPI_REDUCE(var_loc, var_glb, 1, mpi_2p, MPI_MAXLOC, 0, MPI_COMM_WORLD, ierr)
call MPI_BCAST(var_glb, 1, mpi_2p, 0, MPI_COMM_WORLD, ierr)
var_loc = var_glb
#endif
end subroutine s_mpi_reduce_maxloc
!> The subroutine terminates the MPI execution environment.
impure subroutine s_mpi_abort(prnt, code)
character(len=*), intent(in), optional :: prnt
integer, intent(in), optional :: code
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
#endif
if (present(prnt)) then
print *, prnt
call flush (6)
end if
#ifndef MFC_MPI
if (present(code)) then
stop code
else
stop 1
end if
#else
if (present(code)) then
call MPI_ABORT(MPI_COMM_WORLD, code, ierr)
else
call MPI_ABORT(MPI_COMM_WORLD, 1, ierr)
end if
#endif
end subroutine s_mpi_abort
!> Halts all processes until all have reached barrier.
impure subroutine s_mpi_barrier
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_BARRIER(MPI_COMM_WORLD, ierr)
#endif
end subroutine s_mpi_barrier
!> The subroutine finalizes the MPI execution environment.
impure subroutine s_mpi_finalize
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call MPI_FINALIZE(ierr)
#endif
end subroutine s_mpi_finalize
!> The goal of this procedure is to populate the buffers of the cell-average conservative variables by communicating with the
!! neighboring processors.
subroutine s_mpi_sendrecv_variables_buffers(q_comm, mpi_dir, pbc_loc, nVar, pb_in, mv_in, q_T_sf)
type(scalar_field), dimension(1:), intent(inout) :: q_comm
real(stp), optional, dimension(idwbuff(1)%beg:,idwbuff(2)%beg:,idwbuff(3)%beg:,1:,1:), intent(inout) :: pb_in, mv_in
integer, intent(in) :: mpi_dir, pbc_loc, nVar
integer :: i, j, k, l, r, q !< Generic loop iterators
integer :: buffer_counts(1:3), buffer_count
type(int_bounds_info) :: boundary_conditions(1:3)
integer :: beg_end(1:2), grid_dims(1:3)
integer :: dst_proc, src_proc, recv_tag, send_tag
logical :: beg_end_geq_0, qbmm_comm, chem_diff_comm
integer :: pack_offset, unpack_offset
type(scalar_field), optional, intent(inout) :: q_T_sf
#ifdef MFC_MPI
integer :: ierr !< Generic flag used to identify and report MPI errors
call nvtxStartRange("RHS-COMM-PACKBUF")
qbmm_comm = .false.
chem_diff_comm = .false.
if (present(pb_in) .and. present(mv_in) .and. qbmm .and. .not. polytropic) then
qbmm_comm = .true.
v_size = nVar + 2*nb*nnode
buffer_counts = (/buff_size*v_size*(n + 1)*(p + 1), buff_size*v_size*(m + 2*buff_size + 1)*(p + 1), &
& buff_size*v_size*(m + 2*buff_size + 1)*(n + 2*buff_size + 1)/)
else if (present(q_T_sf) .and. chemistry .and. (chem_params%diffusion .or. exchange_all_chemistry_temperatures)) then
! Consumers that convert over ghost-inclusive bounds request temperature exchange for every chemistry run.
! The temperature Newton guess must be valid at rank seams even when diffusion is disabled:
! an unexchanged seam ghost is an uninitialized guess -> NaN T/pres/c in the output
chem_diff_comm = .true.
v_size = nVar + 1
buffer_counts = (/buff_size*v_size*(n + 1)*(p + 1), buff_size*v_size*(m + 2*buff_size + 1)*(p + 1), &
& buff_size*v_size*(m + 2*buff_size + 1)*(n + 2*buff_size + 1)/)
else
v_size = nVar
buffer_counts = (/buff_size*v_size*(n + 1)*(p + 1), buff_size*v_size*(m + 2*buff_size + 1)*(p + 1), &
& buff_size*v_size*(m + 2*buff_size + 1)*(n + 2*buff_size + 1)/)
end if
$:GPU_UPDATE(device='[v_size]')
buffer_count = buffer_counts(mpi_dir)
boundary_conditions = (/bc_x, bc_y, bc_z/)
beg_end = (/boundary_conditions(mpi_dir)%beg, boundary_conditions(mpi_dir)%end/)
beg_end_geq_0 = beg_end(max(pbc_loc, 0) - pbc_loc + 1) >= 0
! Implements: pbc_loc bc_x >= 0 -> [send/recv]_tag [dst/src]_proc -1 (=0) 0 -> [1,0] [0,0] | 0 0 [1,0] [beg,beg] -1 (=0) 1
! -> [0,0] [1,0] | 0 1 [0,0] [end,beg] +1 (=1) 0 -> [0,1] [1,1] | 1 0 [0,1] [end,end] +1 (=1) 1 -> [1,1] [0,1] | 1 1 [1,1]
! [beg,end]
send_tag = f_logical_to_int(.not. f_xor(beg_end_geq_0, pbc_loc == 1))
recv_tag = f_logical_to_int(pbc_loc == 1)
dst_proc = beg_end(1 + f_logical_to_int(f_xor(pbc_loc == 1, beg_end_geq_0)))
src_proc = beg_end(1 + f_logical_to_int(pbc_loc == 1))
grid_dims = (/m, n, p/)
pack_offset = 0
if (f_xor(pbc_loc == 1, beg_end_geq_0)) then
pack_offset = grid_dims(mpi_dir) - buff_size + 1
end if
unpack_offset = 0
if (pbc_loc == 1) then
unpack_offset = grid_dims(mpi_dir) + buff_size + 1
end if
! Pack Buffer to Send
#:for mpi_dir in [1, 2, 3]
if (mpi_dir == ${mpi_dir}$) then
#:if mpi_dir == 1
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do l = 0, p
do k = 0, n
do j = 0, buff_size - 1
do i = 1, nVar
r = (i - 1) + v_size*(j + buff_size*(k + (n + 1)*l))
buff_send(r) = real(q_comm(i)%sf(j + pack_offset, k, l), kind=wp)
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
if (chem_diff_comm) then
$:GPU_PARALLEL_LOOP(collapse=3,private='[r]')
do l = 0, p
do k = 0, n
do j = 0, buff_size - 1
r = nVar + v_size*(j + buff_size*(k + (n + 1)*l))
buff_send(r) = real(q_T_sf%sf(j + pack_offset, k, l), kind=wp)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
if (qbmm_comm) then
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do l = 0, p
do k = 0, n
do j = 0, buff_size - 1
do i = nVar + 1, nVar + nnode
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + v_size*(j + buff_size*(k + (n + 1)*l))
buff_send(r) = real(pb_in(j + pack_offset, k, l, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do l = 0, p
do k = 0, n
do j = 0, buff_size - 1
do i = nVar + 1, nVar + nnode
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + nb*nnode + v_size*(j + buff_size*(k + (n + 1)*l))
buff_send(r) = real(mv_in(j + pack_offset, k, l, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
#:elif mpi_dir == 2
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do i = 1, nVar
do l = 0, p
do k = 0, buff_size - 1
do j = -buff_size, m + buff_size
r = (i - 1) + v_size*((j + buff_size) + (m + 2*buff_size + 1)*(k + buff_size*l))
buff_send(r) = real(q_comm(i)%sf(j, k + pack_offset, l), kind=wp)
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
if (chem_diff_comm) then
$:GPU_PARALLEL_LOOP(collapse=3,private='[r]')
do l = 0, p
do k = 0, buff_size - 1
do j = -buff_size, m + buff_size
r = nVar + v_size*((j + buff_size) + (m + 2*buff_size + 1)*(k + buff_size*l))
buff_send(r) = real(q_T_sf%sf(j, k + pack_offset, l), kind=wp)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
if (qbmm_comm) then
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, p
do k = 0, buff_size - 1
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + v_size*((j + buff_size) + (m + 2*buff_size + 1)*(k &
& + buff_size*l))
buff_send(r) = real(pb_in(j, k + pack_offset, l, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, p
do k = 0, buff_size - 1
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + nb*nnode + v_size*((j + buff_size) + (m + 2*buff_size &
& + 1)*(k + buff_size*l))
buff_send(r) = real(mv_in(j, k + pack_offset, l, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
#:else
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do i = 1, nVar
do l = 0, buff_size - 1
do k = -buff_size, n + buff_size
do j = -buff_size, m + buff_size
r = (i - 1) + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k + buff_size) + (n &
& + 2*buff_size + 1)*l))
buff_send(r) = real(q_comm(i)%sf(j, k, l + pack_offset), kind=wp)
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
if (chem_diff_comm) then
$:GPU_PARALLEL_LOOP(collapse=3,private='[r]')
do l = 0, buff_size - 1
do k = -buff_size, n + buff_size
do j = -buff_size, m + buff_size
r = nVar + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k + buff_size) + (n &
& + 2*buff_size + 1)*l))
buff_send(r) = real(q_T_sf%sf(j, k, l + pack_offset), kind=wp)
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
if (qbmm_comm) then
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, buff_size - 1
do k = -buff_size, n + buff_size
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k &
& + buff_size) + (n + 2*buff_size + 1)*l))
buff_send(r) = real(pb_in(j, k, l + pack_offset, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, buff_size - 1
do k = -buff_size, n + buff_size
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + nb*nnode + v_size*((j + buff_size) + (m + 2*buff_size &
& + 1)*((k + buff_size) + (n + 2*buff_size + 1)*l))
buff_send(r) = real(mv_in(j, k, l + pack_offset, i - nVar, q), kind=wp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
#:endif
end if
#:endfor
call nvtxEndRange ! Packbuf
! Send/Recv
#:for rdma_mpi in [False, True]
if (use_rdma_transport .eqv. ${'.true.' if rdma_mpi else '.false.'}$) then
#:if rdma_mpi
#:call GPU_HOST_DATA(use_device_addr='[buff_send, buff_recv]')
call nvtxStartRange("RHS-COMM-SENDRECV-RDMA")
call MPI_SENDRECV(buff_send, buffer_count, mpi_p, dst_proc, send_tag, buff_recv, buffer_count, mpi_p, &
& src_proc, recv_tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierr)
call nvtxEndRange ! RHS-MPI-SENDRECV-(NO)-RDMA
#:endcall GPU_HOST_DATA
$:GPU_WAIT()
#:else
call nvtxStartRange("RHS-COMM-DEV2HOST")
$:GPU_UPDATE(host='[buff_send]')
call nvtxEndRange
call nvtxStartRange("RHS-COMM-SENDRECV-NO-RMDA")
call MPI_SENDRECV(buff_send, buffer_count, mpi_p, dst_proc, send_tag, buff_recv, buffer_count, mpi_p, &
& src_proc, recv_tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE, ierr)
call nvtxEndRange ! RHS-MPI-SENDRECV-(NO)-RDMA
call nvtxStartRange("RHS-COMM-HOST2DEV")
$:GPU_UPDATE(device='[buff_recv]')
call nvtxEndRange
#:endif
end if
#:endfor
! Unpack Received Buffer
call nvtxStartRange("RHS-COMM-UNPACKBUF")
#:for mpi_dir in [1, 2, 3]
if (mpi_dir == ${mpi_dir}$) then
#:if mpi_dir == 1
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do l = 0, p
do k = 0, n
do j = -buff_size, -1
do i = 1, nVar
r = (i - 1) + v_size*(j + buff_size*((k + 1) + (n + 1)*l))
q_comm(i)%sf(j + unpack_offset, k, l) = real(buff_recv(r), kind=stp)
#if defined(__INTEL_COMPILER)
if (ieee_is_nan(q_comm(i)%sf(j + unpack_offset, k, l))) then
print *, "Error", j, k, l, i
call s_mpi_abort("NaN(s) in recv")
end if
#endif
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
if (chem_diff_comm) then
$:GPU_PARALLEL_LOOP(collapse=3,private='[r]')
do l = 0, p
do k = 0, n
do j = -buff_size, -1
r = nVar + v_size*(j + buff_size*((k + 1) + (n + 1)*l))
q_T_sf%sf(j + unpack_offset, k, l) = real(buff_recv(r), kind=stp)
#if defined(__INTEL_COMPILER)
if (ieee_is_nan(q_T_sf%sf(j + unpack_offset, k, l))) then
print *, "Error", j, k, l
call s_mpi_abort("NaN(s) in recv")
end if
#endif
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
if (qbmm_comm) then
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do l = 0, p
do k = 0, n
do j = -buff_size, -1
do i = nVar + 1, nVar + nnode
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + v_size*(j + buff_size*((k + 1) + (n + 1)*l))
pb_in(j + unpack_offset, k, l, i - nVar, q) = real(buff_recv(r), kind=stp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do l = 0, p
do k = 0, n
do j = -buff_size, -1
do i = nVar + 1, nVar + nnode
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + nb*nnode + v_size*(j + buff_size*((k + 1) + (n + 1)*l))
mv_in(j + unpack_offset, k, l, i - nVar, q) = real(buff_recv(r), kind=stp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
#:elif mpi_dir == 2
$:GPU_PARALLEL_LOOP(collapse=4,private='[r]')
do i = 1, nVar
do l = 0, p
do k = -buff_size, -1
do j = -buff_size, m + buff_size
r = (i - 1) + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k + buff_size) + buff_size*l))
q_comm(i)%sf(j, k + unpack_offset, l) = real(buff_recv(r), kind=stp)
#if defined(__INTEL_COMPILER)
if (ieee_is_nan(q_comm(i)%sf(j, k + unpack_offset, l))) then
print *, "Error", j, k, l, i
call s_mpi_abort("NaN(s) in recv")
end if
#endif
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
if (chem_diff_comm) then
$:GPU_PARALLEL_LOOP(collapse=3,private='[r]')
do l = 0, p
do k = -buff_size, -1
do j = -buff_size, m + buff_size
r = nVar + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k + buff_size) + buff_size*l))
q_T_sf%sf(j, k + unpack_offset, l) = real(buff_recv(r), kind=stp)
#if defined(__INTEL_COMPILER)
if (ieee_is_nan(q_T_sf%sf(j, k + unpack_offset, l))) then
print *, "Error", j, k, l
call s_mpi_abort("NaN(s) in recv")
end if
#endif
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
if (qbmm_comm) then
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, p
do k = -buff_size, -1
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + v_size*((j + buff_size) + (m + 2*buff_size + 1)*((k &
& + buff_size) + buff_size*l))
pb_in(j, k + unpack_offset, l, i - nVar, q) = real(buff_recv(r), kind=stp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
$:GPU_PARALLEL_LOOP(collapse=5,private='[r]')
do i = nVar + 1, nVar + nnode
do l = 0, p
do k = -buff_size, -1
do j = -buff_size, m + buff_size
do q = 1, nb
r = (i - 1) + (q - 1)*nnode + nb*nnode + v_size*((j + buff_size) + (m + 2*buff_size &
& + 1)*((k + buff_size) + buff_size*l))
mv_in(j, k + unpack_offset, l, i - nVar, q) = real(buff_recv(r), kind=stp)
end do
end do
end do
end do
end do
$:END_GPU_PARALLEL_LOOP()
end if
#:else