Skip to content
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

change default threading scheduler to :static #50019

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions base/threadingconstructs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ function _threadsfor(iter, lbody, schedule)
end
end
end
if $(schedule === :dynamic || schedule === :default)
if $(schedule === :dynamic)
threading_run(threadsfor_fun, false)
elseif ccall(:jl_in_threaded_region, Cint, ()) != 0 # :static
error("`@threads :static` cannot be used concurrently or nested")
else # :static
elseif $(schedule === :static || schedule === :default) # :static
threading_run(threadsfor_fun, true)
end
nothing
Expand Down Expand Up @@ -234,12 +234,12 @@ For example, the above conditions imply that:
## Schedulers

Without the scheduler argument, the exact scheduling is unspecified and varies across Julia
releases. Currently, `:dynamic` is used when the scheduler is not specified.
releases. Currently, `:static` is used when the scheduler is not specified.

!!! compat "Julia 1.5"
The `schedule` argument is available as of Julia 1.5.

### `:dynamic` (default)
### `:dynamic`

`:dynamic` scheduler executes iterations dynamically to available worker threads. Current
implementation assumes that the workload for each iteration is uniform. However, this
Expand All @@ -256,9 +256,15 @@ smaller than the cost of spawning and synchronizing a task (typically less than
microseconds).

!!! compat "Julia 1.8"
The `:dynamic` option for the `schedule` argument is available and the default as of Julia 1.8.
The `:dynamic` option for the `schedule` argument is available as of Julia 1.8.

### `:static`
!!! warning
If your code rely on indexing buffer with [`Threads.threadid()`](@ref), you should
use `:static` shcedule. For example, the now-discouraged pattern from
[1.3 release blog post](https://julialang.org/blog/2019/07/multithreading/#thread-local_state)
Specifically, the use of `temps[Threads.threadid()]` is incorrect when using dynamic scheduler.

### `:static` (default)

`:static` scheduler creates one task per thread and divides the iterations equally among
them, assigning each task specifically to each thread. In particular, the value of
Expand All @@ -267,10 +273,11 @@ Specifying `:static` is an error if used from inside another `@threads` loop or
thread other than 1.

!!! note
`:static` scheduling exists for supporting transition of code written before Julia 1.3.
`:static` scheduling exists for supporting transition of code written earlier Julia.
In newly written library functions, `:static` scheduling is discouraged because the
functions using this option cannot be called from arbitrary worker threads.


## Example

To illustrate of the different scheduling strategies, consider the following function
Expand Down
4 changes: 2 additions & 2 deletions test/threads_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ test_load_and_lookup_18020(10000)
# This may not be efficient/fully supported but should work without crashing.....
function test_nested_loops()
a = zeros(Int, 100, 100)
@threads for i in 1:100
@threads for j in 1:100
@threads :dynamic for i in 1:100
@threads :dynamic for j in 1:100
a[j, i] = i + j
end
end
Expand Down