Macro to turn on/off parallelisation

It’s hard to find the problem when the code you post runs fine. :slight_smile:

My guess is that the issue you’re encountering is related to macro hygiene. For example,

macro parallel(ex::Expr)
    return :(Threads.@threads $ex)
end

function run!()
    sleep_time = 1.
    @parallel for i = 1:2
        sleep(sleep_time)
     end
end

run!()

throws an error:

ERROR: TaskFailedException

    nested task error: UndefVarError: `sleep_time` not defined
    ...

Changing to macro to

macro parallel(ex::Expr)
    return esc(:(Threads.@threads $ex))
end

and keeping the rest of the code the same, everything now runs just fine.

2 Likes