MPI.jl icon indicating copy to clipboard operation
MPI.jl copied to clipboard

Broadcasting without a bang

Open carstenbauer opened this issue 2 years ago • 2 comments

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 Refs 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).

carstenbauer avatar Aug 24 '21 15:08 carstenbauer

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.

simonbyrne avatar Aug 26 '21 04:08 simonbyrne

Would have wanted this again today (for broadcasting scalars). What do you think Simon, should we merge this?

carstenbauer avatar Dec 12 '21 19:12 carstenbauer

Alright, I've dropped the copying definitions and restricted the introduced Bcast to isbitstypes 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?

carstenbauer avatar Nov 28 '22 14:11 carstenbauer

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 🤷

sloede avatar Nov 28 '22 15:11 sloede

Okay, I'm happy to merge it. Needs to be added to the docs though.

simonbyrne avatar Nov 28 '22 19:11 simonbyrne

Needs to be added to the docs though.

Done.

carstenbauer avatar Nov 29 '22 10:11 carstenbauer