Conditional @threads macro causing @threads requires a for loop expression error #58875
Replies: 3 comments
-
Note that this type of question is better suited for https://discourse.julialang.org/. There's much more activity than here in github Discussions. You can do something like this, though there may be more intuitive ways.
|
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
Hi, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
I frequently use @threads in my code, especially in personal packages. However, I often face tricky-to-debug errors when code is run "behind" a @threads macro. Because of that, I wanted to create a sort of overload or conditional version of @threads, something like @conditionalThreads(cond::Bool, loop).
I came up with this solution:
` using Base.Threads
macro Conditionalthreads(cond, loop)
if loop.head != :for
error("@Conditionalthreads must be applied to a for loop.")end
quoteif $(esc(cond))
@threads $(esc(loop))else
$(esc(loop))end
end` end
@Conditionalthreads !isinteractive() for i in 1:5
println("i = ", i)` end
However, I get this error:
ERROR: LoadError: ArgumentError: @threads requires a
forloop expression
Stacktrace:[1] var"@threads"(__source__::LineNumberNode, __module__::Module, args::Vararg{Any})
@ Base.Threads ./threadingconstructs.jl:400[2] eval
@ ./boot.jl:430 [inlined][3] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)
@ Base ./loading.jl:2643`in expression starting at /home/francois.roux/OPTILE/readnetcdf/testmaybe_thread.jl:9
While I understand the error (there is some kind of verification inside @threads), I do not know how to fix this problem. :(
If anyone has an idea...
Beta Was this translation helpful? Give feedback.
All reactions