Skip to content

Commit

Permalink
Implement automatically serializing gather (#756)
Browse files Browse the repository at this point in the history
* Implement automatically serializing `gather`
* docs: add missing reference to gather
* bump patch

---------

Co-authored-by: Simon Byrne <[email protected]>
  • Loading branch information
lukas-weber and simonbyrne authored Jul 29, 2023
1 parent fd2c626 commit 3882775
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MPI"
uuid = "da04e1cc-30fd-572f-bb4f-1f8673147195"
authors = []
version = "0.20.12"
version = "0.20.13"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/collective.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ MPI.bcast
```@docs
MPI.Gather!
MPI.Gather
MPI.gather
MPI.Gatherv!
MPI.Allgather!
MPI.Allgather
Expand Down
34 changes: 34 additions & 0 deletions src/collective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,40 @@ Gather(sendbuf::AbstractArray, root::Integer, comm::Comm) =
Gather(object::T, root::Integer, comm::Comm) where {T} =
Gather!(Ref(object), Comm_rank(comm) == root ? Array{T}(undef, Comm_size(comm)) : nothing, root, comm)

"""
gather(obj, comm::Comm; root::Integer=0)
Gather the objects `obj` from all ranks on `comm` to rank `root`. This is able to to handle arbitrary data. On `root`, it returns a vector of the objects, and `nothing` otherwise.
# See also
- [`Gather!`](@ref)
"""
function gather(obj, comm::Comm; root::Integer=0)
isroot = Comm_rank(comm) == root

sendbuf = MPI.serialize(obj)
count = length(sendbuf)

counts = Gather(count, comm; root = root)

if isroot
data = Array{UInt8}(undef, sum(counts))
recvbuf = VBuffer(data, counts)

Gatherv!(sendbuf, recvbuf, comm; root = root)

objs = [
MPI.deserialize(view(recvbuf.data, displ+1:displ+count)) for (displ, count) in zip(recvbuf.displs, recvbuf.counts)
]
return objs
else
Gatherv!(sendbuf, nothing, comm; root = root)
return nothing
end
end


"""
Gatherv!(sendbuf, recvbuf, comm::Comm; root::Integer=0)
Expand Down
19 changes: 19 additions & 0 deletions test/test_gather.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ for T in MPITestTypes
@test A == ArrayType{T}(repeat(1:sz, inner=4))
end

# serializing version
C = MPI.gather(T(rank+1), comm; root=root)
if isroot
@test C isa Vector{T}
@test C == Vector{T}(1:sz)
else
@test C === nothing
end
end


objs = ["test", 1, Array{Int}, [1,"test"]]
obj = objs[mod(rank, length(objs))+1]

C = MPI.gather(obj, comm; root=root)
if isroot
@test C == [objs[mod1(i, length(objs))] for i = 1:sz]
else
@test C === nothing
end

MPI.Finalize()
Expand Down

0 comments on commit 3882775

Please sign in to comment.