-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
add BLAS.with_num_threads #41785
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
Closed
johnnychen94
wants to merge
5
commits into
JuliaLang:master
from
johnnychen94:jc/blas_with_num_threads
Closed
add BLAS.with_num_threads #41785
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba9ad6c
add BLAS.with_num_threads
johnnychen94 58cbcb6
add news entry and function cross-reference
johnnychen94 40c78ce
add experimental flag
johnnychen94 fddd155
apply suggestions on simplifying the implementation
johnnychen94 f7a6648
rework the thread unsafe test to make it more meaningful
johnnychen94 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,9 @@ Set the number of threads the BLAS library should use equal to `n::Integer`. | |
|
|
||
| Also accepts `nothing`, in which case julia tries to guess the default number of threads. | ||
| Passing `nothing` is discouraged and mainly exists for historical reasons. | ||
|
|
||
| See also [`with_num_threads`](@ref BLAS.with_num_threads) to temporarily change | ||
| number of BLAS threads. | ||
| """ | ||
| set_num_threads(nt::Integer)::Nothing = lbt_set_num_threads(Int32(nt)) | ||
| function set_num_threads(::Nothing) | ||
|
|
@@ -157,6 +160,56 @@ function check() | |
| end | ||
| end | ||
|
|
||
| """ | ||
| with_num_threads(f, num_threads::Integer) | ||
|
|
||
| Run function `f()` with BLAS threads `num_threads` and then | ||
| restore to previous threads setting. | ||
|
|
||
| !!! compat "Julia 1.8" | ||
| `with_num_threads` requires at least Julia 1.8. | ||
|
|
||
| # Example | ||
|
|
||
| Depending on the number of available CPU cores, the result can be different: | ||
|
|
||
| ```julia | ||
| julia> BLAS.get_num_threads() | ||
| 8 | ||
|
|
||
| julia> with_num_threads(4) do | ||
| BLAS.get_num_threads() | ||
| # or doing some basic BLAS computation | ||
| end | ||
| 4 | ||
|
|
||
| julia> BLAS.get_num_threads() | ||
| 8 | ||
| ``` | ||
|
|
||
| !!! warning | ||
| This function is not thread safe. If there are multiple | ||
| threads calling BLAS routines, then the threads they are | ||
| using will also be changed until this function finishes. | ||
|
|
||
| !!! warning | ||
| This interface is experimental and subject to change or | ||
| removal without notice. | ||
|
|
||
| See also [`set_num_threads`](@ref BLAS.set_num_threads) to permanently change | ||
| number of BLAS threads. | ||
| """ | ||
| function with_num_threads(f, num_threads::Integer) | ||
| prev_num_threads = BLAS.get_num_threads() | ||
| BLAS.set_num_threads(num_threads) | ||
| try | ||
| return f() | ||
| catch | ||
|
Member
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. Unnecessary |
||
| rethrow() | ||
| finally | ||
| BLAS.set_num_threads(prev_num_threads) | ||
| end | ||
| end | ||
|
|
||
| # Level 1 | ||
| ## copy | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -641,6 +641,30 @@ end | |
| @test BLAS.get_num_threads() === default | ||
| end | ||
|
|
||
| @testset "with_num_threads" begin | ||
| prev_num_threads = BLAS.get_num_threads() | ||
| context_num_threads = BLAS.with_num_threads(1) do | ||
| BLAS.get_num_threads() | ||
| end | ||
| @test context_num_threads == 1 | ||
| @test prev_num_threads == BLAS.get_num_threads() | ||
|
|
||
| @testset "thread unsafe" begin | ||
| prev_num_threads = BLAS.get_num_threads() | ||
| context_num_threads = 1 | ||
| # task A | ||
| t = @async BLAS.with_num_threads(context_num_threads) do | ||
| sleep(0.5) | ||
| end | ||
| sleep(0.1) | ||
|
Member
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. This execution order is not guaranteed. Use a barrier instead to guarantee the ordering of events |
||
| # check that main thread is affected by task A | ||
| @test BLAS.get_num_threads() == context_num_threads | ||
| # when the task finishes, the num threads get restored | ||
| wait(t) | ||
| @test prev_num_threads == BLAS.get_num_threads() | ||
| end | ||
| end | ||
|
|
||
| # https://github.com/JuliaLang/julia/pull/39845 | ||
| @test LinearAlgebra.BLAS.libblas == "libblastrampoline" | ||
| @test LinearAlgebra.BLAS.liblapack == "libblastrampoline" | ||
|
|
||
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.