Skip to content

Commit

Permalink
Document external MPI initialization (#748)
Browse files Browse the repository at this point in the history
* Document external MPI initialization
* Add newly documented functions to reference docs
  • Loading branch information
sloede committed Jul 18, 2023
1 parent 2a2bd68 commit 4ba36fa
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/src/external.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ There are two typical solutions to this problem:
end
REFS[obj] = nothing
```

## Externally initialized MPI

When working with non-Julia libraries or tools, `MPI_Init` may be invoked in another part
of the execution flow and not via MPI.jl's [`MPI.Init`](@ref) function. This leaves some
package-internal settings uninitialized. In this case, you need to call
[`MPI.run_init_hooks()`)(@ref) manually to fully initialize MPI.jl. You may also want to
consider calling [`MPI.set_default_error_handler_return()`](@ref).
1 change: 1 addition & 0 deletions docs/src/reference/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ MPI.infoval
MPI.Errhandler
MPI.get_errorhandler
MPI.set_errorhandler!
MPI.set_default_error_handler_return
```

## Miscellaneous
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ MPI.Initialized
MPI.Finalize
MPI.Finalized
MPI.add_init_hook!
MPI.run_init_hooks
MPI.add_finalize_hook!
```

Expand Down
10 changes: 10 additions & 0 deletions src/environment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ Register a function `f` that will be called as `f()` when `MPI.Init` is
called. These are invoked in a first-in, first-out (FIFO) order.
"""
add_init_hook!(f) = push!(mpi_init_hooks, f)

"""
MPI.run_init_hooks()
Execute all functions that have been registered using [`MPI.add_init_hook!()`](@ref).
This function is executed automatically by [`MPI.Init()`](@ref) but *must* be invoked
manually if MPI has been initialized externally by a direct call to `MPI_Init()`. It is safe
to call this function multiple times (subsequent runs will be a no-op).
"""
function run_init_hooks()
while !isempty(mpi_init_hooks)
f = popfirst!(mpi_init_hooks) # FIFO
Expand Down
10 changes: 10 additions & 0 deletions src/errhandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ function free(errh::Errhandler)
return nothing
end

"""
MPI.set_default_error_handler_return()
Set the error handler for `MPI_COMM_SELF` and `MPI_COMM_WORLD` to `MPI_ERRORS_RETURN`. This
will cause certain MPI errors to appear as Julia exceptions.
This function is executed automatically by [`MPI.Init()`](@ref) but *may* be invoked
manually if MPI has been initialized externally by a direct call to `MPI_Init()`. It is safe
to call this function multiple times.
"""
function set_default_error_handler_return()
set_errorhandler!(COMM_SELF, ERRORS_RETURN)
set_errorhandler!(COMM_WORLD, ERRORS_RETURN)
Expand Down

0 comments on commit 4ba36fa

Please sign in to comment.