diff --git a/base/sharedarray.jl b/base/sharedarray.jl index 65d95ed7f2e8c..9e4b0a98de2d7 100644 --- a/base/sharedarray.jl +++ b/base/sharedarray.jl @@ -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...) = @@ -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 @@ -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 @@ -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 @@ -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 @@ -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