Julia 0.6 parallelization of modules and functions


#1

Hi all,

I’m trying to modify some example code from the documentation and am running into an error. Here’s my module that I’m trying to create:

@everywhere module parallel_test

using Base.Distributed
@everywhere using Base.SharedArray
@everywhere import Distributions

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

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


@everywhere function advection_chunk!(q, u, z, 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] + z[i] - Distributions.cdf(Distributions.TDist(10), 34)
   end
   q
end

@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

function test_function(q, u, z)

	advection_shared!(q,u,z);
end

end

Then, from the terminal (after loading with julia -p 4):

 q = SharedArray{Float64,3}((500,500,500));
 u = SharedArray{Float64,3}((500,500,500));
 z = rand(500,1)
parallel_test.test_function(q, u, z)

I’m running into this error:

**ERROR:** UndefVarError: advection_shared_chunk! not defined

(::parallel_test.##9#10{SharedArray{Float64,3},SharedArray{Float64,3},Array{Float64,2}})() at ./task.jl:335

...and 3 more exception(s).

Stacktrace:

[1] **sync_end()** at **./task.jl:287**

[2] **macro expansion** at **./task.jl:303** [inlined]

[3] **advection_shared!(** ::SharedArray{Float64,3}, ::SharedArray{Float64,3}, ::Array{Float64,2} **)** at **/Users/james.nance/Documents/1225/parallel_test.jl:9**

[4] **test_function(** ::SharedArray{Float64,3}, ::SharedArray{Float64,3}, ::Array{Float64,2} **)** at **/Users/james.nance/Documents/1225/parallel_test.jl:40**

Any idea what’s going on?


#2

Would you mind fixing the quoting of you code by changing the ‘…’ to ‘```’ please? It’ll make your code much easier for others to read and comprehend.

I’m thinking that you might have too many @everywhere calls here, since you have one already at the module level (which I suppose should cover everything contained within it). Maybe try removing all but that one?

Importantly, how are you launching your processes? Are you using a flag from the shell, or doing addprocs earlier before you run your actual code?


#3

Thanks, @jpsamaroo, I’m very new to this site and didn’t know how to do that. Looks much nicer now!

And removing all but the first @everywhere did the trick. I’m starting Julia with 4 processes and then running the code.