Skip to content

Commit 6ddf9bc

Browse files
authored
Merge pull request #17095 from JuliaLang/amitm/shmemperf
initialize underlying array in sharedarray at construction time
2 parents e10bd49 + e135774 commit 6ddf9bc

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

base/sharedarray.jl

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ type SharedArray{T,N} <: DenseArray{T,N}
2222
# a subset of workers.
2323
loc_subarr_1d::SubArray{T,1,Array{T,1},Tuple{UnitRange{Int}},true}
2424

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

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

6062
local shm_seg_name = ""
61-
local s
63+
local s = Array{T}(ntuple(d->0,N))
6264
local S = nothing
6365
local shmmem_create_pid
6466
try
@@ -97,8 +99,8 @@ function SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
9799
end
98100
systemerror("Error unlinking shmem segment " * shm_seg_name, rc != 0)
99101
end
100-
S = SharedArray{T,N}(dims, pids, refs, shm_seg_name)
101-
initialize_shared_array(S, s, onlocalhost, init, pids)
102+
S = SharedArray{T,N}(dims, pids, refs, shm_seg_name, s)
103+
initialize_shared_array(S, onlocalhost, init, pids)
102104
shm_seg_name = ""
103105

104106
finally
@@ -167,7 +169,7 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
167169
func_mmap = mode -> open(filename, mode) do io
168170
Mmap.mmap(io, Array{T,N}, dims, offset; shared=true)
169171
end
170-
local s
172+
s = Array{T}(ntuple(d->0,N))
171173
if onlocalhost
172174
s = func_mmap(mode)
173175
refs[1] = remotecall(pids[1]) do
@@ -191,17 +193,14 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
191193
wait(ref)
192194
end
193195

194-
S = SharedArray{T,N}(dims, pids, refs, filename)
195-
initialize_shared_array(S, s, onlocalhost, init, pids)
196+
S = SharedArray{T,N}(dims, pids, refs, filename, s)
197+
initialize_shared_array(S, onlocalhost, init, pids)
196198
S
197199
end
198200

199-
function initialize_shared_array(S, s, onlocalhost, init, pids)
201+
function initialize_shared_array(S, onlocalhost, init, pids)
200202
if onlocalhost
201203
init_loc_flds(S)
202-
# In the event that myid() is not part of pids, s will not be set
203-
# in the init function above, hence setting it here if available.
204-
S.s = s
205204
else
206205
S.pidx = 0
207206
end
@@ -248,9 +247,8 @@ function reshape{T,N}(a::SharedArray{T}, dims::NTuple{N,Int})
248247
end
249248
end
250249

251-
A = SharedArray{T,N}(dims, a.pids, refs, a.segname)
250+
A = SharedArray{T,N}(dims, a.pids, refs, a.segname, reshape(a.s, dims))
252251
init_loc_flds(A)
253-
(a.pidx == 0) && isdefined(a, :s) && (A.s = reshape(a.s, dims))
254252
A
255253
end
256254

0 commit comments

Comments
 (0)