-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add Threads.foreach for convenient multithreaded Channel consumption
#34543
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7d963d1
add threaded_foreach function to convenient multithreaded Channel con…
jrevels e6ba991
remove async flag since callers can just use async macro
jrevels 2f859c5
threaded_foreach --> Threads.foreach
jrevels 863eb76
really basic Threads.foreach tests
jrevels be1bb55
manually use sync_end to avoid sync macro state mutation
jrevels 2768ed2
Update base/channels.jl
jrevels 55c071b
define Threads.foreach after dependent code mechanisms
jrevels 9ea2858
expand comment
jrevels 2c60672
Update base/threadfuncs.jl
jrevels 9e26dfe
Update base/threadfuncs.jl
jrevels ba972dd
add schedule argument to docs
jrevels bc241da
interpolate `apply`
jrevels 956bf66
remove implementation details for Threads.foreach docstring
jrevels ee4500c
hit both fair and static schedule codepaths in Threads.foreach tests
jrevels 9e31f46
use traits instead of symbols for schedule selection to avoid unneces…
jrevels ea3626e
threadfuncs.jl --> threads_overloads.jl since it only exists to provi…
jrevels 0fee2f1
[ci skip] add NEWS entry
jrevels 5f2b801
[ci skip] fix typo
jrevels dcf47e1
make `schedule` a kwarg and test default values
jrevels 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.