Neko 1.99.6
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
comm.F90
Go to the documentation of this file.
1module comm
2 use mpi_f08, only : mpi_comm, mpi_datatype, mpi_initialized, mpi_init_thread, &
3 mpi_init, mpi_thread_single, mpi_thread_funneled, &
4 mpi_thread_serialized, mpi_thread_multiple, mpi_comm_rank, &
5 mpi_comm_split, mpi_comm_dup, mpi_barrier, mpi_comm_free, mpi_finalize, &
6 mpi_comm_world, mpi_double_precision, mpi_real, mpi_comm_size, &
7 mpi_query_thread
8 use utils, only : neko_error
10 use shmem
11 !$ use omp_lib
12 implicit none
13 private
14
15 interface
16 subroutine neko_comm_wrapper_init(fcomm) &
17 bind(c, name='neko_comm_wrapper_init')
18 use, intrinsic :: iso_c_binding, only : c_int
19 integer(c_int), value :: fcomm
20 end subroutine neko_comm_wrapper_init
21
22#ifdef HAVE_NVSHMEM
23 subroutine neko_comm_nvshmem_init() &
24 bind(c, name='neko_comm_nvshmem_init')
25 end subroutine neko_comm_nvshmem_init
26
27 subroutine neko_comm_nvshmem_finalize() &
28 bind(c, name='neko_comm_nvshmem_finalize')
29 end subroutine neko_comm_nvshmem_finalize
30#endif
31
32#if defined(HAVE_NCCL) || defined(HAVE_RCCL)
33 subroutine neko_comm_nccl_init() &
34 bind(c, name='neko_comm_nccl_init')
35 end subroutine neko_comm_nccl_init
36
37 subroutine neko_comm_nccl_finalize() &
38 bind(c, name='neko_comm_nccl_finalize')
39 end subroutine neko_comm_nccl_finalize
40#endif
41
42 end interface
43
44
46 type(mpi_comm), public :: neko_comm
47 type(mpi_comm), public :: neko_global_comm
48
50#ifdef HAVE_MPI_PARAM_DTYPE
51 type(mpi_datatype), public, parameter :: mpi_real_precision = mpi_double_precision
52 type(mpi_datatype), public, parameter :: mpi_extra_precision = mpi_double_precision
53#else
54 type(mpi_datatype), public :: mpi_real_precision
55 type(mpi_datatype), public :: mpi_extra_precision
56#endif
57
59 integer, public :: pe_rank
60
62 integer, public :: pe_size
63
65 logical, public :: nio
66
68 integer, public :: global_pe_rank
69
71 integer, public :: global_pe_size
72
74 integer, public :: neko_mpi_thread_provided
75
76 public :: comm_init, comm_free
77
78contains
79 subroutine comm_init
80 integer :: ierr
81 logical :: initialized
82 integer :: provided, nthrds
83 integer :: color = 0
84 integer :: envvar_len
85 character(len=255) :: color_str
86 character(len=32) :: thread_str
87 integer :: thread_envvar_len
88 integer :: requested_thread_level
89 logical :: user_thread_level
90#ifdef HAVE_OPENSHMEM
91 integer :: shmem_ierr
92 integer :: shmem_requested
93#endif
94
95 pe_rank = -1
96 pe_size = 0
97 nio = .false.
98
99 call mpi_initialized(initialized, ierr)
100
101 call get_environment_variable("NEKO_COMM_ID", color_str, envvar_len)
102 if (envvar_len .gt. 0) then
103 read(color_str(1:envvar_len), *) color
104 else
105 color = 0
106 end if
107
108 nthrds = 1
109 !$omp parallel
110 !$omp master
111 !$ nthrds = omp_get_num_threads()
112 !$omp end master
113 !$omp end parallel
114
115 call get_environment_variable("NEKO_MPI_THREAD_LEVEL", thread_str, &
116 thread_envvar_len)
117 user_thread_level = thread_envvar_len .gt. 0
118 if (user_thread_level) then
119 select case (trim(adjustl(thread_str(1:thread_envvar_len))))
120 case ("single", "SINGLE")
121 requested_thread_level = mpi_thread_single
122 case ("funneled", "FUNNELED")
123 requested_thread_level = mpi_thread_funneled
124 case ("serialized", "SERIALIZED")
125 requested_thread_level = mpi_thread_serialized
126 case ("multiple", "MULTIPLE")
127 requested_thread_level = mpi_thread_multiple
128 case default
129 call neko_error('Unknown NEKO_MPI_THREAD_LEVEL: '// &
130 trim(thread_str(1:thread_envvar_len)))
131 end select
132 end if
133
134 if (.not.initialized) then
135 if (user_thread_level) then
136 if (requested_thread_level .eq. mpi_thread_single) then
137 call mpi_init(ierr)
138 provided = mpi_thread_single
139 else
140 call mpi_init_thread(requested_thread_level, provided, ierr)
141 if (provided .lt. requested_thread_level) then
142 call neko_error('Requested MPI thread level not provided')
143 end if
144 end if
145 else if (nthrds .gt. 1) then
146 call mpi_init_thread(mpi_thread_multiple, provided, ierr)
147 if (provided .lt. mpi_thread_multiple) then
148 ! MPI_THREAD_MULTIPLE is required for mt. device backends. For
149 ! host backends the gather-scatter MPI calls are issued from the
150 ! OpenMP master thread, so MPI_THREAD_SERIALIZED (or FUNNELED) is
151 ! sufficient as a last resort.
152 if (neko_bcknd_device .eq. 1) then
153 call neko_error('Invalid thread support provided by MPI')
154 else if (provided .lt. mpi_thread_funneled) then
155 call neko_error('Invalid thread support provided by MPI')
156 end if
157 end if
158 else
159 call mpi_init(ierr)
160 end if
161 else
162 call mpi_query_thread(provided, ierr)
163 end if
164
165 neko_mpi_thread_provided = provided
166
167#ifndef HAVE_MPI_PARAM_DTYPE
168 mpi_real_precision = mpi_double_precision
169 mpi_extra_precision = mpi_double_precision
170#endif
171
172
173#ifdef HAVE_ADIOS2
174 ! We split the communicator it to work asynchronously (MPMD)
175 call mpi_comm_rank(mpi_comm_world, pe_rank, ierr)
176 call mpi_comm_split(mpi_comm_world, 0, pe_rank, neko_global_comm, ierr)
177#else
178 ! Original version duplicates the communicator:
179 call mpi_comm_dup(mpi_comm_world, neko_global_comm, ierr)
180#endif
181
182 call mpi_comm_rank(neko_global_comm, global_pe_rank, ierr)
183 call mpi_comm_size(neko_global_comm, global_pe_size, ierr)
184 if (envvar_len .gt. 0) then
185 call mpi_comm_split(neko_global_comm, color, global_pe_rank, &
186 neko_comm, ierr)
187 else
188 call mpi_comm_dup(neko_global_comm, neko_comm, ierr)
189 end if
190 call mpi_comm_rank(neko_comm, pe_rank, ierr)
191 call mpi_comm_size(neko_comm, pe_size, ierr)
192
193 ! Setup C/C++ wrapper
195
196
197#ifdef HAVE_NVSHMEM
198 ! Setup NVSHMEM (if requested)
199 call neko_comm_nvshmem_init()
200#endif
201
202#if defined(HAVE_NCCL) | defined(HAVE_RCCL)
203 ! Setup NCCL (if requested)
204 call neko_comm_nccl_init()
205#endif
206
207#ifdef HAVE_OPENSHMEM
208 ! Setup OpenSHMEM (if requested)
209 if (user_thread_level) then
210 select case (requested_thread_level)
211 case (mpi_thread_single)
212 shmem_requested = shmem_thread_single
213 case (mpi_thread_funneled)
214 shmem_requested = shmem_thread_funneled
215 case (mpi_thread_serialized)
216 shmem_requested = shmem_thread_serialized
217 case (mpi_thread_multiple)
218 shmem_requested = shmem_thread_multiple
219 end select
220 if (shmem_requested .eq. shmem_thread_single) then
221 call shmem_init()
222 else
223 shmem_ierr = shmem_init_thread(shmem_requested, provided)
224 if (provided .lt. shmem_requested) then
225 call neko_error('Requested SHMEM thread level not provided')
226 end if
227 end if
228 else if (nthrds .gt. 1) then
229 shmem_ierr = shmem_init_thread(shmem_thread_multiple, provided)
230 if (provided .ne. shmem_thread_multiple) then
231 if (neko_bcknd_device .eq. 1) then
232 call neko_error('Invalid thread support provided by SHMEM')
233 else
234 shmem_ierr = shmem_init_thread(shmem_thread_serialized, provided)
235 if (provided .ne. shmem_thread_serialized) then
236 call neko_error('Invalid thread support provided by SHMEM')
237 end if
238 end if
239 end if
240 else
241 call shmem_init()
242 end if
243#endif
244
245
246 end subroutine comm_init
247
248 subroutine comm_free
249 integer :: ierr
250
251 call mpi_barrier(neko_comm, ierr)
252 call mpi_comm_free(neko_comm, ierr)
253 call mpi_comm_free(neko_global_comm, ierr)
254
255#ifdef HAVE_NCCL
256 call neko_comm_nccl_finalize()
257#endif
258
259#ifdef HAVE_NVSHMEM
260 call neko_comm_nvshmem_finalize()
261#endif
262
263#ifdef HAVE_OPENSHMEM
264 call shmem_finalize()
265#endif
266
267 call mpi_finalize(ierr)
268
269 end subroutine comm_free
270
271end module comm
Definition comm.F90:1
subroutine, public comm_free
Definition comm.F90:249
subroutine, public comm_init
Definition comm.F90:80
logical, public nio
I/O node.
Definition comm.F90:65
type(mpi_comm), public neko_global_comm
Definition comm.F90:47
type(mpi_datatype), public mpi_real_precision
MPI type for working precision of REAL types.
Definition comm.F90:54
integer, public global_pe_rank
Global MPI rank.
Definition comm.F90:68
integer, public pe_size
MPI size of communicator.
Definition comm.F90:62
integer, public pe_rank
MPI rank.
Definition comm.F90:59
integer, public global_pe_size
Global MPI size of communicator.
Definition comm.F90:71
type(mpi_comm), public neko_comm
MPI communicator.
Definition comm.F90:46
integer, public neko_mpi_thread_provided
Thread support provided by the MPI library.
Definition comm.F90:74
type(mpi_datatype), public mpi_extra_precision
Definition comm.F90:55
Build configurations.
integer, parameter neko_bcknd_device
Fortran bindings to SHMEM's C API.
Definition shmem.F90:34
@ shmem_thread_serialized
Definition shmem.F90:56
@ shmem_thread_multiple
Definition shmem.F90:57
@ shmem_thread_single
Definition shmem.F90:54
@ shmem_thread_funneled
Definition shmem.F90:55
Utilities.
Definition utils.f90:35