How to run a function in parallel with Julia language?
<p>I am trying to figure out how to work with parallel computing with Julia. The documentation looks great, even for someone like me that has never worked with Parallel Computing (and that does not understand most of the concepts behind the documentation ;)).</p>

<p>Just to mention: I am working in a PC with Ubuntu. It has a 4-core processor.</p>

<p>To run the code I describe below I am calling the julia terminal as:</p>

<pre><code>$ julia -p 4
</code></pre>

<p>I am following the documentation <a href="http://docs.julialang.org/en/latest/manual/parallel-computing/" rel="nofollow">here</a>. I am facing some problems with examples described in <a href="http://docs.julialang.org/en/latest/manual/parallel-computing/#id2" rel="nofollow">this section</a> </p>

<p>I am trying to run the following piece of code:</p>

<pre><code>@everywhere advection_shared_chunk!(q, u) = advection_chunk!(q, u, myrange(q)..., 1:size(q,3)-1)

function advection_shared!(q, u)
    @sync begin
        for p in procs(q)
            @async remotecall_wait(advection_shared_chunk!, p, q, u)
        end
    end
    q
end

q = SharedArray(Float64, (500,500,500))
u = SharedArray(Float64, (500,500,500))

#Run once to JIT-compile
advection_shared!(q,u)
</code></pre>

<p>But I am facing the following error:</p>

<pre><code>ERROR: MethodError: `remotecall_wait` has no method matching remotecall_wait(::Function, ::Int64, ::SharedArray{Float64,3}, ::SharedArray{Float64,3})
Closest candidates are:
  remotecall_wait(::LocalProcess, ::Any, ::Any...)
  remotecall_wait(::Base.Worker, ::Any, ::Any...)
  remotecall_wait(::Integer, ::Any, ::Any...)
 in anonymous at task.jl:447

...and 3 other exceptions.

 in sync_end at ./task.jl:413
 [inlined code] from task.jl:422
 in advection_shared! at none:2
</code></pre>

<p>What am I doing wrong here? As far as I know I am just reproducing the example in the docs... or not?</p>

<p>Thanks for any help,</p>

<hr>

<p>Thanks @Daniel Arndt, you found the trick! I was looking at the docs in: <a href="http://docs.julialang.org/en/latest/manual/parallel-computing/" rel="nofollow">http://docs.julialang.org/en/latest/manual/parallel-computing/</a> I thought it was supposed to be the one relative to Julia 0.4.x (the latest stable version so far) but it seems that it is relative to Julia 0.5.x (the latest version among all versions). </p>

<p>I did the changes you suggested (changed the order and added the functions that were missing) and everything worked like a charm. I will leave here the updated code</p>

<pre><code># Here's the kernel
@everywhere function advection_chunk!(q, u, irange, jrange, trange)
    @show (irange, jrange, trange)  # display so we can see what's happening
    for t in trange, j in jrange, i in irange
        q[i,j,t+1] = q[i,j,t] +  u[i,j,t]
    end
    q
end

# This function retuns the (irange,jrange) indexes assigned to this worker
@everywhere function myrange(q::SharedArray)
    idx = indexpids(q)
    if idx == 0
        # This worker is not assigned a piece
        return 1:0, 1:0
    end
    nchunks = length(procs(q))
    splits = [round(Int, s) for s in linspace(0,size(q,2),nchunks+1)]
    1:size(q,1), splits[idx]+1:splits[idx+1]
end

@everywhere advection_shared_chunk!(q, u) = advection_chunk!(q, u, myrange(q)..., 1:size(q,3)-1)

function advection_shared!(q, u)
    @sync begin
        for p in procs(q)
            @async remotecall_wait(p, advection_shared_chunk!, q, u)
        end
    end
    q
end

q = SharedArray(Float64, (500,500,500))
u = SharedArray(Float64, (500,500,500))

#Run once to JIT-compile
advection_shared!(q,u)
</code></pre>

<p>Done!</p>