-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathComms.F90
More file actions
1636 lines (1305 loc) · 43.1 KB
/
Comms.F90
File metadata and controls
1636 lines (1305 loc) · 43.1 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
#include "MAPL.h"
!BOP
! !MODULE: mapl_Comms -- A Module to parallel comunications until ESMF fully supports it
! !INTERFACE:
module MAPL_CommsMod
use ESMF, only: ESMF_DELayout, ESMF_DELayoutGet, &
ESMF_DistGrid, ESMF_DistGridGet, &
ESMF_Grid, ESMF_GridGet, &
ESMF_KIND_I4, ESMF_KIND_R4, ESMF_KIND_R8, &
ESMF_MAXSTR, ESMF_SUCCESS, &
ESMF_VM, ESMF_VMGatherV, ESMF_VMGet, ESMF_VMGetCurrent, &
ESMF_VMScatterV, ESMF_VmBarrier, ESMF_VmGet
use mapl_Shmem_mod, only: MAPL_ShmInitialized, MAPL_SyncSharedMemory, &
MAPL_BroadcastToNodes, MAPL_NodeRankList, &
MAPL_GetNewRank
use MAPL_Constants, only: MAPL_Unknown, MAPL_IsGather, MAPL_IsScatter, MAPL_UNDEF
use mapl_ErrorHandling_mod, only: MAPL_Assert, MAPL_Verify, MAPL_Return
use mapl_GridGetGlobal_mod, only: GridGetGlobalCellCountPerDim
use mpi
use, intrinsic :: iso_fortran_env, only: REAL64
implicit none
private
public mapl_CommsBcast
public mapl_CommsScatterV
public mapl_CommsGatherV
public mapl_CommsAllGather
public mapl_CommsAllGatherV
public mapl_CommsAllReduceMin
public mapl_CommsAllReduceMax
public mapl_CommsAllReduceSum
public mapl_CommsSend
public mapl_CommsRecv
public mapl_CommsSendRecv
public mapl_AM_I_ROOT
public mapl_AM_I_RANK
public mapl_NPES
public ArrayGather
public ArrayScatter
public MAPL_ROOT
public mapl_CreateRequest
public mapl_CommRequest
public mapl_ArrayIGather
public mapl_ArrayIScatter
public mapl_CollectiveWait
public mapl_CollectiveScatter3D
public mapl_CollectiveGather3D
public mapl_RoundRobinPEList
public mapl_BcastShared
type ArrPtr
real, pointer :: A(:,:)
end type ArrPtr
public ArrPtr
type mapl_CommRequest
integer, pointer :: i1(:),in(:),j1(:),jn(:),im(:),jm(:)
integer :: im_world, jm_world, im0, jm0
integer, pointer :: recv(:)=>null()
integer, pointer :: send(:)=>null()
real, pointer :: var(:)=>null()
real, pointer :: DstArray(:,:)=>null()
real, pointer :: Local_Array(:,:)=>null()
real, pointer :: Trans_Array(:,:,:)=>null()
real, pointer :: Read_Array(:,:)=>null()
type(ArrPtr), pointer :: Buff(:)
integer :: nDEs, MYPE, comm, root
logical :: active=.false., amRoot=.false.
logical :: IsPrePosted
integer :: RequestType=MAPL_Unknown
integer :: tag, s_rqst
end type mapl_CommRequest
interface mapl_Am_I_Root
module procedure mapl_Am_I_Root_Layout
module procedure mapl_Am_I_Root_Vm
end interface
interface mapl_Am_I_Rank
module procedure mapl_Am_I_Rank_Only
module procedure mapl_Am_I_Rank_Layout
module procedure mapl_Am_I_Rank_Vm
end interface
interface mapl_NPES
module procedure mapl_NPES_Layout
module procedure mapl_NPES_Vm
end interface
interface mapl_CommsBcast
module procedure mapl_CommsBcast_STRING_0
module procedure mapl_CommsBcast_L4_0
module procedure mapl_CommsBcast_I4_0
module procedure mapl_CommsBcast_R4_0
module procedure mapl_CommsBcast_R8_0
module procedure mapl_CommsBcast_I4_1
module procedure mapl_CommsBcast_R4_1
module procedure mapl_CommsBcast_R8_1
module procedure mapl_CommsBcast_I4_2
module procedure mapl_CommsBcast_R4_2
module procedure mapl_CommsBcast_R8_2
module procedure mapl_CommsBcastVm_STRING_0
module procedure mapl_CommsBcastVm_L4_0
module procedure mapl_CommsBcastVm_I4_0
module procedure mapl_CommsBcastVm_R4_0
module procedure mapl_CommsBcastVm_R8_0
module procedure mapl_CommsBcastVm_I4_1
module procedure mapl_CommsBcastVm_R4_1
module procedure mapl_CommsBcastVm_R8_1
module procedure mapl_CommsBcastVm_I4_2
module procedure mapl_CommsBcastVm_R4_2
module procedure mapl_CommsBcastVm_R8_2
end interface
interface mapl_BcastShared
module procedure mapl_BcastShared_1DR4
module procedure mapl_BcastShared_1DR8
module procedure mapl_BcastShared_2DI4
module procedure mapl_BcastShared_2DR4
module procedure mapl_BcastShared_2DR8
end interface
interface mapl_CommsScatterV
module procedure mapl_CommsScatterV_I4_1
module procedure mapl_CommsScatterV_R4_1
module procedure mapl_CommsScatterV_R4_2
module procedure mapl_CommsScatterV_R8_1
module procedure mapl_CommsScatterV_R8_2
end interface
interface mapl_CommsGatherV
module procedure mapl_CommsGatherV_I4_1
module procedure mapl_CommsGatherV_R4_1
module procedure mapl_CommsGatherV_R4_2
module procedure mapl_CommsGatherV_R8_1
module procedure mapl_CommsGatherV_R8_2
end interface
interface mapl_CommsAllGather
module procedure mapl_CommsAllGather_I4_1
module procedure mapl_CommsAllGather_L4_1
end interface
interface mapl_ArrayIGather
module procedure mapl_ArrayIGather_R4_2
end interface
interface mapl_ArrayIScatter
module procedure mapl_ArrayIScatter_R4_2
end interface
interface mapl_CommsAllGatherV
module procedure mapl_CommsAllGatherV_I4_1
module procedure mapl_CommsAllGatherV_R4_1
module procedure mapl_CommsAllGatherV_R8_1
end interface
interface mapl_CommsAllReduceMin
module procedure mapl_CommsAllReduceMin_I4_0
module procedure mapl_CommsAllReduceMin_R4_0
module procedure mapl_CommsAllReduceMin_R8_0
module procedure mapl_CommsAllReduceMin_I4_1
module procedure mapl_CommsAllReduceMin_R4_1
module procedure mapl_CommsAllReduceMin_R8_1
module procedure mapl_CommsAllReduceMin_I4_2
module procedure mapl_CommsAllReduceMin_R4_2
module procedure mapl_CommsAllReduceMin_R8_2
end interface
interface mapl_CommsAllReduceMax
module procedure mapl_CommsAllReduceMax_I4_0
module procedure mapl_CommsAllReduceMax_R4_0
module procedure mapl_CommsAllReduceMax_R8_0
module procedure mapl_CommsAllReduceMax_I4_1
module procedure mapl_CommsAllReduceMax_R4_1
module procedure mapl_CommsAllReduceMax_R8_1
module procedure mapl_CommsAllReduceMax_I4_2
module procedure mapl_CommsAllReduceMax_R4_2
module procedure mapl_CommsAllReduceMax_R8_2
end interface
interface mapl_CommsAllReduceSum
module procedure mapl_CommsAllReduceSum_I4_0
module procedure mapl_CommsAllReduceSum_R4_0
module procedure mapl_CommsAllReduceSum_R8_0
module procedure mapl_CommsAllReduceSum_I4_1
module procedure mapl_CommsAllReduceSum_R4_1
module procedure mapl_CommsAllReduceSum_R8_1
module procedure mapl_CommsAllReduceSum_I4_2
module procedure mapl_CommsAllReduceSum_R4_2
module procedure mapl_CommsAllReduceSum_R8_2
end interface
interface mapl_CommsSend
module procedure mapl_CommsSend_I4_0
module procedure mapl_CommsSend_I4_1
module procedure mapl_CommsSend_R4_1
module procedure mapl_CommsSend_R4_2
module procedure mapl_CommsSend_R8_1
module procedure mapl_CommsSend_R8_2
end interface
interface mapl_CommsRecv
module procedure mapl_CommsRecv_I4_0
module procedure mapl_CommsRecv_I4_1
module procedure mapl_CommsRecv_R4_1
module procedure mapl_CommsRecv_R4_2
module procedure mapl_CommsRecv_R8_1
module procedure mapl_CommsRecv_R8_2
end interface
interface mapl_CommsSendRecv
module procedure mapl_CommsSendRecv_I4_0
module procedure mapl_CommsSendRecv_R4_0
module procedure mapl_CommsSendRecv_R4_1
module procedure mapl_CommsSendRecv_R4_2
module procedure mapl_CommsSendRecv_R8_1
module procedure mapl_CommsSendRecv_R8_2
end interface
interface ArrayScatter
module procedure ArrayScatter_R4_1
module procedure ArrayScatter_R8_1
module procedure ArrayScatter_R4_2
module procedure ArrayScatter_R8_2
module procedure ArrayScatterRcvCnt_I4_1
module procedure ArrayScatterRcvCnt_R4_1
end interface
interface ArrayGather
module procedure ArrayGather_I4_1
module procedure ArrayGather_R4_1
module procedure ArrayGather_R8_1
module procedure ArrayGather_R4_2
module procedure ArrayGather_R8_2
module procedure ArrayGatherRcvCnt_I4_1
module procedure ArrayGatherRcvCnt_R4_1
end interface
integer, parameter :: MAPL_ROOT=0
integer, parameter :: msg_tag=11
contains
!-------------------------------------------------------------------------------
!---------------------------
!---------------------------
!---------------------------
function mapl_Am_I_Root_Vm(VM) result(R)
type (ESMF_VM), optional :: VM
logical :: R
if (present(VM)) then
R = mapl_Am_I_Rank(VM)
else
R = mapl_Am_I_Rank()
end if
end function mapl_Am_I_Root_Vm
function mapl_Am_I_Root_Layout(layout) result(R)
type (ESMF_DELayout) :: layout
logical :: R
R = mapl_Am_I_Rank(layout)
end function mapl_Am_I_Root_Layout
function mapl_Am_I_Rank_Vm(VM, rank) result(R)
type (ESMF_VM) :: VM
integer, optional :: rank
logical :: R
integer :: deId
integer :: status
integer :: rank_
rank_ = mapl_Root
if (present(rank)) rank_ = rank
call ESMF_VMGet(VM, localPet=deId, rc=status)
R = .false.
if (deId == rank_) R = .true.
end function mapl_Am_I_Rank_Vm
function mapl_Am_I_Rank_Layout(layout, rank) result(R)
type (ESMF_DELayout) :: layout
integer, optional :: rank
logical :: R
integer :: status
type (ESMF_VM) :: vm
call ESMF_DELayoutGet(layout, vm=vm, rc=status)
if (present(rank)) then
R = mapl_Am_I_Rank(vm, rank)
else
R = mapl_Am_I_Rank(vm)
end if
end function mapl_Am_I_Rank_Layout
function mapl_Am_I_Rank_Only(rank) result(R)
integer, optional :: rank
logical :: R
integer :: status
type (ESMF_VM) :: vm
call ESMF_VMGetCurrent(vm, rc=status)
if (present(rank)) then
R = mapl_Am_I_Rank(vm, rank)
else
R = mapl_Am_I_Rank(vm)
end if
end function
subroutine mapl_CreateRequest(grid, Root, request, tag, RequestType, &
DstArray, PrePost, hw, rc)
type (ESMF_Grid), intent(IN ) :: grid
integer, intent(IN ) :: Root
type (mapl_CommRequest), intent(INOUT) :: request
integer, intent(IN ) :: tag, RequestType
real, target, optional, intent(IN ) :: DstArray(:,:)
logical, optional, intent(IN ) :: PrePost
integer, optional, intent(IN ) :: hw
integer, optional, intent( OUT) :: rc
! Local variables
integer :: status
type (ESMF_VM) :: VM
type (ESMF_DistGrid) :: distGrid
integer, allocatable :: AL(:,:), AU(:,:)
integer :: count
integer :: displs
integer :: n
integer :: myPE, nDEs
integer :: gridRank
integer :: comm
integer :: hw_
! Begin
!------
if (present(hw)) then
hw_ = hw
else
hw_ = 0
end if
_ASSERT(.not.request%active, 'request is already active')
! Communicator info all comes from the ESMF VM
!---------------------------------------------
call ESMF_VMGetCurrent(vm, RC=STATUS)
_VERIFY(STATUS)
call ESMF_VMGet (VM, mpiCommunicator =comm, RC=STATUS)
_VERIFY(STATUS)
call ESMF_VMGet (VM, localpet=MYPE, petcount=nDEs, RC=STATUS)
_VERIFY(STATUS)
call ESMF_GridGet(GRID, dimCount=gridRank, rc=status)
_VERIFY(STATUS)
! Does not support 1D grids
!--------------------------
_ASSERT(gridRank > 1, 'rank 1 is not supported')
! Get the local grid bounds for all pes. We will use only
! the first 2 dimensions.
!--------------------------------------------------------
call ESMF_GridGet(GRID, distGrid=distGrid, RC=STATUS); _VERIFY(STATUS)
allocate (AL(gridRank,0:nDEs-1), _STAT)
allocate (AU(gridRank,0:nDEs-1), _STAT)
call ESMF_DistGridGet(distgrid, minIndexPDe=AL, maxIndexPDe=AU, RC=STATUS); _VERIFY(STATUS)
! Allocate space for request variables
!-------------------------------------
allocate (request%i1(0:nDEs-1), _STAT)
allocate (request%in(0:nDEs-1), _STAT)
allocate (request%j1(0:nDEs-1), _STAT)
allocate (request%jn(0:nDEs-1), _STAT)
allocate (request%im(0:nDEs-1), _STAT)
allocate (request%jm(0:nDEs-1), _STAT)
allocate (request%RECV (0:nDEs-1 ), _STAT)
allocate (request%SEND (0:nDEs-1 ), _STAT)
! Fill the request variables
!---------------------------
request%amRoot = (myPE == Root)
request%active = .true.
request%nDEs = nDEs
request%myPE = myPE
request%comm = comm
request%root = root
request%RequestType = RequestType
request%tag = tag
request%I1 = AL(1,:)-hw_
request%In = AU(1,:)+hw_
request%J1 = AL(2,:)-hw_
request%Jn = AU(2,:)+hw_
request%IM = request%IN-request%I1+1
request%JM = request%JN-request%J1+1
request%IM_WORLD = request%IN(nDEs-1)- request%I1(0) + 1 - (2*hw_)
request%JM_WORLD = request%JN(nDEs-1)- request%J1(0) + 1 - (2*hw_)
request%IM0 = request%IN(mype )- request%I1(mype) + 1
request%JM0 = request%JN(mype )- request%J1(mype) + 1
if(present(PrePost)) then
request%IsPrePosted = PrePost
else
request%IsPrePosted = .false.
end if
deallocate(AL,AU)
! Verify that we have a valid destination area
!---------------------------------------------
if(requestType==MAPL_IsGather) then
if(request%amRoot) then
if(present(DstArray)) then
request%DstArray => DstArray
_ASSERT(all(shape(DstArray)==(/ request%IM_WORLD, request%JM_WORLD/)), 'inconsistent shape')
else
allocate(request%DstArray(request%IM_WORLD, request%JM_WORLD), _STAT)
end if
endif
elseif(requestType==MAPL_IsScatter) then
if(present(DstArray)) then
request%DstArray => DstArray
_ASSERT(all(shape(DstArray)==(/ request%IM0 , request%JM0 /)), 'inconsistent shape')
else
allocate(request%DstArray(request%IM0 , request%JM0 ), _STAT)
end if
else
_FAIL( 'unsupported action')
end if
! Allocate a contiguous buffer for communication
!-----------------------------------------------
if(requestType==MAPL_IsGather .and. request%amRoot) then
allocate (request%Var(0:request%IM_WORLD*request%JM_WORLD-1), _STAT)
elseif(requestType==MAPL_IsScatter) then
allocate (request%Var(0:request%IM0*request%JM0-1), _STAT)
else
allocate (request%Var(1), _STAT)
endif
! We also PrePost the request here
!---------------------------------
POST_REQUEST: if(request%IsPrePosted) then
if(requestType==MAPL_IsGather) then
if(request%amRoot) then
displs = 0
do n=0,nDEs-1
count = request%IM(n)*request%JM(n)
if(n /= mype) then
call MPI_IRecv(request%VAR(displs), count, MPI_REAL, &
n, tag, comm, request%recv(n), status)
_VERIFY(STATUS)
end if
displs = displs + count
end do
endif
else
if(.not.request%amRoot) then
call MPI_IRecv(request%Var, size(request%Var), MPI_REAL, &
request%Root, tag, comm, request%recv(0), status)
_VERIFY(STATUS)
end if
end if
end if POST_REQUEST
_RETURN(ESMF_SUCCESS)
end subroutine mapl_CreateRequest
!===================================================================
subroutine mapl_ArrayIGather_R4_2(local_array, request, rc)
real, intent(IN ) :: local_array (:,:)
type (mapl_CommRequest), intent(INOUT) :: request
integer, optional, intent( OUT) :: rc
! Local variables
integer :: status
integer :: i1, in, j1, jn
allocate(request%local_array(size(LOCAL_ARRAY,1),size(LOCAL_ARRAY,2)), _STAT)
! In senders, copy input to contiguous buffer for safety
!-------------------------------------------------------
request%local_array = local_array
if(request%amRoot) then
i1 = request%i1(request%mype)
in = request%in(request%mype)
j1 = request%j1(request%mype)
jn = request%jn(request%mype)
request%DstArray(i1:in,j1:jn) = local_array
else
call MPI_ISend(request%Local_Array, size(Local_Array), MPI_REAL, &
request%root, request%tag, request%comm, request%send(0), status)
_VERIFY(STATUS)
end if
_RETURN(ESMF_SUCCESS)
end subroutine mapl_ArrayIGather_R4_2
!===================================================================
subroutine mapl_ArrayIScatter_R4_2(global_array, request, hw, rc)
real, intent(IN ) :: global_array (:,:)
type (mapl_CommRequest), intent(INOUT) :: request
integer, optional, intent( IN) :: hw
integer, optional, intent( OUT) :: rc
! Local variables
integer :: status
integer :: i1,in,j1,jn
integer :: n, count, hw_, j
real, allocatable :: global_array_(:,:)
if (present(hw)) then
hw_ = hw
else
hw_ = 0
end if
! Post sends from all processors except root
!-------------------------------------------
if(request%amRoot) then
!if have halo, make local copy and halo global
if (hw_ > 0) then
allocate(Global_Array_(1-hw_:request%im_world+hw_,1-hw_:request%jm_world+hw_))
Global_Array_(1:request%im_world,1:request%jm_world) = Global_Array
do j=1,hw_
! x-direction
Global_Array_(1-j,:) = Global_Array_(request%im_world-j+1,:)
Global_Array_(request%im_world+j,:) = Global_Array_(j,:)
! y-direction
Global_Array_(:,1-j) = MAPL_UNDEF
Global_Array_(:,request%jm_world+j) = MAPL_UNDEF
enddo
endif
allocate(request%Buff(0:request%nDEs-1))
PEs: do n=0,request%nDEs-1
count = request%IM(n)*request%JM(n)
i1 = request%i1(n)
in = request%in(n)
j1 = request%j1(n)
jn = request%jn(n)
if(n == request%mype) then
if (hw_ > 0) then
request%DstArray = Global_Array_(i1:in,j1:jn)
else
request%DstArray = Global_Array(i1:in,j1:jn)
end if
else
allocate(request%Buff(n)%A(request%im(n), request%jm(n)))
if (hw_ > 0) then
request%Buff(n)%A = Global_Array_(i1:in,j1:jn)
else
request%Buff(n)%A = Global_Array(i1:in,j1:jn)
end if
call MPI_ISend(request%Buff(n)%A, count, MPI_REAL, &
n, request%tag, request%comm, request%send(n), status)
_VERIFY(STATUS)
end if
end do PEs
if (hw_ > 0) deallocate(Global_Array_)
end if
_RETURN(ESMF_SUCCESS)
end subroutine mapl_ArrayIScatter_R4_2
!=========================================================
subroutine mapl_CollectiveWait(request, DstArray, rc)
type (mapl_COMMRequest), intent(INOUT) :: request
real, pointer, optional :: DstArray(:,:)
integer, optional, intent( OUT) :: rc
integer :: status
integer :: i,j,k,n
integer :: count
REQUEST_TYPE: if(request%RequestType==MAPL_IsGather) then
ROOT_GATH: if(request%amRoot) then
k = 0
PE_GATH: do n=0,request%nDEs-1
count = request%IM(n)*request%JM(n)
if(request%mype/=n) then
if(request%IsPrePosted) then
call MPI_Wait(request%recv(n),MPI_STATUS_IGNORE,status)
_VERIFY(STATUS)
else
call MPI_Recv(request%var(k), count, MPI_REAL, &
n, request%tag, request%comm, MPI_STATUS_IGNORE, status)
_VERIFY(STATUS)
endif
do J=request%J1(n),request%JN(n)
do I=request%I1(n),request%IN(n)
request%DstArray(I,J) = request%var(k)
k = k+1
end do
end do
else
k = k + count
end if
end do PE_GATH
if(present(DstArray)) DstArray => request%DstArray
else
call MPI_WAIT(request%send(0),MPI_STATUS_IGNORE,status)
_VERIFY(STATUS)
endif ROOT_GATH
elseif(request%RequestType==MAPL_IsScatter) then
ROOT_SCAT: if(.not.request%amRoot) then
if(request%IsPrePosted) then
call MPI_Wait(request%recv(0),MPI_STATUS_IGNORE,status)
_VERIFY(STATUS)
else
call MPI_Recv(request%Var, size(request%Var), MPI_REAL, &
request%Root, request%tag, request%comm, &
MPI_STATUS_IGNORE, status)
_VERIFY(status)
endif
k=0
do J=1,request%JM0
do I=1,request%IM0
request%DstArray(I,J) = request%var(k)
k = k+1
end do
end do
else
PE_SCAT: do n=0,request%nDEs-1
if(n /= request%mype) then
call MPI_Wait(request%send(n),MPI_STATUS_IGNORE,status)
_VERIFY(STATUS)
deallocate(request%buff(n)%A)
end if
end do PE_SCAT
deallocate(request%Buff)
end if ROOT_SCAT
if(present(DstArray)) DstArray => request%DstArray
end if REQUEST_TYPE
! Destroy the request
!--------------------
deallocate(request%var )
deallocate(request%recv)
deallocate(request%send)
deallocate(request%i1 )
deallocate(request%in )
deallocate(request%j1 )
deallocate(request%jn )
deallocate(request%im )
deallocate(request%jm )
nullify(request%var )
nullify(request%send )
nullify(request%recv )
nullify(request%DstArray)
if(associated(request%Local_Array)) deallocate(request%Local_Array)
nullify(request%Local_Array)
request%active = .false.
_RETURN(ESMF_SUCCESS)
end subroutine mapl_CollectiveWait
!---------------------------
!---------------------------
!---------------------------
subroutine mapl_CollectiveGather3D(Grid, LocArray, GlobArray, &
CoresPerNode, rc)
type (ESMF_Grid), intent(INout) :: Grid
real, intent(IN ) :: LocArray(:,:,:)
real, pointer :: GlobArray(:,:,:)
integer, optional, intent(In ) :: CoresPerNode
integer, optional, intent( OUT) :: rc
! Locals
!-------
integer :: status
type (mapl_CommRequest) :: reqs(size(LocArray,3))
integer :: root(size(LocArray,3))
integer :: Nnodes
integer :: nn
integer :: LM, L, nc, npes, mype, dims(5)
integer, allocatable :: dims_alloc(:)
type(ESMF_VM) :: VM
integer :: comm
! Begin
!------
_ASSERT(.not.associated(GlobArray), 'GlobalArray already associated')
call ESMF_VMGetCurrent(VM, RC=STATUS)
_VERIFY(STATUS)
call ESMF_VMGet(VM, petcount=npes, localpet=MYPE, mpiCommunicator=comm, RC=STATUS)
_VERIFY(STATUS)
LM = size(LocArray,3)
nNodes = size(MAPL_NodeRankList)
call mapl_RoundRobinPEList(Root, nNodes, RC=STATUS)
_VERIFY(STATUS)
if(any(root==mype)) then
call GridGetGlobalCellCountPerDim(grid, globalCellCountPerDim=dims_alloc, RC=STATUS)
_VERIFY(STATUS)
dims(1:size(dims_alloc)) = dims_alloc
_VERIFY(STATUS)
nc = count(Root==mype)
allocate(GlobArray(dims(1),dims(2),nc), _STAT)
else
allocate(GlobArray(1,1,1) , _STAT)
endif
nn = 0
do L=1,LM
if(root(L) == mype) then
nn = nn + 1
call mapl_CreateRequest(GRID, Root(L), reqs(L), tag=L, &
RequestType=MAPL_IsGather, &
DstArray=GlobArray(:,:,nn), &
PrePost=.true., RC=STATUS)
_VERIFY(STATUS)
else
call mapl_CreateRequest(GRID, Root(L), reqs(L), tag=L, &
RequestType=MAPL_IsGather, &
DstArray=GlobArray(:,:,1), &
PrePost=.true., RC=STATUS)
_VERIFY(STATUS)
end if
enddo ! Do not fuse with next
do L=1,LM
call mapl_ArrayIGather (LocArray(:,:,L), reqs(L), RC=STATUS)
_VERIFY(STATUS)
enddo ! Do not fuse with next
do L=1,LM
call mapl_CollectiveWait(reqs(L), rc=status)
_VERIFY(STATUS)
end do
_RETURN(ESMF_SUCCESS)
_UNUSED_DUMMY(corespernode)
end subroutine mapl_CollectiveGather3D
subroutine mapl_CollectiveScatter3D(Grid, GlobArray, LocArray, hw, rc)
type (ESMF_Grid), intent(IN ) :: Grid
real, target, intent(INOUT) :: LocArray(:,:,:)
real, intent(IN ) :: GlobArray(:,:,:)
integer, optional, intent(IN ) :: hw
integer, optional, intent( OUT) :: rc
! Locals
!-------
integer :: status
type (mapl_CommRequest) :: reqs(size(LocArray,3))
integer :: root(size(LocArray,3))
integer :: nNodes
integer :: LM, L, nc, npes, mype
integer :: nn
type(ESMF_VM) :: VM
logical :: HaveGlobal
integer :: comm
integer :: hw_
! Begin
!------
call ESMF_VMGetCurrent(VM, RC=STATUS)
_VERIFY(STATUS)
call ESMF_VMGet(VM, petcount=npes, localpet=MYPE, mpiCommunicator=comm, RC=STATUS)
_VERIFY(STATUS)
if(present(hw)) then
hw_ = hw
else
hw_ = 0
endif
nNodes = size(MAPL_NodeRankList)
call mapl_RoundRobinPEList(Root, nNodes, RC=STATUS)
_VERIFY(STATUS)
LM = size(LocArray,3)
NC = count(Root==mype)
HaveGlobal = NC>0
do L=1,LM
call mapl_CreateRequest(GRID, Root(L), reqs(L), tag=L, &
RequestType=MAPL_IsScatter, &
DstArray=LocArray(:,:,L), &
PrePost=.true., hw=hw_, RC=STATUS)
_VERIFY(STATUS)
enddo
if(HaveGlobal) then
_ASSERT(size(GlobArray,3)==NC, 'inconsisntent rank')
nn = 0
do L=1,LM
if(Root(L)==mype) then
nn = nn + 1
call mapl_ArrayIScatter (GlobArray(:,:,nn), reqs(L), hw=hw_, RC=STATUS)
_VERIFY(STATUS)
if(nn==NC) exit
endif
enddo
end if
do L=1,LM
call mapl_CollectiveWait(reqs(L), rc=status)
_VERIFY(STATUS)
end do
_RETURN(ESMF_SUCCESS)
end subroutine mapl_CollectiveScatter3D
subroutine mapl_RoundRobinPEList(List,nNodes,Root,UseFirstRank,FirstRank,RC)
integer, intent( OUT) :: List(:)
integer, intent(IN ) :: nNodes
integer, optional, intent(IN ) :: Root
logical, optional, intent(IN ) :: UseFirstRank
integer, optional, intent(out ) :: FirstRank
integer, optional, intent( OUT) :: RC
integer :: status
integer, allocatable :: filled(:),nPerNode(:)
integer :: i,n,nlist,locRoot
logical :: gotFirstRank,lUseFirstRank
if (present(Root)) then
locRoot = Root
else
locRoot = 1
endif
if (present(UseFirstRank)) then
lUseFirstRank=UseFirstRank
else
lUseFirstRank=.true.
end if
gotFirstRank = .false.
if (present(UseFirstRank)) then
lUseFirstRank=UseFirstRank
else
lUseFirstRank=.true.
end if
allocate(filled(nNodes),nPerNode(nNodes), _STAT)
do i=1,nNodes
nPerNode(i) = size(MAPL_NodeRankList(locRoot+i-1)%rank)
if (lUseFirstRank) then
filled(i)=0
else
filled(i)=MAPL_GetNewRank(locRoot+i-1,rc=status)-1
_VERIFY(status)
end if
enddo
nlist = size(list)
n=0
do
do i=1,nNodes
if (filled(i) < size(MAPL_NodeRankList(locRoot+i-1)%rank)) then
filled(i) = filled(i) + 1
n=n+1
list(n) = MAPL_NodeRankList(locRoot+i-1)%rank(filled(i))
if (.not.gotFirstRank .and. present(FirstRank)) then
gotFirstRank=.true.
FirstRank = list(n)
end if
end if
if (n == nlist) exit
enddo
if (n == nlist) exit
if (All(filled == nPerNode)) filled = 0
enddo
deallocate(filled,nPerNode)
_RETURN(ESMF_SUCCESS)
end subroutine mapl_RoundRobinPEList
!---------------------------
!---------------------------
!---------------------------
function mapl_NPES_Vm(VM) result(R)
type (ESMF_VM) :: VM
integer :: R
integer :: petCnt
integer :: status
call ESMF_VMGet(vm, petCount=petCnt, rc=status)
R = petCnt
end function mapl_NPES_Vm
function mapl_NPES_Layout(layout) result(R)
type (ESMF_DELayout), optional :: layout
integer :: R
integer :: status
type(ESMF_VM) :: vm
call ESMF_DELayoutGet(layout, vm=vm, rc=status)
R = mapl_NPES_Vm(vm)
end function mapl_NPES_Layout
!--BCAST -----------------
subroutine mapl_CommsBcast_STRING_0( layout, data, N, ROOT, RC)
type (ESMF_DELayout) :: layout
character(len=*), intent(INOUT) :: data
integer, intent(in ) :: N
integer, intent(in ) :: ROOT
integer , intent( out), optional :: RC
integer :: status