Is there a way to enable/disable calls to Threads.@threads?
I’m developing an optimization solver, and when the problem is sufficiently small, using multi-threading hurts performance, and incurs allocations (my code doesn’t make any allocations until I wrap it in @threads). However, when the problem size grows and the individual operations being threaded become more computationally intensive (booting the arithmetic intensity) I get some good performance increases.
I’d like to be able to enable/disable multi-threading as a user-specified option. I can’t just restart Julia with JULIA_NUM_THREADS=1, since @threads still allocates memory and is slower than had I left it out. Can I do this using metaprogramming?
I know I can obviously just create duplicates of my functions and wrap some with @threads, but I’d rather avoid this if possible.
FYI: nearly all of my functions are trivially parallelizable, like this
function foo(vals, vars) for k in eachindex(vals) vals[k] = somefunction(vars[k]) end end