-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclosure.jl
525 lines (502 loc) · 16.2 KB
/
closure.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
function getgensym!(defined::Dict{Symbol,Symbol}, s::Symbol)
snew = get!(defined, s) do
gensym(s)
end
snew ≢ s && (defined[snew] = snew)
return snew
end
extractargs!(arguments::Vector{Symbol}, defined::Dict{Symbol,Symbol}, sym, mod) = nothing
# function extractargs!(arguments::Vector{Symbol}, defined::Dict{Symbol,Symbol}, sym::Symbol, mod)
# if ((sym ∉ keys(defined)) && sym ∉ (:nothing, :(+), :(*), :(-), :(/), :(÷), :(<<), :(>>), :(>>>), :zero, :one)) && !Base.isconst(mod, sym)
# @show getgensym!(defined, sym)
# @assert false
# push!(arguments, getgensym!(defined, sym))
# end
# nothing
# end
function define_tup!(arguments::Vector{Symbol}, defined::Dict{Symbol,Symbol}, ex::Expr, mod)
for (i, a) ∈ enumerate(ex.args)
if a isa Symbol
ex.args[i] = getgensym!(defined, a)
elseif Meta.isexpr(a, :tuple)
define_tup!(Symbol[a.args...], defined, a, mod)
elseif Meta.isexpr(a, :ref)
extractargs!(arguments, defined, a, mod)
elseif Meta.isexpr(a, :parameters)
define_tup!(Symbol[a.args...], defined, a, mod)
else
throw("Don't know how to handle:\n $a")
end
end
end
function define1!(
arguments::Vector{Symbol},
defined::Dict{Symbol,Symbol},
x::Vector{Any},
mod,
)
s = x[1]
if s isa Symbol
x[1] = getgensym!(defined, s)
else
define_tup!(arguments, defined, s::Expr, mod)
end
end
function define_induction_variables!(
arguments::Vector{Symbol},
defined::Dict{Symbol,Symbol},
ex::Expr,
mod,
) # add `i` in `for i ∈ looprange` to `defined`
ex.head === :for || return
loops = ex.args[1]
if loops.head === :block
for loop ∈ loops.args
define1!(arguments, defined, loop.args, mod)
end
else
define1!(arguments, defined, loops.args, mod)
end
end
function extractargs_equal!(
arguments::Vector{Symbol},
defined::Dict{Symbol,Symbol},
args::Vector{Any},
mod,
)
arg1 = first(args)
if arg1 isa Symbol
args[1] = getgensym!(defined, arg1)
elseif Meta.isexpr(arg1, :tuple)
define_tup!(arguments, defined, arg1, mod)
end
nothing
end
function must_add_sym(defined::Dict{Symbol,Symbol}, arg::Symbol, mod)
(
(arg ∉ keys(defined)) &&
arg ∉ (:nothing, :(+), :(*), :(-), :(/), :(÷), :(<<), :(>>), :(>>>), :zero, :one)
) && !Base.isconst(mod, arg)
end
function get_sym!(defined::Dict{Symbol,Symbol}, arguments::Vector{Symbol}, arg::Symbol, mod)
if must_add_sym(defined, arg, mod)
# @show getgensym!(defined, sym)
# @assert false
push!(arguments, arg)
getgensym!(defined, arg)
else
get(defined, arg, arg)
end
end
function extractargs!(
arguments::Vector{Symbol},
defined::Dict{Symbol,Symbol},
expr::Expr,
mod,
)
define_induction_variables!(arguments, defined, expr, mod)
head = expr.head
args = expr.args
startind = 1
if head === :call
startind = 2
elseif head === :(=)
extractargs_equal!(arguments, defined, args, mod)
elseif head ∈ (:inbounds, :loopinfo)#, :(->))
return
elseif head === :(.)
arg1 = args[1]
if arg1 isa Symbol
args[1] = get_sym!(defined, arguments, arg1, mod)
else
extractargs!(arguments, defined, arg1, mod)
end
elseif head === :(->)
td = copy(defined)
define1!(arguments::Vector{Symbol}, td, args, mod)
extractargs!(arguments, td, args[1], mod)
extractargs!(arguments, td, args[2], mod)
return
elseif (head === :local) || (head === :global)
for (i, arg) in enumerate(args)
if Meta.isexpr(arg, :(=))
extractargs_equal!(arguments, defined, arg.args, mod)
args = arg.args
startind = 2
else
args[i] = getgensym!(defined, arg)
return
end
end
elseif head === :kw
if args[2] isa Symbol
args[2] = get_sym!(defined, arguments, args[2], mod)
else
extractargs!(arguments, defined, args[2], mod)
end
return
elseif head === :parameters
for (i, arg) in enumerate(args)
if arg isa Symbol
sym = get_sym!(defined, arguments, arg, mod)
args[i] = Expr(:kw, arg, sym)
else
extractargs!(arguments, defined, arg, mod)
end
end
return
end
for i ∈ startind:length(args)
argᵢ = args[i]
(head === :ref && ((argᵢ === :end) || (argᵢ === :begin))) && continue
if argᵢ isa Symbol
args[i] = get_sym!(defined, arguments, argᵢ, mod)
elseif argᵢ isa Expr
extractargs!(arguments, defined, argᵢ, mod)
else
extractargs!(arguments, defined, argᵢ, mod)
end
end
return
end
function symbolsubs(e::Expr, old::Symbol, new::Symbol)
return Expr(e.head, (symbolsubs(a, old, new) for a in e.args)...)
end
function symbolsubs(e::Symbol, old::Symbol, new::Symbol)
e == old ? new : e
end
symbolsubs(e, old::Symbol, new::Symbol) = e
struct NoLoop end
Base.iterate(::NoLoop) = (NoLoop(), NoLoop())
Base.iterate(::NoLoop, ::NoLoop) = nothing
@inline splitloop(x) = NoLoop(), x, CombineIndices()
struct CombineIndices end
@inline splitloop(x::AbstractRange) = NoLoop(), x, CombineIndices()
@inline function splitloop(x::CartesianIndices)
axes = x.indices
CartesianIndices(Base.front(axes)), last(axes), CombineIndices()
end
@inline function splitloop(x::AbstractArray)
inds = eachindex(x)
inner, outer = splitloop(inds)
inner, outer, x
end
struct TupleIndices end
@inline function splitloop(x::Base.Iterators.ProductIterator{Tuple{T1,T2}}) where {T1,T2}
iters = x.iterators
iters[1], iters[2], TupleIndices()
end
@inline function splitloop(
x::Base.Iterators.ProductIterator{<:Tuple{Vararg{Any,N}}},
) where {N}
iters = x.iterators
Base.front(iters), iters[N], TupleIndices()
end
combine(::CombineIndices, ::NoLoop, x) = x
combine(::CombineIndices, I::CartesianIndex, j) = CartesianIndex((I.I..., j))
combine(::TupleIndices, i::Tuple, j) = (i..., j)
combine(::TupleIndices, i::Number, j) = (i, j)
Base.@propagate_inbounds combine(x::AbstractArray, I, j) =
x[combine(CombineIndices(), I, j)]
Base.@propagate_inbounds combine(x::AbstractArray, ::NoLoop, j) = x[j]
function makestatic!(expr)
expr isa Expr || return expr
for i in eachindex(expr.args)
ex = expr.args[i]
if ex isa Int
expr.args[i] = static(ex)
elseif ex isa Symbol
j = findfirst(==(ex), (:axes, :size, :length))
if j !== nothing
expr.args[i] =
GlobalRef(ArrayInterface, (:static_axes, :static_size, :static_length)[j])
end
elseif ex isa Expr
makestatic!(ex)
end
end
expr
end
function enclose(
exorig::Expr,
reserve_per,
minbatchsize,
per::Symbol,
threadlocal_tuple,
mod,
)
Meta.isexpr(exorig, :for, 2) ||
throw(ArgumentError("Expression invalid; should be a for loop."))
ex = copy(exorig)
loop_sym = Symbol("##LOOP##")
loopstart = Symbol("##LOOPSTART##")
loop_step = Symbol("##LOOP_STEP##")
loop_stop = Symbol("##LOOP_STOP##")
iter_leng = Symbol("##ITER_LENG##")
loop_offs = Symbol("##LOOPOFFSET##")
innerloop = Symbol("##inner##loop##")
rcombiner = Symbol("##split##recombined##")
threadlocal_var = Symbol("threadlocal")
# arguments = Symbol[]#loop_offs, loop_step]
arguments = Symbol[innerloop, rcombiner]#loop_offs, loop_step]
defined = Dict{Symbol,Symbol}(loop_offs => loop_offs, loop_step => loop_step)
threadlocal_var_gen = getgensym!(defined, threadlocal_var)
define_induction_variables!(arguments, defined, ex, mod)
firstloop = ex.args[1]
if firstloop.head === :block
secondaryloopsargs = firstloop.args[2:end]
firstloop = firstloop.args[1]
else
secondaryloopsargs = Any[]
end
loop = firstloop.args[2]
# @show ex loop
firstlooprange = Expr(:call, GlobalRef(Base, :(:)), loopstart, loop_step, loop_stop)
body = ex.args[2]
if length(secondaryloopsargs) == 1
body = Expr(:for, only(secondaryloopsargs), body)
elseif length(secondaryloopsargs) > 1
sl = Expr(:block)
append!(sl.args, secondaryloopsargs)
body = Expr(:for, sl, body)
end
fla1 = firstloop.args[1]
excomb = if fla1 isa Symbol
fla1 = getgensym!(defined, fla1)
quote
# for $(firstloop.args[1]) in
for var"##outer##" in $firstlooprange, var"##inner##" in $innerloop
$fla1 = $combine($rcombiner, var"##inner##", var"##outer##")
$body
end
end
else
@assert fla1 isa Expr
for i in eachindex(fla1.args)
fla1.args[i] = getgensym!(defined, fla1.args[i])
end
quote
# for $(firstloop.args[1]) in
for var"##outer##" in $firstlooprange, var"##inner##" in $innerloop
$fla1 = $combine($rcombiner, var"##inner##", var"##outer##")
$body
end
end
end
if ex.args[1].head === :block
for i ∈ 2:length(ex.args[1].args)
extractargs!(arguments, defined, ex.args[1].args[i], mod)
end
end
for i ∈ 2:length(ex.args)
extractargs!(arguments, defined, ex.args[i], mod)
end
# @show ex.args[1] firstloop body
# if length(ex.args[
# ex = quote
# # for $(firstloop.args[1]) in
# for var"##outer##" in $firstlooprange, var"##inner##" in $innerloop
# $(firstloop.args[1]) = $combine($rcombiner, var"##inner##", var"##outer##")
# $body
# end
# end
# typexpr_incomplete is missing `funcs`
# outerloop = Symbol("##outer##")
q = quote
$(esc(innerloop)), $loop_sym, $(esc(rcombiner)) = $splitloop($(esc(makestatic!(loop))))
$iter_leng = $static_length($loop_sym)
$loop_step = $static_step($loop_sym)
$loop_offs = $static_first($loop_sym)
end
threadtup = Expr(:tuple, iter_leng)
num_thread_expr = Expr(:call, Threads.nthreads)
if per === :core
num_thread_expr = Expr(:call, min, num_thread_expr, Expr(:call, num_cores))
end
if minbatchsize isa Integer && minbatchsize ≤ 1
# if reserve_per ≤ 0
push!(threadtup.args, :(min($iter_leng, $num_thread_expr)))
# else
# push!(threadtup.args, :(min($iter_leng, cld($num_thread_expr, $reserve_per))), reserve_per)
# end
else
il = :(div(
$iter_leng,
$(minbatchsize isa Int ? StaticInt(minbatchsize) : esc(minbatchsize)),
))
# if reserve_per ≤ 0
push!(threadtup.args, :(min($il, $num_thread_expr)))
# else
# push!(threadtup.args, :(min($il, cld($num_thread_expr, $reserve_per))), reserve_per)
# end
end
closure = Symbol("##closure##")
threadlocal, threadlocal_type = threadlocal_tuple
threadlocal_var_single = gensym(threadlocal_var)
q_single = symbolsubs(exorig, threadlocal_var, threadlocal_var_single)
donothing = Expr(:block)
threadlocal_init_single =
threadlocal === Symbol("") ? donothing : :($threadlocal_var_single = $threadlocal)
threadlocal_repack_single =
threadlocal === Symbol("") ? donothing : :($threadlocal_var_single)
threadlocal_single_store =
threadlocal === Symbol("") ? donothing :
:($(esc(threadlocal_var)) = [single_thread_result])
threadlocal_init1 =
threadlocal === Symbol("") ? donothing :
:($threadlocal_var = Vector{$threadlocal_type}(undef, 0))
threadlocal_init2 =
threadlocal === Symbol("") ? donothing :
:(resize!($(esc(threadlocal_var)), max(1, $(threadtup.args[2]))))
threadlocal_get =
threadlocal === Symbol("") ? donothing :
:($threadlocal_var_gen = $threadlocal::$threadlocal_type)
threadlocal_set =
threadlocal === Symbol("") ? donothing :
:($threadlocal_var[var"##THREAD##"] = $threadlocal_var_gen)
push!(q.args, threadlocal_init2)
args = Expr(:tuple, Symbol("##LOOPOFFSET##"), Symbol("##LOOP_STEP##"))
closure_args = if threadlocal === Symbol("")
:($args, var"##SUBSTART##"::Int, var"##SUBSTOP##"::Int)
else
:($args, var"##SUBSTART##"::Int, var"##SUBSTOP##"::Int, var"##THREAD##"::Int)
end
closureq = quote
$closure = let
@inline $closure_args -> begin
var"##LOOPSTART##" =
var"##SUBSTART##" * var"##LOOP_STEP##" + var"##LOOPOFFSET##" - var"##LOOP_STEP##"
var"##LOOP_STOP##" =
var"##SUBSTOP##" * var"##LOOP_STEP##" + var"##LOOPOFFSET##" - var"##LOOP_STEP##"
$threadlocal_get
@inbounds begin
$excomb
end
$threadlocal_set
nothing
end
end
end
push!(q.args, esc(closureq))
batchcall = if threadlocal !== Symbol("")
Expr(
:call,
batch,
esc(closure),
Val(true),
threadtup,
Symbol("##LOOPOFFSET##"),
Symbol("##LOOP_STEP##"),
)
else
Expr(
:call,
batch,
esc(closure),
Val(false),
threadtup,
Symbol("##LOOPOFFSET##"),
Symbol("##LOOP_STEP##"),
)
end
for a ∈ arguments
push!(args.args, get(defined, a, a))
push!(batchcall.args, esc(a))
end
push!(q.args, batchcall)
quote
if $(Threads.nthreads)() == 1
single_thread_result = begin
$(esc(threadlocal_init_single)) # Initialize threadlocal storage
$(esc(q_single))
$(esc(threadlocal_repack_single))
end
# Put the single-thread threadlocal storage in a single-element Vector
$threadlocal_single_store
else
$(esc(threadlocal_init1))
let
$q
end
end
end
end
"""
@batch for i in Iter; ...; end
Evaluate the loop on multiple threads.
@batch minbatch=N for i in Iter; ...; end
Create a thread-local storage used in the loop.
@batch localthread=init() for i in Iter; ...; end
The `init` function will be called at the start at each thread. `localthread` will
refer to storage local for the thread. At the end of the loop, a `localthread`
vector containing all the thread-local values will be available. A type can be specified
with `threadlocal=init()::Type`.
Evaluate at least N iterations per thread. Will use at most `length(Iter) ÷ N` threads.
@batch per=core for i in Iter; ...; end
@batch per=thread for i in Iter; ...; end
Use at most 1 thread per physical core, or 1 thread per CPU thread, respectively.
One thread per core will mean less threads competing for the cache, while (for example) if
there are two hardware threads per physical core, then using each thread means that there
are two independent instruction streams feeding the CPU's execution units. When one of
these streams isn't enough to make the most of out of order execution, this could increase
total throughput.
Which performs better will depend on the workload, so if you're not sure it may be worth
benchmarking both.
LoopVectorization.jl currently only uses up to 1 thread per physical core. Because there
is some overhead to switching the number of threads used, `per=core` is `@batch`'s default,
so that `Polyester.@batch` and `LoopVectorization.@tturbo` work well together by default.
Threads are not pinned to a given CPU core and the total number of available threads is
still governed by `--threads` or `JULIA_NUM_THREADS`.
You can pass both `per=(core/thread)` and `minbatch=N` options at the same time, e.g.
@batch per=thread minbatch=2000 for i in Iter; ...; end
@batch minbatch=5000 per=core for i in Iter; ...; end
"""
macro batch(ex)
enclose(macroexpand(__module__, ex), 0, 1, :core, (Symbol(""), :Any), __module__)
end
function interpret_kwarg(
arg,
reserve_per = 0,
minbatch = 1,
per = :core,
threadlocal = (Symbol(""), :Any),
)
a = arg.args[1]
v = arg.args[2]
if a === :reserve
@assert v ≥ 0
reserve_per = v
elseif a === :minbatch
minbatch = v
elseif a === :per
per = v::Symbol
@assert (per === :core) | (per === :thread)
elseif a === :threadlocal
if Meta.isexpr(v, :(::), 2) && v.head == :(::)
threadlocal = (v.args[1], v.args[2])
else
threadlocal = (v, :Any)
end
else
throw(ArgumentError("kwarg $(a) not recognized."))
end
reserve_per, minbatch, per, threadlocal
end
macro batch(arg1, ex)
reserve, minbatch, per, threadlocal = interpret_kwarg(arg1)
enclose(macroexpand(__module__, ex), reserve, minbatch, per, threadlocal, __module__)
end
macro batch(arg1, arg2, ex)
reserve, minbatch, per, threadlocal = interpret_kwarg(arg1)
reserve, minbatch, per, threadlocal =
interpret_kwarg(arg2, reserve, minbatch, per, threadlocal)
enclose(macroexpand(__module__, ex), reserve, minbatch, per, threadlocal, __module__)
end
macro batch(arg1, arg2, arg3, ex)
reserve, minbatch, per, threadlocal = interpret_kwarg(arg1)
reserve, minbatch, per, threadlocal =
interpret_kwarg(arg2, reserve, minbatch, per, threadlocal)
reserve, minbatch, per, threadlocal =
interpret_kwarg(arg3, reserve, minbatch, per, threadlocal)
enclose(macroexpand(__module__, ex), reserve, minbatch, per, threadlocal, __module__)
end