-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_mpi.f90
5022 lines (3682 loc) · 135 KB
/
mod_mpi.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
!-------------------------------------------------------------------------
! project : azalea@fantasy
! program : mmpi
! source : mod_mpi.f90
! type : module
! author : li huang (email:huangli712yahoo.com.cn)
! history : 08/09/2006 by li huang
! 08/14/2006 by li huang
! 08/18/2006 by li huang
! 08/21/2006 by li huang
! 08/25/2006 by li huang
! 08/29/2006 by li huang
! 09/11/2006 by li huang
! 09/13/2006 by li huang
! 09/26/2006 by li huang
! 10/31/2006 by li huang
! 11/15/2006 by li huang
! 11/26/2006 by li huang
! 12/10/2006 by li huang
! 02/20/2007 by li huang
! 02/28/2007 by li huang
! 03/02/2007 by li huang
! 03/14/2007 by li huang
! 04/17/2007 by li huang
! 04/19/2007 by li huang
! 01/11/2008 by li huang
! 10/24/2008 by li huang
! 12/15/2008 by li huang
! 04/27/2009 by li huang
! 07/29/2009 by li huang
! 08/22/2009 by li huang
! 12/18/2009 by li huang
! 02/27/2010 by li huang
! purpose : to define my own mpi calls, inspired by quantum-espresso code.
! note that the original mpi subroutines are rather complicated
! for newbies. thus we try to wrap the most important and useful
! (not all) mpi subroutines, including
! MPI_INIT(),
! MPI_FINALIZE(),
! MPI_WTIME(),
! MPI_WTICK(),
! MPI_BARRIER(),
! MPI_DIMS_CREATE(),
! MPI_CART_CREATE(),
! MPI_CART_COORDS(),
! MPI_COMM_SPLIT(),
! MPI_COMM_RANK(),
! MPI_COMM_SIZE(),
! MPI_GET_PROCESSOR_NAME(),
! MPI_BCAST(),
! MPI_GATHER(),
! MPI_GATHERV(),
! MPI_ALLGATHER(),
! MPI_ALLGATHERV(),
! MPI_REDUCE(),
! MPI_ALLREDUCE(),
! etc, to facilite the usage of mpi. in the module, we also
! implement a new light-weight error handler. enjoy it!
! status : unstable
! comment : this module has been tested under the following environment:
! mpich1 1.2.7p1
! mpich2 1.2.1p1
! mvapich2 1.2.0p1
! intel mpi 3.2.0
!-------------------------------------------------------------------------
! whether the compiler support mpi environment, i.e, mpif90
# if defined (MPI)
module mmpi
use mpi
implicit none
!-------------------------------------------------------------------------
!::: declare global constants :::
!-------------------------------------------------------------------------
! dp: number precision, double precision for reals
integer, private, parameter :: dp = kind(1.0d0)
! mystd: device descriptor, console output
integer, private, parameter :: mystd = 6
!-------------------------------------------------------------------------
!::: declare mpi constants (datatypes) :::
!-------------------------------------------------------------------------
! mpi_log: datatype, boolean
integer, private, parameter :: mpi_log = MPI_LOGICAL
! mpi_mint: datatype, integer
integer, private, parameter :: mpi_mint = MPI_INTEGER
! mpi_dreal: datatype, double precision float
integer, private, parameter :: mpi_dreal = MPI_DOUBLE_PRECISION
! mpi_dcmplx: datatype, double precision complex
integer, private, parameter :: mpi_dcmplx = MPI_DOUBLE_COMPLEX
!-------------------------------------------------------------------------
!::: declare common constants :::
!-------------------------------------------------------------------------
! ndims: number of cartesian dimensions, and we need a 2D grid
integer, private, parameter :: ndims = 2
! reorder: ranking may be reordered (true) or not (false) in a new grid
logical, private, parameter :: reorder = .false.
! periods: specifying whether the grid is periodic or not in each dimens
logical, private, parameter :: periods(ndims) = .false.
!-------------------------------------------------------------------------
!::: declare common variables :::
!-------------------------------------------------------------------------
! ierror: error code for mpi subroutines
integer, private :: ierror
! istat: status code for mpi subroutines
integer, private :: istat
! group: default communicator
integer, private :: group
! isize: size of elements
integer, private :: isize
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
! mpi_comm_cart: communicator for cartesian topology
integer, public, save :: mpi_comm_cart
! mpi_comm_row: rowwise-striped communicator for cartesian topology
integer, public, save :: mpi_comm_row
! mpi_comm_col: columnwise-striped communicator for cartesian topology
integer, public, save :: mpi_comm_col
!-------------------------------------------------------------------------
!::: declare accessibility for module routines :::
!-------------------------------------------------------------------------
!>>> mpi information operation
public :: mp_info
!>>> mpi environment operation
! initialize mpi environment
public :: mp_init
! finalize mpi environment
public :: mp_finalize
! obtain id of current process
public :: mp_comm_rank
! obtain the size of process group
public :: mp_comm_size
! query machine name
public :: mp_processor
!>>> mpi cartesian topology operation
! creates a division of processors in a cartesian grid
public :: mp_dims_create
! makes a new communicator to which topology is cartesian
public :: mp_cart_create
! determines process coords in cartesian topology given rank in group
public :: mp_cart_coords
! creates new communicators based on colors and keys for rowwise grid
public :: mp_comm_split_row
! creates new communicators based on colors and keys for columnwise grid
public :: mp_comm_split_col
!>>> synchronics operations
! manually block until all processes reach
public :: mp_barrier
!>>> time handler
! obtain time consuming by parallelized program
public :: mp_wtime
! obtain time precision of mp_wtime
public :: mp_wtick
!>>> broadcasting operations
! broadcasting bool
private :: mp_bcast_bool
! broadcasting bool(:)
private :: mp_bcast_bool1
! broadcasting bool(:,:)
private :: mp_bcast_bool2
! broadcasting int
private :: mp_bcast_int
! broadcasting int(:)
private :: mp_bcast_int1
! broadcasting int(:,:)
private :: mp_bcast_int2
! broadcasting int(:,:,:)
private :: mp_bcast_int3
! broadcasting int(:,:,:,:)
private :: mp_bcast_int4
! broadcasting int(:,:,:,:,:)
private :: mp_bcast_int5
! broadcasting real
private :: mp_bcast_rdp
! broadcasting real(:)
private :: mp_bcast_rdp1
! broadcasting real(:,:)
private :: mp_bcast_rdp2
! broadcasting real(:,:,:)
private :: mp_bcast_rdp3
! broadcasting real(:,:,:,:)
private :: mp_bcast_rdp4
! broadcasting real(:,:,:,:,:)
private :: mp_bcast_rdp5
! broadcasting complex
private :: mp_bcast_cdp
! broadcasting complex(:)
private :: mp_bcast_cdp1
! broadcasting complex(:,:)
private :: mp_bcast_cdp2
! broadcasting complex(:,:,:)
private :: mp_bcast_cdp3
! broadcasting complex(:,:,:,:)
private :: mp_bcast_cdp4
! broadcasting complex(:,:,:,:,:)
private :: mp_bcast_cdp5
!>>> gathering operations
! gathering int(:)
private :: mp_gather_int1
! gathering int(:,:)
private :: mp_gather_int2
! gathering int(:,:,:)
private :: mp_gather_int3
! gathering int(:,:,:,:)
private :: mp_gather_int4
! gathering int(:,:,:,:,:)
private :: mp_gather_int5
! gathering real(:)
private :: mp_gather_rdp1
! gathering real(:,:)
private :: mp_gather_rdp2
! gathering real(:,:,:)
private :: mp_gather_rdp3
! gathering real(:,:,:,:)
private :: mp_gather_rdp4
! gathering real(:,:,:,:,:)
private :: mp_gather_rdp5
! gathering complex(:)
private :: mp_gather_cdp1
! gathering complex(:,:)
private :: mp_gather_cdp2
! gathering complex(:,:,:)
private :: mp_gather_cdp3
! gathering complex(:,:,:,:)
private :: mp_gather_cdp4
! gathering complex(:,:,:,:,:)
private :: mp_gather_cdp5
!>>> gatherving operations
! gatherving int(:)
private :: mp_gatherv_int1
! gatherving int(:,:)
private :: mp_gatherv_int2
! gatherving int(:,:,:)
private :: mp_gatherv_int3
! gatherving int(:,:,:,:)
private :: mp_gatherv_int4
! gatherving int(:,:,:,:,:)
private :: mp_gatherv_int5
! gatherving real(:)
private :: mp_gatherv_rdp1
! gatherving real(:,:)
private :: mp_gatherv_rdp2
! gatherving real(:,:,:)
private :: mp_gatherv_rdp3
! gatherving real(:,:,:,:)
private :: mp_gatherv_rdp4
! gatherving real(:,:,:,:,:)
private :: mp_gatherv_rdp5
! gatherving complex(:)
private :: mp_gatherv_cdp1
! gatherving complex(:,:)
private :: mp_gatherv_cdp2
! gatherving complex(:,:,:)
private :: mp_gatherv_cdp3
! gatherving complex(:,:,:,:)
private :: mp_gatherv_cdp4
! gatherving complex(:,:,:,:,:)
private :: mp_gatherv_cdp5
!>>> allgathering operations
! allgathering int(:)
private :: mp_allgather_int1
! allgathering int(:,:)
private :: mp_allgather_int2
! allgathering int(:,:,:)
private :: mp_allgather_int3
! allgathering int(:,:,:,:)
private :: mp_allgather_int4
! allgathering int(:,:,:,:,:)
private :: mp_allgather_int5
! allgathering real(:)
private :: mp_allgather_rdp1
! allgathering real(:,:)
private :: mp_allgather_rdp2
! allgathering real(:,:,:)
private :: mp_allgather_rdp3
! allgathering real(:,:,:,:)
private :: mp_allgather_rdp4
! allgathering real(:,:,:,:,:)
private :: mp_allgather_rdp5
! allgathering complex(:)
private :: mp_allgather_cdp1
! allgathering complex(:,:)
private :: mp_allgather_cdp2
! allgathering complex(:,:,:)
private :: mp_allgather_cdp3
! allgathering complex(:,:,:,:)
private :: mp_allgather_cdp4
! allgathering complex(:,:,:,:,:)
private :: mp_allgather_cdp5
!>>> allgatherving operations
! allgatherving int(:)
private :: mp_allgatherv_int1
! allgatherving int(:,:)
private :: mp_allgatherv_int2
! allgatherving int(:,:,:)
private :: mp_allgatherv_int3
! allgatherving int(:,:,:,:)
private :: mp_allgatherv_int4
! allgatherving int(:,:,:,:,:)
private :: mp_allgatherv_int5
! allgatherving real(:)
private :: mp_allgatherv_rdp1
! allgatherving real(:,:)
private :: mp_allgatherv_rdp2
! allgatherving real(:,:,:)
private :: mp_allgatherv_rdp3
! allgatherving real(:,:,:,:)
private :: mp_allgatherv_rdp4
! allgatherving real(:,:,:,:,:)
private :: mp_allgatherv_rdp5
! allgatherving complex(:)
private :: mp_allgatherv_cdp1
! allgatherving complex(:,:)
private :: mp_allgatherv_cdp2
! allgatherving complex(:,:,:)
private :: mp_allgatherv_cdp3
! allgatherving complex(:,:,:,:)
private :: mp_allgatherv_cdp4
! allgatherving complex(:,:,:,:,:)
private :: mp_allgatherv_cdp5
!>>> reducing operations
! readucing int
private :: mp_reduce_int
! reducing int(:)
private :: mp_reduce_int1
! reducing int(:,:)
private :: mp_reduce_int2
! reducing int(:,:,:)
private :: mp_reduce_int3
! reducing int(:,:,:,:)
private :: mp_reduce_int4
! reducing int(:,:,:,:,:)
private :: mp_reduce_int5
! reducing real
private :: mp_reduce_rdp
! reducing real(:)
private :: mp_reduce_rdp1
! reducing real(:,:)
private :: mp_reduce_rdp2
! reducing real(:,:,:)
private :: mp_reduce_rdp3
! reducing real(:,:,:,:)
private :: mp_reduce_rdp4
! reducing real(:,:,:,:,:)
private :: mp_reduce_rdp5
! reducing complex
private :: mp_reduce_cdp
! reducing complex(:)
private :: mp_reduce_cdp1
! reducing complex(:,:)
private :: mp_reduce_cdp2
! reducing complex(:,:,:)
private :: mp_reduce_cdp3
! reducing complex(:,:,:,:)
private :: mp_reduce_cdp4
! reducing complex(:,:,:,:,:)
private :: mp_reduce_cdp5
!>>> allreducing operations
! allreducing int
private :: mp_allreduce_int
! allreducing int(:)
private :: mp_allreduce_int1
! allreducing int(:,:)
private :: mp_allreduce_int2
! allreducing int(:,:,:)
private :: mp_allreduce_int3
! allreducing int(:,:,:,:)
private :: mp_allreduce_int4
! allreducing int(:,:,:,:,:)
private :: mp_allreduce_int5
! allreducing real
private :: mp_allreduce_rdp
! allreducing real(:)
private :: mp_allreduce_rdp1
! allreducing real(:,:)
private :: mp_allreduce_rdp2
! allreducing real(:,:,:)
private :: mp_allreduce_rdp3
! allreducing real(:,:,:,:)
private :: mp_allreduce_rdp4
! allreducing real(:,:,:,:,:)
private :: mp_allreduce_rdp5
! allreducing complex
private :: mp_allreduce_cdp
! allreducing complex(:)
private :: mp_allreduce_cdp1
! allreducing complex(:,:)
private :: mp_allreduce_cdp2
! allreducing complex(:,:,:)
private :: mp_allreduce_cdp3
! allreducing complex(:,:,:,:)
private :: mp_allreduce_cdp4
! allreducing complex(:,:,:,:,:)
private :: mp_allreduce_cdp5
!>>> error handler
! echo the error information
private :: mp_error
!-------------------------------------------------------------------------
!::: declare interface and module procedure :::
!-------------------------------------------------------------------------
! we define these interfaces to implement the so called "generic" software
! engineering technique
! mpi_bcast subroutines
public :: mp_bcast
interface mp_bcast
module procedure mp_bcast_bool
module procedure mp_bcast_bool1
module procedure mp_bcast_bool2
module procedure mp_bcast_int
module procedure mp_bcast_int1
module procedure mp_bcast_int2
module procedure mp_bcast_int3
module procedure mp_bcast_int4
module procedure mp_bcast_int5
module procedure mp_bcast_rdp
module procedure mp_bcast_rdp1
module procedure mp_bcast_rdp2
module procedure mp_bcast_rdp3
module procedure mp_bcast_rdp4
module procedure mp_bcast_rdp5
module procedure mp_bcast_cdp
module procedure mp_bcast_cdp1
module procedure mp_bcast_cdp2
module procedure mp_bcast_cdp3
module procedure mp_bcast_cdp4
module procedure mp_bcast_cdp5
end interface mp_bcast
! mpi_gather subroutines
public :: mp_gather
interface mp_gather
module procedure mp_gather_int1
module procedure mp_gather_int2
module procedure mp_gather_int3
module procedure mp_gather_int4
module procedure mp_gather_int5
module procedure mp_gather_rdp1
module procedure mp_gather_rdp2
module procedure mp_gather_rdp3
module procedure mp_gather_rdp4
module procedure mp_gather_rdp5
module procedure mp_gather_cdp1
module procedure mp_gather_cdp2
module procedure mp_gather_cdp3
module procedure mp_gather_cdp4
module procedure mp_gather_cdp5
end interface mp_gather
! mpi_gatherv subroutines
public :: mp_gatherv
interface mp_gatherv
module procedure mp_gatherv_int1
module procedure mp_gatherv_int2
module procedure mp_gatherv_int3
module procedure mp_gatherv_int4
module procedure mp_gatherv_int5
module procedure mp_gatherv_rdp1
module procedure mp_gatherv_rdp2
module procedure mp_gatherv_rdp3
module procedure mp_gatherv_rdp4
module procedure mp_gatherv_rdp5
module procedure mp_gatherv_cdp1
module procedure mp_gatherv_cdp2
module procedure mp_gatherv_cdp3
module procedure mp_gatherv_cdp4
module procedure mp_gatherv_cdp5
end interface mp_gatherv
! mpi_allgather subroutines
public :: mp_allgather
interface mp_allgather
module procedure mp_allgather_int1
module procedure mp_allgather_int2
module procedure mp_allgather_int3
module procedure mp_allgather_int4
module procedure mp_allgather_int5
module procedure mp_allgather_rdp1
module procedure mp_allgather_rdp2
module procedure mp_allgather_rdp3
module procedure mp_allgather_rdp4
module procedure mp_allgather_rdp5
module procedure mp_allgather_cdp1
module procedure mp_allgather_cdp2
module procedure mp_allgather_cdp3
module procedure mp_allgather_cdp4
module procedure mp_allgather_cdp5
end interface mp_allgather
! mpi_allgatherv subroutines
public :: mp_allgatherv
interface mp_allgatherv
module procedure mp_allgatherv_int1
module procedure mp_allgatherv_int2
module procedure mp_allgatherv_int3
module procedure mp_allgatherv_int4
module procedure mp_allgatherv_int5
module procedure mp_allgatherv_rdp1
module procedure mp_allgatherv_rdp2
module procedure mp_allgatherv_rdp3
module procedure mp_allgatherv_rdp4
module procedure mp_allgatherv_rdp5
module procedure mp_allgatherv_cdp1
module procedure mp_allgatherv_cdp2
module procedure mp_allgatherv_cdp3
module procedure mp_allgatherv_cdp4
module procedure mp_allgatherv_cdp5
end interface mp_allgatherv
! mpi_reduce subroutines
public :: mp_reduce
interface mp_reduce
module procedure mp_reduce_int
module procedure mp_reduce_int1
module procedure mp_reduce_int2
module procedure mp_reduce_int3
module procedure mp_reduce_int4
module procedure mp_reduce_int5
module procedure mp_reduce_rdp
module procedure mp_reduce_rdp1
module procedure mp_reduce_rdp2
module procedure mp_reduce_rdp3
module procedure mp_reduce_rdp4
module procedure mp_reduce_rdp5
module procedure mp_reduce_cdp
module procedure mp_reduce_cdp1
module procedure mp_reduce_cdp2
module procedure mp_reduce_cdp3
module procedure mp_reduce_cdp4
module procedure mp_reduce_cdp5
end interface mp_reduce
! mpi_allreduce subroutines
public :: mp_allreduce
interface mp_allreduce
module procedure mp_allreduce_int
module procedure mp_allreduce_int1
module procedure mp_allreduce_int2
module procedure mp_allreduce_int3
module procedure mp_allreduce_int4
module procedure mp_allreduce_int5
module procedure mp_allreduce_rdp
module procedure mp_allreduce_rdp1
module procedure mp_allreduce_rdp2
module procedure mp_allreduce_rdp3
module procedure mp_allreduce_rdp4
module procedure mp_allreduce_rdp5
module procedure mp_allreduce_cdp
module procedure mp_allreduce_cdp1
module procedure mp_allreduce_cdp2
module procedure mp_allreduce_cdp3
module procedure mp_allreduce_cdp4
module procedure mp_allreduce_cdp5
end interface mp_allreduce
contains
!-------------------------------------------------------------------------
!::: MPI information operations :::
!-------------------------------------------------------------------------
! mp_info: return the current information about mpi environment
subroutine mp_info()
implicit none
# if defined (OMPI)
# define STR_MPI 'Good news, your compiler is mpi-compatible (openmpi).'
write(mystd,'(a)') STR_MPI
# else /* OMPI */
# define STR_MPI 'Good news, your compiler is mpi-compatible (mpich).'
write(mystd,'(a)') STR_MPI
# endif /* OMPI */
return
end subroutine mp_info
!-------------------------------------------------------------------------
!::: MPI initialize and finalize operations :::
!-------------------------------------------------------------------------
! mp_init: initialize mpi environment
subroutine mp_init()
implicit none
! invoke related mpi subroutines
call MPI_INIT(ierror)
! handler for return code
call mp_error('mp_init', ierror)
return
end subroutine mp_init
! mp_finalize: finalize mpi environment
subroutine mp_finalize()
implicit none
! invoke related mpi subroutines
call MPI_FINALIZE(ierror)
! handler for return code
call mp_error('mp_finalize', ierror)
return
end subroutine mp_finalize
!-------------------------------------------------------------------------
!::: MPI setup operations :::
!-------------------------------------------------------------------------
! mp_comm_rank: determine the rank of the current process
subroutine mp_comm_rank(myid, gid)
implicit none
! external arguments
integer, intent(out) :: myid
integer, optional, intent(in) :: gid
! set current communicator
if ( present(gid) .eqv. .true. ) then
group = gid
else
group = MPI_COMM_WORLD
endif
! invoke related mpi subroutines
call MPI_COMM_RANK(group, myid, ierror)
! handler for return code
call mp_error('mp_comm_rank', ierror)
return
end subroutine mp_comm_rank
! mp_comm_size: evaluate the number of processes in current communicator
subroutine mp_comm_size(nprocs, gid)
implicit none
! external arguments
integer, intent(out) :: nprocs
integer, optional, intent(in) :: gid
! set current communicator
if ( present(gid) .eqv. .true. ) then
group = gid
else
group = MPI_COMM_WORLD
endif
! invoke related mpi subroutines
call MPI_COMM_SIZE(group, nprocs, ierror)
! handler for return code
call mp_error('mp_comm_size', ierror)
return
end subroutine mp_comm_size
! mp_processor: determine the current workstation's name
subroutine mp_processor(workstation)
implicit none
! external arguments
character(len=MPI_MAX_PROCESSOR_NAME), intent(out) :: workstation
! invoke related mpi subroutines
call MPI_GET_PROCESSOR_NAME(workstation, istat, ierror)
! handler for return code
call mp_error('mp_processor', ierror)
return
end subroutine mp_processor
!-------------------------------------------------------------------------
!::: MPI cartesian topology operations :::
!-------------------------------------------------------------------------
! mp_dims_create: creates a division of processors in a cartesian grid
subroutine mp_dims_create(nprocs, dims)
implicit none
! external arguments
integer, intent(in) :: nprocs
integer, intent(inout) :: dims(ndims)
! invoke related mpi subroutines
call MPI_DIMS_CREATE(nprocs, ndims, dims, ierror)
! handler for return code
call mp_error('mp_dims_create', ierror)
return
end subroutine mp_dims_create
! mp_cart_create: makes a new communicator to which topology is cartesian
subroutine mp_cart_create(dims)
implicit none
! external arguments
integer, intent(in) :: dims(ndims)
! invoke related mpi subroutines
! note: mpi_comm_cart should be overwriten in output
call MPI_CART_CREATE(MPI_COMM_WORLD, ndims, dims, periods, reorder, mpi_comm_cart, ierror)
! handler for return code
call mp_error('mp_cart_create', ierror)
return
end subroutine mp_cart_create
! mp_cart_coords: determines process coords in cartesian topology
subroutine mp_cart_coords(myid, cx, cy)
implicit none
! external arguments
integer, intent(in) :: myid
integer, intent(out) :: cx
integer, intent(out) :: cy
! local variables
integer :: coords(ndims)
! invoke related mpi subroutines
call MPI_CART_COORDS(mpi_comm_cart, myid, ndims, coords, ierror)
! copy coordinates to cx and cy, in principle, the dimension of coords
! is ndims (now ndims is equal to 2)
cx = coords(1)
cy = coords(2)
! handler for return code
call mp_error('mp_cart_coords', ierror)
return
end subroutine mp_cart_coords
! mp_comm_split_row: creates new communicators based on colors and keys
subroutine mp_comm_split_row(color, key)
implicit none
! external arguments
integer :: color
integer :: key
! invoke related mpi subroutines
! note: mpi_comm_row should be overwritten in output
call MPI_COMM_SPLIT(mpi_comm_cart, color, key, mpi_comm_row, ierror)
! handler for return code
call mp_error('mp_comm_split_row', ierror)
return
end subroutine mp_comm_split_row
! mp_comm_split_col: creates new communicators based on colors and keys
subroutine mp_comm_split_col(color, key)
implicit none
! external arguments
integer :: color
integer :: key
! invoke related mpi subroutines
! note: mpi_comm_col should be overwritten in output
call MPI_COMM_SPLIT(mpi_comm_cart, color, key, mpi_comm_col, ierror)
! handler for return code
call mp_error('mp_comm_split_col', ierror)
return
end subroutine mp_comm_split_col
!-------------------------------------------------------------------------
!::: MPI barrier operations :::
!-------------------------------------------------------------------------
! mp_barrier: blocks until all process have reached this routine
subroutine mp_barrier(gid)
implicit none
! external arguments
integer, optional, intent(in) :: gid
! set current communicator
if ( present(gid) .eqv. .true. ) then
group = gid
else
group = MPI_COMM_WORLD
endif
! invoke related mpi subroutines
call MPI_BARRIER(group, ierror)
! handler for return code
call mp_error('mp_barrier', ierror)
return
end subroutine mp_barrier
!-------------------------------------------------------------------------
!::: MPI time operations :::
!-------------------------------------------------------------------------
! mp_wtime: returns an elapsed time on the calling processor
subroutine mp_wtime(time)
implicit none
! external arguments
real(dp), intent(out) :: time
! invoke related mpi subroutines