Pmap cannot write into SharedArray

parallel

#1

Consider the following code:

nw = nworkers()
@show nw

N=20
@eval @everywhere N = $N

spw = ceil(Int, N / nw)
@show spw

@everywhere yr = SharedArray(Float64, N)
@everywhere yr = 1:N

@everywhere sigma = SharedArray(Float64, N)

@everywhere function dummy_batch(arange, yr, sigma)
    @show arange
    for i in arange
      sigma[i] = yr[i] * yr[i]
      @show sigma[i]
    end
end

my_ranges = [(i:min(i+spw-1,N)) for i = 1:spw:N]
@show my_ranges

@time pmap((arg)->dummy_batch(arg,yr,sigma), my_ranges)

@show sigma
@show sdata(sigma)

As you can see from the following log the SharedArray is assigned the correct values by the the workers however after pmap sigma is all zeros. I have used SharedArrays in the past with 0.4.7, I am running this code with 0.5.1. Did anything change in the new version?

-Arrigo

nw = 5
spw = 4
my_ranges = UnitRange{Int64}[1:4,5:8,9:12,13:16,17:20]
From worker 2: arange = 1:4
From worker 2: sigma[i] = 1.0
From worker 5: arange = 9:12
From worker 4: arange = 5:8
From worker 2: sigma[i] = 4.0
From worker 5: sigma[i] = 81.0
From worker 4: sigma[i] = 25.0
From worker 2: sigma[i] = 9.0
From worker 5: sigma[i] = 100.0
From worker 4: sigma[i] = 36.0
From worker 2: sigma[i] = 16.0
From worker 5: sigma[i] = 121.0
From worker 4: sigma[i] = 49.0
From worker 5: sigma[i] = 144.0
From worker 4: sigma[i] = 64.0
From worker 3: arange = 17:20
From worker 3: sigma[i] = 289.0
From worker 3: sigma[i] = 324.0
From worker 3: sigma[i] = 361.0
From worker 3: sigma[i] = 400.0
From worker 6: arange = 13:16
From worker 6: sigma[i] = 169.0
From worker 6: sigma[i] = 196.0
From worker 6: sigma[i] = 225.0
. From worker 6: sigma[i] = 256.0
041796 seconds (5.67 M allocations: 240.650 MB, 2.37% gc time)
sigma = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]
sdata(sigma) = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]


#2

The @everywhere will define different instances of the SharedArray on each worker.

Also in 0.5, globals referenced in a closure are not automatically serialized to remote nodes and the pmap will throw UndefVar errors if the globals are not localized.

The following should work:

nw = nworkers()
@show nw

N=20
@eval @everywhere N = $N

spw = ceil(Int, N / nw)
@show spw

yr = 1:N
sigma = SharedArray(Float64, N)

@everywhere function dummy_batch(arange, yr, sigma)
    @show arange
    for i in arange
      sigma[i] = yr[i] * yr[i]
      @show sigma[i]
    end
end

my_ranges = [(i:min(i+spw-1,N)) for i = 1:spw:N]
@show my_ranges

let yr=yr, sigma=sigma
    @time pmap((arg)->dummy_batch(arg,yr,sigma), my_ranges)
end

@show sigma
@show sdata(sigma)