Skip to content
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

make shared array constructor consistent to array constructor #29930

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
34 changes: 17 additions & 17 deletions stdlib/SharedArrays/src/SharedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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) ])
Expand Down
28 changes: 14 additions & 14 deletions stdlib/SharedArrays/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down