-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmacros.jl
297 lines (265 loc) · 7.14 KB
/
macros.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
function _pthread_macro(pool, log, args...)
na = length(args)
if na != 1
throw(ArgumentError("wrong number of arguments in thread macro"))
end
ex = args[1]
if !isa(ex, Expr)
throw(ArgumentError("need an expression argument to thread macro"))
end
if ex.head === :for
if ex.args[1] isa Expr && ex.args[1].head === :(=)
index = ex.args[1].args[1]
range = ex.args[1].args[2]
body = ex.args[2]
return quote
pool = twith($pool) do p
tforeach(p, $(esc(range))) do $(esc(index))
for _tmp_var_dontusename in [0] # To allow `continue` to function in body
$(esc(body))
end
end
end
$(esc(log)) ? pool : nothing
end
else
throw(ArgumentError("nested outer loops are not currently supported by thread macro"))
end
else
throw(ArgumentError("unrecognized argument tothread macro"))
end
end
"""
@bthreads
Mimics `Base.Threads.@threads, but keeps the iterated tasks off if the primary
thread.`
# Example
```julia
julia> @bthreads for x in 1:8
println((x, Threads.threadid()))
end
(1, 2)
(6, 4)
(3, 3)
(7, 4)
(4, 3)
(8, 4)
(5, 3)
(2, 2)
```
Note that execution order is not guaranteed, but the primary thread does not
show up on any of the jobs.
"""
macro bthreads(args...)
return _pthread_macro(:(StaticPool(2)), false, args...)
nothing
end
"""
@qthreads
Mimics `Base.Threads.@threads`, but uses a task queueing strategy, only starting
a new task when an previous one (on any thread) has completed. This can provide
performance advantages when the iterated tasks are very nonuniform in length.
The primary thread is used. To prevent usage of the primary thread, see
[`@qbthreads`](@ref).
# Example
```julia
julia> @qthreads for x in 1:8
println((x, Threads.threadid()))
end
(2, 4)
(3, 3)
(4, 2)
(5, 4)
(6, 3)
(7, 2)
(8, 4)
(1, 1)
```
Note that execution order is not guaranteed and the primary thread is used.
"""
macro qthreads(args...)
return _pthread_macro(:(QueuePool(1)), false, args...)
end
"""
@qbthreads
Mimics `Base.Threads.@threads`, but uses a task queueing strategy, only starting
a new task when an previous one (on any thread) has completed. This can provide
performance advantages when the iterated tasks are very nonuniform in length.
The primary thread is not used. To allow usage of the primary thread, see
[`@qthreads`](@ref).
# Example
```julia
julia> @qbthreads for x in 1:8
println((x, Threads.threadid()))
end
(2, 4)
(3, 2)
(1, 3)
(4, 4)
(5, 2)
(6, 3)
(7, 4)
(8, 2)
```
Note that execution order is not guaranteed, but the primary thread does not
show up on any of the jobs.
"""
macro qbthreads(args...)
return _pthread_macro(:(QueuePool(2)), false, args...)
end
"""
@logthreads -> pool
Mimics `Base.Threads.@threads`. Returns a logged pool that can be analyzed with
the logging functions and `plot`ted.
# Example
```julia
julia> pool = @logthreads for x in 1:8
println((x, Threads.threadid()))
end;
(1, 1)
(5, 3)
(7, 4)
(2, 1)
(6, 3)
(8, 4)
(3, 2)
(4, 2)
julia> plot(pool)
```
Note that execution order is not guaranteed and the primary thread is used.
"""
macro logthreads(args...)
return _pthread_macro(:(LoggedStaticPool(1)), true, args...)
end
"""
@logbthreads -> pool
Mimics `Base.Threads.@threads, but keeps the iterated tasks off if the primary
thread.` Returns a logged pool that can be analyzed with the logging functions
and `plot`ted.
# Example
```julia
julia> pool = @logbthreads for x in 1:8
println((x, Threads.threadid()))
end;
(3, 4)
(2, 3)
(1, 2)
(4, 4)
(5, 3)
(6, 2)
(8, 3)
(7, 4)
julia> plot(pool)
```
Note that execution order is not guaranteed, but the primary thread does not
show up on any of the jobs.
"""
macro logbthreads(args...)
return _pthread_macro(:(LoggedStaticPool(2)), true, args...)
end
"""
@logqthreads -> pool
Mimics `Base.Threads.@threads`, but uses a task queueing strategy, only starting
a new task when an previous one (on any thread) has completed. Returns a logged
pool that can be analyzed with the logging functions and `plot`ted. The primary
thread is used. To prevent usage of the primary thread, see
[`@logqbthreads`](@ref).
# Example
```julia
julia> pool = @logqthreads for x in 1:8
println((x, Threads.threadid()))
end;
(1, 1)
(3, 2)
(7, 4)
(5, 3)
(2, 1)
(8, 4)
(6, 3)
(4, 2)
julia> plot(pool)
```
Note that execution order is not guaranteed and the primary thread is used.
"""
macro logqthreads(args...)
return _pthread_macro(:(LoggedQueuePool(1)), true, args...)
end
"""
@logqbthreads -> pool
Mimics `Base.Threads.@threads`, but uses a task queueing strategy, only starting
a new task when an previous one (on any thread) has completed. Returns a logged
pool that can be analyzed with the logging functions and `plot`ted. The primary
thread is not used. To allow usage of the primary thread, see
[`@logqthreads`](@ref).
# Example
```julia
julia> pool = @logqbthreads for x in 1:8
println((x, Threads.threadid()))
end;
(2, 3)
(1, 4)
(3, 2)
(4, 3)
(5, 4)
(6, 2)
(7, 3)
(8, 4)
julia> plot(pool)
```
Note that execution order is not guaranteed, but the primary thread does not
show up on any of the jobs.
"""
macro logqbthreads(args...)
return _pthread_macro(:(LoggedQueuePool(2)), true, args...)
end
"""
@tspawnat tid -> task
Mimics `Base.Threads.@spawn`, but assigns the task to thread `tid`.
# Example
```julia
julia> t = @tspawnat 4 Threads.threadid()
Task (runnable) @0x0000000010743c70
julia> fetch(t)
4
```
"""
macro tspawnat(thrdid, expr)
@static if VERSION >= v"1.4"
letargs = Base._lift_one_interp!(expr)
thunk = esc(:(()->($expr)))
var = esc(Base.sync_varname)
tid = esc(thrdid)
quote
if $tid < 1 || $tid > Threads.nthreads()
throw(AssertionError("@tspawnat thread assignment ($($tid)) must be between 1 and Threads.nthreads() (1:$(Threads.nthreads()))"))
end
let $(letargs...)
local task = Task($thunk)
task.sticky = VERSION >= v"1.7" # disallow task migration which was introduced in 1.7
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), task, $tid-1)
if $(Expr(:islocal, var))
put!($var, task)
end
schedule(task)
task
end
end
else
thunk = esc(:(()->($expr)))
var = esc(Base.sync_varname)
tid = esc(thrdid)
quote
if $tid < 1 || $tid > Threads.nthreads()
throw(AssertionError("@tspawnat thread assignment ($($tid)) must be between 1 and Threads.nthreads() (1:$(Threads.nthreads()))"))
end
local task = Task($thunk)
task.sticky = false
ccall(:jl_set_task_tid, Cvoid, (Any, Cint), task, $tid-1)
if $(Expr(:isdefined, var))
push!($var, task)
end
schedule(task)
task
end
end
end