-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenMP's master
, single
, critical
blocks
#73
Comments
I am not sure if this is a sensible operation in Julia given tasks. The task starting the parallel for-loop is waiting on the sub-tasks and is not participating in the pfor-loop. |
FWIW, I would probably just declare the first spawned parallel task to be the "master" of this pool of tasks. But I agree, we should think about what we can/should port and what not. I'm creating these issues just to think about this at some point :) |
Also I strongly dislike the term "master" these days and try to use "primary" instead. |
|
Alternatively, if we had a @tasks for i in 1:10
@master println("hi")
do_stuff()
end it could expand to something like tasks = map(chunks(1:10; ...))
@spawn do_stuff()
end
println("hi") # <--------- the @master code here runs before we fetch, but after we spawn
foreach(fetch, tasks) |
These pragmas are typically used within a bigger parallel block (e.g. inside of |
I see. In that case, I dont really see any advantage over |
@vchuravy I tried to implement this (#93) and came up with struct SectionSingle
first::Base.RefValue{Bool}
lck::ReentrantLock
SectionSingle() = new(Ref(true), ReentrantLock())
end
function try_enter(f, s::SectionSingle)
run_f = false
lock(s.lck) do
if s.first[]
run_f = true # The first task to try_enter → run f
s.first[] = false
end
end
run_f && f()
end which can be used as s = SectionSingle()
@tasks for i in 1:10
@set ntasks = 10
println(i, ": before")
try_enter(s) do
println(i, ": only printed by a single task")
sleep(1)
end
println(i, ": after")
end Any comments about my attempt (or better implementations)? |
If you have to use a You also don't need a lock.
|
In fact, it was a
Nice, completely forgot about per-field atomics. What's the purpose of |
It is an optimization. It's a typical test-test-and-set pattern. Make a weak memory load for the original value and only do the expensive compare and swap when necessary. |
#pragma omp master
→ only the master thread runs this block, the rest skip over it#pragma omp single
→ only a single thread (any) runs this block, the rest skip over it#pragma omp critical
→ all threads run this block but only one at a time (any order)The text was updated successfully, but these errors were encountered: