@@ -28,6 +28,17 @@ export close, darray_closeall
28
28
const registry= Dict {Tuple, Any} ()
29
29
const refs= Set () # Collection of darray identities created on this node
30
30
31
+ let DID:: Int = 1
32
+ global next_did
33
+ next_did () = (id = DID; DID += 1 ; (myid (), id))
34
+ end
35
+
36
+ """
37
+ next_did()
38
+
39
+ Produces an incrementing ID that will be used for DArrays.
40
+ """
41
+ next_did
31
42
32
43
"""
33
44
DArray(init, dims, [procs, dist])
@@ -91,13 +102,49 @@ typealias SubOrDArray{T,N} Union{DArray{T,N}, SubDArray{T,N}}
91
102
92
103
# # core constructors ##
93
104
105
+ function DArray (identity, init, dims, pids, idxs, cuts)
106
+ r= Channel (1 )
107
+ @sync begin
108
+ for i = 1 : length (pids)
109
+ @async begin
110
+ local typA
111
+ if isa (init, Function)
112
+ typA= remotecall_fetch (construct_localparts, pids[i], init, identity, dims, pids, idxs, cuts)
113
+ else
114
+ # constructing from an array of remote refs.
115
+ typA= remotecall_fetch (construct_localparts, pids[i], init[i], identity, dims, pids, idxs, cuts)
116
+ end
117
+ ! isready (r) && put! (r, typA)
118
+ end
119
+ end
120
+ end
121
+
122
+ typA = take! (r)
123
+ if myid () in pids
124
+ d = registry[(identity, :DARRAY )]
125
+ else
126
+ d = DArray {eltype(typA),length(dims),typA} (identity, dims, pids, idxs, cuts)
127
+ end
128
+ d
129
+ end
130
+
131
+ function construct_localparts (init, identity, dims, pids, idxs, cuts)
132
+ A = isa (init, Function) ? init (idxs[localpartindex (pids)]) : fetch (init)
133
+ global registry
134
+ registry[(identity, :LOCALPART )] = A
135
+ typA = typeof (A)
136
+ d = DArray {eltype(typA),length(dims),typA} (identity, dims, pids, idxs, cuts)
137
+ registry[(identity, :DARRAY )] = d
138
+ typA
139
+ end
140
+
94
141
function DArray (init, dims, procs, dist)
95
142
np = prod (dist)
96
143
procs = reshape (procs[1 : np], ntuple (i-> dist[i], length (dist)))
97
144
idxs, cuts = chunk_idxs ([dims... ], dist)
98
145
identity = next_did ()
99
146
100
- return construct_darray (identity, init, dims, procs, idxs, cuts)
147
+ return DArray (identity, init, dims, procs, idxs, cuts)
101
148
end
102
149
103
150
function DArray (init, dims, procs)
@@ -146,7 +193,7 @@ function DArray(refs)
146
193
ncuts = Array{Int,1 }[unshift! (sort (unique (lastidxs[x,:])), 1 ) for x in 1 : length (dimdist)]
147
194
ndims = tuple ([sort (unique (lastidxs[x,:]))[end ]- 1 for x in 1 : length (dimdist)]. .. )
148
195
149
- construct_darray (identity, refs, ndims, reshape (npids, dimdist), nindexes, ncuts)
196
+ DArray (identity, refs, ndims, reshape (npids, dimdist), nindexes, ncuts)
150
197
end
151
198
if VERSION < v " 0.5.0-"
152
199
macro DArray (ex:: Expr )
@@ -185,48 +232,7 @@ else
185
232
end
186
233
187
234
# new DArray similar to an existing one
188
- DArray (init, d:: DArray ) = construct_darray (next_did (), init, size (d), procs (d), d. indexes, d. cuts)
189
-
190
- function construct_darray (identity, init, dims, pids, idxs, cuts)
191
- r= Channel (1 )
192
- @sync begin
193
- for i = 1 : length (pids)
194
- @async begin
195
- local typA
196
- if isa (init, Function)
197
- typA= remotecall_fetch (construct_localparts, pids[i], init, identity, dims, pids, idxs, cuts)
198
- else
199
- # constructing from an array of remote refs.
200
- typA= remotecall_fetch (construct_localparts, pids[i], init[i], identity, dims, pids, idxs, cuts)
201
- end
202
- ! isready (r) && put! (r, typA)
203
- end
204
- end
205
- end
206
-
207
- typA = take! (r)
208
- if myid () in pids
209
- d = registry[(identity, :DARRAY )]
210
- else
211
- d = DArray {eltype(typA),length(dims),typA} (identity, dims, pids, idxs, cuts)
212
- end
213
- d
214
- end
215
-
216
- function construct_localparts (init, identity, dims, pids, idxs, cuts)
217
- A = isa (init, Function) ? init (idxs[localpartindex (pids)]) : fetch (init)
218
- global registry
219
- registry[(identity, :LOCALPART )] = A
220
- typA = typeof (A)
221
- d = DArray {eltype(typA),length(dims),typA} (identity, dims, pids, idxs, cuts)
222
- registry[(identity, :DARRAY )] = d
223
- typA
224
- end
225
-
226
- let DID:: Int = 1
227
- global next_did
228
- next_did () = (id = DID; DID += 1 ; (myid (), id))
229
- end
235
+ DArray (init, d:: DArray ) = DArray (next_did (), init, size (d), procs (d), d. indexes, d. cuts)
230
236
231
237
function release_localpart (identity)
232
238
global registry
@@ -523,6 +529,25 @@ function distribute(A::AbstractArray;
523
529
return DArray (I-> verify_and_get (pas, I), size (A), procs, dist)
524
530
end
525
531
532
+ """
533
+ distribute(A, DA)
534
+
535
+ Distribute a local array `A` like the distributed array `DA`.
536
+
537
+ """
538
+ function distribute (A:: AbstractArray , DA:: DArray )
539
+ size (DA) == size (A) || throw (DimensionMismatch (" Distributed array has size $(size (DA)) but array has $(size (A)) " ))
540
+
541
+ owner = myid ()
542
+ rr = RemoteChannel ()
543
+ put! (rr, A)
544
+
545
+ d = DArray (DA) do I
546
+ remotecall_fetch (() -> fetch (rr)[I... ], owner)
547
+ end
548
+ return d
549
+ end
550
+
526
551
Base. convert {T,N,S<:AbstractArray} (:: Type{DArray{T,N,S}} , A:: S ) = distribute (convert (AbstractArray{T,N}, A))
527
552
528
553
Base. convert {S,T,N} (:: Type{Array{S,N}} , d:: DArray{T,N} ) = begin
@@ -743,7 +768,7 @@ mapreducedim_within(f, op, A::DArray, region) = begin
743
768
indx[i] = ntuple (j -> j in region ? (i. I[j]: i. I[j]) : A. indexes[i][j], ndims (A))
744
769
end
745
770
cuts = [i in region ? collect (1 : arraysize[i] + 1 ) : A. cuts[i] for i in 1 : ndims (A)]
746
- return construct_darray (next_did (), I -> mapreducedim (f, op, localpart (A), region),
771
+ return DArray (next_did (), I -> mapreducedim (f, op, localpart (A), region),
747
772
tuple (arraysize... ), procs (A), indx, cuts)
748
773
end
749
774
0 commit comments