Skip to content

Accept any DenseArray in MPI.Buffer - #996

Draft
vchuravy wants to merge 1 commit into
JuliaParallel:masterfrom
vchuravy:vc/buffer-densearray
Draft

vchuravy wants to merge 1 commit into
JuliaParallel:masterfrom
vchuravy:vc/buffer-densearray

Conversation

@vchuravy

Copy link
Copy Markdown
Member

MPI.Buffer(data) has a method only for Array (plus Ref, some SubArrays and, via extensions, GPU arrays). Since #941, Base.cconvert(MPIPtr, x) works for any AbstractArray through mpi_ptr_type, so the remaining blocker for using other dense arrays directly as communication buffers is Buffer itself. For example, MPI.Send(m, ...) or MPI.Allreduce!(m, +, comm) with m::Memory{Float64} or a FixedSizeArray from FixedSizeArrays.jl throws a MethodError.

This PR changes Buffer(arr::Array) to Buffer(arr::DenseArray). A DenseArray stores its elements contiguously in column-major order, so sending length(arr) elements of eltype(arr) is correct. Pointer conversion is unchanged and still has to be provided by the array type (mpi_ptr_type and Base.cconvert/Base.unsafe_convert). Types that can't provide a pointer fail at the ccall as before.

BitArray is a DenseArray{Bool}, but it stores packed bits, so it gets an explicit ArgumentError. Before this PR, Buffer(::BitArray) was a MethodError.

The existing Buffer methods for CuArray, ROCArray and oneArray in the extensions are now redundant, since those are DenseArrays. I left them in place to keep this PR minimal.

Motivation

Trixi.jl is replacing unsafe_wrap(Array, pointer(v), dims) with FixedSizeArrays that wrap resize!able vectors, and some of those arrays are passed to MPI. A package extension in FixedSizeArrays.jl (JuliaArrays/FixedSizeArrays.jl#198) covers released MPI.jl v0.20.x; with this PR and #941, no extension would be needed.

Tests

The new test/test_buffer_densearray.jl uses a minimal DenseArray wrapper type and Memory (on Julia ≥ 1.11). It checks the Buffer fields, a ring Isend/Irecv!, Allreduce! and Bcast!, and that BitArray throws. It runs only for the CPU ArrayType.

Locally (Julia 1.12.6, MPICH_jll), with mpiexec -n 2 and -n 3:

  • The new test passes, and fails without the change (MethodError for MPI.Buffer).
  • test_sendrecv, test_gatherv, test_scatterv, test_allreduce, test_bcast, test_subarray, test_reduce and test_onesided pass.
  • Test.detect_ambiguities(MPI) reports no ambiguities involving Buffer.

I haven't run the full suite or the GPU backends.

Opening as a draft to check whether this direction is welcome. Happy to also remove the now-redundant GPU Buffer methods or add a NEWS/docs entry.

This PR was prepared with substantial assistance from Claude Code (Anthropic's AI coding assistant). I reviewed the changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MEJYUviAUshj5fZt8LTe8A

`MPI.Buffer(data)` had a method only for `Array`, so dense arrays such as
`Memory` or `FixedSizeArray` could not be used directly as buffers even
though they can be converted to an `MPIPtr`.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MEJYUviAUshj5fZt8LTe8A

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants