forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncmap.jl
245 lines (193 loc) · 6.88 KB
/
asyncmap.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# This file is a part of Julia. License is MIT: https://julialang.org/license
using Base.Iterators.Enumerate
"""
AsyncCollector(f, results, c...; ntasks=0) -> iterator
Apply `f` to each element of `c` using at most `ntasks` asynchronous
tasks.
If `ntasks` is unspecified, uses `max(100, nworkers())` tasks.
For multiple collection arguments, apply `f` elementwise.
Output is collected into `results`.
!!! note
`next(::AsyncCollector, state) -> (nothing, state)`
!!! note
`for task in AsyncCollector(f, results, c...) end` is equivalent to
`map!(f, results, c...)`.
"""
type AsyncCollector
f
results
enumerator::Enumerate
max_tasks::Function
task_chnl::Channel{Tuple{Int, Any}} # to communicate with the tasks
AsyncCollector(f, r, en::Enumerate, mt::Function, c::Channel) = new(f, r, en, mt, c)
end
function AsyncCollector(f, results, c...; ntasks=0)
if ntasks == 0
ntasks = max(nworkers(), 100)
max_tasks = ()->ntasks
elseif isa(ntasks, Integer)
max_tasks = ()->ntasks
elseif isa(ntasks, Function)
max_tasks = ntasks
else
throw(ArgumentError("ntasks must be an Integer or a zero-arg function returning the maximum number of tasks allowed."))
end
AsyncCollector(f, results, enumerate(zip(c...)), max_tasks, Channel{Tuple{Int, Any}}(typemax(Int)))
end
type AsyncCollectorState
enum_state
active_count::Int
item_done::Condition
done::Bool
in_error::Bool
nfree::Int # number of free tasks
end
isbusy(itr::AsyncCollector, state::AsyncCollectorState) = (state.nfree == 0)
# Wait for @async task to end.
wait(state::AsyncCollectorState) = wait(state.item_done)
function start_collector_task(itr::AsyncCollector, state::AsyncCollectorState)
t = @async begin
try
for (i, args) in itr.task_chnl
state.nfree -= 1
itr.results[i] = itr.f(args...)
notify(state.item_done, nothing)
state.nfree += 1
end
catch e
# The in_error flag causes done() to end the iteration early and call sync_end().
# sync_end() then re-throws "e" in the main task.
state.in_error = true
clear_collector_channel(itr)
notify(state.item_done, nothing)
rethrow(e)
end
end
state.active_count += 1
t
end
function clear_collector_channel(itr::AsyncCollector)
try
# empty out the channel and close it, ignore any errors in doing this
while isready(itr.task_chnl)
take!(itr.task_chnl)
end
close(itr.task_chnl)
catch
end
nothing
end
# Open a @sync block and initialise iterator state.
function start(itr::AsyncCollector)
sync_begin()
state = AsyncCollectorState(start(itr.enumerator), 0, Condition(), false, false, 0)
for _ in 1:itr.max_tasks()
start_collector_task(itr, state)
state.nfree += 1
end
state
end
# Close @sync block when iterator is done.
function done(itr::AsyncCollector, state::AsyncCollectorState)
if state.in_error
@assert !isopen(itr.task_chnl) # Channel should have been cleared and closed in the async task.
sync_end()
# state.in_error is only being set in the @async block (and an error thrown),
# which in turn should have been caught and thrown by the sync_end() call above.
# Control should not come here.
@assert false "Error should have been captured and thrown previously."
end
if !state.done && done(itr.enumerator, state.enum_state)
state.done = true
close(itr.task_chnl)
sync_end()
end
return state.done
end
function next(itr::AsyncCollector, state::AsyncCollectorState)
# start a task if required.
# Note: Shouldn't need to check on every iteration. Do this at a periodic interval?
if state.active_count < itr.max_tasks()
start_collector_task(itr, state)
end
while isbusy(itr, state)
wait(state)
if state.in_error
# Stop processing immediately on error.
return (nothing, state)
end
end
# Get index and mapped function arguments from enumeration iterator.
(i, args), state.enum_state = next(itr.enumerator, state.enum_state)
put!(itr.task_chnl, (i, args))
return (nothing, state)
end
"""
AsyncGenerator(f, c...; ntasks=0) -> iterator
Apply `f` to each element of `c` using at most `ntasks` asynchronous tasks.
If `ntasks` is unspecified, uses `max(100, nworkers())` tasks.
For multiple collection arguments, apply `f` elementwise.
Results are returned by the iterator as they become available.
!!! note
`collect(AsyncGenerator(f, c...; ntasks=1))` is equivalent to
`map(f, c...)`.
"""
type AsyncGenerator
collector::AsyncCollector
end
function AsyncGenerator(f, c...; ntasks=0)
AsyncGenerator(AsyncCollector(f, Dict{Int,Any}(), c...; ntasks=ntasks))
end
type AsyncGeneratorState
i::Int
async_state::AsyncCollectorState
end
start(itr::AsyncGenerator) = AsyncGeneratorState(0, start(itr.collector))
# Done when source async collector is done and all results have been consumed.
function done(itr::AsyncGenerator, state::AsyncGeneratorState)
done(itr.collector, state.async_state) && isempty(itr.collector.results)
end
# Pump the source async collector if it is not already busy.
function pump_source(itr::AsyncGenerator, state::AsyncGeneratorState)
if !isbusy(itr.collector, state.async_state) &&
!done(itr.collector, state.async_state)
ignored, state.async_state = next(itr.collector, state.async_state)
return true
else
return false
end
end
function next(itr::AsyncGenerator, state::AsyncGeneratorState)
state.i += 1
results = itr.collector.results
while !haskey(results, state.i)
# Wait for results to become available.
if !pump_source(itr,state) && !haskey(results, state.i)
wait(state.async_state)
end
end
r = results[state.i]
delete!(results, state.i)
return (r, state)
end
# pass-through iterator traits to the iterable
# on which the mapping function is being applied
iteratorsize(itr::AsyncGenerator) = iteratorsize(itr.collector.enumerator)
size(itr::AsyncGenerator) = size(itr.collector.enumerator)
length(itr::AsyncGenerator) = length(itr.collector.enumerator)
"""
asyncmap(f, c...) -> collection
Transform collection `c` by applying `@async f` to each element.
For multiple collection arguments, apply `f` elementwise.
"""
asyncmap(f, c...) = collect(AsyncGenerator(f, c...))
"""
asyncmap!(f, c)
In-place version of [`asyncmap()`](@ref).
"""
asyncmap!(f, c) = (for x in AsyncCollector(f, c, c) end; c)
"""
asyncmap!(f, results, c...)
Like [`asyncmap()`](@ref), but stores output in `results` rather returning a collection.
"""
asyncmap!(f, r, c1, c...) = (for x in AsyncCollector(f, r, c1, c...) end; r)