-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add @threads :dynamic
#40908
Closed
Closed
Add @threads :dynamic
#40908
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ function _threadsfor(iter, lbody, schedule) | |
range = iter.args[2] | ||
quote | ||
local threadsfor_fun | ||
local threadsfor_fun_dynamic | ||
let range = $(esc(range)) | ||
function threadsfor_fun(onethread=false) | ||
r = range # Load into local variable | ||
|
@@ -85,6 +86,15 @@ function _threadsfor(iter, lbody, schedule) | |
$(esc(lbody)) | ||
end | ||
end | ||
idx = Atomic{UInt}(1) | ||
function threadsfor_fun_dynamic() | ||
r = range # Load into local variable | ||
lenr = length(r) | ||
while (i = atomic_add!(idx, UInt(1))) <= lenr | ||
local $(esc(lidx)) = @inbounds r[i] | ||
$(esc(lbody)) | ||
end | ||
end | ||
end | ||
if threadid() != 1 || ccall(:jl_in_threaded_region, Cint, ()) != 0 | ||
$(if schedule === :static | ||
|
@@ -94,7 +104,11 @@ function _threadsfor(iter, lbody, schedule) | |
:(Base.invokelatest(threadsfor_fun, true)) | ||
end) | ||
else | ||
threading_run(threadsfor_fun) | ||
$(if schedule === :dynamic | ||
:(threading_run(threadsfor_fun_dynamic)) | ||
else | ||
:(threading_run(threadsfor_fun)) | ||
end) | ||
end | ||
nothing | ||
end | ||
|
@@ -110,15 +124,20 @@ A barrier is placed at the end of the loop which waits for all tasks to finish | |
execution. | ||
|
||
The `schedule` argument can be used to request a particular scheduling policy. | ||
The only currently supported value is `:static`, which creates one task per thread | ||
Currently supported values are `:static` and `:dynamic`. `:static` creates one task per thread | ||
and divides the iterations equally among them. Specifying `:static` is an error | ||
if used from inside another `@threads` loop or from a thread other than 1. | ||
`:dynamic` creates one task per thread if possible and gets one new item from the iterations | ||
only if previous item is processed until all items are processed. | ||
|
||
The default schedule (used when no `schedule` argument is present) is subject to change. | ||
|
||
!!! compat "Julia 1.5" | ||
The `schedule` argument is available as of Julia 1.5. | ||
|
||
!!! compat "Julia 1.7" | ||
The `schedule` argument supports `:dynamic` as of Julia 1.7. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4 space indent |
||
|
||
See also: [`@spawn`](@ref Threads.@spawn), [`nthreads()`](@ref Threads.nthreads), | ||
[`threadid()`](@ref Threads.threadid), `pmap` in [`Distributed`](@ref man-distributed), | ||
`BLAS.set_num_threads` in [`LinearAlgebra`](@ref man-linalg). | ||
|
@@ -133,7 +152,7 @@ macro threads(args...) | |
# for now only allow quoted symbols | ||
sched = nothing | ||
end | ||
if sched !== :static | ||
if sched ∉ [:static, :dynamic] | ||
throw(ArgumentError("unsupported schedule argument in @threads")) | ||
end | ||
elseif na == 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will need to come up with a different approach here. We need to avoid generating two copies of the code: the code size is 2^n for nesting depth n. Even though nesting
@threads
loops is not common, avoiding this is the best practice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the compiler be able to constprop the schedule to delete the branch not taken?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said, I'd just check if
schedule === :dynamic
orschedule === :static
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for late reply.
I think the
:dynamic
version is just like the default version which only use multi-threads in thread 1. So I don't think the 2^n thing will happen.This is why I said "
:dynamic
creates one task per thread if possible" in the doc string.