I’m trying to benchmark various parallelization approaches, and realized I’m not sure how to change which macro is called…? Is this possible?
function count_threads(macro_name=Threads.@threads)
n = Threads.nthreads()
a = zeros(n)
# next line is psuedocode
@macro_name for i = 1:n
a[i] = Threads.threadid()
end
length(unique(a))
end
A little-known fact is that macros are represented as values just like functions or anything else, they are just a bit harder to get at. And calling them gives the expanded code.
julia> m = Base.var"@time"
@time (macro with 1 method)
julia> typeof(m)
Base.var"#@time"
julia> m(LineNumberNode(@__LINE__, @__FILE__), @__MODULE__, :(1+2))
quote
#= timing.jl:206 =#
...
end
So you can do what you want if you make count_threads a generated function.