Debian Codegraph @

src/mbd_mpi.F90

raw
1 ! This Source Code Form is subject to the terms of the Mozilla Public
2 ! License, v. 2.0. If a copy of the MPL was not distributed with this
3 ! file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 module mbd_mpi
5
6 use mbd_constants, only: dp
7 #ifndef WITH_MPIFH
8 # ifndef WITH_MPIF08
9 use mpi
10 # else
11 use mpi_f08
12 # endif
13 #endif
14
15 implicit none
16
17 #ifdef WITH_MPIFH
18 include 'mpif.h'
19 #endif
20
21 private :: dp
22
23 interface mpi_all_reduce
24 module procedure mpi_all_reduce_real_0d
25 module procedure mpi_all_reduce_real_1d
26 module procedure mpi_all_reduce_real_2d
27 end interface
28
29 contains
30
31 subroutine mpi_all_reduce_real_0d(x, comm)
32 real(dp), intent(inout) :: x
33 #ifdef WITH_MPIF08
34 type(MPI_Comm), intent(in) :: comm
35 #else
36 integer, intent(in) :: comm
37 #endif
38
39 real(dp) :: x_arr(1), x_buffer(1)
40 integer :: ierr
41
42 x_arr(1) = x
43 call MPI_ALLREDUCE(x_arr, x_buffer, 1, MPI_DOUBLE_PRECISION, MPI_SUM, comm, ierr)
44 x = x_buffer(1)
45 end subroutine
46
47 subroutine mpi_all_reduce_real_1d(x, comm)
48 real(dp), intent(inout) :: x(:)
49 #ifdef WITH_MPIF08
50 type(MPI_Comm), intent(in) :: comm
51 #else
52 integer, intent(in) :: comm
53 #endif
54
55 real(dp), allocatable :: x_buffer(:)
56 integer :: ierr
57
58 allocate (x_buffer(size(x)))
59 call MPI_ALLREDUCE(x, x_buffer, size(x), MPI_DOUBLE_PRECISION, MPI_SUM, comm, ierr)
60 x = x_buffer
61 end subroutine
62
63 subroutine mpi_all_reduce_real_2d(x, comm)
64 real(dp), contiguous, target, intent(inout) :: x(:, :)
65 #ifdef WITH_MPIF08
66 type(MPI_Comm), intent(in) :: comm
67 #else
68 integer, intent(in) :: comm
69 #endif
70
71 real(dp), pointer :: x_p(:)
72
73 x_p(1:size(x)) => x
74 call mpi_all_reduce_real_1d(x_p, comm)
75 end subroutine
76
77 integer function mpi_get_rank() result(rank)
78 integer :: err
79
80 call MPI_COMM_RANK(MPI_COMM_WORLD, rank, err)
81 end function
82
83 end module