Repository navigation
Conversation
Make long `gemm!`, `symm!` and `hemm!` calls stop early when the cancellation source governing the calling task is cancelled. The calls are made from Julia code on the calling thread, with no handler running in signal context. Julia's cancellation state is a byte, `CancellationTokenSource.state`. It is zero until the source is cancelled and nonzero afterwards, and it is never cleared, which is all a foreign library needs to poll. The wrapper takes the address of that byte from `Base.pointer_from_cancellation_token`. It passes the address to `openblas_set_thread_cancel_flag` for the calling thread, makes the call, and restores the previous designation in a `finally`, so nested designations work. The level-3 drivers poll the byte between blocks and return once it is nonzero. The abandoned call leaves garbage in its output, so `checkcancel` runs after every large call and throws before the result can escape. The designation belongs to the thread, so it must not span a yield point. The setter is looked up with the suffix of the library that libblastrampoline forwards `dgemm_` to, so a dormant library is never designated and LP64 and ILP64 copies do not collide. The lookup is cached in a typed global, which `lbt_forward` and `__init__` reset. Calls below 2^24 multiply-adds are left as they were. If the library has no setter, a cancelled large call runs to completion and then throws. This needs `Base.pointer_from_cancellation_token` from JuliaLang/julia and the OpenBLAS patch that adds `openblas_set_thread_cancel_flag`. Co-authored-by: Keno Fischer <Keno@users.noreply.github.com> Assisted-by: Claude Code (Opus 5.5)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## jn/revert-cancel-unsafe #2008 +/- ##
===========================================================
- Coverage 99.58% 99.56% -0.03%
===========================================================
Files 35 35
Lines 18674 18700 +26
===========================================================
+ Hits 18596 18618 +22
- Misses 78 82 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Make long
gemm!,symm!andhemm!calls stop early when the cancellation source governing the calling task is cancelled. The calls are made from Julia code on the calling thread, with no handler running in signal context.Julia's cancellation state is a byte,
CancellationTokenSource.state. It is zero until the source is cancelled and nonzero afterwards, and it is never cleared, which is all a foreign library needs to poll. The wrapper takes the address of that byte fromBase.pointer_from_cancellation_token. It passes the address toopenblas_set_thread_cancel_flagfor the calling thread, makes the call, and restores the previous designation in afinally, so nested designations work. The level-3 drivers poll the byte between blocks and return once it is nonzero. The abandoned call leaves garbage in its output, socheckcancelruns after every large call and throws before the result can escape. The designation belongs to the thread, so it must not span a yield point.The setter is looked up with the suffix of the library that libblastrampoline forwards
dgemm_to, so a dormant library is never designated and LP64 and ILP64 copies do not collide. The lookup is cached in a typed global, whichlbt_forwardand__init__reset. Calls below 2^24 multiply-adds are left as they were. If the library has no setter, a cancelled large call runs to completion and then throws.This needs
Base.pointer_from_cancellation_tokenfrom JuliaLang/julia#TBD and the OpenBLAS patch that addsopenblas_set_thread_cancel_flag.Assisted-by: Claude Code (Opus 5.5)