Conversation
MPICH >= 4.3 provides the experimental `MPIX_Op_create_x`, which passes a `void *extra_state` context pointer to the user function (the implementation of mpi-forum/mpi-issues#839). With it, `MPI.Op(f, T)` no longer needs a closure cfunction: the `OpWrapper` is passed as `extra_state` and recovered via `unsafe_pointer_to_objref` in a plain `@cfunction`. This makes user-defined reduction operators work on aarch64/ppc64le/arm with MPICH. * `MPI.API.MPIX_Op_create_x` binding and `MPI.API.HAS_MPIX_Op_create_x` (determined via `dlsym` at precompile time, like `@mpichk`'s version checks) * `Op(f, T)` uses the new path if available, otherwise falls back to the existing closure-cfunction path (and its platform restrictions) * `OpWrapper` becomes `mutable` so its address can be taken; the kernel is shared between both callback signatures via `_reduce!` * a no-op destructor is passed since MPICH's argument checks reject NULL * test: enable custom-op tests when `HAS_MPIX_Op_create_x`, add a test with a state-capturing closure * docs: update the custom reduction operator known issue Assisted-by: Claude Code (Fable 5.1)
vchuravy
commented
Sep 9, 2026
| op = Op(OP_NULL.val, w) | ||
| # int MPIX_Op_create_x(MPIX_User_function_x *user_fn_x, MPIX_Destructor_function *destructor_fn, | ||
| # int commute, void *extra_state, MPI_Op *op) | ||
| API.MPIX_Op_create_x(fptr, dptr, iscommutative, pointer_from_objref(w), op) |
Member
Author
There was a problem hiding this comment.
We must GC preserve w globally
Contributor
There was a problem hiding this comment.
Yes, w must be preserved until we are sure the operator is not used any more. I don't think we can tell when this is the case, since MPI might keep the operator around even after we call MPI_Op_free. I think we can never free the closure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MPICH ≥ 4.3 ships the experimental
MPIX_Op_create_x, which passes avoid *extra_statecontext pointer to the user-defined reduction function (the implementation of the MPI Forum proposal mpi-forum/mpi-issues#839):With it,
MPI.Op(f, T)no longer needs a closure@cfunction: theOpWrapperis passed asextra_stateand recovered withunsafe_pointer_to_objrefinside a plain@cfunction. This removes the LLVM-trampoline dependency and should make user-defined reduction operators work on aarch64 / ppc64le / arm (#404) when MPICH is the backend. Other backends keep using the existing closure path unchanged.Changes
MPI.API.MPIX_Op_create_xbinding andMPI.API.HAS_MPIX_Op_create_x, determined withdlsymat precompile time (same mechanism@mpichkuses for its version checks).Op(f, T)takes the new path when available, otherwise the existing closure-cfunction path with its platform errors.OpWrapperbecomesmutable structso its address can be taken; both callback signatures share the reduction kernel_reduce!. TheT = Anycase now receives theMPI_Datatypeby value.destructor_fn = NULLwithMPI_ERR_ARG, even thoughMPIR_Op_free_impltolerates it.test_reduce.jlare enabled wheneverHAS_MPIX_Op_create_x, plus a new test with a state-capturing closure.Testing
Locally on x86_64 Linux, 3 ranks:
HAS_MPIX_Op_create_x == true):test_reduce.jl,test_allreduce.jl,test_scan.jl,test_exscan.jl,test_onesided.jlpass. Also checkedOp(f, Any)withInt32andVector{Int}inputs, and freeing the op followed by GC.HAS_MPIX_Op_create_x == false):test_reduce.jlpasses via the unchanged fallback path.Not tested: an actual aarch64 machine. CI on macOS aarch64 with MPICH_jll would be the real check for this, which is the main reason this is a draft.
Open questions
HAS_MPIX_Op_create_xbe surfaced inMPI.versioninfo()?Opsuggest switching to MPICH?Op.fptrfield now holds either a closureCFunctionor anOpWrapper; a rename would be a (minor) breaking change for anyone poking at it.🤖 Generated with Claude Code