From ea259841e5fbee1ac79c1055c808e67042963a0b Mon Sep 17 00:00:00 2001 From: Roger-luo Date: Sun, 4 Nov 2018 17:47:56 -0500 Subject: [PATCH 1/3] WIP: make shared array constructor consistent to array constructor --- stdlib/SharedArrays/src/SharedArrays.jl | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/stdlib/SharedArrays/src/SharedArrays.jl b/stdlib/SharedArrays/src/SharedArrays.jl index 89e6f11fd9693..56f26a4ecd474 100644 --- a/stdlib/SharedArrays/src/SharedArrays.jl +++ b/stdlib/SharedArrays/src/SharedArrays.jl @@ -101,7 +101,7 @@ beginning of the file. """ SharedArray -function SharedArray{T,N}(dims::Dims{N}; init=false, pids=Int[]) where {T,N} +function SharedArray{T,N}(::UndefInitializer, dims::Dims{N}; init=false, pids=Int[]) where {T,N} isbitstype(T) || throw(ArgumentError("type of SharedArray elements must be bits types, got $(T)")) pids, onlocalhost = shared_pids(pids) @@ -158,18 +158,18 @@ function SharedArray{T,N}(dims::Dims{N}; init=false, pids=Int[]) where {T,N} S end -SharedArray{T,N}(I::Integer...; kwargs...) where {T,N} = - SharedArray{T,N}(I; kwargs...) -SharedArray{T}(d::NTuple; kwargs...) where {T} = - SharedArray{T,length(d)}(d; kwargs...) -SharedArray{T}(I::Integer...; kwargs...) where {T} = - SharedArray{T,length(I)}(I; kwargs...) -SharedArray{T}(m::Integer; kwargs...) where {T} = - SharedArray{T,1}(m; kwargs...) -SharedArray{T}(m::Integer, n::Integer; kwargs...) where {T} = - SharedArray{T,2}(m, n; kwargs...) -SharedArray{T}(m::Integer, n::Integer, o::Integer; kwargs...) where {T} = - SharedArray{T,3}(m, n, o; kwargs...) +SharedArray{T,N}(::UndefInitializer, I::Integer...; kwargs...) where {T,N} = + SharedArray{T,N}(undef, I; kwargs...) +SharedArray{T}(::UndefInitializer, d::NTuple; kwargs...) where {T} = + SharedArray{T,length(d)}(undef, d; kwargs...) +SharedArray{T}(::UndefInitializer, I::Integer...; kwargs...) where {T} = + SharedArray{T,length(I)}(undef, I; kwargs...) +SharedArray{T}(::UndefInitializer, m::Integer; kwargs...) where {T} = + SharedArray{T,1}(undef, m; kwargs...) +SharedArray{T}(::UndefInitializer, m::Integer, n::Integer; kwargs...) where {T} = + SharedArray{T,2}(undef, m, n; kwargs...) +SharedArray{T}(::UndefInitializer, m::Integer, n::Integer, o::Integer; kwargs...) where {T} = + SharedArray{T,3}(undef, m, n, o; kwargs...) function SharedArray{T,N}(filename::AbstractString, dims::NTuple{N,Int}, offset::Integer=0; mode=nothing, init=false, pids::Vector{Int}=Int[]) where {T,N} From 0dd5e8afdc6288f86922ae268e2c6f00466edd72 Mon Sep 17 00:00:00 2001 From: Roger-luo Date: Sun, 4 Nov 2018 20:29:49 -0500 Subject: [PATCH 2/3] similar returns SharedArray now --- stdlib/SharedArrays/src/SharedArrays.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/SharedArrays/src/SharedArrays.jl b/stdlib/SharedArrays/src/SharedArrays.jl index 56f26a4ecd474..8850b0fbcdd79 100644 --- a/stdlib/SharedArrays/src/SharedArrays.jl +++ b/stdlib/SharedArrays/src/SharedArrays.jl @@ -549,10 +549,10 @@ function shmem_randn(dims; kwargs...) end shmem_randn(I::Int...; kwargs...) = shmem_randn(I; kwargs...) -similar(S::SharedArray, T::Type, dims::Dims) = similar(S.s, T, dims) -similar(S::SharedArray, T::Type) = similar(S.s, T, size(S)) -similar(S::SharedArray, dims::Dims) = similar(S.s, eltype(S), dims) -similar(S::SharedArray) = similar(S.s, eltype(S), size(S)) +similar(S::SharedArray, T::Type, dims::Dims{N}; pids=S.pids) where N = SharedArray{T, N}(undef, dims; pids=pids) +similar(S::SharedArray{ST, N}, T::Type; pids=S.pids) where {ST, N} = similar(S, T, size(S); pids=pids) +similar(S::SharedArray, dims::Dims; pids=S.pids) = similar(S, eltype(S), dims; pids=pids) +similar(S::SharedArray; pids=S.pids) = similar(S, eltype(S), size(S); pids=pids) reduce(f, S::SharedArray) = mapreduce(fetch, f, Any[ @spawnat p reduce(f, S.loc_subarr_1d) for p in procs(S) ]) From 384f40379ff052657c510a36a27327e145646365 Mon Sep 17 00:00:00 2001 From: Roger-luo Date: Sun, 4 Nov 2018 20:36:31 -0500 Subject: [PATCH 3/3] fix test --- stdlib/SharedArrays/test/runtests.jl | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/stdlib/SharedArrays/test/runtests.jl b/stdlib/SharedArrays/test/runtests.jl index fc4d7429c18f2..430e275e78957 100644 --- a/stdlib/SharedArrays/test/runtests.jl +++ b/stdlib/SharedArrays/test/runtests.jl @@ -14,11 +14,11 @@ id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))] dims = (20,20,20) if Sys.islinux() - S = SharedArray{Int64,3}(dims) + S = SharedArray{Int64,3}(undef, dims) @test startswith(S.segname, "/jl") @test !ispath("/dev/shm" * S.segname) - S = SharedArray{Int64,3}(dims; pids=[id_other]) + S = SharedArray{Int64,3}(undef, dims; pids=[id_other]) @test startswith(S.segname, "/jl") @test !ispath("/dev/shm" * S.segname) end @@ -81,7 +81,7 @@ copyto!(s, sdata(d)) a = rand(Float64, dims) @test sdata(a) == a -d = SharedArray{Int}(dims, init = D->fill!(D.loc_subarr_1d, myid())) +d = SharedArray{Int}(undef, dims, init = D->fill!(D.loc_subarr_1d, myid())) for p in procs(d) idxes_in_p = remotecall_fetch(p, d) do D parentindices(D.loc_subarr_1d)[1] @@ -92,7 +92,7 @@ for p in procs(d) @test d[idxl] == p end -d = @inferred(SharedArray{Float64,2}((2,3))) +d = @inferred(SharedArray{Float64,2}(undef, (2,3))) @test isa(d[:,2], Vector{Float64}) ### SharedArrays from a file @@ -153,16 +153,16 @@ rm(fn); rm(fn2); rm(fn3) ### Utility functions # construct PR #13514 -S = @inferred(SharedArray{Int}((1,2,3))) +S = @inferred(SharedArray{Int}(undef, (1,2,3))) @test size(S) == (1,2,3) @test typeof(S) <: SharedArray{Int} -S = @inferred(SharedArray{Int}(2)) +S = @inferred(SharedArray{Int}(undef, 2)) @test size(S) == (2,) @test typeof(S) <: SharedArray{Int} -S = @inferred(SharedArray{Int}(1,2)) +S = @inferred(SharedArray{Int}(undef, 1,2)) @test size(S) == (1,2) @test typeof(S) <: SharedArray{Int} -S = @inferred(SharedArray{Int}(1,2,3)) +S = @inferred(SharedArray{Int}(undef, 1,2,3)) @test size(S) == (1,2,3) @test typeof(S) <: SharedArray{Int} @@ -236,12 +236,12 @@ map!(x->1, d, d) # Shared arrays of singleton immutables @everywhere struct ShmemFoo end for T in [Nothing, ShmemFoo] - local s = @inferred(SharedArray{T}(10)) + local s = @inferred(SharedArray{T}(undef, 10)) @test T() === remotecall_fetch(x->x[3], workers()[1], s) end # Issue #14664 -d = SharedArray{Int}(10) +d = SharedArray{Int}(undef, 10) @sync @distributed for i=1:10 d[i] = i end @@ -251,8 +251,8 @@ for (x,i) in enumerate(d) end # complex -sd = SharedArray{Int}(10) -se = SharedArray{Int}(10) +sd = SharedArray{Int}(undef, 10) +se = SharedArray{Int}(undef, 10) @sync @distributed for i=1:10 sd[i] = i se[i] = i @@ -276,13 +276,13 @@ for id in [id_me, id_other] finalize_and_test((r=RemoteChannel(id); put!(r, 1); r)) end -d = SharedArray{Int}(10) +d = SharedArray{Int}(undef, 10) finalize(d) @test_throws BoundsError d[1] # Issue 22139 let - aorig = a1 = SharedArray{Float64}((3, 3)) + aorig = a1 = SharedArray{Float64}(undef, (3, 3)) a1 = remotecall_fetch(fill!, id_other, a1, 1.0) @test objectid(aorig) == objectid(a1) id = a1.id