`@parallel` macro

I have been trying to understand how @parallel works for some time now, but I have not been able to reason why the below code works the way it works:

julia> nprocs()
4

julia> res = @parallel (+) for n in 1:3
         n
       end
6

julia> res = @parallel for n in 1:3
         n
       end
3-element Array{Future,1}:
 Future(2,1,8,#NULL) 
 Future(3,1,9,#NULL) 
 Future(4,1,10,#NULL)

julia> fetch.(res)
3-element Array{Void,1}:
 nothing
 nothing
 nothing

According to the documentation, @parallel without the reduction operation returns an array of Futures (that I can see). However, fetching these Futures results in nothing. If these Futures are nothing, what does (+) aggregate in @parallel? I’d expect to have either the last n value, or simply the vectors of these ns stored in the Future, when I issue a fetch.

I’d appreciate if you told me what I am missing.

My understanding is that for loop returns nothing and hence @parallel for returns nothing as well if no reducer is given. If you’d like to do parallel computation using multiple processors, using pmap may be handier and more deterministic.

1 Like

Thanks for the answer, @bicycle1885. Apparently, you are right; only when there is a reduction, the result of the last expression becomes the return value. Returning Future values just adds to my confusion.