Parallelizing a for-loop with a matrix

I have a simple for-loop with a matrix, where I calculate a slice of the matrix via each step of the for-loop. e.g.

for ilat = 1 : nlat, ilon = 1 : nlon

    for ip = 1 : np; esat[ip+1] = t2esat(Ta[ilon,ilat,ip],p[ip+1]) end

    p[end] = ps[ilon,ilat]
    esat[end] = t2esat(Ts[ilon,ilat],p[end])
    swp[ilon,ilat,it] = calcswp(esat,p,ps[ilon,ilat])


I wish to parallelize the outer for-loop here. What packages should I use? Should I use DistributedArrays, or is Distributed enough? I’m not sure if I should use multi-threading, but I can use multiple cores. Do I need to specify the number of cores?

You might like


If you post runnable code (with inputs and maybe timings using BenchmarkTools.jl), you’ll likely get much more response.

But in any case, I think you may want to start with multithreading by starting julia with julia --threads 4 and adding Threads.@threads in front of your first for loop. Distributed is for distributed computing - when you have e.g. a cluster with multiple compute nodes.

1 Like