-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMPIFileModule.f90
2729 lines (2127 loc) · 73.1 KB
/
MPIFileModule.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
! ____ _ __ ____ __ ____
! / __/___(_) / ___ ____/ __ \__ _____ ___ / /_ / _/__ ____
! _\ \/ __/ / _ \/ -_) __/ /_/ / // / -_|_-</ __/ _/ // _ \/ __/
! /___/\__/_/_.__/\__/_/ \___\_\_,_/\__/___/\__/ /___/_//_/\__(_)
!
!
! Copyright 2010 SciberQuest Inc.
!
! No permission is granted to reproduce this software.
!
! This is experimental software and is provided ‘‘as is’’, with no
! warranties of any kind whatsoever, no support, no promise of updates,
! or printed documentation.
!==============================================================================
module MPIFileModule
use UnitManagerModule
use StringModule
use BoxModule
use CartesianDecompModule
use MPIFileOffsetModule
use LogFileModule
use MPIFileHintModule
use mpi
use iso_c_binding
!============================================================================
! File handle type for MPI IO
! This object and associated code handles splitting communicator
! and file for a single dataset.
type MPIFile
integer WorldComm ! all proccesses including the gate master
integer WorldRank ! my rank in comm world
integer WorldSize ! number of processes
integer MasterRank ! process that controls the gating, does no IO
integer GateOp ! 0=none, 1=stripe by comm color, 2=stripe by process id
integer FileSplitOp ! 0=none shared file 1=all file per proc, 2=comm single file per comm
integer CommSplitOp ! 0=none COMM_WORLD, 1=all COMM_SELF, 2=slab comms, 3=stripe comms
integer DataType ! elemental data type
character(len=1024) FileName ! Name of file this process writes
integer Mode ! MPI file mode flags
integer NFiles ! Number of subfiles for split
integer IOComm ! processes to participate in IO
integer IORank ! my rank in io comm
integer IOSize ! number of processes participating in io
integer CommId ! IO Communicator Id
integer NComms ! Number of IO communicators
integer Handle ! MPI file handle/fortran unit
integer FilePointerType ! 0=ExplicitOffset, 1=IndepFilePointer, 2=SharedFilePointer
logical UseMPI ! switches between POSIX and MPI-IO api
logical UseCollectiveAPI ! switches between colective and independent IO ops
integer FileHints ! MPI information object
integer FileView ! Describes data layout in the file
integer MemView ! Describes memory layout in the file
integer WriteGateSize ! Number concurrent writes
integer OpenGateSize ! Number concurrent opens
integer CloseGateSize ! Number concurrent closes
integer GateId ! Gate group id TODO remove?
integer NGates ! Number of gates TODO remove?
type(CartesianDecomp), pointer :: UGridDecomp ! Data decompositon (Uniform grid mode only)
type(MPIFileOffset), pointer :: FilePointer ! Shared file pointer (Contiguous mode only)
type(LogFile),pointer :: Logger ! IO benchmark log file
end type
!----------------------------------------------------------------------------
interface NewMPIFile
module procedure NewMPIFileDefault
module procedure NewMPIFileUniformGrid
module procedure NewMPIFileContiguous
end interface
!----------------------------------------------------------------------------
interface MPIFileWrite
! these wrap our internal write routine which takes
! a c_ptr object. These cast from the native fortran
! type to the c_ptr. The internal write routine casts
! the c_ptr back into a fortran pointer and passes
! it into mpi. We're doing this so that we do not
! have to duplicate all of the complex write code
! for each different data type and array shape.
module procedure MPIFileWrite3Real4
module procedure MPIFileWrite1Real4
module procedure MPIFileWrite3Real8
module procedure MPIFileWrite1Real8
module procedure MPIFileWrite3Int4
module procedure MPIFileWrite1Int4
module procedure MPIFileWrite3Int8
module procedure MPIFileWrite1Int8
end interface
!----------------------------------------------------------------------------
interface MPIFilePrintSelf
module procedure MPIFilePrintSelfString
module procedure MPIFilePrintSelfUnit
end interface
!----------------------------------------------------------------------------
interface MPIFileSetDecomp
module procedure MPIFileSetUniformGridDecomp
end interface
contains
!----------------------------------------------------------------------------
! Default constructor
function NewMPIFileDefault() result(fh)
use mpi
implicit none
type(MPIFile), pointer :: fh
integer iErr
nullify(fh)
allocate(fh,stat=iErr)
if (iErr.ne.0) then
write(0,*)"Error: failed to allocate MPIFile."
stop
end if
fh%WorldComm=MPI_COMM_NULL
fh%WorldSize=-1
fh%WorldRank=-1
fh%GateOp=0
fh%FileSplitOp=0
fh%CommSplitOp=0
fh%DataType=MPI_REAL
fh%FileName=""
fh%Handle=0
fh%Mode=0
fh%IOComm=MPI_COMM_NULL
fh%IOSize=-1
fh%IORank=-1
fh%WorldRank=-1
fh%CommId=-1
fh%NComms=1
fh%FileView=MPI_DATATYPE_NULL
fh%MemView=MPI_DATATYPE_NULL
fh%FilePointerType=1 ! indep
fh%UseCollectiveAPI=.true.
fh%UseMPI=.true.
fh%FileHints=MPI_INFO_NULL
fh%NFiles=1
fh%MasterRank=0
fh%OpenGateSize=0
fh%WriteGateSize=0
fh%CloseGateSize=0
fh%GateId=1
fh%NGates=1
nullify(fh%UGridDecomp)
nullify(fh%FilePointer)
nullify(fh%Logger)
end function
!------------------------------------------------------------------------------
! Uniform Grid Constructor
function NewMPIFileUniformGrid( &
comm, &
masterRank, &
fileName, &
mode, &
dataType, &
decomp, &
commSplitOp, &
nComms, &
fileSplitOp, &
gateOp, &
openGateSize, &
writeGateSize, &
closeGateSize, &
API, &
fileHints, &
logger, &
iErr) result(fh)
implicit none
type(MPIFile), pointer :: fh
integer comm
integer masterRank
character(len=*) :: fileName
integer dataType
integer mode
type(CartesianDecomp), pointer :: decomp
integer nComms
integer commSplitOp
integer fileSplitOp
integer openGateSize
integer writeGateSize
integer closeGateSize
integer gateOp
integer API
integer fileHints
type(LogFile),pointer :: logger
integer iErr
nullify(fh)
fh => NewMPIFile()
if (.not.associated(fh)) then
iErr=1
return
end if
! paramters
call MPIFileSetDataType(fh,dataType,iErr)
call MPIFileSetFileName(fh,fileName,iErr)
call MPIFileSetMode(fh,mode,iErr)
call MPIFileSetDecomp(fh,decomp,iErr)
call MPIFileSetNComms(fh,nComms,iErr)
call MPIFileSetAPI(fh,API,iErr)
call MPIFileSetFilePointerType(fh,1,iErr)
call MPIFileSetOpenGateSize(fh,openGateSize,iErr)
call MPIFileSetWriteGateSize(fh,writeGateSize,iErr)
call MPIFileSetCloseGateSize(fh,closeGateSize,iErr)
! communicator
call MPIFileSetCommunicator(fh,comm,masterRank,iErr)
if (iErr.ne.0) return
call MPIFileSetCommSplitOperation(fh,commSplitOp,iErr)
if (iErr.ne.0) return
! gating
call MPIFileSetGateOperation(fh,gateOp,iErr)
if (iErr.ne.0) return
! file
call MPIFileSetFileSplitOperationUGrid(fh,fileSplitOp,iErr)
if (iErr.ne.0) return
! hints
call MPIFileSetFileHints(fh,fileHints,iErr)
if (iErr.ne.0) return
! logger
fh%Logger => NewLogFile(logger)
call LogFileSetComm(fh%Logger,MPI_COMM_SELF,iErr)
call MPIFileLogNComms(fh,iErr)
call MPIFileLogIOAPI(fh,iErr)
! open the file
call MPIFileOpen(fh,iErr)
end function
!------------------------------------------------------------------------------
! Contiguous Constructor
function NewMPIFileContiguous( &
comm, &
masterRank, &
fileName, &
mode, &
commSplitOp, &
nComms, &
fileSplitOp, &
gateOp, &
openGateSize, &
writeGateSize, &
closeGateSize, &
API, &
fileHints, &
logger, &
iErr) result(fh)
implicit none
type(MPIFile), pointer :: fh
integer comm
integer masterRank
character(len=*) :: fileName
integer mode
integer nComms
integer commSplitOp
integer fileSplitOp
integer openGateSize
integer writeGateSize
integer closeGateSize
integer gateOp
integer API
integer fileHints
type(LogFile),pointer :: logger
integer iErr
nullify(fh)
fh => NewMPIFile()
if (.not.associated(fh)) then
iErr=1
return
end if
! paramters
call MPIFileSetFileName(fh,fileName,iErr)
call MPIFileSetMode(fh,mode,iErr)
call MPIFileSetNComms(fh,nComms,iErr)
call MPIFileSetAPI(fh,API,iErr)
call MPIFileSetFilePointerType(fh,0,iErr)
call MPIFileSetOpenGateSize(fh,openGateSize,iErr)
call MPIFileSetWriteGateSize(fh,writeGateSize,iErr)
call MPIFileSetCloseGateSize(fh,closeGateSize,iErr)
! communicator
call MPIFileSetCommunicator(fh,comm,masterRank,iErr)
if (iErr.ne.0) return
call MPIFileSetCommSplitOperation(fh,commSplitOp,iErr)
if (iErr.ne.0) return
! file pointer
fh%FilePointer => NewMPIFileOffset(fh%IOComm)
! gating
call MPIFileSetGateOperation(fh,gateOp,iErr)
if (iErr.ne.0) return
! file
call MPIFileSetFileSplitOperationContig(fh,fileSplitOp,iErr)
if (iErr.ne.0) return
! hints
call MPIFileSetFileHints(fh,fileHints,iErr)
if (iErr.ne.0) return
! note: in above sequence the order is important
! logger
fh%Logger => NewLogFile(logger)
call LogFileSetComm(fh%Logger,MPI_COMM_SELF,iErr)
call MPIFileLogNComms(fh,iErr)
call MPIFileLogIOAPI(fh,iErr)
! open the file
call MPIFileOpen(fh,iErr)
if (iErr.ne.0) return
end function
!------------------------------------------------------------------------------
subroutine DeleteMPIFile(fh)
implicit none
type(MPIFile), pointer :: fh
integer iErr
if (.not.associated(fh)) return
if (fh%Handle.ne.0) then
write(0,*)'Error: File was not closed.'
endif
call MPIFileSetCommunicator(fh,MPI_COMM_NULL,-1,iErr)
if (fh%FileHints.ne.MPI_INFO_NULL) then
call MPI_Info_free(fh%FileHints,iErr)
fh%FileHints=MPI_INFO_NULL
end if
if (fh%FileView.ne.MPI_DATATYPE_NULL) then
call MPI_Type_free(fh%FileView,iErr)
fh%FileView=MPI_DATATYPE_NULL
end if
if (fh%MemView.ne.MPI_DATATYPE_NULL) then
call MPI_Type_free(fh%MemView,iErr)
fh%MemView=MPI_DATATYPE_NULL
end if
call DeleteMPIFileOffset(fh%FilePointer)
nullify(fh%FilePointer)
call DeleteLogFile(fh%Logger)
nullify(fh%FilePointer)
deallocate(fh)
nullify(fh)
end subroutine
!----------------------------------------------------------------------------
subroutine MPIFileSetMemView(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
type(Box), pointer :: simGrid,localGrid
simGrid => fh%UGridDecomp%SimGrid
localGrid => fh%UGridDecomp%LocalGrid
! in memory - no ghosts
call MPI_Type_contiguous( &
BoxGetSize(localGrid), &
fh%DataType, &
fh%MemView, &
iErr)
if (iErr.ne.0) return
call MPI_Type_commit(fh%MemView,iErr)
if (iErr.ne.0) return
! TODO handle ghost cells
! call MPIFileCreateSubArray(simGrid,localGrid,fh%DataType,fh%MemView,iErr)
end subroutine
!---------------------------------------------------------------------------
! for uniform grid io
subroutine MPIFileSetFileViewShared(fh,iErr)
implicit none
type(MPIFile) fh
type(Box), pointer :: simGrid,localGrid
integer iErr
iErr=0
! data layouts
simGrid => fh%UGridDecomp%SimGrid
localGrid => fh%UGridDecomp%LocalGrid
! in file
call MPIFileCreateSubArray(simGrid,localGrid,fh%DataType,fh%FileView,iErr)
end subroutine
!----------------------------------------------------------------------------
! for uniform grid io
subroutine MPIFileSetFileViewFilePerProc(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
type(Box), pointer :: simGrid,localGrid
simGrid => fh%UGridDecomp%SimGrid
localGrid => fh%UGridDecomp%LocalGrid
! in memory - no ghosts
call MPI_Type_contiguous(BoxGetSize(localGrid),fh%DataType,fh%FileView,iErr)
if (iErr.ne.0) return
call MPI_Type_commit(fh%FileView,iErr)
if (iErr.ne.0) return
! TODO handle ghost cells
! call MPIFileCreateSubArray(simGrid,localGrid,fh%DataType,fh%MemView,iErr)
end subroutine
!----------------------------------------------------------------------------
! for uniform grid io
subroutine MPIFileSetFileViewSlab(fh,iErr)
implicit none
type(MPIFile) fh
type(Box),pointer :: simGrid,localGrid,fileGrid
integer kDecomp ! k index into cartesian decomp
integer kDecompMax ! largest index
integer kFileLo,kFileHi ! k index into computation grid
integer nLarge,slabHeight
integer iErr
iErr=0
! sanity check, for a split file there should be at least
! 2 comms
if (fh%NComms.lt.2) then
write(0,*)"Error: Split file requires at least two comms."
iErr=1
return
end if
kDecomp=fh%UGridDecomp%ProcCoords(2)
kDecompMax=fh%UGridDecomp%ProcExt(2)
! sanity check, can't have more slabs than process groups
if (kDecompMax.lt.fh%NComms) then
fh%NComms=kDecompMax
end if
! Given a 1D decomposition along the z dimension of the cartesian
! process group figure out which sub group(slab) we belong in.
slabHeight=kDecompMax/fh%NComms
nLarge=mod(kDecompMax,fh%NComms)
! Create data layouts
! file domain covers multiple slabs
if (fh%CommId.lt.nLarge) then
kFileLo=fh%CommId*(slabHeight+1)
kFileHi=kFileLo+slabHeight
else
kFileLo=nLarge+fh%CommId*slabHeight
kFileHi=kFileLo+slabHeight-1
end if
simGrid => fh%UGridDecomp%SimGrid
localGrid => fh%UGridDecomp%LocalGrid
! in file - slab is a contigous in i-j, k block
fileGrid => NewBox( &
simGrid%I(1), &
simGrid%I(2), &
simGrid%I(3), &
simGrid%I(4), &
kFileLo, &
kFileHi)
write(0,*)kDecomp,kDecompMax
write(0,*)slabHeight
write(0,*)kFileLo,kFileHi
call BoxPrintSelf(simGrid,0)
call BoxPrintSelf(localGrid,0)
call BoxPrintSelf(fileGrid,0)
call MPIFileCreateSubArray(fileGrid,localGrid,fh%DataType,fh%FileView,iErr)
call DeleteBox(fileGrid)
if (iErr.ne.0) return
end subroutine
!---------------------------------------------------------------------------
subroutine MPIFileSetFileViewStriped(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
!iErr=0
iErr=iand(fh%Mode,0) ! avoid compiler warning
! use comm split file.
write(0,*)"Error: MPIFileSetFileViewStriped is not implemented."
stop
end subroutine
!------------------------------------------------------------------------------
! This configures the object in split file mode, where processes assigned the
! same color write to the same file. It creates the top level directory to hold
! the files, and sets the views etc. The objects CommId must be set appropriately
! first.
subroutine MPIFileInitializeCommSplitFile(fh,iErr)
implicit none
type(MPIFile) fh
logical exists
character*1024 subFileName,cFileName
integer iErr
iErr=0
! sanity - comm better be split.
if (fh%NComms.lt.2) then
write(0,*)"Error: Split by comm requires more than 1 comm."
iErr=1
return
end if
! sanity - comm id better be set.
if (fh%CommId.lt.0) then
write(0,*)"Error: CommId has not been set."
iErr=1
return
end if
call MPIFileStartLogEvent(fh)
! number of sub-files should match number of comms, else
! MPI_File_open fails.
fh%NFiles=fh%NComms
! one process handles file deletion/creation
if (fh%IORank.eq.0) then
cFileName=trim(fh%FileName)//char(0)
#if defined __GFORTRAN__
inquire(file=fh%FileName,exist=exists)
#elif defined __INTEL_COMPILER
inquire(directory=fh%FileName,exist=exists)
#endif
if (exists) then
! remove the existing file.
call lfdeletesplit(cFileName,iErr)
if (iErr.ne.0) then
write(0,*)"Error: could not remove existing split file at: ",trim(fh%FileName)
return
end if
end if
! create the new split file
call lfcreatedir(cFileName,iErr)
if (iErr.ne.0) then
write(0,*)"Error: could not create split file at: ",trim(fh%FileName)
return
end if
end if
! set this processes file name
write(subFileName,'(2A,I0.5)')trim(fh%FileName),"/",fh%CommId
fh%FileName=trim(subFileName)
call MPIFileEndLogEvent(fh,"File_init_split")
end subroutine
!----------------------------------------------------------------------------
subroutine MPIFileInitializeSharedFile(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
! logical exists
! character*1024 cFileName
iErr=0
! sanity check , must have set the comm color
if (fh%CommId.lt.0) then
write(0,*)"Error: CommId has not been set."
iErr=1
return
end if
call MPIFileStartLogEvent(fh)
fh%NFiles=1
! NOTE: disabled for consistency with file-per-proc case
! in MPI case no syncronization is needed because it is
! a collective operation.
!
! TODO: verify MPI_File_open resets striping parameters
! on existing files
! if (fh%IORank.eq.0) then
! ! one process handles file deletion
!
! cFileName=trim(fh%FileName)//char(0)
!
! inquire(file=fh%FileName,exist=exists)
! if (exists) then
!
! ! remove the existing file.
! call lfdelete(cFileName,iErr)
! if (iErr.ne.0) then
! write(0,*)"Error: could not remove existing file at: ",trim(fh%FileName)
! return
! end if
!
! end if
!
! end if
call MPIFileEndLogEvent(fh,"File_init_shared")
end subroutine
!----------------------------------------------------------------------------
subroutine MPIFileInitializeFilePerProc(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
character(len=512) fileName
! character(len=512) cFileName
! integer rankId,maxRankId
! logical exists
iErr=0
fileName=""
! cFileName=""
call MPIFileStartLogEvent(fh)
! NOTE: disabled , syncronization needed to avoid
! race condition harms performance.
! TODO: verify llapi_file_create resets striping parameters
! on existing files
! if (fh%WorldRank.eq.fh%MasterRank) then
! ! one process cleans up any existing files.
!
! maxRankId=fh%IOSize-1
! do rankId=0,maxRankId
!
! fileName= &
! trim(fh%FileName) // &
! "." // &
! trim(ConvertTight(rankId))
!
! cFileName=trim(fileName)//char(0)
!
! inquire(file=fileName,exist=exists)
! if (exists) then
!
! call lfdelete(cFileName,iErr)
! if (iErr.ne.0) then
! write(0,*)"Error: could not remove existing file at: ",trim(fileName)
! return
! end if
!
! end if
!
! end do
!
! call MPI_Barrier(MPI_COMM_WORLD,iErr)
!
! else
! ! other process must wait for rank 0 to delete to avoid a
! ! race condition between their open and rank 0 delete
! call MPI_Barrier(MPI_COMM_WORLD,iErr)
!
! end if
! set up for file per io
fh%NFiles=fh%IOSize
fileName= &
trim(fh%FileName) // &
"." // &
trim(ConvertTight(fh%IORank))
fh%FileName=fileName
call MPIFileEndLogEvent(fh,"File_init_per_proc")
end subroutine
!----------------------------------------------------------------------------
! split the file, set the views
subroutine MPIFileSetFileSplitOperationUGrid(fh,operation,iErr)
implicit none
type(MPIFile) fh
integer operation
integer iErr
iErr=0
fh%FileSplitOp=operation
! uniform grid
! set up a mem view of data on a grid (possibly with ghosts)
call MPIFileSetMemView(fh,iErr)
if (iErr.ne.0) return
! set a file view to a subarray region of the global grid
! and (optionally) split the file.
select case (operation)
case (0)
! none - single file
call MPIFileInitializeSharedFile(fh,iErr)
if (iErr.ne.0) return
call MPIFileSetFileViewShared(fh,iErr)
if (iErr.ne.0) return
case (1)
! all - single file per process
call MPIFileInitializeFilePerProc(fh,iErr)
if (iErr.ne.0) return
call MPIFileSetFileViewFilePerProc(fh,iErr)
if (iErr.ne.0) return
case (2)
! comm - single file per comm
call MPIFileInitializeCommSplitFile(fh,iErr)
if (iErr.ne.0) return
call MPIFileSetFileViewSlab(fh,iErr)
if (iErr.ne.0) return
case default
! invalid
write(0,*)"Error: invalid file split operation requested."
iErr=1
return
end select
end subroutine
!----------------------------------------------------------------------------
! split the file, set the views
subroutine MPIFileSetFileSplitOperationContig(fh,operation,iErr)
implicit none
type(MPIFile) fh
integer operation
integer iErr
iErr=0
fh%FileSplitOp=operation
! contigous chunks
! use the default file view (ie. a stream of bytes)
select case (operation)
case (0)
! none - single file
call MPIFileInitializeSharedFile(fh,iErr)
if (iErr.ne.0) return
case (1)
! all - single file per process
call MPIFileInitializeFilePerProc(fh,iErr)
if (iErr.ne.0) return
case (2)
! comm - single file per comm
call MPIFileInitializeCommSplitFile(fh,iErr)
if (iErr.ne.0) return
case default
! invalid
write(0,*)"Error: invalid file split operation requested."
iErr=1
return
end select
end subroutine
!----------------------------------------------------------------------------
! All processes share a comm, this is the recommended way to use
! MPI IO.
subroutine MPIFileInitializeCommSingle(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
iErr=0
fh%CommId=0
fh%NComms=1
end subroutine
!----------------------------------------------------------------------------
! Each process has it's own communicator. This is equivalent to
! posix, single file per process IO.
subroutine MPIFileInitializeCommIndividual(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
iErr=0
fh%CommId=fh%IORank
fh%NComms=fh%IOSize
end subroutine
!----------------------------------------------------------------------------
! Assign color such that each is associated with a contiguous slab of
! the simulation domain. Kdecomp is the k-direction coordinate of the
! cartesian comunicator decompositoon,see MPI_Cart_create. The nSlabs
! paramter controls how many new colors are produced.
subroutine MPIFileInitializeCommSlab(fh,iErr)
use mpi
implicit none
type(MPIFile) fh
integer kDecomp,kDecompMax
integer nLarge,slabHeight,smallSlabStart
! integer jDecompMax,njDecomps
! integer procsPerNode,procsPerFile,magicNo
integer iErr
iErr=0
! jDecompMax=fh%UGridDecomp%ProcExt(1)
kDecomp=fh%UGridDecomp%ProcCoords(2)
kDecompMax=fh%UGridDecomp%ProcExt(2)
! interpret -1 to mean an automitic split.
if (fh%NComms.eq.-1) then
! use the number of processes in the z-rdirection
! of our coartesian decomp.
fh%NComms=kDecompMax
! NOTE: nothing wrong with the following.
! ! WARNING:
! ! 12 is no. of cores per node on kraken, 160 is the max
! ! no. procs that can access a file on lustre. So this
! ! default is taylored for Kraken.
! procsPerNode=12
! procsPerFile=160
! magicNo=procsPerFile*procsPerNode
!
! ! if this is not zero then we want more than
! ! one slab per comm. note: currently iDecompMax=1
! njDecomps=jDecompMax/magicNo
!
! if (njDecomps.gt.1) then
! ! fewer comms, ie. multiple slabs per comm
! fh%NComms=kDecompMax/njDecomps
!
! else
! ! we already have more than the magic number in a single
! ! slab (the finest unit of measurement for slab io),
! fh%NComms=kDecompMax
!
! end if
end if
! Warn - NComms==1 means no split.
if (fh%NComms.lt.2) then
write(0,*)"Error: Slab split comm. NComms < 2."
iErr=1
return
end if
! sanity check, can't have more slabs than process groups
if (kDecompMax.lt.fh%NComms) then
fh%NComms=kDecompMax
end if
! Given a 1D decomposition along the z dimension of the cartesian
! process group figure out which sub group(slab) we belong in.
slabHeight=kDecompMax/fh%NComms
nLarge=mod(kDecompMax,fh%NComms)
smallSlabStart=nLarge*(slabHeight+1)
if (kDecomp.lt.smallSlabStart) then
fh%CommId=kDecomp/(slabHeight+1)
else
fh%CommId=nLarge+(kDecomp-smallSlabStart)/slabHeight
endif
call MPIFileSplitCommInternal(fh,iErr)
end subroutine
!----------------------------------------------------------------------------
! Assigns a color in stripes of fixed size. note: this is not
! related to file system striping.
subroutine MPIFileInitializeCommStripe(fh,iErr)
implicit none
type(MPIFile) fh
integer stripeSize
integer procsPerNode,procsPerFile
integer iErr
iErr=0
! sanity - can't have more comms than processes
if (fh%NComms.gt.fh%IOSize) then
fh%NComms=fh%IOSize
end if
if (fh%NComms.eq.-1) then
! interpret -1 to mean an automitic split.
! WARNING:
! 12 is no. of cores per node on kraken, 160 is the max
! no. procs that can access a file on lustre. So this
! default is taylored for Kraken.
procsPerNode=12
procsPerFile=160
stripeSize=procsPerFile*procsPerNode
else
! split as user specified
! Warn - NComms==1 means no split.
if (fh%NComms.lt.2) then
write(0,*)"Warning: Stripe comm. NComms < 2."
end if
stripeSize=fh%IOSize/fh%NComms
end if
if (stripeSize.eq.0) then
write(0,*)"Error: NComms > WorldSize."
iErr=1
return
end if
fh%CommId=fh%IORank/stripeSize
fh%NComms=fh%IOSize/stripeSize
if(mod(fh%IOSize,stripeSize).ne.0) fh%NComms=fh%NComms+1
call MPIFileSplitCommInternal(fh,iErr)
end subroutine
!----------------------------------------------------------------------------
! Split sthe communicator by color, processes with the same color
! are in the same communicator.
subroutine MPIFileSplitCommInternal(fh,iErr)
implicit none
type(MPIFile) fh
integer iErr
stop
call MPI_Comm_split(MPI_COMM_WORLD,fh%CommId,fh%IORank,fh%IOComm,iErr)
if (iErr.ne.0) then
write(0,*)"Error: Failed to split the communicator."
end if
end subroutine
!----------------------------------------------------------------------------
! Split the communicator
subroutine MPIFileSetCommSplitOperation(fh,operation,iErr)
implicit none
type(MPIFile) fh
integer operation
integer iErr
iErr=0