Make every for-loop parallel in Julia   julia programming

If reading the documentation is confusing, making any for-loop into a parallel loop is rediculously easy. Just add the parallel macro! If it's code that needs to execute before moving on, just pass an operator to to macro.

# non-parallel loop
for i=1:100
    println(i)
    j = my_array[i]
    some_function(j)
end

and this becomes

# parallel loop
@parallel for i=1:100
    println(i)
    j = my_array[i]
    some_function(j)
end

, you're done! If you find that the code moves on too quickly, just add an operator to force it to wait:

# parallel loop
my_zero_result = @parallel (+) for i=1:100
    println(i)
    j = my_array[i]
    some_function(j)
    0
end

Julia!

previous | next | random