@@ -38,30 +38,30 @@ dfill(v, args...) = DArray(I->fill(v, map(length,I)), args...)
38
38
```
39
39
"""
40
40
type DArray{T,N,A} <: AbstractArray{T,N}
41
- identity :: Tuple
41
+ id :: Tuple
42
42
dims:: NTuple{N,Int}
43
43
pids:: Array{Int,N} # pids[i]==p ⇒ processor p has piece i
44
44
indexes:: Array{NTuple{N,UnitRange{Int}},N} # indexes held by piece i
45
45
cuts:: Vector{Vector{Int}} # cuts[d][i] = first index of chunk i in dimension d
46
+ localpart:: A
46
47
47
48
release:: Bool
48
49
49
- function DArray (identity , dims, pids, indexes, cuts)
50
+ function DArray (id , dims, pids, indexes, cuts, lp )
50
51
# check invariants
51
52
if dims != map (last, last (indexes))
52
53
throw (ArgumentError (" dimension of DArray (dim) and indexes do not match" ))
53
54
end
54
- release = (myid () == identity [1 ])
55
+ release = (myid () == id [1 ])
55
56
56
- global registry
57
- haskey (registry, (identity, :DARRAY )) && return registry[(identity, :DARRAY )]
57
+ haskey (registry, id) && return registry[id]
58
58
59
- d = new (identity , dims, pids, indexes, cuts, release)
59
+ d = new (id , dims, pids, indexes, cuts, lp , release)
60
60
if release
61
- push! (refs, identity )
62
- registry[(identity, :DARRAY ) ] = d
61
+ push! (refs, id )
62
+ registry[id ] = d
63
63
64
- # println("Installing finalizer for : ", d.identity , ", : ", object_id(d), ", isbits: ", isbits(d))
64
+ # println("Installing finalizer for : ", d.id , ", : ", object_id(d), ", isbits: ", isbits(d))
65
65
finalizer (d, close)
66
66
end
67
67
d
@@ -70,6 +70,9 @@ type DArray{T,N,A} <: AbstractArray{T,N}
70
70
DArray () = new ()
71
71
end
72
72
73
+ eltype {T} (:: Type{DArray{T}} ) = T
74
+ empty_localpart (T,N,A) = convert (A, Array (T, ntuple (zero, N)))
75
+
73
76
typealias SubDArray{T,N,D<: DArray } SubArray{T,N,D}
74
77
typealias SubOrDArray{T,N} Union{DArray{T,N}, SubDArray{T,N}}
75
78
@@ -79,49 +82,51 @@ localtype(A::AbstractArray) = typeof(A)
79
82
80
83
# # core constructors ##
81
84
82
- function DArray (identity , init, dims, pids, idxs, cuts)
85
+ function DArray (id , init, dims, pids, idxs, cuts)
83
86
r= Channel (1 )
84
87
@sync begin
85
88
for i = 1 : length (pids)
86
89
@async begin
87
90
local typA
88
91
if isa (init, Function)
89
- typA= remotecall_fetch (construct_localparts, pids[i], init, identity , dims, pids, idxs, cuts)
92
+ typA= remotecall_fetch (construct_localparts, pids[i], init, id , dims, pids, idxs, cuts)
90
93
else
91
94
# constructing from an array of remote refs.
92
- typA= remotecall_fetch (construct_localparts, pids[i], init[i], identity , dims, pids, idxs, cuts)
95
+ typA= remotecall_fetch (construct_localparts, pids[i], init[i], id , dims, pids, idxs, cuts)
93
96
end
94
97
! isready (r) && put! (r, typA)
95
98
end
96
99
end
97
100
end
98
101
99
- typA = take! (r)
102
+ A = take! (r)
100
103
if myid () in pids
101
- d = registry[(identity, :DARRAY ) ]
104
+ d = registry[id ]
102
105
else
103
- d = DArray {eltype(typA),length(dims),typA} (identity, dims, pids, idxs, cuts)
106
+ T = eltype (A)
107
+ N = length (dims)
108
+ d = DArray {T,N,A} (id, dims, pids, idxs, cuts, empty_localpart (T,N,A))
104
109
end
105
110
d
106
111
end
107
112
108
- function construct_localparts (init, identity , dims, pids, idxs, cuts)
109
- A = isa (init, Function) ? init (idxs[localpartindex (pids)]) : fetch (init)
110
- global registry
111
- registry[(identity, :LOCALPART )] = A
112
- typA = typeof (A )
113
- d = DArray {eltype(typA),length(dims),typA} (identity , dims, pids, idxs, cuts)
114
- registry[(identity, :DARRAY ) ] = d
115
- typA
113
+ function construct_localparts (init, id , dims, pids, idxs, cuts)
114
+ localpart = isa (init, Function) ? init (idxs[localpartindex (pids)]) : fetch (init)
115
+ A = typeof (localpart)
116
+ T = eltype (A)
117
+ N = length (dims )
118
+ d = DArray {T,N,A} (id , dims, pids, idxs, cuts, localpart )
119
+ registry[id ] = d
120
+ A
116
121
end
117
122
118
123
function DArray (init, dims, procs, dist)
119
124
np = prod (dist)
120
125
procs = reshape (procs[1 : np], ntuple (i-> dist[i], length (dist)))
121
126
idxs, cuts = chunk_idxs ([dims... ], dist)
122
- identity = next_did ()
127
+ id = next_did ()
123
128
124
- return DArray (identity , init, dims, procs, idxs, cuts)
129
+ return DArray (id , init, dims, procs, idxs, cuts)
125
130
end
126
131
127
132
function DArray (init, dims, procs)
@@ -140,13 +145,13 @@ DArray(init, dims) = DArray(init, dims, workers()[1:min(nworkers(), maximum(dims
140
145
# FIXME : Empty parts are currently not supported.
141
146
function DArray (refs)
142
147
dimdist = size (refs)
143
- identity = next_did ()
148
+ id = next_did ()
144
149
145
150
npids = [r. where for r in refs]
146
151
nsizes = Array (Tuple, dimdist)
147
152
@sync for i in 1 : length (refs)
148
153
let i= i
149
- @async nsizes[i] = remotecall_fetch (rr_localpart , npids[i], refs[i], identity )
154
+ @async nsizes[i] = remotecall_fetch (sz_localpart_ref , npids[i], refs[i], id )
150
155
end
151
156
end
152
157
@@ -170,7 +175,7 @@ function DArray(refs)
170
175
ncuts = Array{Int,1 }[unshift! (sort (unique (lastidxs[x,:])), 1 ) for x in 1 : length (dimdist)]
171
176
ndims = tuple ([sort (unique (lastidxs[x,:]))[end ]- 1 for x in 1 : length (dimdist)]. .. )
172
177
173
- DArray (identity , refs, ndims, reshape (npids, dimdist), nindexes, ncuts)
178
+ DArray (id , refs, ndims, reshape (npids, dimdist), nindexes, ncuts)
174
179
end
175
180
176
181
macro DArray (ex0:: Expr )
@@ -195,57 +200,44 @@ end
195
200
# new DArray similar to an existing one
196
201
DArray (init, d:: DArray ) = DArray (next_did (), init, size (d), procs (d), d. indexes, d. cuts)
197
202
198
- function release_localpart (identity)
199
- global registry
200
- delete! (registry, (identity, :DARRAY ))
201
- delete! (registry, (identity, :LOCALPART ))
202
- nothing
203
- end
204
- release_localpart (d:: DArray ) = release_localpart (d. identity)
203
+ release_localpart (id) = (delete! (registry, id); nothing )
204
+ release_localpart (d:: DArray ) = release_localpart (d. id)
205
205
206
- function close_by_identity (identity , pids)
207
- # @schedule println("Finalizer for : ", identity )
206
+ function close_by_id (id , pids)
207
+ # @schedule println("Finalizer for : ", id )
208
208
global refs
209
209
@sync begin
210
210
for p in pids
211
- @async remotecall_fetch (release_localpart, p, identity )
211
+ @async remotecall_fetch (release_localpart, p, id )
212
212
end
213
213
if ! (myid () in pids)
214
- release_localpart (identity )
214
+ release_localpart (id )
215
215
end
216
216
end
217
- delete! (refs, identity )
217
+ delete! (refs, id )
218
218
nothing
219
219
end
220
220
221
221
function close (d:: DArray )
222
- # @schedule println("close : ", d.identity , ", object_id : ", object_id(d), ", myid : ", myid() )
223
- if (myid () == d. identity [1 ]) && d. release
224
- @schedule close_by_identity (d. identity , d. pids)
222
+ # @schedule println("close : ", d.id , ", object_id : ", object_id(d), ", myid : ", myid() )
223
+ if (myid () == d. id [1 ]) && d. release
224
+ @schedule close_by_id (d. id , d. pids)
225
225
d. release = false
226
226
end
227
227
nothing
228
228
end
229
229
230
230
function darray_closeall ()
231
- global registry
232
- global refs
233
231
crefs = copy (refs)
234
- for identity in crefs
235
- if identity [1 ] == myid () # sanity check
236
- haskey (registry, (identity, :DARRAY )) && close (registry[(identity, :DARRAY ) ])
232
+ for id in crefs
233
+ if id [1 ] == myid () # sanity check
234
+ haskey (registry, id) && close (registry[id ])
237
235
yield ()
238
236
end
239
237
end
240
238
end
241
239
242
- function rr_localpart (ref, identity)
243
- global registry
244
- lp = fetch (ref)
245
- registry[(identity, :LOCALPART )] = lp
246
- return size (lp)
247
- end
248
-
240
+ sz_localpart_ref (ref, id) = size (fetch (ref))
249
241
250
242
Base. similar (d:: DArray , T:: Type , dims:: Dims ) = DArray (I-> Array (T, map (length,I)), dims, procs (d))
251
243
Base. similar (d:: DArray , T:: Type ) = similar (d, T, size (d))
@@ -335,11 +327,10 @@ Returns an empty array if no local part exists on the calling process.
335
327
function localpart {T,N,A} (d:: DArray{T,N,A} )
336
328
lpidx = localpartindex (d)
337
329
if lpidx == 0
338
- return convert (A, Array (T, ntuple (zero, N)) ):: A
330
+ return empty_localpart (T,N,A ):: A
339
331
end
340
332
341
- global registry
342
- return registry[(d. identity, :LOCALPART )]:: A
333
+ return registry[d. id]. localpart:: A
343
334
end
344
335
345
336
localpart (d:: DArray , localidx... ) = localpart (d)[localidx... ]
0 commit comments