MPI.jl
MPI.jl copied to clipboard
Broadcasting without a bang
Introduces Bcast
to complement Bcast!
. Similar to Send(obj, ...)
the new variant is useful to directly broadcast objects, scalars in particular, to all ranks without having to introduce buffers or the like.
The usecase that made me create this PR is as simple as Bcast(1.23, ...)
or Bcast(p, ...)
where p
is an instantiation of a struct, say:
struct Params
a::Float64
b::Float64
n::Int64
end
Before the PR, one had to wrap these objects manually into Ref
s to then use Bcast!(Ref(obj),...)
. This automates this process (and special cases arrays, where we'll simply make a copy, and Ref
input objects).
I was a bit skeptical, as it does require allocating objects that aren't actually used. But I can see the benefit in this case. I'll leave it for a few days to let others chime in.
Would have wanted this again today (for broadcasting scalars). What do you think Simon, should we merge this?
Alright, I've dropped the copying definitions and restricted the introduced Bcast
to isbitstype
s where we can just wrap the object into a Ref
. This still removes the ugly manual wrapping/unwrapping ala
result = Bcast!(Ref(1.23), ...)
result[] # actually retrieve the data
in favor of the nicer
result = Bcast(1.23, ...)
for (isbits) structs and scalars such as simple numbers (my original motivation). However, I must admit that I'm not sure it's worth introducing a separate function for this. What do you think?
From a usability point of view, I'd be very much in favor of this PR getting merged. Not sure on the question of whether it is detriment to having a "nice" interface 🤷
Okay, I'm happy to merge it. Needs to be added to the docs though.
Needs to be added to the docs though.
Done.