-
Notifications
You must be signed in to change notification settings - Fork 18
/
BinIO.F90
4291 lines (3552 loc) · 136 KB
/
BinIO.F90
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
!------------------------------------------------------------------------------
! Global Modeling and Assimilation Office (GMAO) !
! Goddard Earth Observing System (GEOS) !
! MAPL Component !
!------------------------------------------------------------------------------
!
#include "MAPL_ErrLog.h"
#define DEALOC_(A) if(associated(A))then;if(MAPL_ShmInitialized)then;call MAPL_SyncSharedMemory(rc=STATUS);call MAPL_DeAllocNodeArray(A,rc=STATUS);else;deallocate(A,stat=STATUS);endif;_VERIFY(STATUS);NULLIFY(A);endif
!
!>
!### MODULE: `BinIO`
!
! Author: GMAO SI-Team
!
! `BinIO` - A Module to do I/O (ASCII+binary) until ESMF fully supports it
!
module BinIOMod
use FileIOSharedMod, only: ArrDescr, MAPL_TileMaskGet, WRITE_PARALLEL, alloc_, dealloc_
use FileIOSharedMod, only: STD_OUT_UNIT_NUMBER, LAST_UNIT, TAKEN, MTAKEN, mname
use FileIOSharedMod, only: r4_2, r4_1, r8_2, r8_1, i4_1
use FileIOSharedMod, only: MEM_UNITS, munit, REC
use ESMF
use MAPL_BaseMod
use MAPL_SortMod
use MAPL_CommsMod
use MAPL_ShmemMod
use MAPL_ExceptionHandling
use, intrinsic :: ISO_C_BINDING
use, intrinsic :: iso_fortran_env
use mpi
implicit none
private
! public routines
public READ_PARALLEL
public GETFILEUNIT
public GETFILE
public FREE_FILE
public MAPL_VarRead
public MAPL_VarWrite
public MAPL_Skip
public MAPL_Backspace
public MAPL_Rewind
public MAPL_ClimUpdate
public MAPL_DestroyFile
public MAPL_MemFileInquire
!#define TIME_MPIIO
#ifdef TIME_MPIIO
real(kind=ESMF_KIND_R8), save :: peak_ioread_bandwidth=0
real(kind=ESMF_KIND_R8), save :: mean_ioread_bandwidth=0
real(kind=ESMF_KIND_R8), save :: ioread_counter=0
real(kind=ESMF_KIND_R8), save :: peak_iowrite_bandwidth=0
real(kind=ESMF_KIND_R8), save :: mean_iowrite_bandwidth=0
real(kind=ESMF_KIND_R8), save :: iowrite_counter=0
#endif
integer, parameter :: UNDEF = 999
interface READ_PARALLEL
module procedure READ_PARALLEL_STRING_0
module procedure READ_PARALLEL_I4_0
module procedure READ_PARALLEL_I4_1
module procedure READ_PARALLEL_I4_2
module procedure READ_PARALLEL_R4_0
module procedure READ_PARALLEL_R4_1
module procedure READ_PARALLEL_R4_2
module procedure READ_PARALLEL_R8_0
module procedure READ_PARALLEL_R8_1
module procedure READ_PARALLEL_R8_2
end interface
interface ArrayScatterShm
module procedure ArrayScatterShmR4D1
end interface ArrayScatterShm
interface MAPL_MemFileInquire
module procedure InqFileMem
end interface
interface MAPL_VarRead
!Binary procedures
module procedure MAPL_StateVarRead
module procedure MAPL_BundleRead
module procedure MAPL_FieldRead
module procedure MAPL_VarRead_R4_1D
module procedure MAPL_VarRead_R4_2D
module procedure MAPL_VarRead_R4_3d
module procedure MAPL_VarRead_R4_4D
module procedure MAPL_VarRead_R8_1D
module procedure MAPL_VarRead_R8_2D
module procedure MAPL_VarRead_R8_3D
module procedure MAPL_VarRead_R8_4D
end interface
interface MAPL_VarWrite
!Binary procedures
module procedure MAPL_StateVarWrite
module procedure MAPL_BundleWrite
module procedure MAPL_FieldWrite
module procedure MAPL_VarWrite_I4_1D
module procedure MAPL_VarWrite_R4_1D
module procedure MAPL_VarWrite_R4_2d
module procedure MAPL_VarWrite_R4_3D
module procedure MAPL_VarWrite_R4_4D
module procedure MAPL_VarWrite_R8_1D
module procedure MAPL_VarWrite_R8_2D
module procedure MAPL_VarWrite_R8_3D
module procedure MAPL_VarWrite_R8_4D
end interface
contains
!-READS --------------------
! Rank 0
!---------------------------
#define RANK_ 0
#define VARTYPE_ 0
#include "read_parallel.H"
!---------------------------
#define RANK_ 0
#define VARTYPE_ 1
#include "read_parallel.H"
!---------------------------
#define RANK_ 0
#define VARTYPE_ 3
#include "read_parallel.H"
!---------------------------
#define RANK_ 0
#define VARTYPE_ 4
#include "read_parallel.H"
! Rank 1
!---------------------------
#define RANK_ 1
#define VARTYPE_ 1
#include "read_parallel.H"
!---------------------------
#define RANK_ 1
#define VARTYPE_ 3
#include "read_parallel.H"
!---------------------------
#define RANK_ 1
#define VARTYPE_ 4
#include "read_parallel.H"
! Rank 2
!---------------------------
#define RANK_ 2
#define VARTYPE_ 1
#include "read_parallel.H"
!---------------------------
#define RANK_ 2
#define VARTYPE_ 3
#include "read_parallel.H"
!---------------------------
#define RANK_ 2
#define VARTYPE_ 4
#include "read_parallel.H"
LOGICAL FUNCTION INQFILEMEM(name)
IMPLICIT NONE
character(LEN=*), intent(in ) :: Name
integer :: i
logical :: found
found = .false.
do i = 3, last_unit
if(name==Mname(i)) then
found = .true.
exit
end if
end do
InqFileMem = found
return
end FUNCTION INQFILEMEM
INTEGER FUNCTION GETFILEUNIT(name, RC )
IMPLICIT NONE
character(LEN=*), intent(in ) :: Name
integer , intent( out), OPTIONAL :: RC
integer :: i
logical :: found
found = .false.
do i = 2, last_unit
if(name==Mname(i)) then
found = .true.
exit
end if
end do
if (.not. found) then
do i = 2,last_unit
if(.not.MTAKEN(i)) then
found = .true.
exit
endif
enddo
end if
if (.not. found) then
if(present(rc)) rc = 1
return
endif
mname(i) = name
mtaken(i) = .true.
getfileunit = i
if(present(rc)) rc = 0
return
end function getfileunit
!---------------------------------------------------------------------------
SUBROUTINE FREE_FILE(UNIT, RC)
implicit none
integer , intent(out), OPTIONAL :: RC
integer :: UNIT
if(UNIT < 0) then
_ASSERT(-UNIT<=LAST_UNIT, 'illegal io unit')
_ASSERT(MTAKEN(-UNIT), 'illegal io unit')
MEM_units(-unit)%PREVREC=0
ELSE
if (UNIT == STD_OUT_UNIT_NUMBER) return
if (UNIT /= UNDEF) then
close(UNIT)
IF (UNIT.LT.1 .OR. UNIT.GT.LAST_UNIT) THEN
WRITE (0,*) ' BAD UNIT NUMBER ZFILCLR UNIT = ', UNIT
_RETURN(ESMF_FAILURE)
ELSE
TAKEN(UNIT) = .FALSE.
MTAKEN(UNIT) = .FALSE.
ENDIF
end if
END IF
_RETURN(ESMF_SUCCESS)
END SUBROUTINE FREE_FILE
!---------------------------------------------------------------------------
subroutine MAPL_DestroyFile(unit, RC )
IMPLICIT NONE
integer , intent(in ) :: unit
integer , intent( out), OPTIONAL :: RC
integer :: i,k
!ALT: Currently, this is NOP except for RAM files
if (unit < 0) then
! this is RAM "file", do it!
i = -unit
if (associated(mem_units(i)%records)) then
do k=1,size(mem_units(i)%records)
call dealloc_(mem_units(i)%records(k))
end do
deallocate(mem_units(i)%records)
nullify(mem_units(i)%records)
end if
mtaken(i) = .false.
mname(i) = ''
end if
if(present(rc)) rc = 0
return
end subroutine MAPL_DestroyFile
!---------------------------------------------------------------------------
subroutine MAPL_StateVarRead(UNIT, STATE, NAME, arrdes, bootstrapable, RC)
integer , intent(IN ) :: UNIT
type (ESMF_State) , intent(INOUT) :: STATE
character(len=*), optional, intent(IN ) :: NAME
type(ArrDescr), optional, intent(INOUT) :: ARRDES
logical, optional, intent(IN ) :: bootstrapable
integer, optional, intent( OUT) :: RC
! Local vars
type (ESMF_FieldBundle) :: bundle
type (ESMF_Field) :: field
type (ESMF_Grid) :: grid
integer :: status
integer :: I
integer :: ITEMCOUNT
type (ESMF_StateItem_Flag), pointer :: ITEMTYPES(:)
character(len=ESMF_MAXSTR ), pointer :: ITEMNAMES(:)
logical, pointer :: DOIT(:)
integer :: DIMS
integer, pointer :: MASK(:) => null()
logical :: skipReading
integer :: RST
integer :: dna
logical :: ignoreEOF
logical :: bootstrapable_
logical :: isPresent
integer, allocatable :: orderlist(:)
integer :: jj
character(len=ESMF_MAXSTR) :: attrName
character(len=ESMF_MAXSTR), allocatable :: currList(:)
integer :: natt
call ESMF_StateGet(STATE,ITEMCOUNT=ITEMCOUNT,RC=STATUS)
_VERIFY(STATUS)
_ASSERT(ITEMCOUNT>0, 'itemcount must be > 0')
allocate(ITEMNAMES(ITEMCOUNT),STAT=STATUS)
_VERIFY(STATUS)
allocate(ITEMTYPES(ITEMCOUNT),STAT=STATUS)
_VERIFY(STATUS)
allocate( DOIT(ITEMCOUNT),STAT=STATUS)
_VERIFY(STATUS)
call ESMF_StateGet(STATE,ITEMNAMELIST=ITEMNAMES,&
ITEMTYPELIST=ITEMTYPES,RC=STATUS)
_VERIFY(STATUS)
if(present(NAME)) then
DOIT = ITEMNAMES==NAME
_ASSERT(count(DOIT)/=0, 'cont(doit) must be > 0')
else
DOIT = .true.
endif
attrName = MAPL_StateItemOrderList
call ESMF_AttributeGet(state, NAME=attrName, itemcount=natt, RC=STATUS)
_VERIFY(STATUS)
_ASSERT(natt > 0, 'natt not > 0')
allocate(orderlist(natt), stat=status)
_VERIFY(STATUS)
allocate(currList(natt), stat=status)
_VERIFY(STATUS)
! get the current list
call ESMF_AttributeGet(state, NAME=attrName, VALUELIST=currList, rc=status)
_VERIFY(STATUS)
orderList = -1 ! not found
do i = 1, natt
! search loop
do jj = 1, ITEMCOUNT
if (itemNames(jj) == currList(i)) then
orderList(i) = jj
exit
end if
end do
end do
deallocate(currList)
if (present(bootstrapable)) then
bootstrapable_ = bootstrapable
else
bootstrapable_ = .false.
end if
do JJ = 1, natt
I = ORDERLIST(JJ)
if (DOIT(I)) then
#ifdef TIME_MPIIO
call write_parallel(itemnames(i))
#endif
if (ITEMTYPES(I) == ESMF_StateItem_FieldBundle) then
call ESMF_StateGet(state, itemnames(i), bundle, rc=status)
_VERIFY(STATUS)
skipReading = .false.
call ESMF_AttributeGet(bundle, name='RESTART', isPresent=isPresent, rc=status)
_VERIFY(STATUS)
if (isPresent) then
call ESMF_AttributeGet(bundle, name='RESTART', value=RST, rc=status)
_VERIFY(STATUS)
else
RST = MAPL_RestartOptional
end if
skipReading = (RST == MAPL_RestartSkip)
if (skipReading) cycle
if (RST == MAPL_RestartRequired) then
bootstrapable_ = .true.
end if
call MAPL_BundleRead(unit, bundle, arrdes=arrdes, &
bootstrapable=bootstrapable_, rc=status)
_VERIFY(STATUS)
else if (ITEMTYPES(I) == ESMF_StateItem_Field) then
call ESMF_StateGet(state, itemnames(i), field, rc=status)
_VERIFY(STATUS)
skipReading = .false.
call ESMF_AttributeGet(field, name='RESTART', isPresent=isPresent, rc=status)
_VERIFY(STATUS)
if (isPresent) then
call ESMF_AttributeGet(field, name='RESTART', value=RST, rc=status)
_VERIFY(STATUS)
else
RST = MAPL_RestartOptional
end if
skipReading = (RST == MAPL_RestartSkip)
if (skipReading) cycle
call ESMF_AttributeGet(field, name='doNotAllocate', isPresent=isPresent, rc=status)
_VERIFY(STATUS)
if (isPresent) then
call ESMF_AttributeGet(field, name='doNotAllocate', value=dna, rc=status)
_VERIFY(STATUS)
skipReading = (dna /= 0)
end if
if (skipReading) cycle
ignoreEOF = .false.
if (bootstrapable_ .and. (RST == MAPL_RestartOptional)) then
ignoreEOF = .true.
end if
if(.not.associated(MASK)) then
call ESMF_AttributeGet(field, name='DIMS', value=DIMS, rc=status)
_VERIFY(STATUS)
if (DIMS == MAPL_DimsTileOnly .or. DIMS == MAPL_DimsTileTile) then
call ESMF_FieldGet (field, grid=grid, rc=status)
_VERIFY(STATUS)
call MAPL_TileMaskGet(grid, mask, rc=status)
_VERIFY(STATUS)
!@ else
!@ allocate(Mask(1))
endif
endif
call MAPL_FieldRead(unit, field, arrdes=arrdes, HomePE=Mask, ignoreEOF=ignoreEOF, rc=status)
_VERIFY(STATUS)
!ALT else
!ALT _FAIL('failed mapl_statevarread')
end if
end if
end do
deallocate(orderlist)
deallocate(ITEMNAMES)
deallocate(ITEMTYPES)
deallocate( DOIT)
if(associated(MASK)) then
DEALOC_(MASK)
end if
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_StateVarRead
!---------------------------
subroutine MAPL_BundleRead(UNIT,BUNDLE, ARRDES, BOOTSTRAPABLE, RC)
integer , intent(IN ) :: UNIT
type (ESMF_FieldBundle) , intent(INOUT) :: BUNDLE
type(ArrDescr), optional , intent(INOUT) :: ARRDES
logical, optional , intent(IN ) :: BOOTSTRAPABLE
integer, optional , intent( OUT) :: RC
integer :: status
integer :: J, N, fieldCount
type (ESMF_Field) :: field
character(len=ESMF_MAXSTR),allocatable :: nameList(:)
character(len=ESMF_MAXSTR) :: BundleName
integer :: RST
logical :: ignoreEOF
logical :: skipReading
logical :: bootstrapable_
logical :: isPresent
call ESMF_FieldBundleGet(bundle, fieldCount=N, name=BundleName, rc=STATUS)
_VERIFY(STATUS)
allocate(namelist(N), stat=status)
_VERIFY(STATUS)
call ESMF_FieldBundleGet(bundle, fieldNameList=nameList, fieldCount=FieldCount, rc=STATUS)
_VERIFY(STATUS)
_ASSERT(N==fieldCount, 'inconsistent fieldCount')
if (present(bootstrapable)) then
bootstrapable_ = bootstrapable
else
bootstrapable_ = .false.
end if
do J = 1, N
call MAPL_FieldBundleGet(bundle, fieldIndex=J, field=field, rc=status)
_VERIFY(STATUS)
call ESMF_AttributeGet(field, name='RESTART', isPresent=isPresent, rc=status)
_VERIFY(STATUS)
if (isPresent) then
call ESMF_AttributeGet(field, name='RESTART', value=RST, rc=status)
_VERIFY(STATUS)
else
RST = MAPL_RestartOptional
end if
skipReading = (RST == MAPL_RestartSkip)
if (skipReading) cycle
ignoreEOF=.false.
if (bootstrapable_ .and. (RST == MAPL_RestartOptional)) then
ignoreEOF = .true.
end if
call MAPL_FieldRead(unit, field, arrdes=ARRDES, ignoreEOF=ignoreEOF, rc=status)
_VERIFY(STATUS)
end do
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_BundleRead
subroutine MAPL_FieldRead(UNIT,FIELD, ARRDES, HomePE, ignoreEOF, RC)
integer , intent(IN ) :: UNIT
type (ESMF_Field) , intent(INOUT) :: field
type(ArrDescr), optional, intent(INOUT) :: ARRDES
integer, target, optional , intent(IN ) :: HomePE(:)
logical, optional , intent(IN ) :: ignoreEOF
integer, optional , intent( OUT) :: RC
! Local vars
type (ESMF_Array) :: array
type (ESMF_DELayout) :: layout
type (ESMF_Grid) :: GRID
integer :: rank
integer :: status
real(KIND=ESMF_KIND_R4), pointer, dimension(:) :: var_1d
real(KIND=ESMF_KIND_R4), pointer, dimension(:,:) :: var_2d
real(KIND=ESMF_KIND_R4), pointer, dimension(:,:,:) :: var_3d
real(KIND=ESMF_KIND_R4), pointer, dimension(:,:,:,:) :: var_4d
real(KIND=ESMF_KIND_R8), pointer, dimension(:) :: vr8_1d
real(KIND=ESMF_KIND_R8), pointer, dimension(:,:) :: vr8_2d
real(KIND=ESMF_KIND_R8), pointer, dimension(:,:,:) :: vr8_3d
real(KIND=ESMF_KIND_R8), pointer, dimension(:,:,:,:) :: vr8_4d
type(ESMF_TypeKind_Flag) :: tk
character(len=ESMF_MAXSTR) :: FORMATTED
integer :: dims
integer :: J, K
integer, pointer :: mask(:)
type (ESMF_DistGrid) :: distGrid
integer :: stat
logical :: ignoreEOF_
if (unit < 0 .or. present(arrdes)) then
FORMATTED = "NO"
else
inquire(unit=UNIT, formatted=FORMATTED)
end if
if (present(ignoreEOF)) then
ignoreEOF_ = ignoreEOF
else
ignoreEOF_ = .false.
end if
call ESMF_FieldGet(field, grid=grid, rc=status)
_VERIFY(STATUS)
call ESMF_GridGet(grid, distGrid=distGrid, rc=STATUS)
_VERIFY(STATUS)
call ESMF_DistGridGet(distGrid, delayout=layout, rc=STATUS)
_VERIFY(STATUS)
if (ignoreEOF_ .and. (unit > 0)) then
! test for end-of-file by
! making a blank read followed by backspace
if (MAPL_am_i_root(layout)) then
read (UNIT, IOSTAT=status)
end if
call MAPL_CommsBcast(layout, status, n=1, ROOT=MAPL_Root, rc=stat)
_VERIFY(STAT)
if (status == IOSTAT_END) then
_RETURN(ESMF_SUCCESS)
end if
_VERIFY(STATUS)
call MAPL_Backspace(UNIT, layout, rc=status)
_VERIFY(STATUS)
end if
call ESMF_AttributeGet(field, name='DIMS', value=DIMS, rc=status)
_VERIFY(STATUS)
if (DIMS == MAPL_DimsTileOnly .or. DIMS == MAPL_DimsTileTile) then
if(present(HomePE)) then
mask => HomePE
else
call MAPL_TileMaskGet(grid, mask, rc=status)
_VERIFY(STATUS)
endif
end if
call ESMF_FieldGet(field, Array=array, rc=status)
_VERIFY(STATUS)
call ESMF_ArrayGet(array, typekind=tk, rank=rank, rc=status)
_VERIFY(STATUS)
if (rank == 1) then
if (tk == ESMF_TYPEKIND_R4) then
call ESMF_ArrayGet(array, localDE=0, farrayptr=var_1d, rc=status)
_VERIFY(STATUS)
if (associated(var_1d)) then
if (DIMS == MAPL_DimsTileOnly .or. DIMS == MAPL_DimsTileTile) then
call MAPL_VarRead(unit, grid, var_1d, arrdes=arrdes, mask=mask, rc=status)
_VERIFY(STATUS)
else if (DIMS == MAPL_DimsVertOnly .or. DIMS==MAPL_DimsNone) then
call READ_PARALLEL(layout, var_1d, unit, arrdes=arrdes, rc=status)
else
_RETURN(ESMF_FAILURE)
endif
end if
else
call ESMF_ArrayGet(array, localDE=0, farrayptr=vr8_1d, rc=status)
_VERIFY(STATUS)
if (associated(vr8_1d)) then
if (DIMS == MAPL_DimsTileOnly .or. DIMS == MAPL_DimsTileTile) then
call MAPL_VarRead(unit, grid, vr8_1d, arrdes=arrdes, mask=mask, rc=status)
else if (DIMS == MAPL_DimsVertOnly .or. DIMS==MAPL_DimsNone) then
call READ_PARALLEL(layout, vr8_1d, unit, arrdes=arrdes, rc=status)
else
_RETURN(ESMF_FAILURE)
endif
end if
end if
else if (rank == 2) then
if (tk == ESMF_TYPEKIND_R4) then
call ESMF_ArrayGet(array, localDE=0, farrayptr=var_2d, rc=status)
_VERIFY(STATUS)
if (associated(var_2d)) then !ALT: temp kludge
if (FORMATTED=="YES") THEN
call READ_PARALLEL(layout, &
var_2d(lbound(var_2d,1),:), unit, rc=status)
else
if (DIMS == MAPL_DimsTileOnly) then
do J = 1,size(var_2d,2)
call MAPL_VarRead(unit, grid, var_2d(:,J), arrdes=arrdes, mask=mask, rc=status)
end do
else if (DIMS == MAPL_DimsTileTile) then
call MAPL_VarRead(unit, grid, var_2d, arrdes=arrdes, mask=mask, rc=status)
else
call MAPL_VarRead(unit, grid, var_2d, arrdes=arrdes, rc=status)
end if
end if
end if
else
call ESMF_ArrayGet(array, localDE=0, farrayptr=vr8_2d, rc=status)
_VERIFY(STATUS)
if (associated(vr8_2d)) then !ALT: temp kludge
if (FORMATTED=="YES") THEN
call READ_PARALLEL(layout, &
vr8_2d(lbound(vr8_2d,1),:), unit, rc=status)
else
if (DIMS == MAPL_DimsTileOnly) then
do J = 1,size(vr8_2d,2)
call MAPL_VarRead(unit, grid, vr8_2d(:,J), arrdes=arrdes, mask=mask, rc=status)
end do
else if (DIMS == MAPL_DimsTileTile) then
call MAPL_VarRead(unit, grid, vr8_2d, arrdes=arrdes, mask=mask, rc=status)
else
call MAPL_VarRead(unit, grid, vr8_2d, arrdes=arrdes, rc=status)
end if
end if
end if
endif
else if (rank == 3) then
if (tk == ESMF_TYPEKIND_R4) then
call ESMF_ArrayGet(array, localDE=0, farrayptr=var_3d, rc=status)
_VERIFY(STATUS)
if (associated(var_3d)) then !ALT: temp kludge
if (FORMATTED=="YES") THEN
call READ_PARALLEL(layout, &
var_3d(lbound(var_3d,1),lbound(var_3d,2),:), unit)
else
if (DIMS == MAPL_DimsTileOnly) then
do J = 1,size(var_3d,2)
do K = 1,size(var_3d,3)
call MAPL_VarRead(unit, grid, var_3d(:,J,K), arrdes=arrdes, mask=mask, rc=status)
end do
end do
else
call MAPL_VarRead(unit, grid, var_3d, arrdes=arrdes, rc=status)
end if
endif
end if
else
call ESMF_ArrayGet(array, localDE=0, farrayptr=vr8_3d, rc=status)
_VERIFY(STATUS)
if (associated(vr8_3d)) then !ALT: temp kludge
if (FORMATTED=="YES") THEN
call READ_PARALLEL(layout, &
vr8_3d(lbound(vr8_3d,1),lbound(vr8_3d,2),:), unit)
else
if (DIMS == MAPL_DimsTileOnly) then
do J = 1,size(vr8_3d,2)
do K = 1,size(vr8_3d,3)
call MAPL_VarRead(unit, grid, vr8_3d(:,J,K), arrdes=arrdes, mask=mask, rc=status)
end do
end do
else
call MAPL_VarRead(unit, grid, vr8_3d, arrdes=arrdes, rc=status)
end if
endif
end if
endif
else if (rank == 4) then
if (tk == ESMF_TYPEKIND_R4) then
call ESMF_ArrayGet(array, localDE=0, farrayptr=var_4d, rc=status)
_VERIFY(STATUS)
call MAPL_VarRead(unit, grid, var_4d, rc=status)
else
call ESMF_ArrayGet(array, localDE=0, farrayptr=vr8_4d, rc=status)
_VERIFY(STATUS)
call MAPL_VarRead(unit, grid, vr8_4d, rc=status)
end if
else
_FAIL( "ERROR: unsupported RANK")
endif
_VERIFY(STATUS)
if (DIMS == MAPL_DimsTileOnly .or. DIMS == MAPL_DimsTileTile) then
if(.not.present(HomePE)) then
DEALOC_(mask)
end if
end if
_RETURN(ESMF_SUCCESS)
end subroutine MAPL_FieldRead
!---------------------------
subroutine MAPL_VarRead_R4_1d(UNIT, GRID, A, MASK, arrdes, RC)
integer , intent(IN ) :: UNIT
type (ESMF_Grid) , intent(INout) :: GRID
real(kind=ESMF_KIND_R4) , intent( OUT) :: A(:)
integer, optional , intent(IN ) :: MASK(:)
type(ArrDescr), optional, intent(INOUT) :: ARRDES
integer, optional , intent( OUT) :: RC
! Local variables
real(kind=ESMF_KIND_R4), pointer :: VAR(:)
integer :: IM_WORLD
integer :: status
integer :: DIMS(ESMF_MAXGRIDDIM)
type (ESMF_DELayout) :: layout
type (ESMF_DistGrid) :: distgrid
integer, allocatable :: msk(:), sendcounts(:), displs(:)
integer, allocatable :: idx(:)
integer :: nrdrs, mype, npes, recvcount
integer :: mypeRd
integer :: Rsize, first, last
integer(KIND=MPI_OFFSET_KIND) :: offset
integer(KIND=MPI_OFFSET_KIND) :: loffset
integer :: i, k, n, i1, in
real(kind=ESMF_KIND_R4) :: dummy
integer :: group, newgroup
integer :: thiscomm
integer :: nactive
integer :: ntransl
integer, allocatable :: pes(:)
integer, allocatable :: r2g(:)
integer, allocatable :: rpes(:)
integer, allocatable :: activeranks(:)
integer, allocatable :: activesendcounts(:)
integer :: numread, mpistatus(MPI_STATUS_SIZE)
integer :: cnt
logical :: amIRoot
if(present(arrdes)) then
_ASSERT(present(mask), 'mask must be present if arrdes is present')
IM_WORLD = arrdes%im_world
call mpi_comm_size(arrdes%ioscattercomm,npes ,status)
_VERIFY(STATUS)
if(arrdes%readers_comm /= MPI_COMM_NULL) then
call mpi_comm_rank(arrdes%readers_comm,mypeRd ,status)
_VERIFY(STATUS)
call mpi_comm_size(arrdes%readers_comm,nrdrs,status)
_VERIFY(STATUS)
else
mypeRd = -1
endif
call ESMF_GridGet(grid, distGrid=distGrid, rc=STATUS)
_VERIFY(STATUS)
call ESMF_DistGridGet(distGrid, delayout=layout, rc=STATUS)
_VERIFY(STATUS)
call MAPL_CommsBcast(layout, nrdrs, 1, 0, rc = status)
Rsize = im_world/nrdrs + 1
first = mypeRd*Rsize + 1
if(mypeRd >= mod(im_world,nrdrs)) then
Rsize = Rsize - 1
first = first - (mypeRd-mod(im_world,nrdrs))
endif
last = first + Rsize - 1
#ifdef DEBUG_MPIIO
if (mypeRd <= nrdrs-1) write(*,'(5i)') mypeRd, IM_WORLD, first, last, Rsize
#endif
allocate(VAR(Rsize), stat=status)
_VERIFY(STATUS)
allocate(msk(Rsize), stat=status)
_VERIFY(STATUS)
allocate (sendcounts(0:npes-1), stat=status)
_VERIFY(STATUS)
allocate (r2g(0:nrdrs-1), stat=status)
_VERIFY(STATUS)
if(arrdes%readers_comm /= MPI_COMM_NULL) then
if(arrdes%offset<=0) then
offset = 4
else
offset = arrdes%offset
endif
loffset = offset + (first-1)*4
cnt = Rsize
call MPI_FILE_READ_AT_ALL(UNIT, loffset, VAR, cnt, MPI_REAL, mpistatus, STATUS)
_VERIFY(STATUS)
call MPI_GET_COUNT( mpistatus, MPI_REAL, numread, STATUS )
_VERIFY(STATUS)
_ASSERT(cnt == numread, 'inconsistent numread')
#ifdef DEBUG_MPIIO
write(*,'(3i,1f)') IM_WORLD, loffset, numread, VAR(1)
#endif
_ASSERT( (lbound(mask,1) <= first), 'location not in bounds')
_ASSERT( (ubound(mask,1) >= last ), 'location not in bounds')
msk = mask(first:last)
allocate(idx(Rsize), stat=status)
_VERIFY(STATUS)
do i=1,Rsize
idx(i) = i
enddo
msk = mask(first:last)
call MAPL_Sort(msk,idx)
msk = mask(first:last)
call MAPL_Sort(msk,var)
arrdes%offset = offset + IM_WORLD*4 + 8
endif
call mpi_comm_rank(arrdes%ioscattercomm,mype ,status)
_VERIFY(STATUS)
call MPI_COMM_GROUP (arrdes%ioscattercomm, GROUP, STATUS)
_VERIFY(STATUS)
#if 1
if (arrdes%readers_comm /= MPI_COMM_NULL) then
allocate(rpes(0:nrdrs-1), stat=status)
_VERIFY(STATUS)
call MPI_COMM_GROUP (arrdes%readers_comm, NEWGROUP, STATUS)
_VERIFY(STATUS)
do n=0,nrdrs-1
rpes(n) = n
end do
call MPI_Group_translate_ranks(newgroup, nrdrs, rpes, group, r2g, status)
_VERIFY(STATUS)
call MPI_GROUP_FREE (NEWGROUP, STATUS)
_VERIFY(STATUS)
deallocate(rpes)
end if
call MAPL_CommsBcast(layout, r2g, nrdrs, 0, rc = status)
#else
do n=0,nrdrs-1
r2g(n) = (npes/nrdrs)*n
end do
#endif
offset = 1
do n=0,nrdrs-1
Rsize = im_world/nrdrs + 1
first = n*Rsize + 1
if(n >= mod(im_world,nrdrs)) then
Rsize = Rsize - 1
first = first - (n-mod(im_world,nrdrs))
endif
last = first + Rsize - 1
sendcounts = 0
do i=first,last
sendcounts(mask(i)) = sendcounts(mask(i)) + 1
enddo
! Reader "n" must be included in the mpi group + evevybody that need the data
nactive = count(sendcounts > 0)
if (sendcounts(r2g(n)) == 0) then
nactive = nactive + 1
end if
allocate (activeranks(0:nactive-1), activesendcounts(0:nactive-1), stat=status)
_VERIFY(STATUS)
allocate(pes(0:nactive-1), stat=status)
_VERIFY(STATUS)
allocate (displs(0:nactive), stat=status)
_VERIFY(STATUS)
k = 0
do i=0, npes-1
if (sendcounts(i) > 0) then
pes(k) = i
k = k+1
end if
enddo
if (k /= nactive) then
k = k+1
_ASSERT(k == nactive, 'inconsistent nactive')
_ASSERT(sendcounts(r2g(n)) == 0, 'sendcounts should be 0')
pes(nactive-1) = r2g(n)
end if
call MPI_GROUP_INCL (GROUP, nactive, PES, newgroup, STATUS)
_VERIFY(STATUS)
call MPI_COMM_CREATE(arrdes%ioscattercomm, newgroup, thiscomm, STATUS)
_VERIFY(STATUS)
call MPI_Group_translate_ranks(group, nactive, pes, newgroup, activeranks, status)
_VERIFY(STATUS)
call MPI_GROUP_FREE (NEWGROUP, STATUS)
_VERIFY(STATUS)
if (thiscomm /= MPI_COMM_NULL) then
activesendcounts = 0
do i=0,nactive-1
activesendcounts(activeranks(i)) = sendcounts(pes(i))
if (pes(i) == r2g(n)) ntransl = activeranks(i)
end do
displs(0) = 0
do i=1,nactive
displs(i) = displs(i-1) + activesendcounts(i-1)
enddo
if(n==mypeRd) then
do i=0,nactive-1
if(activesendcounts(i)>0) then
i1 = displs(i ) + 1
in = displs(i+1)
call MAPL_Sort(idx(i1:in),var(i1:in))
endif
end do
endif
recvcount = sendcounts(mype)
if (recvcount == 0) then
call MPI_SCATTERV( var, activesendcounts, displs, MPI_REAL, &
dummy, recvcount, MPI_REAL, &
ntransl, thiscomm, status )
else
call MPI_SCATTERV( var, activesendcounts, displs, MPI_REAL, &
a(offset), recvcount, MPI_REAL, &
ntransl, thiscomm, status )
endif
_VERIFY(STATUS)
call MPI_Comm_Free(thiscomm, status)
_VERIFY(STATUS)
offset = offset + recvcount
end if
deallocate (displs)
deallocate(pes)
deallocate (activesendcounts, activeranks)
enddo