Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement automatically serializing gather #756

Merged
merged 4 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading