Skip to content

Commit 2fa11d1

Browse files
committed
Deprecate SharedArray(T, dims...)
1 parent 7025505 commit 2fa11d1

File tree

4 files changed

+89
-75
lines changed

4 files changed

+89
-75
lines changed

base/deprecated.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1685,4 +1685,12 @@ isempty(::Task) = error("isempty not defined for Tasks")
16851685
@deprecate Array{T}(::Type{T}, m::Integer,n::Integer) Array{T,2}(Int(m),Int(n))
16861686
@deprecate Array{T}(::Type{T}, m::Integer,n::Integer,o::Integer) Array{T,3}(Int(m),Int(n),Int(o))
16871687

1688+
# Likewise for SharedArrays
1689+
@deprecate SharedArray{T,N}(::Type{T}, dims::Dims{N}; kwargs...) SharedArray{T,N}(dims; kwargs...)
1690+
@deprecate SharedArray{T}(::Type{T}, dims::Int...; kwargs...) SharedArray{T,length(dims)}(dims...; kwargs...)
1691+
@deprecate(SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,Int}, offset; kwargs...),
1692+
SharedArray{T,N}(filename, dims, offset; kwargs...))
1693+
@deprecate(SharedArray{T}(filename::AbstractString, ::Type{T}, dims::NTuple, offset; kwargs...),
1694+
SharedArray{T,length(dims)}(filename, dims, offset; kwargs...))
1695+
16881696
# End deprecations scheduled for 0.6

base/sharedarray.jl

+59-53
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,8 @@ type SharedArray{T,N} <: DenseArray{T,N}
2828
end
2929
end
3030

31-
(::Type{SharedArray{T}}){T,N}(d::NTuple{N,Int}; kwargs...) =
32-
SharedArray(T, d; kwargs...)
33-
(::Type{SharedArray{T}}){T}(d::Integer...; kwargs...) =
34-
SharedArray(T, d; kwargs...)
35-
(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) =
36-
SharedArray(T, m; kwargs...)
37-
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) =
38-
SharedArray(T, m, n; kwargs...)
39-
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) =
40-
SharedArray(T, m, n, o; kwargs...)
41-
4231
"""
43-
SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
32+
SharedArray{T,N}(dims::NTuple; init=false, pids=Int[])
4433
4534
Construct a `SharedArray` of a bitstype `T` and size `dims` across the processes specified
4635
by `pids` - all of which have to be on the same host.
@@ -52,8 +41,39 @@ computation with the master process acting as a driver.
5241
5342
If an `init` function of the type `initfn(S::SharedArray)` is specified, it is called on all
5443
the participating workers.
44+
45+
SharedArray{T,N}(filename::AbstractString, dims::NTuple, [offset=0]; mode=nothing, init=false, pids=Int[])
46+
47+
Construct a `SharedArray` backed by the file `filename`, with element
48+
type `T` (must be a `bitstype`) and size `dims`, across the processes
49+
specified by `pids` - all of which have to be on the same host. This
50+
file is mmapped into the host memory, with the following consequences:
51+
52+
- The array data must be represented in binary format (e.g., an ASCII
53+
format like CSV cannot be supported)
54+
55+
- Any changes you make to the array values (e.g., `A[3] = 0`) will
56+
also change the values on disk
57+
58+
If `pids` is left unspecified, the shared array will be mapped across
59+
all processes on the current host, including the master. But,
60+
`localindexes` and `indexpids` will only refer to worker
61+
processes. This facilitates work distribution code to use workers for
62+
actual computation with the master process acting as a driver.
63+
64+
`mode` must be one of `"r"`, `"r+"`, `"w+"`, or `"a+"`, and defaults
65+
to `"r+"` if the file specified by `filename` already exists, or
66+
`"w+"` if not. If an `init` function of the type
67+
`initfn(S::SharedArray)` is specified, it is called on all the
68+
participating workers. You cannot specify an `init` function if the
69+
file is not writable.
70+
71+
`offset` allows you to skip the specified number of bytes at the
72+
beginning of the file.
5573
"""
56-
function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
74+
SharedArray
75+
76+
function (::Type{SharedArray{T,N}}){T,N}(dims::Dims{N}; init=false, pids=Int[])
5777
isbits(T) || throw(ArgumentError("type of SharedArray elements must be bits types, got $(T)"))
5878

5979
pids, onlocalhost = shared_pids(pids)
@@ -110,39 +130,20 @@ function SharedArray{T,N}(::Type{T}, dims::Dims{N}; init=false, pids=Int[])
110130
S
111131
end
112132

113-
SharedArray(T, I::Int...; kwargs...) = SharedArray(T, I; kwargs...)
114-
115-
"""
116-
SharedArray(filename::AbstractString, T::Type, dims::NTuple, [offset=0]; mode=nothing, init=false, pids=Int[])
117-
118-
Construct a `SharedArray` backed by the file `filename`, with element
119-
type `T` (must be a `bitstype`) and size `dims`, across the processes
120-
specified by `pids` - all of which have to be on the same host. This
121-
file is mmapped into the host memory, with the following consequences:
122-
123-
- The array data must be represented in binary format (e.g., an ASCII
124-
format like CSV cannot be supported)
125-
126-
- Any changes you make to the array values (e.g., `A[3] = 0`) will
127-
also change the values on disk
128-
129-
If `pids` is left unspecified, the shared array will be mapped across
130-
all processes on the current host, including the master. But,
131-
`localindexes` and `indexpids` will only refer to worker
132-
processes. This facilitates work distribution code to use workers for
133-
actual computation with the master process acting as a driver.
134-
135-
`mode` must be one of `"r"`, `"r+"`, `"w+"`, or `"a+"`, and defaults
136-
to `"r+"` if the file specified by `filename` already exists, or
137-
`"w+"` if not. If an `init` function of the type
138-
`initfn(S::SharedArray)` is specified, it is called on all the
139-
participating workers. You cannot specify an `init` function if the
140-
file is not writable.
133+
(::Type{SharedArray{T,N}}){T,N}(I::Integer...; kwargs...) =
134+
SharedArray{T,N}(I; kwargs...)
135+
(::Type{SharedArray{T}}){T}(d::NTuple; kwargs...) =
136+
SharedArray{T,length(d)}(d; kwargs...)
137+
(::Type{SharedArray{T}}){T}(I::Integer...; kwargs...) =
138+
SharedArray{T,length(I)}(I; kwargs...)
139+
(::Type{SharedArray{T}}){T}(m::Integer; kwargs...) =
140+
SharedArray{T,1}(m; kwargs...)
141+
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer; kwargs...) =
142+
SharedArray{T,2}(m, n; kwargs...)
143+
(::Type{SharedArray{T}}){T}(m::Integer, n::Integer, o::Integer; kwargs...) =
144+
SharedArray{T,3}(m, n, o; kwargs...)
141145

142-
`offset` allows you to skip the specified number of bytes at the
143-
beginning of the file.
144-
"""
145-
function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,Int},
146+
function (::Type{SharedArray{T,N}}){T,N}(filename::AbstractString, dims::NTuple{N,Int},
146147
offset::Integer=0; mode=nothing, init=false, pids::Vector{Int}=Int[])
147148
if !isabspath(filename)
148149
throw(ArgumentError("$filename is not an absolute path; try abspath(filename)?"))
@@ -208,6 +209,10 @@ function SharedArray{T,N}(filename::AbstractString, ::Type{T}, dims::NTuple{N,In
208209
S
209210
end
210211

212+
(::Type{SharedArray{T}}){T,N}(filename::AbstractString, dims::NTuple{N,Int}, offset::Integer=0;
213+
mode=nothing, init=false, pids::Vector{Int}=Int[]) =
214+
SharedArray{T,N}(filename, dims, offset; mode=mode, init=init, pids=pids)
215+
211216
function initialize_shared_array(S, onlocalhost, init, pids)
212217
if onlocalhost
213218
init_loc_flds(S)
@@ -246,6 +251,7 @@ typealias SharedMatrix{T} SharedArray{T,2}
246251

247252
length(S::SharedArray) = prod(S.dims)
248253
size(S::SharedArray) = S.dims
254+
ndims(S::SharedArray) = length(S.dims)
249255
linearindexing{S<:SharedArray}(::Type{S}) = LinearFast()
250256

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

309315
function convert(::Type{SharedArray}, A::Array)
310-
S = SharedArray(eltype(A), size(A))
316+
S = SharedArray{eltype(A),ndims(A)}(size(A))
311317
copy!(S, A)
312318
end
313319
function convert{T}(::Type{SharedArray{T}}, A::Array)
314-
S = SharedArray(T, size(A))
320+
S = SharedArray{T,ndims(A)}(size(A))
315321
copy!(S, A)
316322
end
317323
function convert{TS,TA,N}(::Type{SharedArray{TS,N}}, A::Array{TA,N})
318-
S = SharedArray(TS, size(A))
324+
S = SharedArray{TS,ndims(A)}(size(A))
319325
copy!(S, A)
320326
end
321327

322328
function deepcopy_internal(S::SharedArray, stackdict::ObjectIdDict)
323329
haskey(stackdict, S) && return stackdict[S]
324-
R = SharedArray(eltype(S), size(S); pids = S.pids)
330+
R = SharedArray{eltype(S),ndims(S)}(size(S); pids = S.pids)
325331
copy!(sdata(R), sdata(S))
326332
stackdict[S] = R
327333
return R
@@ -468,16 +474,16 @@ end
468474

469475
# convenience constructors
470476
function shmem_fill(v, dims; kwargs...)
471-
SharedArray(typeof(v), dims; init = S->fill!(S.loc_subarr_1d, v), kwargs...)
477+
SharedArray{typeof(v),length(dims)}(dims; init = S->fill!(S.loc_subarr_1d, v), kwargs...)
472478
end
473479
shmem_fill(v, I::Int...; kwargs...) = shmem_fill(v, I; kwargs...)
474480

475481
# rand variant with range
476482
function shmem_rand(TR::Union{DataType, UnitRange}, dims; kwargs...)
477483
if isa(TR, UnitRange)
478-
SharedArray(Int, dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
484+
SharedArray{Int,length(dims)}(dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
479485
else
480-
SharedArray(TR, dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
486+
SharedArray{TR,length(dims)}(dims; init = S -> map!(x -> rand(TR), S.loc_subarr_1d, S.loc_subarr_1d), kwargs...)
481487
end
482488
end
483489
shmem_rand(TR::Union{DataType, UnitRange}, i::Int; kwargs...) = shmem_rand(TR, (i,); kwargs...)
@@ -487,7 +493,7 @@ shmem_rand(dims; kwargs...) = shmem_rand(Float64, dims; kwargs...)
487493
shmem_rand(I::Int...; kwargs...) = shmem_rand(I; kwargs...)
488494

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

doc/src/manual/parallel-computing.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Parallel for loops like these must be avoided. Fortunately, [Shared Arrays](@ref
312312
to get around this limitation:
313313

314314
```julia
315-
a = SharedArray(Float64,10)
315+
a = SharedArray{Float64}(10)
316316
@parallel for i=1:10
317317
a[i] = i
318318
end
@@ -720,10 +720,10 @@ just returns the object itself, so it's safe to use [`sdata()`](@ref) on any `Ar
720720
The constructor for a shared array is of the form:
721721

722722
```julia
723-
SharedArray(T::Type, dims::NTuple; init=false, pids=Int[])
723+
SharedArray{T,N}(dims::NTuple; init=false, pids=Int[])
724724
```
725725

726-
which creates a shared array of a bits type `T` and size `dims` across the processes specified
726+
which creates an `N`-dimensional shared array of a bits type `T` and size `dims` across the processes specified
727727
by `pids`. Unlike distributed arrays, a shared array is accessible only from those participating
728728
workers specified by the `pids` named argument (and the creating process too, if it is on the
729729
same host).
@@ -741,7 +741,7 @@ julia> addprocs(3)
741741
3
742742
4
743743

744-
julia> S = SharedArray(Int, (3,4), init = S -> S[Base.localindexes(S)] = myid())
744+
julia> S = SharedArray{Int,2}((3,4), init = S -> S[Base.localindexes(S)] = myid())
745745
3×4 SharedArray{Int64,2}:
746746
2 2 3 4
747747
2 3 3 4
@@ -762,7 +762,7 @@ convenient for splitting up tasks among processes. You can, of course, divide th
762762
you wish:
763763

764764
```julia
765-
julia> S = SharedArray(Int, (3,4), init = S -> S[indexpids(S):length(procs(S)):length(S)] = myid())
765+
julia> S = SharedArray{Int,2}((3,4), init = S -> S[indexpids(S):length(procs(S)):length(S)] = myid())
766766
3×4 SharedArray{Int64,2}:
767767
2 2 2 2
768768
3 3 3 3
@@ -861,8 +861,8 @@ end
861861
If we create `SharedArray`s and time these functions, we get the following results (with `julia -p 4`):
862862

863863
```julia
864-
q = SharedArray(Float64, (500,500,500))
865-
u = SharedArray(Float64, (500,500,500))
864+
q = SharedArray{Float64,3}((500,500,500))
865+
u = SharedArray{Float64,3}((500,500,500))
866866

867867
# Run once to JIT-compile
868868
advection_serial!(q, u)

test/parallel_exec.jl

+15-15
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,11 @@ test_indexing(RemoteChannel(id_other))
231231
dims = (20,20,20)
232232

233233
if is_linux()
234-
S = SharedArray(Int64, dims)
234+
S = SharedArray{Int64,3}(dims)
235235
@test startswith(S.segname, "/jl")
236236
@test !ispath("/dev/shm" * S.segname)
237237

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

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

312-
d = @inferred(SharedArray(Float64, (2,3)))
312+
d = @inferred(SharedArray{Float64,2}((2,3)))
313313
@test isa(d[:,2], Vector{Float64})
314314

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

323-
S = @inferred(SharedArray(fn, Int, sz))
323+
S = @inferred(SharedArray{Int,2}(fn, sz))
324324
@test S == Atrue
325325
@test length(procs(S)) > 1
326326
@sync begin
@@ -338,14 +338,14 @@ read!(fn, filedata)
338338
finalize(S)
339339

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

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

346346
# Creating a new file
347347
fn2 = tempname()
348-
S = SharedArray(fn2, Int, sz, init=D->D[localindexes(D)] = myid())
348+
S = SharedArray{Int,2}(fn2, sz, init=D->D[localindexes(D)] = myid())
349349
@test S == filedata
350350
filedata2 = similar(Atrue)
351351
read!(fn2, filedata2)
@@ -355,7 +355,7 @@ finalize(S)
355355
# Appending to a file
356356
fn3 = tempname()
357357
write(fn3, ones(UInt8, 4))
358-
S = SharedArray(fn3, UInt8, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
358+
S = SharedArray{UInt8}(fn3, sz, 4, mode="a+", init=D->D[localindexes(D)]=0x02)
359359
len = prod(sz)+4
360360
@test filesize(fn3) == len
361361
filedata = Array{UInt8}(len)
@@ -438,7 +438,7 @@ A = @inferred(convert(SharedArray, AA))
438438
B = @inferred(convert(SharedArray, AA'))
439439
@test B*A == ctranspose(AA)*AA
440440

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

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

466466
# Issue #14664
467-
d = SharedArray(Int,10)
467+
d = SharedArray{Int}(10)
468468
@sync @parallel for i=1:10
469469
d[i] = i
470470
end
@@ -474,8 +474,8 @@ for (x,i) in enumerate(d)
474474
end
475475

476476
# complex
477-
sd = SharedArray(Int,10)
478-
se = SharedArray(Int,10)
477+
sd = SharedArray{Int}(10)
478+
se = SharedArray{Int}(10)
479479
@sync @parallel for i=1:10
480480
sd[i] = i
481481
se[i] = i
@@ -498,7 +498,7 @@ for id in [id_me, id_other]
498498
finalize_and_test((r=RemoteChannel(id); put!(r, 1); r))
499499
end
500500

501-
d = SharedArray(Int,10)
501+
d = SharedArray{Int}(10)
502502
finalize(d)
503503
@test_throws BoundsError d[1]
504504

0 commit comments

Comments
 (0)