@@ -23,44 +23,40 @@ dfill(v, args...) = DArray(I->fill(v, map(length,I)), args...)
2323```
2424"""
2525mutable struct DArray{T,N,A} <: AbstractArray{T,N}
26- id:: Tuple
26+ id:: Tuple{Int,Int}
2727 dims:: NTuple{N,Int}
2828 pids:: Array{Int,N} # pids[i]==p ⇒ processor p has piece i
2929 indices:: Array{NTuple{N,UnitRange{Int}},N} # indices held by piece i
3030 cuts:: Vector{Vector{Int}} # cuts[d][i] = first index of chunk i in dimension d
3131 localpart:: Union{A,Nothing}
32- release:: Bool
3332
34- function DArray {T,N,A} (id, dims, pids, indices, cuts, lp) where {T,N,A}
33+ function DArray {T,N,A} (id:: Tuple{Int,Int} , dims:: NTuple{N,Int} , pids, indices, cuts, lp) where {T,N,A}
3534 # check invariants
3635 if dims != map (last, last (indices))
3736 throw (ArgumentError (" dimension of DArray (dim) and indices do not match" ))
3837 end
39- release = (myid () == id[1 ])
4038
4139 d = d_from_weakref_or_d (id)
4240 if d === nothing
43- d = new (id, dims, pids, indices, cuts, lp, release )
41+ d = new (id, dims, pids, indices, cuts, lp)
4442 end
4543
46- if release
47- push! (refs , id)
48- registry [id] = WeakRef (d)
49-
50- # println("Installing finalizer for : ", d.id, ", : ", object_id(d), ", isbits: ", isbits(d) )
51- finalizer (close, d)
44+ if first (id) == myid ()
45+ push! (REFS , id)
46+ REGISTRY [id] = WeakRef (d)
47+ finalizer (d) do d
48+ @async close_by_id ( d. id, d . pids )
49+ end
5250 end
5351 d
5452 end
5553
5654 DArray {T,N,A} () where {T,N,A} = new ()
5755end
5856
59- function d_from_weakref_or_d (id)
60- d = get (registry, id, nothing )
61- isa (d, WeakRef) && return d. value
62- return d
63- end
57+ unpack_weakref (x) = x
58+ unpack_weakref (x:: WeakRef ) = x. value
59+ d_from_weakref_or_d (id:: Tuple{Int,Int} ) = unpack_weakref (get (REGISTRY, id, nothing ))
6460
6561Base. eltype (:: Type{DArray{T}} ) where {T} = T
6662empty_localpart (T,N,A) = A (Array {T} (undef, ntuple (zero, N)))
@@ -77,41 +73,34 @@ Base.hash(d::DArray, h::UInt) = Base.hash(d.id, h)
7773
7874# # core constructors ##
7975
80- function DArray (id, init, dims, pids, idxs, cuts)
76+ function DArray (id:: Tuple{Int,Int} , init:: I , dims, pids, idxs, cuts) where {I}
8177 localtypes = Vector {DataType} (undef,length (pids))
82-
83- @sync begin
84- for i = 1 : length (pids)
85- @async begin
86- local typA
87- if isa (init, Function)
88- typA = remotecall_fetch (construct_localparts, pids[i], init, id, dims, pids, idxs, cuts)
89- else
90- # constructing from an array of remote refs.
91- typA = remotecall_fetch (construct_localparts, pids[i], init[i], id, dims, pids, idxs, cuts)
92- end
93- localtypes[i] = typA
94- end
78+ if init isa Function
79+ asyncmap! (localtypes, pids) do pid
80+ return remotecall_fetch (construct_localparts, pid, init, id, dims, pids, idxs, cuts)
81+ end
82+ else
83+ asyncmap! (localtypes, pids, init) do pid, pid_init
84+ # constructing from an array of remote refs.
85+ return remotecall_fetch (construct_localparts, pid, pid_init, id, dims, pids, idxs, cuts)
9586 end
9687 end
9788
98- if length ( unique ( localtypes)) != 1
89+ if ! allequal ( localtypes)
9990 @sync for p in pids
10091 @async remotecall_fetch (release_localpart, p, id)
10192 end
102- throw (ErrorException (" Constructed localparts have different `eltype`: $(localtypes) " ))
93+ throw (ErrorException (lazy " Constructed localparts have different `eltype`: $(localtypes)" ))
10394 end
10495 A = first (localtypes)
10596
10697 if myid () in pids
107- d = registry[id]
108- d = isa (d, WeakRef) ? d. value : d
98+ return unpack_weakref (REGISTRY[id])
10999 else
110100 T = eltype (A)
111101 N = length (dims)
112- d = DArray {T,N,A} (id, dims, pids, idxs, cuts, empty_localpart (T,N,A))
102+ return DArray {T,N,A} (id, dims, pids, idxs, cuts, empty_localpart (T,N,A))
113103 end
114- d
115104end
116105
117106function construct_localparts (init, id, dims, pids, idxs, cuts; T= nothing , A= nothing )
@@ -124,7 +113,7 @@ function construct_localparts(init, id, dims, pids, idxs, cuts; T=nothing, A=not
124113 end
125114 N = length (dims)
126115 d = DArray {T,N,A} (id, dims, pids, idxs, cuts, localpart)
127- registry [id] = d
116+ REGISTRY [id] = d
128117 A
129118end
130119
@@ -152,12 +141,10 @@ function ddata(;T::Type=Any, init::Function=I->nothing, pids=workers(), data::Ve
152141 end
153142
154143 if myid () in pids
155- d = registry[id]
156- d = isa (d, WeakRef) ? d. value : d
144+ return unpack_weakref (REGISTRY[id])
157145 else
158- d = DArray {T,1,T} (id, (npids,), pids, idxs, cuts, nothing )
146+ return DArray {T,1,T} (id, (npids,), pids, idxs, cuts, nothing )
159147 end
160- d
161148end
162149
163150function gather (d:: DArray{T,1,T} ) where T
428415function Base.:(== )(d:: SubDArray , a:: AbstractArray )
429416 cd = copy (d)
430417 t = cd == a
431- close (cd)
418+ finalize (cd)
432419 return t
433420end
434421Base.:(== )(a:: AbstractArray , d:: DArray ) = d == a
@@ -437,19 +424,19 @@ Base.:(==)(d1::DArray, d2::DArray) = invoke(==, Tuple{DArray, AbstractArray}, d1
437424function Base.:(== )(d1:: SubDArray , d2:: DArray )
438425 cd1 = copy (d1)
439426 t = cd1 == d2
440- close (cd1)
427+ finalize (cd1)
441428 return t
442429end
443430function Base.:(== )(d1:: DArray , d2:: SubDArray )
444431 cd2 = copy (d2)
445432 t = d1 == cd2
446- close (cd2)
433+ finalize (cd2)
447434 return t
448435end
449436function Base.:(== )(d1:: SubDArray , d2:: SubDArray )
450437 cd1 = copy (d1)
451438 t = cd1 == d2
452- close (cd1)
439+ finalize (cd1)
453440 return t
454441end
455442
@@ -845,4 +832,3 @@ function Random.rand!(A::DArray, ::Type{T}) where T
845832 remotecall_wait ((A, T)-> rand! (localpart (A), T), p, A, T)
846833 end
847834end
848-
0 commit comments