-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
Threads.foreach
for convenient multithreaded Channel consumption (
#34543) Co-authored-by: Takafumi Arakaki <[email protected]> Co-authored-by: Alex Arslan <[email protected]> Co-authored-by: Valentin Churavy <[email protected]>
- Loading branch information
1 parent
150311f
commit 39fc4ee
Showing
5 changed files
with
98 additions
and
1 deletion.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
""" | ||
Threads.foreach(f, channel::Channel; | ||
schedule::Threads.AbstractSchedule=Threads.FairSchedule(), | ||
ntasks=Threads.nthreads()) | ||
Similar to `foreach(f, channel)`, but iteration over `channel` and calls to | ||
`f` are split across `ntasks` tasks spawned by `Threads.@spawn`. This function | ||
will wait for all internally spawned tasks to complete before returning. | ||
If `schedule isa FairSchedule`, `Threads.foreach` will attempt to spawn tasks in a | ||
manner that enables Julia's scheduler to more freely load-balance work items across | ||
threads. This approach generally has higher per-item overhead, but may perform | ||
better than `StaticSchedule` in concurrence with other multithreaded workloads. | ||
If `schedule isa StaticSchedule`, `Threads.foreach` will spawn tasks in a manner | ||
that incurs lower per-item overhead than `FairSchedule`, but is less amenable | ||
to load-balancing. This approach thus may be more suitable for fine-grained, | ||
uniform workloads, but may perform worse than `FairSchedule` in concurrence | ||
with other multithreaded workloads. | ||
!!! compat "Julia 1.6" | ||
This function requires Julia 1.6 or later. | ||
""" | ||
function Threads.foreach(f, channel::Channel; | ||
schedule::Threads.AbstractSchedule=Threads.FairSchedule(), | ||
ntasks=Threads.nthreads()) | ||
apply = _apply_for_schedule(schedule) | ||
stop = Threads.Atomic{Bool}(false) | ||
@sync for _ in 1:ntasks | ||
Threads.@spawn try | ||
for item in channel | ||
$apply(f, item) | ||
# do `stop[] && break` after `f(item)` to avoid losing `item`. | ||
# this isn't super comprehensive since a task could still get | ||
# stuck on `take!` at `for item in channel`. We should think | ||
# about a more robust mechanism to avoid dropping items. See also: | ||
# https://github.com/JuliaLang/julia/pull/34543#discussion_r422695217 | ||
stop[] && break | ||
end | ||
catch | ||
stop[] = true | ||
rethrow() | ||
end | ||
end | ||
return nothing | ||
end | ||
|
||
_apply_for_schedule(::Threads.StaticSchedule) = (f, x) -> f(x) | ||
_apply_for_schedule(::Threads.FairSchedule) = (f, x) -> wait(Threads.@spawn f(x)) |
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
39fc4ee
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.
Executing the daily package evaluation, I will reply here when finished:
@nanosoldier
runtests(ALL, isdaily = true)
39fc4ee
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.
Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt