Skip to content

Commit

Permalink
Deprecate SharedArray(T, dims...)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jan 13, 2017
1 parent 190504f commit 753861e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 39 deletions.
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1649,4 +1649,12 @@ isempty(::Task) = error("isempty not defined for Tasks")
@deprecate Array{T}(::Type{T}, m::Integer,n::Integer) Array{T,2}(Int(m),Int(n))
@deprecate Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) Array{T,3}(Int(m),Int(n),Int(o))

# Likewise for SharedArrays
@deprecate SharedArray{T,N}(::Type{T}, dims::Dims{N}; kwargs...) SharedArray{T,N}(dims; kwargs...)
@deprecate SharedArray{T}(::Type{T}, dims::Int...; kwargs...) SharedArray{T,length(dims)}(dims...; kwargs...)
@deprecate(SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,Int}, offset; kwargs...),
SharedArray{T,N}(filename, dims, offset; kwargs...))
@deprecate(SharedArray{T}(filename::AbstractString, ::Type{T}, dims::NTuple, offset; kwargs...),
SharedArray{T,length(dims)}(filename, dims, offset; kwargs...))

# End deprecations scheduled for 0.6
49 changes: 25 additions & 24 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,8 @@ type SharedArray{T,N} <: DenseArray{T,N}
end
end

(::Type{SharedArray{T}}){T,N}(d::NTuple{N,Int}; kwargs...) =
SharedArray(T, d; kwargs...)
(::Type{SharedArray{T}}){T}(d::Integer...; kwargs...) =
SharedArray(T, d; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) =
SharedArray(T, m; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) =
SharedArray(T, m, n; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) =
SharedArray(T, m, n, o; kwargs...)

"""
SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
SharedArray{T,N}(dims::NTuple; init=false, pids=Int[])
Construct a `SharedArray` of a bitstype `T` and size `dims` across the processes specified
by `pids` - all of which have to be on the same host.
Expand All @@ -53,7 +42,7 @@ computation with the master process acting as a driver.
If an `init` function of the type `initfn(S::SharedArray)` is specified, it is called on all
the participating workers.
"""
function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
function (::Type{SharedArray{T,N}}){T,N}(dims::Dims{N}; init=false, pids=Int[])
isbits(T) || throw(ArgumentError("type of SharedArray elements must be bits types, got $(T)"))

pids, onlocalhost = shared_pids(pids)
Expand Down Expand Up @@ -110,10 +99,17 @@ function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
S
end

SharedArray(T, I::Int...; kwargs...) = SharedArray(T, I; kwargs...)
(::Type{SharedArray{T}}){T}(I::Integer...; kwargs...) =
SharedArray{T,length(I)}(I; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) =
SharedArray{T,1}(m; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) =
SharedArray{T,2}(m, n; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) =
SharedArray{T,3}(m, n, o; kwargs...)

"""
SharedArray(filename::AbstractString, T::Type, dims::NTuple, [offset=0]; mode=nothing, init=false, pids=Int[])
SharedArray{T,N}(filename::AbstractString, dims::NTuple, [offset=0]; mode=nothing, init=false, pids=Int[])
Construct a `SharedArray` backed by the file `filename`, with element
type `T` (must be a `bitstype`) and size `dims`, across the processes
Expand Down Expand Up @@ -142,7 +138,7 @@ file is not writable.
`offset` allows you to skip the specified number of bytes at the
beginning of the file.
"""
function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,Int},
function (::Type{SharedArray{T,N}}){T,N}(filename::AbstractString, dims::NTuple{N,Int},
offset::Integer=0; mode=nothing, init=false, pids::Vector{Int}=Int[])
if !isabspath(filename)
throw(ArgumentError("$filename is not an absolute path; try abspath(filename)?"))
Expand Down Expand Up @@ -208,6 +204,10 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
S
end

(::Type{SharedArray{T}}){T,N}(filename::AbstractString, dims::NTuple{N,Int}, offset::Integer=0;
mode=nothing, init=false, pids::Vector{Int}=Int[]) =
SharedArray{T,N}(filename, dims, offset; mode=mode, init=init, pids=pids)

function initialize_shared_array(S, onlocalhost, init, pids)
if onlocalhost
init_loc_flds(S)
Expand Down Expand Up @@ -246,6 +246,7 @@ typealias SharedMatrix{T} SharedArray{T,2}

length(S::SharedArray) = prod(S.dims)
size(S::SharedArray) = S.dims
ndims(S::SharedArray) = length(S.dims)
linearindexing{S<:SharedArray}(::Type{S}) = LinearFast()

function reshape{T,N}(a::SharedArray{T}, dims::NTuple{N,Int})
Expand Down Expand Up @@ -307,21 +308,21 @@ localindexes(S::SharedArray) = S.pidx > 0 ? range_1dim(S, S.pidx) : 1:0
unsafe_convert{T}(::Type{Ptr{T}}, S::SharedArray) = unsafe_convert(Ptr{T}, sdata(S))

function convert(::Type{SharedArray}, A::Array)
S = SharedArray(eltype(A), size(A))
S = SharedArray{eltype(A),ndims(A)}(size(A))
copy!(S, A)
end
function convert{T}(::Type{SharedArray{T}}, A::Array)
S = SharedArray(T, size(A))
S = SharedArray{T,ndims(A)}(size(A))
copy!(S, A)
end
function convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N})
S = SharedArray(TS, size(A))
S = SharedArray{TS,ndims(A)}(size(A))
copy!(S, A)
end

function deepcopy_internal(S::SharedArray, stackdict::ObjectIdDict)
haskey(stackdict, S) && return stackdict[S]
R = SharedArray(eltype(S), size(S); pids = S.pids)
R = SharedArray{eltype(S),ndims(S)}(size(S); pids = S.pids)
copy!(sdata(R), sdata(S))
stackdict[S] = R
return R
Expand Down Expand Up @@ -468,16 +469,16 @@ end

# convenience constructors
function shmem_fill(v, dims; kwargs...)
SharedArray(typeof(v), dims; init = S->fill!(S.loc_subarr_1d, v), kwargs...)
SharedArray{typeof(v),length(dims)}(dims; init = S->fill!(S.loc_subarr_1d, v), kwargs...)
end
shmem_fill(v, I::Int...; kwargs...) = shmem_fill(v, I; kwargs...)

# rand variant with range
function shmem_rand(TR::Union{DataType, UnitRange}, dims; kwargs...)
if isa(TR, UnitRange)
SharedArray(Int, dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
SharedArray{Int,length(dims)}(dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
else
SharedArray(TR, dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
SharedArray{TR,length(dims)}(dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
end
end
shmem_rand(TR::Union{DataType, UnitRange}, i::Int; kwargs...) = shmem_rand(TR, (i,); kwargs...)
Expand All @@ -487,7 +488,7 @@ shmem_rand(dims; kwargs...) = shmem_rand(Float64, dims; kwargs...)
shmem_rand(I::Int...; kwargs...) = shmem_rand(I; kwargs...)

function shmem_randn(dims; kwargs...)
SharedArray(Float64, dims; init = S-> map!(x -> randn(), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
SharedArray{Float64,length(dims)}(dims; init = S-> map!(x -> randn(), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
end
shmem_randn(I::Int...; kwargs...) = shmem_randn(I; kwargs...)

Expand Down
30 changes: 15 additions & 15 deletions test/parallel_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ test_indexing(RemoteChannel(id_other))
dims = (20,20,20)

if is_linux()
S = SharedArray(Int64, dims)
S = SharedArray{Int64,3}(dims)
@test startswith(S.segname, "/jl")
@test !ispath("/dev/shm" * S.segname)

S = SharedArray(Int64, dims; pids=[id_other])
S = SharedArray{Int64,3}(dims; pids=[id_other])
@test startswith(S.segname, "/jl")
@test !ispath("/dev/shm" * S.segname)
end
Expand Down Expand Up @@ -298,7 +298,7 @@ copy!(s, sdata(d))
a = rand(dims)
@test sdata(a) == a

d = SharedArray(Int, dims, init = D->fill!(D.loc_subarr_1d, myid()))
d = SharedArray{Int}(dims, init = D->fill!(D.loc_subarr_1d, myid()))
for p in procs(d)
idxes_in_p = remotecall_fetch(p, d) do D
parentindexes(D.loc_subarr_1d)[1]
Expand All @@ -309,7 +309,7 @@ for p in procs(d)
@test d[idxl] == p
end

d = @inferred(SharedArray(Float64, (2,3)))
d = @inferred(SharedArray{Float64,2}((2,3)))
@test isa(d[:,2], Vector{Float64})

### SharedArrays from a file
Expand All @@ -320,7 +320,7 @@ write(fn, 1:30)
sz = (6,5)
Atrue = reshape(1:30, sz)

S = @inferred(SharedArray(fn, Int, sz))
S = @inferred(SharedArray{Int,2}(fn, sz))
@test S == Atrue
@test length(procs(S)) > 1
@sync begin
Expand All @@ -338,14 +338,14 @@ read!(fn, filedata)
finalize(S)

# Error for write-only files
@test_throws ArgumentError SharedArray(fn, Int, sz, mode="w")
@test_throws ArgumentError SharedArray{Int,2}(fn, sz, mode="w")

# Error for file doesn't exist, but not allowed to create
@test_throws ArgumentError SharedArray(joinpath(tempdir(),randstring()), Int, sz, mode="r")
@test_throws ArgumentError SharedArray{Int,2}(joinpath(tempdir(),randstring()), sz, mode="r")

# Creating a new file
fn2 = tempname()
S = SharedArray(fn2, Int, sz, init=D->D[localindexes(D)] = myid())
S = SharedArray{Int,2}(fn2, sz, init=D->D[localindexes(D)] = myid())
@test S == filedata
filedata2 = similar(Atrue)
read!(fn2, filedata2)
Expand All @@ -355,7 +355,7 @@ finalize(S)
# Appending to a file
fn3 = tempname()
write(fn3, ones(UInt8, 4))
S = SharedArray(fn3, UInt8, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
S = SharedArray{UInt8}(fn3, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
len = prod(sz)+4
@test filesize(fn3) == len
filedata = Array{UInt8}(len)
Expand Down Expand Up @@ -438,7 +438,7 @@ A = @inferred(convert(SharedArray, AA))
B = @inferred(convert(SharedArray, AA'))
@test B*A == ctranspose(AA)*AA

d=SharedArray(Int64, (10,10); init = D->fill!(D.loc_subarr_1d, myid()), pids=[id_me, id_other])
d=SharedArray{Int64,2}((10,10); init = D->fill!(D.loc_subarr_1d, myid()), pids=[id_me, id_other])
d2 = map(x->1, d)
@test reduce(+, d2) == 100

Expand All @@ -459,12 +459,12 @@ map!(x->1, d, d)
# Shared arrays of singleton immutables
@everywhere immutable ShmemFoo end
for T in [Void, ShmemFoo]
s = @inferred(SharedArray(T, 10))
s = @inferred(SharedArray{T}(10))
@test T() === remotecall_fetch(x->x[3], workers()[1], s)
end

# Issue #14664
d = SharedArray(Int,10)
d = SharedArray{Int}(10)
@sync @parallel for i=1:10
d[i] = i
end
Expand All @@ -474,8 +474,8 @@ for (x,i) in enumerate(d)
end

# complex
sd = SharedArray(Int,10)
se = SharedArray(Int,10)
sd = SharedArray{Int}(10)
se = SharedArray{Int}(10)
@sync @parallel for i=1:10
sd[i] = i
se[i] = i
Expand All @@ -498,7 +498,7 @@ 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}(10)
finalize(d)
@test_throws BoundsError d[1]

Expand Down

0 comments on commit 753861e

Please sign in to comment.