forked from wannier-developers/wannier90
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomms.F90
1940 lines (1524 loc) · 70.7 KB
/
comms.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
!-*- mode: F90 -*-!
!------------------------------------------------------------!
! This file is distributed as part of the Wannier90 code and !
! under the terms of the GNU General Public License. See the !
! file `LICENSE' in the root directory of the Wannier90 !
! distribution, or http://www.gnu.org/copyleft/gpl.txt !
! !
! The webpage of the Wannier90 code is www.wannier.org !
! !
! The Wannier90 code is hosted on GitHub: !
! !
! https://github.com/wannier-developers/wannier90 !
!------------------------------------------------------------!
! !
! COMMS: set of MPI wrappers !
! written 2006-2012 Jonathan R. Yates !
! later additions Giovanni Pizzi !
! !
!------------------------------------------------------------!
module w90_comms
!! This module handles all of the communications
use w90_constants, only: dp
use w90_error_base
#ifdef MPI
# if !(defined(MPI08) || defined(MPI90) || defined(MPIH))
# error "You need to define which MPI interface you are using"
# endif
#else
#define MPI_COMM_NULL -1
#endif
#ifdef MPI08
use mpi_f08 ! use f08 interface if possible
#endif
#ifdef MPI90
use mpi ! next best, use fortran90 interface
#endif
implicit none
#ifdef MPIH
include 'mpif.h' ! worst case, use legacy interface
#endif
private
integer, parameter :: code_mpi = 4 ! this is duplicated here to avoid circular dependency with error.F90
integer, parameter :: mpi_send_tag = 77 ! arbitrary
integer, parameter :: root_id = 0 ! not arbitrary
public :: comms_allreduce ! reduce data onto all nodes
public :: comms_array_split
public :: comms_barrier ! puts a barrier so that the code goes on only when all nodes reach the barrier
public :: comms_bcast ! send data from the root node
public :: comms_gatherv ! gets chunks of an array from all nodes and gathers them on the root node
!public :: comms_recv ! accept data from one node to another
public :: comms_reduce ! reduce data onto root node (n.b. not allreduce); data is lost on all other nodes
public :: comms_scatterv ! sends chunks of an array to all nodes scattering them from the root node
!public :: comms_send ! send data from one node to another
public :: mpirank
public :: mpisize
public :: comms_sync_error
public :: valid_communicator
!! test whether communicator is initialised; returns true always for serial build
! versions without error synchronisation, use at own risk
public :: comms_no_sync_allreduce ! reduce data onto all nodes
public :: comms_no_sync_barrier ! puts a barrier so that the code goes on only when all nodes reach the barrier
public :: comms_no_sync_bcast ! send data from the root node
public :: comms_no_sync_gatherv ! gets chunks of an array from all nodes and gathers them on the root node
public :: comms_no_sync_recv ! accept data from one node to another
public :: comms_no_sync_reduce ! reduce data onto root node (n.b. not allreduce); data is lost on all other nodes
public :: comms_no_sync_scatterv ! sends chunks of an array to all nodes scattering them from the root node
public :: comms_no_sync_send ! send data from one node to another
type, public :: w90_comm_type
#ifdef MPI08
type(mpi_comm) :: comm = MPI_COMM_NULL ! f08 mpi interface
#else
integer :: comm = MPI_COMM_NULL ! f90 mpi or no mpi
#endif
end type
type, private :: w90stat_type
#ifdef MPI08
type(mpi_status) :: stat ! f08 mpi interface
#elif MPI90
integer :: stat(MPI_STATUS_SIZE)
#else
integer :: stat ! not used
#endif
end type
interface comms_bcast
module procedure comms_bcast_int
module procedure comms_bcast_logical
module procedure comms_bcast_real
module procedure comms_bcast_cmplx
module procedure comms_bcast_char
end interface comms_bcast
! We don't currently have error synchronisation for point-to-point comms
!interface comms_send
! module procedure comms_send_int
! module procedure comms_send_logical
! module procedure comms_send_real
! module procedure comms_send_cmplx
! module procedure comms_send_char
!end interface comms_send
!interface comms_recv
! module procedure comms_recv_int
! module procedure comms_recv_logical
! module procedure comms_recv_real
! module procedure comms_recv_cmplx
! module procedure comms_recv_char
!end interface comms_recv
interface comms_reduce
module procedure comms_reduce_int
module procedure comms_reduce_real
module procedure comms_reduce_cmplx
end interface comms_reduce
interface comms_allreduce
! module procedure comms_allreduce_int ! to be done
module procedure comms_allreduce_real
module procedure comms_allreduce_cmplx
end interface comms_allreduce
interface comms_gatherv
! module procedure comms_gatherv_int ! to be done
module procedure comms_gatherv_logical
module procedure comms_gatherv_real_1
module procedure comms_gatherv_real_2
module procedure comms_gatherv_real_3
module procedure comms_gatherv_real_2_3
module procedure comms_gatherv_cmplx_1
module procedure comms_gatherv_cmplx_2
module procedure comms_gatherv_cmplx_3
module procedure comms_gatherv_cmplx_3_4
module procedure comms_gatherv_cmplx_4
end interface comms_gatherv
interface comms_scatterv
module procedure comms_scatterv_int_1
module procedure comms_scatterv_int_2
module procedure comms_scatterv_int_3
module procedure comms_scatterv_real_1
module procedure comms_scatterv_real_2
module procedure comms_scatterv_real_3
! module procedure comms_scatterv_cmplx
module procedure comms_scatterv_cmplx_4
end interface comms_scatterv
!! These routines do not synchronise errors. You should not normally use them.
!! If you do use them, you are required to make sure that the error conditions
!! between the MPI processes are correctly synced up on exit from your code.
interface comms_no_sync_bcast
module procedure comms_no_sync_bcast_int
module procedure comms_no_sync_bcast_logical
module procedure comms_no_sync_bcast_real
module procedure comms_no_sync_bcast_cmplx
module procedure comms_no_sync_bcast_char
end interface comms_no_sync_bcast
interface comms_no_sync_send
module procedure comms_no_sync_send_int
module procedure comms_no_sync_send_logical
module procedure comms_no_sync_send_real
module procedure comms_no_sync_send_cmplx
module procedure comms_no_sync_send_char
end interface comms_no_sync_send
interface comms_no_sync_recv
module procedure comms_no_sync_recv_int
module procedure comms_no_sync_recv_logical
module procedure comms_no_sync_recv_real
module procedure comms_no_sync_recv_cmplx
module procedure comms_no_sync_recv_char
end interface comms_no_sync_recv
interface comms_no_sync_reduce
module procedure comms_no_sync_reduce_int
module procedure comms_no_sync_reduce_real
module procedure comms_no_sync_reduce_cmplx
end interface comms_no_sync_reduce
interface comms_no_sync_allreduce
! module procedure comms_no_sync_allreduce_int ! to be done
module procedure comms_no_sync_allreduce_real
module procedure comms_no_sync_allreduce_cmplx
end interface comms_no_sync_allreduce
interface comms_no_sync_gatherv
! module procedure comms_no_sync_gatherv_int ! to be done
module procedure comms_no_sync_gatherv_logical
module procedure comms_no_sync_gatherv_real_1
module procedure comms_no_sync_gatherv_real_2
module procedure comms_no_sync_gatherv_real_3
module procedure comms_no_sync_gatherv_real_2_3
module procedure comms_no_sync_gatherv_cmplx_1
module procedure comms_no_sync_gatherv_cmplx_2
module procedure comms_no_sync_gatherv_cmplx_3
module procedure comms_no_sync_gatherv_cmplx_3_4
module procedure comms_no_sync_gatherv_cmplx_4
end interface comms_no_sync_gatherv
interface comms_no_sync_scatterv
module procedure comms_no_sync_scatterv_int_1
module procedure comms_no_sync_scatterv_int_2
module procedure comms_no_sync_scatterv_int_3
module procedure comms_no_sync_scatterv_real_1
module procedure comms_no_sync_scatterv_real_2
module procedure comms_no_sync_scatterv_real_3
! module procedure comms_no_sync_scatterv_cmplx
module procedure comms_no_sync_scatterv_cmplx_4
end interface comms_no_sync_scatterv
contains
logical function valid_communicator(comm)
type(w90_comm_type), intent(in) :: comm
#ifdef MPI
if (comm%comm == MPI_COMM_NULL) then
valid_communicator = .false.
else
valid_communicator = .true.
endif
#else
valid_communicator = .true.
#endif
end function
! mpi rank function for convenience
integer function mpirank(comm)
type(w90_comm_type), intent(in) :: comm
integer :: ierr
#ifdef MPI
call mpi_comm_rank(comm%comm, mpirank, ierr)
#else
mpirank = 0
#endif
end function
! mpi size function for convenience
integer function mpisize(comm)
type(w90_comm_type), intent(in) :: comm
integer :: ierr
#ifdef MPI
call mpi_comm_size(comm%comm, mpisize, ierr)
#else
mpisize = 1
#endif
end function
! synchronise error condition between MPI processess
subroutine comms_sync_error(comm, error, ierr)
implicit none
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(inout) :: error
integer :: ierr, mpiierr, abserr
#if defined(MPI) && !defined(DISABLE_ERROR_SYNC)
abserr = abs(ierr) ! possibility of -ve values, use abs for safety
call mpi_allreduce(MPI_IN_PLACE, abserr, 1, MPI_INTEGER, MPI_SUM, comm%comm, mpiierr)
! you could check mpiierr here, but truly all bets are off in that case
if (abserr > 0 .and. ierr == 0) call recv_error(error)
#endif
end subroutine comms_sync_error
subroutine comms_array_split(numpoints, counts, displs, comm)
!! Given an array of size numpoints, we want to split on num_nodes nodes. This function returns
!! two arrays: count and displs.
!!
!! The i-th element of the count array gives the number of elements
!! that must be calculated by the process with id (i-1).
!! The i-th element of the displs array gives the displacement of the array calculated locally on
!! the process with id (i-1) with respect to the global array.
!!
!! These values are those to be passed to the functions MPI_Scatterv, MPI_Gatherv and MPI_Alltoallv.
!!
!! one can use the following do loop to run over the needed elements, if the full array is stored
!! on all nodes:
!! do i=displs(my_node_id)+1,displs(my_node_id)+counts(my_node_id)
!!
integer, intent(in) :: numpoints !! Number of elements of the array to be scattered
integer, intent(inout) :: counts(0:) !! Array (of size num_nodes) with the number of elements of the array on each node
integer, intent(inout) :: displs(0:) !! Array (of size num_nodes) with the displacement relative to the global array
type(w90_comm_type), intent(in) :: comm
integer :: ratio, remainder, i
integer :: num_nodes
num_nodes = mpisize(comm)
ratio = numpoints/num_nodes
remainder = MOD(numpoints, num_nodes)
do i = 0, num_nodes - 1
if (i < remainder) then
counts(i) = ratio + 1
displs(i) = i*(ratio + 1)
else
counts(i) = ratio
displs(i) = remainder*(ratio + 1) + (i - remainder)*ratio
end if
end do
end subroutine comms_array_split
subroutine comms_no_sync_barrier(comm)
!! A barrier to synchronise all nodes
implicit none
type(w90_comm_type), intent(in) :: comm
#ifdef MPI
integer :: ierr
call mpi_barrier(comm%comm, ierr)
#endif
end subroutine comms_no_sync_barrier
subroutine comms_no_sync_bcast_int(array, size, error, comm)
!! Send integar array from root node to all nodes
implicit none
integer, intent(inout) :: array
integer, intent(in) :: size
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_bcast(array, size, MPI_INTEGER, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_bcast_int', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_bcast_int
subroutine comms_no_sync_bcast_real(array, size, error, comm)
!! Send real array from root node to all nodes
implicit none
real(kind=dp), intent(inout) :: array
integer, intent(in) :: size
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_bcast(array, size, MPI_DOUBLE_PRECISION, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_bcast_real', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_bcast_real
subroutine comms_no_sync_bcast_logical(array, size, error, comm)
!! Send logical array from root node to all nodes
implicit none
logical, intent(inout) :: array
integer, intent(in) :: size
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_bcast(array, size, MPI_LOGICAL, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_bcast_logical', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_bcast_logical
subroutine comms_no_sync_bcast_char(array, size, error, comm)
!! Send character array from root node to all nodes
implicit none
character(len=*), intent(inout) :: array
integer, intent(in) :: size
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_bcast(array, size, MPI_CHARACTER, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_bcast_char', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_bcast_char
subroutine comms_no_sync_bcast_cmplx(array, size, error, comm)
!! Send character array from root node to all nodes
implicit none
complex(kind=dp), intent(inout) :: array
integer, intent(in) :: size
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_bcast(array, size, MPI_DOUBLE_COMPLEX, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_bcast_cmplx', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_bcast_cmplx
!--------- SEND ----------------
subroutine comms_no_sync_send_logical(array, size, to, error, comm)
!! Send logical array to specified node
implicit none
logical, intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: to
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_send(array, size, MPI_LOGICAL, to, mpi_send_tag, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_send_logical', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_send_logical
subroutine comms_no_sync_send_int(array, size, to, error, comm)
!! Send integer array to specified node
implicit none
integer, intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: to
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_send(array, size, MPI_INTEGER, to, mpi_send_tag, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_send_int', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_send_int
subroutine comms_no_sync_send_char(array, size, to, error, comm)
!! Send character array to specified node
implicit none
character(len=*), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: to
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_send(array, size, MPI_CHARACTER, to, mpi_send_tag, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_send_char', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_send_char
subroutine comms_no_sync_send_real(array, size, to, error, comm)
!! Send real array to specified node
implicit none
real(kind=dp), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: to
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_send(array, size, MPI_DOUBLE_PRECISION, to, mpi_send_tag, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_send_real', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_send_real
subroutine comms_no_sync_send_cmplx(array, size, to, error, comm)
!! Send complex array to specified node
implicit none
complex(kind=dp), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: to
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_send(array, size, MPI_DOUBLE_COMPLEX, to, mpi_send_tag, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_send_cmplx', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_send_cmplx
!--------- RECV ----------------
subroutine comms_no_sync_recv_logical(array, size, from, error, comm)
!! Receive logical array from specified node
implicit none
logical, intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: from
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
type(w90stat_type) :: status
integer :: ierr
call mpi_recv(array, size, MPI_LOGICAL, from, mpi_send_tag, comm%comm, status%stat, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_recv_logical', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_recv_logical
subroutine comms_no_sync_recv_int(array, size, from, error, comm)
!! Receive integer array from specified node
implicit none
integer, intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: from
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
type(w90stat_type) :: status
integer :: ierr
call mpi_recv(array, size, MPI_INTEGER, from, mpi_send_tag, comm%comm, status%stat, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_recv_int', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_recv_int
subroutine comms_no_sync_recv_char(array, size, from, error, comm)
!! Receive character array from specified node
implicit none
character(len=*), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: from
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
type(w90stat_type) :: status
integer :: ierr
call mpi_recv(array, size, MPI_CHARACTER, from, mpi_send_tag, comm%comm, status%stat, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_recv_char', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_recv_char
subroutine comms_no_sync_recv_real(array, size, from, error, comm)
!! Receive real array from specified node
implicit none
real(kind=dp), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: from
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
type(w90stat_type) :: status
integer :: ierr
call mpi_recv(array, size, MPI_DOUBLE_PRECISION, from, mpi_send_tag, comm%comm, &
status%stat, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_recv_real', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_recv_real
subroutine comms_no_sync_recv_cmplx(array, size, from, error, comm)
!! Receive complex array from specified node
implicit none
complex(kind=dp), intent(inout) :: array
integer, intent(in) :: size
integer, intent(in) :: from
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
type(w90stat_type) :: status
integer :: ierr
call mpi_recv(array, size, MPI_DOUBLE_COMPLEX, from, mpi_send_tag, comm%comm, &
status%stat, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_recv_cmplx', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_recv_cmplx
subroutine comms_no_sync_reduce_int(array, size, op, error, comm)
!! Reduce integer data to root node
implicit none
integer, intent(inout) :: array
integer, intent(in) :: size
character(len=*), intent(in) :: op
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
integer :: rank
rank = mpirank(comm)
! note, JJ 23/2/2021
! previously this routine alloc'd/used/dealloc'd a temp array
! to be used as receive buffer for MPI_reduce
! this temp array was then copied to argument "array"
! but: "array" needs to be of scalar type for the polymorphism to work
! so: need to copy array into a (fake) scalar
! previously: a subroutine my_icopy was used to help to do this.
! probably just reducing in place is better?
select case (op)
case ('SUM')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_INTEGER, MPI_SUM, root_id, comm%comm, &
ierr)
else
call mpi_reduce(array, array, size, MPI_INTEGER, MPI_SUM, root_id, comm%comm, ierr)
endif
case ('PRD')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_INTEGER, MPI_PROD, root_id, comm%comm, &
ierr)
else
call mpi_reduce(array, array, size, MPI_INTEGER, MPI_PROD, root_id, comm%comm, ierr)
endif
case default
call set_base_error(error, 'Unknown operation in comms_reduce_int', code_mpi)
return
end select
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_reduce_int', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_reduce_int
subroutine comms_no_sync_reduce_real(array, size, op, error, comm)
!! Reduce real data to root node
implicit none
real(kind=dp), intent(inout) :: array
integer, intent(in) :: size
character(len=*), intent(in) :: op
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
integer :: rank
rank = mpirank(comm)
select case (op)
case ('SUM')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_SUM, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_PRECISION, MPI_SUM, root_id, comm%comm, &
ierr)
endif
case ('PRD')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_PROD, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_PRECISION, MPI_PROD, root_id, comm%comm, &
ierr)
endif
case ('MIN')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_MIN, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_PRECISION, MPI_MIN, root_id, comm%comm, &
ierr)
endif
case ('MAX')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_MAX, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_PRECISION, MPI_MAX, root_id, comm%comm, &
ierr)
endif
case default
call set_base_error(error, 'Unknown operation in comms_reduce_real', code_mpi)
return
end select
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_reduce_real', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_reduce_real
subroutine comms_no_sync_reduce_cmplx(array, size, op, error, comm)
!! Reduce complex data to root node
implicit none
complex(kind=dp), intent(inout) :: array
integer, intent(in) :: size
character(len=*), intent(in) :: op
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
integer :: rank
rank = mpirank(comm)
select case (op)
case ('SUM')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_COMPLEX, MPI_SUM, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_COMPLEX, MPI_SUM, root_id, comm%comm, &
ierr)
end if
case ('PRD')
if (rank == root_id) then
call mpi_reduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_COMPLEX, MPI_PROD, root_id, &
comm%comm, ierr)
else
call mpi_reduce(array, array, size, MPI_DOUBLE_COMPLEX, MPI_PROD, root_id, comm%comm, &
ierr)
end if
case default
call set_base_error(error, 'Unknown operation in comms_reduce_cmplx', code_mpi)
return
end select
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_reduce_cmplx', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_reduce_cmplx
subroutine comms_no_sync_allreduce_real(array, size, op, error, comm)
!! Reduce real data to all nodes
implicit none
real(kind=dp), intent(inout) :: array
integer, intent(in) :: size
character(len=*), intent(in) :: op
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
select case (op)
case ('SUM')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_SUM, comm%comm, &
ierr)
case ('PRD')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_PROD, comm%comm, &
ierr)
case ('MIN')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_MIN, comm%comm, &
ierr)
case ('MAX')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_PRECISION, MPI_MAX, comm%comm, &
ierr)
case default
call set_base_error(error, 'Unknown operation in comms_allreduce_real', code_mpi)
return
end select
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_allreduce_real', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_allreduce_real
subroutine comms_no_sync_allreduce_cmplx(array, size, op, error, comm)
!! Reduce complex data to all nodes
implicit none
complex(kind=dp), intent(inout) :: array
integer, intent(in) :: size
character(len=*), intent(in) :: op
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
select case (op)
case ('SUM')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_COMPLEX, MPI_SUM, comm%comm, &
ierr)
case ('PRD')
call mpi_allreduce(MPI_IN_PLACE, array, size, MPI_DOUBLE_COMPLEX, MPI_PROD, comm%comm, &
ierr)
case default
call set_base_error(error, 'Unknown operation in comms_allreduce_cmplx', code_mpi)
return
end select
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_allreduce_cmplx', code_mpi)
return
end if
#endif
end subroutine comms_no_sync_allreduce_cmplx
subroutine comms_no_sync_gatherv_real_1(array, localcount, rootglobalarray, counts, displs, error, comm)
!! Gather real data to root node (for arrays of rank 1)
implicit none
real(kind=dp), intent(inout) :: array(:) !! local array for sending data
integer, intent(in) :: localcount !! localcount elements will be sent to the root node
real(kind=dp), intent(inout) :: rootglobalarray(:) !! array on the root node to which data will be sent
integer, intent(in) :: counts(0:) !! how data should be partitioned, see MPI documentation or function comms_array_split
integer, intent(in) :: displs(0:)
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_gatherv(array, localcount, MPI_DOUBLE_PRECISION, rootglobalarray, counts, &
displs, MPI_DOUBLE_PRECISION, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_gatherv_real_1', code_mpi)
return
end if
#else
!call dcopy(localcount, array, 1, rootglobalarray, 1)
rootglobalarray = array
#endif
end subroutine comms_no_sync_gatherv_real_1
subroutine comms_no_sync_gatherv_real_2(array, localcount, rootglobalarray, counts, displs, error, comm)
!! Gather real data to root node (for arrays of rank 2)
implicit none
real(kind=dp), intent(inout) :: array(:, :) !! local array for sending data
integer, intent(in) :: localcount !! localcount elements will be sent to the root node
real(kind=dp), intent(inout) :: rootglobalarray(:, :) !! array on the root node to which data will be sent
integer, intent(in) :: counts(0:) !! how data should be partitioned, see MPI documentation or function comms_array_split
integer, intent(in) :: displs(0:)
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_gatherv(array, localcount, MPI_DOUBLE_PRECISION, rootglobalarray, counts, &
displs, MPI_DOUBLE_PRECISION, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then
call set_base_error(error, 'Error in comms_gatherv_real_2', code_mpi)
return
end if
#else
!call dcopy(localcount, array, 1, rootglobalarray, 1)
rootglobalarray = array
#endif
end subroutine comms_no_sync_gatherv_real_2
subroutine comms_no_sync_gatherv_real_3(array, localcount, rootglobalarray, counts, displs, error, comm)
!! Gather real data to root node (for arrays of rank 3)
implicit none
real(kind=dp), intent(inout) :: array(:, :, :) !! local array for sending data
integer, intent(in) :: localcount !! localcount elements will be sent to the root node
real(kind=dp), intent(inout) :: rootglobalarray(:, :, :) !! array on the root node to which data will be sent
integer, intent(in) :: counts(0:) !! how data should be partitioned, see MPI documentation or function comms_array_split
integer, intent(in) :: displs(0:)
type(w90_comm_type), intent(in) :: comm
type(w90_error_type), allocatable, intent(out) :: error
#ifdef MPI
integer :: ierr
call mpi_gatherv(array, localcount, MPI_DOUBLE_PRECISION, rootglobalarray, counts, &
displs, MPI_DOUBLE_PRECISION, root_id, comm%comm, ierr)
if (ierr .ne. MPI_SUCCESS) then