Skip to content

Commit

Permalink
WIP: Deprecate SharedArray(T, dims...) [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jan 13, 2017
1 parent 190504f commit f0faa78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 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
40 changes: 22 additions & 18 deletions base/sharedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ type SharedArray{T,N} <: DenseArray{T,N}
end

(::Type{SharedArray{T}}){T,N}(d::NTuple{N,Int}; kwargs...) =
SharedArray(T, d; kwargs...)
SharedArray{T,N}(d; kwargs...)
(::Type{SharedArray{T}}){T}(d::Integer...; kwargs...) =
SharedArray(T, d; kwargs...)
SharedArray{T,length(d)}(d; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) =
SharedArray(T, m; kwargs...)
SharedArray{T,1}(m; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) =
SharedArray(T, m, n; kwargs...)
SharedArray{T,2}(m, n; kwargs...)
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) =
SharedArray(T, m, n, o; kwargs...)
SharedArray{T,3}(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 +53,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 SharedArray{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 +110,10 @@ function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
S
end

SharedArray(T, I::Int...; kwargs...) = SharedArray(T, I; kwargs...)
SharedArray{T}(I::Int...; kwargs...) = SharedArray{T,length(I)}(I; 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 +142,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 SharedArray{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 @@ -307,21 +307,25 @@ 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))
sz = size(A)
S = SharedArray{eltype(A),length(sz)}(sz)
copy!(S, A)
end
function convert{T}(::Type{SharedArray{T}}, A::Array)
S = SharedArray(T, size(A))
sz = size(A)
S = SharedArray{T,length(sz)}(sz)
copy!(S, A)
end
function convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N})
S = SharedArray(TS, size(A))
sz = size(A)
S = SharedArray{TS,length(sz)}(sz)
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)
sz = size(S)
R = SharedArray{eltype(S),length(sz)}(sz; pids = S.pids)
copy!(sdata(R), sdata(S))
stackdict[S] = R
return R
Expand Down Expand Up @@ -468,16 +472,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 +491,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

0 comments on commit f0faa78

Please sign in to comment.