Skip to content

initialize underlying array in sharedarray at construction time #17095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type SharedArray{T,N} <: DenseArray{T,N}
# a subset of workers.
loc_subarr_1d::SubArray{T,1,Array{T,1},Tuple{UnitRange{Int}},true}

SharedArray(d,p,r,sn) = new(d,p,r,sn)
function SharedArray(d,p,r,sn,s)
new(d,p,r,sn,s,0,view(Array{T}(ntuple(d->0,N)), 1:0))
end
end

(::Type{SharedArray{T}}){T,N}(d::NTuple{N,Int}; kwargs...) =
Expand Down Expand Up @@ -58,7 +60,7 @@ function SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
pids, onlocalhost = shared_pids(pids)

local shm_seg_name = ""
local s
local s = Array{T}(ntuple(d->0,N))
local S = nothing
local shmmem_create_pid
try
Expand Down Expand Up @@ -97,8 +99,8 @@ function SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
end
systemerror("Error unlinking shmem segment " * shm_seg_name, rc != 0)
end
S = SharedArray{T,N}(dims, pids, refs, shm_seg_name)
initialize_shared_array(S, s, onlocalhost, init, pids)
S = SharedArray{T,N}(dims, pids, refs, shm_seg_name, s)
initialize_shared_array(S, onlocalhost, init, pids)
shm_seg_name = ""

finally
Expand Down Expand Up @@ -167,7 +169,7 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
func_mmap = mode -> open(filename, mode) do io
Mmap.mmap(io, Array{T,N}, dims, offset; shared=true)
end
local s
s = Array{T}(ntuple(d->0,N))
if onlocalhost
s = func_mmap(mode)
refs[1] = remotecall(pids[1]) do
Expand All @@ -191,17 +193,14 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
wait(ref)
end

S = SharedArray{T,N}(dims, pids, refs, filename)
initialize_shared_array(S, s, onlocalhost, init, pids)
S = SharedArray{T,N}(dims, pids, refs, filename, s)
initialize_shared_array(S, onlocalhost, init, pids)
S
end

function initialize_shared_array(S, s, onlocalhost, init, pids)
function initialize_shared_array(S, onlocalhost, init, pids)
if onlocalhost
init_loc_flds(S)
# In the event that myid() is not part of pids, s will not be set
# in the init function above, hence setting it here if available.
S.s = s
else
S.pidx = 0
end
Expand Down Expand Up @@ -248,9 +247,8 @@ function reshape{T,N}(a::SharedArray{T}, dims::NTuple{N,Int})
end
end

A = SharedArray{T,N}(dims, a.pids, refs, a.segname)
A = SharedArray{T,N}(dims, a.pids, refs, a.segname, reshape(a.s, dims))
init_loc_flds(A)
(a.pidx == 0) && isdefined(a, :s) && (A.s = reshape(a.s, dims))
A
end

Expand Down