Skip to content

Commit edab8ef

Browse files
authored
Throw more informative error if MPI.Init() is called multiple times (#310)
Fixes #309.
1 parent 3c97558 commit edab8ef

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/environment.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const REFCOUNT = Threads.Atomic{Int}(1)
1+
const REFCOUNT = Threads.Atomic{Int}(-1)
22

33
"""
44
refcount_inc()
@@ -34,7 +34,7 @@ end
3434
3535
Initialize MPI in the current process.
3636
37-
All MPI programs must contain exactly one call to `MPI.Init()`.
37+
All MPI programs must contain exactly one call to `MPI.Init()`. In particular, note that it is not valid to call `MPI.Init` again after calling [`MPI.Finalize`](@ref).
3838
3939
The only MPI functions that may be called before `MPI.Init()` are
4040
[`MPI.Initialized`](@ref) and [`MPI.Finalized`](@ref).
@@ -43,10 +43,9 @@ The only MPI functions that may be called before `MPI.Init()` are
4343
$(_doc_external("MPI_Init"))
4444
"""
4545
function Init()
46-
if REFCOUNT[] != 1
47-
error("MPI REFCOUNT in incorrect state")
48-
end
46+
REFCOUNT[] == -1 || error("MPI.REFCOUNT in incorrect state: MPI may only be initialized once per session.")
4947
@mpichk ccall((:MPI_Init, libmpi), Cint, (Ptr{Cint},Ptr{Cint}), C_NULL, C_NULL)
48+
REFCOUNT[] = 1
5049
atexit(refcount_dec)
5150

5251
for f in mpi_init_hooks

0 commit comments

Comments
 (0)