Skip to content

Commit

Permalink
Broadcasting without a bang (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbauer authored Nov 29, 2022
1 parent 1b9ce10 commit 2e94c5b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/reference/collective.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ MPI.Ibarrier

```@docs
MPI.Bcast!
MPI.Bcast
MPI.bcast
```

Expand Down
13 changes: 13 additions & 0 deletions src/collective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ function Bcast!(data, root::Integer, comm::Comm)
Bcast!(Buffer(data), root, comm)
end

"""
Bcast(obj, root::Integer, comm::Comm)
Broadcast the `obj` from `root` to all processes in `comm`. Returns the object.
Currently `obj` must be `isbits`, i.e. `isbitstype(typeof(obj)) == true`.
"""
function Bcast(obj::T, root::Integer, comm::Comm) where T
if !isbitstype(T)
throw(ArgumentError("Bcast currently only supports `isbitstype`s."))
end
Bcast!(Ref(obj), root, comm)[]
end

"""
bcast(obj, comm::Comm; root::Integer=0)
Expand Down
23 changes: 23 additions & 0 deletions test/test_bcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ MPI.Bcast!(B, comm; root=root)
@test B == A


# Bcast: number
A = 1.23
B = MPI.Comm_rank(comm) == root ? 1.23 : 0.0
res = MPI.Bcast(B, root, comm)
@test typeof(res) == typeof(A)
@test res == A

# Bcast: scalar struct
struct XY
x::Float64
y::Float32
end
A = XY(1.23, 4.56f0)
B = MPI.Comm_rank(comm) == root ? A : XY(0.0, 0.0f0)
res = MPI.Bcast(B, root, comm)
@test typeof(res) == typeof(A)
@test res == A

# Bcast: array
A = rand(3)
B = MPI.Comm_rank(comm) == root ? A : zeros(3)
@test_throws ArgumentError MPI.Bcast(B, root, comm)

g = x -> x^2 + 2x - 1
if MPI.Comm_rank(comm) == root
f = g
Expand Down

0 comments on commit 2e94c5b

Please sign in to comment.