Parallel Processing for nested loops in Julia

Viewed 709

I am trying to implement parallel processing for nested loops. However I am getting the following syntax error.

I am modifying the example from here ( Parallel computing in Julia - running a simple for-loop on multiple cores )

This works

for N in 1:5:20, H in 1:5:20
           println("The N of this iteration in $N, $H")
end

This is giving syntax error

using Distributed

@distributed for N in 1:5:20, H in 1:5:20
           println("The N of this iteration in $N, $H")
       end

enter image description here

2 Answers

The @distributed macro supports iterating only over one parameter. Hence you could do:

@distributed for (N,H) in collect(Iterators.product(1:5:20,1:5:20))
    println("The N of this iteration in $N, $H")
end

Another option is of course having a nested loop. In that case only one of the loops should be @distributed.

An alternative (for single node, multi core parallelization) would be multithreading. This is often easier / more efficient due to lower overhead and shared memory (note that there is no such thing like a Python GIL in Julia). The corresponding Julia commands are Threads.@threads (in front of for loops) or Threads.@spawn for spawning tasks to be executed parallel.

Edit: example added

Threads.@threads for (N,H) in collect(Iterators.product(1:5:20,1:5:20))
    println("The N of this iteration in $N, $H")
end

2nd case:

f(x) = x^2
futures = [Threads.@spawn f(x) for x=1:10]
fetch.(futures)
Related