@@ -152,7 +152,7 @@ Keyword arguments:
152
152
- `workers` -- The list of workers to create pieces on. May be any iterable container of `Integer`s.
153
153
- `per_thread::Bool=false` -- If `true`, creates a piece per each thread, rather than a piece per each worker.
154
154
"""
155
- function shard (f ; procs= nothing , workers= nothing , per_thread= false )
155
+ function shard (@nospecialize (f) ; procs= nothing , workers= nothing , per_thread= false )
156
156
if procs === nothing
157
157
if workers != = nothing
158
158
procs = [OSProc (w) for w in workers]
@@ -176,13 +176,15 @@ function shard(f; procs=nothing, workers=nothing, per_thread=false)
176
176
end
177
177
end
178
178
isempty (procs) && throw (ArgumentError (" Cannot create empty Shard" ))
179
- scopes = [p isa OSProc ? ProcessScope (p) : ExactScope (p) for p in procs]
180
- thunks = [proc=> Dagger. @spawn single= Dagger. get_parent (proc). pid _shard_inner (f, proc, scope) for (proc,scope) in zip (procs,scopes)]
181
- shard = Shard (Dict {Processor,Chunk} (thunk[1 ]=> fetch (thunk[2 ])[] for thunk in thunks))
182
- scope = UnionScope (scopes)
183
- Dagger. tochunk (shard, OSProc (), scope)
179
+ shard_dict = Dict {Processor,Chunk} ()
180
+ for proc in procs
181
+ scope = proc isa OSProc ? ProcessScope (proc) : ExactScope (proc)
182
+ thunk = Dagger. @spawn scope= scope _shard_inner (f, proc, scope)
183
+ shard_dict[proc] = fetch (thunk)[]
184
+ end
185
+ return Shard (shard_dict)
184
186
end
185
- function _shard_inner (f , proc, scope)
187
+ function _shard_inner (@nospecialize (f) , proc, scope)
186
188
Ref (Dagger. @mutable proc scope f ())
187
189
end
188
190
@@ -191,7 +193,7 @@ macro shard(exs...)
191
193
opts = esc .(exs[1 : end - 1 ])
192
194
ex = exs[end ]
193
195
quote
194
- let f = ()-> $ (esc (ex))
196
+ let f = @noinline ()-> $ (esc (ex))
195
197
$ shard (f; $ (opts... ))
196
198
end
197
199
end
@@ -202,28 +204,22 @@ function move(from_proc::Processor, to_proc::Processor, shard::Shard)
202
204
# N.B. This behavior may bypass the piece's scope restriction
203
205
proc = to_proc
204
206
if haskey (shard. chunks, proc)
205
- return shard. chunks[proc]
207
+ return move (from_proc, to_proc, shard. chunks[proc])
206
208
end
207
209
parent = Dagger. get_parent (proc)
208
210
while parent != proc
209
211
proc = parent
210
212
parent = Dagger. get_parent (proc)
211
213
if haskey (shard. chunks, proc)
212
- return shard. chunks[proc]
214
+ return move (from_proc, to_proc, shard. chunks[proc])
213
215
end
214
216
end
215
217
216
218
throw (KeyError (to_proc))
217
219
end
218
- function move (from_proc:: Processor , to_proc:: Processor , x:: Chunk{Shard} )
219
- piece = remotecall_fetch (x. handle. owner, x. handle, from_proc, to_proc) do ref, from_proc, to_proc
220
- shard = MemPool. poolget (ref)
221
- move (from_proc, to_proc, shard)
222
- end :: Chunk
223
- move (from_proc, to_proc, piece)
224
- end
225
- Base. map (f, cs:: Chunk{Shard} ) = map (f, fetch (cs; raw= true ))
226
- Base. map (f, s:: Shard ) = [Dagger. spawn (f, c) for c in values (s. chunks)]
220
+ Base. iterate (s:: Shard ) = iterate (values (s. chunks))
221
+ Base. iterate (s:: Shard , state) = iterate (values (s. chunks), state)
222
+ Base. length (s:: Shard ) = length (s. chunks)
227
223
228
224
# ## Core Stuff
229
225
0 commit comments