Skip to content

Commit 85535ae

Browse files
authored
Merge pull request #17104 from JuliaLang/teh/inferrable_sharedarray
Make the SharedArray constructor inferrable. Fixes #17101
2 parents 1a1a9a6 + 539142e commit 85535ae

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

base/sharedarray.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ computation with the master process acting as a driver.
5252
If an `init` function of the type `initfn(S::SharedArray)` is specified, it is called on all
5353
the participating workers.
5454
"""
55-
function SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
56-
N = length(dims)
55+
function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
5756

5857
isbits(T) || throw(ArgumentError("type of SharedArray elements must be bits types, got $(T)"))
5958

6059
pids, onlocalhost = shared_pids(pids)
6160

6261
local shm_seg_name = ""
6362
local s = Array{T}(ntuple(d->0,N))
64-
local S = nothing
63+
local S
6564
local shmmem_create_pid
6665
try
6766
# On OSX, the shm_seg_name length must be <= 31 characters (including the terminating NULL character)

test/parallel_exec.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ copy!(s, sdata(d))
294294
a = rand(dims)
295295
@test sdata(a) == a
296296

297-
d = SharedArray(Int, dims; init = D->fill!(D.loc_subarr_1d, myid()))
297+
d = SharedArray(Int, dims, init = D->fill!(D.loc_subarr_1d, myid()))
298298
for p in procs(d)
299299
idxes_in_p = remotecall_fetch(p, d) do D
300300
parentindexes(D.loc_subarr_1d)[1]
@@ -305,7 +305,7 @@ for p in procs(d)
305305
@test d[idxl] == p
306306
end
307307

308-
d = SharedArray(Float64, (2,3))
308+
d = @inferred(SharedArray(Float64, (2,3)))
309309
@test isa(d[:,2], Vector{Float64})
310310

311311
### SharedArrays from a file
@@ -316,7 +316,7 @@ write(fn, 1:30)
316316
sz = (6,5)
317317
Atrue = reshape(1:30, sz)
318318

319-
S = SharedArray(fn, Int, sz)
319+
S = @inferred(SharedArray(fn, Int, sz))
320320
@test S == Atrue
321321
@test length(procs(S)) > 1
322322
@sync begin
@@ -370,16 +370,16 @@ rm(fn); rm(fn2); rm(fn3)
370370
### Utility functions
371371

372372
# construct PR #13514
373-
S = SharedArray{Int}((1,2,3))
373+
S = @inferred(SharedArray{Int}((1,2,3)))
374374
@test size(S) == (1,2,3)
375375
@test typeof(S) <: SharedArray{Int}
376-
S = SharedArray{Int}(2)
376+
S = @inferred(SharedArray{Int}(2))
377377
@test size(S) == (2,)
378378
@test typeof(S) <: SharedArray{Int}
379-
S = SharedArray{Int}(1,2)
379+
S = @inferred(SharedArray{Int}(1,2))
380380
@test size(S) == (1,2)
381381
@test typeof(S) <: SharedArray{Int}
382-
S = SharedArray{Int}(1,2,3)
382+
S = @inferred(SharedArray{Int}(1,2,3))
383383
@test size(S) == (1,2,3)
384384
@test typeof(S) <: SharedArray{Int}
385385

@@ -430,8 +430,8 @@ d[2:4] = 7
430430
d[5,1:2:4,8] = 19
431431

432432
AA = rand(4,2)
433-
A = convert(SharedArray, AA)
434-
B = convert(SharedArray, AA')
433+
A = @inferred(convert(SharedArray, AA))
434+
B = @inferred(convert(SharedArray, AA'))
435435
@test B*A == ctranspose(AA)*AA
436436

437437
d=SharedArray(Int64, (10,10); init = D->fill!(D.loc_subarr_1d, myid()), pids=[id_me, id_other])
@@ -455,7 +455,7 @@ map!(x->1, d)
455455
# Shared arrays of singleton immutables
456456
@everywhere immutable ShmemFoo end
457457
for T in [Void, ShmemFoo]
458-
s = SharedArray(T, 10)
458+
s = @inferred(SharedArray(T, 10))
459459
@test T() === remotecall_fetch(x->x[3], workers()[1], s)
460460
end
461461

0 commit comments

Comments
 (0)