The Julia manual (Multi-processing and Distributed Computing · The Julia Language) claims that @distributed
without a reduction operator returns a an array of Future
s:
Using “outside” variables in parallel loops is perfectly reasonable if the variables are read-only:
a = randn(1000)
@distributed (+) for i = 1:100000
f(a[rand(1:end)])
end
Here each iteration applies
f
to a randomly-chosen sample from a vectora
shared by all processes.As you could see, the reduction operator can be omitted if it is not needed. In that case, the loop executes asynchronously, i.e. it spawns independent tasks on all available workers and returns an array of
Future
immediately without waiting for completion. The caller can wait for theFuture
completions at a later point by callingfetch
on them, or wait for completion at the end of the loop by prefixing it with@sync
, like@sync @distributed for
.
But that doesn’t seem to be the case? I ran the following on my laptop (Julia version 1.11.4):
$ julia -p 2
julia> @everywhere f(x) = 2x
julia> a = randn(1000)
@distributed (+) for i = 1:100000
f(a[rand(1:end)])
end
10268.871546420913
julia> @distributed for i = 1:100000
f(a[rand(1:end)])
end
Task (runnable, started) @0x000077c9cf892bd0
julia> ret = @distributed for i = 1:100000
f(a[rand(1:end)])
end
Task (runnable, started) @0x000077c9cf23b3a0
julia> typeof(ret)
Task
It looks to me like @distributed
without a reducer returns a Task
, not an array of Future
s. Am I misunderstanding something?