Charles Santana
Charles Santana

Reputation: 65

How to run a function in parallel with Julia language?

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 ;)).

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

To run the code I describe below I am calling the julia terminal as:

$ julia -p 4

I am following the documentation here. I am facing some problems with examples described in this section

I am trying to run the following piece of 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)

But I am facing the following error:

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

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

Thanks for any help,


Thanks @Daniel Arndt, you found the trick! I was looking at the docs in: http://docs.julialang.org/en/latest/manual/parallel-computing/ 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).

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

# 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)

Done!

Upvotes: 2

Views: 593

Answers (1)

Daniel Arndt
Daniel Arndt

Reputation: 2543

I don't believe you are doing anything wrong, other than you are likely using a newer version of the docs (or we're seeing different things!).

Lets make sure you're using Julia 0.4.x and these docs: http://docs.julialang.org/en/release-0.4/manual/parallel-computing/

In Julia v0.5.0, the order of the first two parameters for remotecall_wait was changed. Switch the order to remotecall_wait(p, advection_shared_chunk!, q, u) and you should be on to your next error (myrange is not defined, which can be found earlier in the docs)

Upvotes: 2

Related Questions