-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
environment.jl
307 lines (248 loc) · 9.3 KB
/
environment.jl
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
"""
mpiexec(fn)
A wrapper function for the MPI launcher executable. Calls `fn(cmd)`, where `cmd` is a `Cmd` object of the MPI launcher.
# Usage
```jldoctest
julia> mpiexec(cmd -> run(`\$cmd -n 3 echo hello world`));
hello world
hello world
hello world
```
"""
mpiexec
# Administrative functions
function _warn_if_wrong_mpi()
# warn if we have only one process but environment variables
# suggest we should have more
if Comm_rank(MPI.COMM_WORLD) == 0 && Comm_size(MPI.COMM_WORLD) == 1
known_envvars = ("MPI_LOCALNRANKS", #MPICH
"OMPI_COMM_WORLD_SIZE" # OpenMPI
)
if any(v -> haskey(ENV, v) && parse(Int, ENV[v]) > 1, known_envvars)
@warn """
You appear to have run julia under a different `mpiexec` than the one used by MPI.jl.
See the documentation for details.
"""
end
end
end
"""
MPI.free(obj)
Free the MPI object handle `obj`. This is typically used as the finalizer, and so need not be called directly unless otherwise noted.
"""
function free
end
const mpi_init_hooks = Any[]
add_init_hook!(f) = push!(mpi_init_hooks, f)
function run_init_hooks()
for f in mpi_init_hooks
f()
end
empty!(mpi_init_hooks)
return nothing
end
"""
Init(;threadlevel=:serialized, finalize_atexit=true, errors_return=true)
Initialize MPI in the current process. The keyword options:
- `threadlevel`: either `:single`, `:funneled`, `:serialized` (default),
`:multiple`, or an instance of [`ThreadLevel`](@ref).
- `finalize_atexit`: if `true` (default), adds an `atexit` hook to call
[`MPI.Finalize`](@ref) if it hasn't already been called.
- `errors_return`: if `true` (default), will set the default error handlers for
[`MPI.COMM_SELF`](@ref) and [`MPI.COMM_WORLD`](@ref) to be
`MPI.ERRORS_RETURN`. MPI errors will then appear as Julia exceptions.
It will return the [`ThreadLevel`](@ref) value which MPI is initialized at.
All MPI programs must call this function at least once before calling any other
MPI operations: the only MPI functions that may be called before `MPI.Init` are
[`MPI.Initialized`](@ref) and [`MPI.Finalized`](@ref).
It is safe to call `MPI.Init` multiple times, however it is not valid to call
it after calling [`MPI.Finalize`](@ref).
# External links
$(_doc_external("MPI_Init"))
$(_doc_external("MPI_Init_thread"))
"""
function Init(;threadlevel=:serialized, finalize_atexit=true, errors_return=true)
if threadlevel isa Symbol
threadlevel = ThreadLevel(threadlevel)
end
if MPI.Finalized()
error("MPI cannot be initialized after MPI.Finalize has been called.")
end
if MPI.Initialized()
provided = Query_thread()
if provided < threadlevel
@warn "MPI already initialized with thread level $provided, requested = $threadlevel"
end
else
provided = _init_thread(threadlevel)
if provided < threadlevel
@warn "MPI thread level requested = $threadlevel, provided = $provided"
end
if finalize_atexit
# MPI_Finalize is a collective and can act like a barrier (this may
# be implementation specific). If we are terminating due to a Julia
# exception, we shouldn't call MPI_Finalize.
@static if VERSION >= v"1.9-"
# In Julia 1.9 we can access the exitcode from the atexit hook
atexit() do exitcode
if exitcode == 0 && !Finalized()
Finalize()
end
end
else
# In Julia 1.8 and earlier we can peek at the current exception,
# and only if that field is nothing do we terminate.
# This does not work in Julia 1.9 or later.
atexit() do
if !Finalized() && ccall(:jl_current_exception, Any, ()) === nothing
Finalize()
end
end
end
end
run_init_hooks()
if errors_return
set_default_error_handler_return()
end
_warn_if_wrong_mpi()
end
return provided
end
"""
ThreadLevel
An Enum denoting the level of threading support in the current process:
- `MPI.THREAD_SINGLE`: Only one thread will execute.
- `MPI.THREAD_FUNNELED`: The process may be multi-threaded, but the application must
ensure that only the main thread makes MPI calls. See [`Is_thread_main`](@ref).
- `MPI.THREAD_SERIALIZED`: The process may be multi-threaded, and multiple threads may
make MPI calls, but only one at a time (i.e. all MPI calls are serialized).
- `MPI.THREAD_MULTIPLE`: Multiple threads may call MPI, with no restrictions.
# See also
- [`Init`](@ref)
- [`Query_thread`](@ref)
"""
mutable struct ThreadLevel
val::Cint
end
const THREAD_SINGLE = ThreadLevel(API.MPI_THREAD_SINGLE[])
const THREAD_FUNNELED = ThreadLevel(API.MPI_THREAD_FUNNELED[])
const THREAD_SERIALIZED = ThreadLevel(API.MPI_THREAD_SERIALIZED[])
const THREAD_MULTIPLE = ThreadLevel(API.MPI_THREAD_MULTIPLE[])
add_load_time_hook!(() -> THREAD_SINGLE.val = API.MPI_THREAD_SINGLE[] )
add_load_time_hook!(() -> THREAD_FUNNELED.val = API.MPI_THREAD_FUNNELED[] )
add_load_time_hook!(() -> THREAD_SERIALIZED.val = API.MPI_THREAD_SERIALIZED[])
add_load_time_hook!(() -> THREAD_MULTIPLE.val = API.MPI_THREAD_MULTIPLE[] )
ThreadLevel(threadlevel::Symbol) =
threadlevel == :single ? THREAD_SINGLE :
threadlevel == :funneled ? THREAD_FUNNELED :
threadlevel == :serialized ? THREAD_SERIALIZED :
threadlevel == :multiple ? THREAD_MULTIPLE :
error("Invalid threadlevel: must be one of :single, :funneled, :serialized, or :multiple")
Base.:(==)(tl1::ThreadLevel, tl2::ThreadLevel) = tl1.val == tl2.val
Base.isless(tl1::ThreadLevel, tl2::ThreadLevel) = tl1.val < tl2.val
function _init_thread(required::ThreadLevel)
r_provided = Ref{Cint}()
# int MPI_Init_thread(int *argc, char ***argv, int required, int *provided)
API.MPI_Init_thread(C_NULL, C_NULL, required.val, r_provided)
return ThreadLevel(r_provided[])
end
"""
Query_thread()
Query the level of threading support in the current process.
Returns a [`ThreadLevel`](@ref) value denoting
# External links
$(_doc_external("MPI_Query_thread"))
"""
function Query_thread()
r_provided = Ref{Cint}()
# int MPI_Query_thread(int *provided)
API.MPI_Query_thread(r_provided)
return ThreadLevel(r_provided[])
end
"""
Is_thread_main()
Queries whether the current thread is the main thread according to MPI. This can be called
by any thread, and is useful for the `THREAD_FUNNELED` [`ThreadLevel`](@ref).
# External links
$(_doc_external("MPI_Is_thread_main"))
"""
function Is_thread_main()
r_flag = Ref{Cint}()
# int MPI_Is_thread_main(int *flag)
API.MPI_Is_thread_main(r_flag)
return r_flag[] != 0
end
"""
Finalize()
Marks MPI state for cleanup. This should be called after [`MPI.Init`](@ref), and
can be called at most once. No further MPI calls (other than
[`Initialized`](@ref) or [`Finalized`](@ref)) should be made after it is called.
[`MPI.Init`](@ref) will automatically insert a hook to call this function when
Julia exits, if it hasn't already been called.
# External links
$(_doc_external("MPI_Finalize"))
"""
function Finalize()
MPI.Finalized() || API.MPI_Finalize()
return nothing
end
"""
Abort(comm::Comm, errcode::Integer)
Make a “best attempt” to abort all tasks in the group of `comm`. This function does not
require that the invoking environment take any action with the error code. However, a Unix
or POSIX environment should handle this as a return errorcode from the main program.
# External links
$(_doc_external("MPI_Abort"))
"""
Abort(comm::Comm, errcode::Integer) = API.MPI_Abort(comm, errcode)
"""
Initialized()
Returns `true` if [`MPI.Init`](@ref) has been called, `false` otherwise.
It is unaffected by [`MPI.Finalize`](@ref), and is one of the few functions that may be
called before [`MPI.Init`](@ref).
# External links
$(_doc_external("MPI_Intialized"))
"""
function Initialized()
flag = Ref{Cint}()
API.MPI_Initialized(flag)
flag[] != 0
end
"""
Finalized()
Returns `true` if [`MPI.Finalize`](@ref) has completed, `false` otherwise.
It is safe to call before [`MPI.Init`](@ref) and after [`MPI.Finalize`](@ref).
# External links
$(_doc_external("MPI_Finalized"))
"""
function Finalized()
flag = Ref{Cint}()
API.MPI_Finalized(flag)
flag[] != 0
end
Wtick() = API.MPI_Wtick()
Wtime() = API.MPI_Wtime()
"""
MPI.has_cuda()
Check if the MPI implementation is known to have CUDA support. Currently only Open MPI
provides a mechanism to check, so it will return `false` with other implementations
(unless overriden).
This can be overriden by setting the `JULIA_MPI_HAS_CUDA` environment variable to `true`
or `false`.
"""
function has_cuda()
flag = get(ENV, "JULIA_MPI_HAS_CUDA", nothing)
if flag === nothing
# Only Open MPI provides a function to check CUDA support
@static if MPI_LIBRARY == "OpenMPI"
# int MPIX_Query_cuda_support(void)
return 0 != ccall((:MPIX_Query_cuda_support, libmpi), Cint, ())
elseif MPI_LIBRARY == "IBMSpectrumMPI"
return true
else
return false
end
else
return parse(Bool, flag)
end
end