Optional macro invocation

I suppose one fundamental distinction is whether you want to make the threads optional at runtime vs at compile time. I think you (and the OP) were primarily looking for a compile-time switch, whereas I’m looking for a runtime switch.

Based on @jlapeyre’s answer, it occurred to me that maybe the easiest way to do this for @threads is to just modify the definition of the macro from the standard library. Indeed, the following seems like it works perfectly for me:

module ConditionalThreads

using Base.Threads
using Base.Threads: threadid, threading_run

macro threadsif(cond, loop)
    if !(isa(loop, Expr) && loop.head === :for)
        throw(ArgumentError("@threadsif requires a `for` loop expression"))
    end
    if !(loop.args[1] isa Expr && loop.args[1].head === :(=))
        throw(ArgumentError("nested outer loops are not currently supported by @threadsif"))
    end
    quote
        if $(esc(cond))
            $(Threads._threadsfor(loop.args[1], loop.args[2], :static))
        else
            $(esc(loop))
        end
    end
end

end

I’ve left out the schedule from the original implementation for conciseness since that’s currently limited to :static anyway. It wouldn’t be hard to add that back in.

Edit (usage example):

I currently have the above macro definition “in production” at QuantumControlBase.jl, and it is used e.g. here:

https://github.com/QuantumControl-jl/Krotov.jl/blob/a13206335003384cda41f2d5279e8c87be241236/src/optimize.jl#L303-L310

1 Like