-
-
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 BLAS.with_num_threads #41785
add BLAS.with_num_threads #41785
Conversation
@KristofferC @dkarrasch @chriselrod @YingboMa Thoughts on this? I generally don't like code and APIs that code in numbers of threads, but this is a reality we have to work with today. Since this is an unexported API, I feel comfortable merging. |
@johnnychen94 Any ideas why the doctests are failing? |
acee657
to
d67f771
Compare
That seems to be noise. After rebasing on the current master the test passes now. |
Would you prefer they're integrated with Julia's threading and the scheduler handles it? I think this is an improvement, but it'd be concerning if we want to break it later (e.g. after/if this merges). |
That PR has not seen any real work since it was created. I believe this PR would be compatible with that, in any case. |
Also, if we make the additions in this PR experimental and not part of the public API, then we can break it (and even remove it) in a future minor release. |
Should I only tag |
Only |
Let's rebase this and get it merged. |
91f50af
to
51d349f
Compare
stdlib/LinearAlgebra/test/blas.jl
Outdated
@testset "thread unsafe" begin | ||
prev_num_threads = BLAS.get_num_threads() | ||
# thread unsafe | ||
@async BLAS.with_num_threads(1) do |
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.
What is this test testing?
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.
This is used to simulate the case that when there exists a task with with_num_threads
, then all threads are affected until that task is finished.
stdlib/LinearAlgebra/src/blas.jl
Outdated
BLAS.set_num_threads(num_threads) | ||
local retval | ||
try | ||
retval = f() |
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.
retval = f() | |
return f() |
and fixup the rest?
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, I must have messed up with rebasing on a computer that has different local git history. This is pointed out in #41785 (comment) already.
Thanks for catching this again!
8ff076e
to
87ea73f
Compare
87ea73f
to
f7a6648
Compare
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.
I am really not a fan of this. Mutating global state like this makes this effectively unusable from a library writers perspective.
Another question that came to mind is if this is portable across blas vendors? Or is this OpenBLAS specific.
t = @async BLAS.with_num_threads(context_num_threads) do | ||
sleep(0.5) | ||
end | ||
sleep(0.1) |
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.
This execution order is not guaranteed. Use a barrier instead to guarantee the ordering of events
BLAS.set_num_threads(num_threads) | ||
try | ||
return f() | ||
catch |
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.
Unnecessary catch
|
Okay. Should I fix the comments or just close this PR? I'm okay with having this on my local projects. I propose this PR as I feel like there might be other people wanting it. |
This isn't a very good solution so let me just close this one. |
Yes, I have been thinking that it isn't a good idea to change global state in base Julia like this. |
I added this
BLAS.with_num_threads
as a convenient wrapper, but I'm not sure if we want it live in Base because it's not thread-safe.In some cases, multi-threading could happen at a more efficient higher level instead of at the BLAS level. Thus we may want to temporarily disable BLAS threads during the julia threads execution.
As an example, SVD is one typical case: if there are a bunch of SVD operations and if we can do embarrassingly parallel at a higher level, then it's better to disable the BLAS threads.
Would need to add a news entry if approved.