List of possible reducer functions when using @distributed in Julia

Viewed 443

I was reading the documentation for writing parallel for loops in Julia using @distributed and saw that it is possible to specify a reducer function that acts on the result of each iteration of the loop.

For instance, as it is shown in the next example taken from the documentation, it is possible to sum the result of every single worker:

nheads = @distributed (+) for i = 1:200000000
    Int(rand(Bool))
end

Unfortunately I couldn't find any list of which functions can be used as reducers and how to exactly do it. Is there such a list?

1 Answers

You can take any function that takes two arguments so the list is open and can be arbitrarily extended. See e.g.

julia> addprocs(4);

julia> x = @distributed (a,b) -> (a,b, "val") for i in 1:10
       i
       end
(((((1, 2, "val"), 3, "val"), ((4, 5, "val"), 6, "val"), "val"), (7, 8, "val"), "val"), (9, 10, "val"), "val")

julia> addprocs(4);

julia> x = @distributed (a,b) -> (a,b, "val") for i in 1:10
       i
       end
((((((((1, 2, "val"), (3, 4, "val"), "val"), 5, "val"), 6, "val"), 7, "val"), 8, "val"), 9, "val"), 10, "val")

However, for the operation to work in typical scenarios the function has a signature fun(::T, ::T)::T where T so that it is guaranteed that the reduction operation can be always performed and preferably the result of reduction does not depend on the order of operations (you can see in the example above that the order of reductions depends on the number of workers and I have chosen a function that does not have this property on purpose).

Related