Skip to content

BLAS: cancel long level-3 calls through a per-thread flag byte - #2008

Draft
vtjnash wants to merge 1 commit into
jn/revert-cancel-unsafefrom
jn/safe-cancel-token
Draft

vtjnash wants to merge 1 commit into
jn/revert-cancel-unsafefrom
jn/safe-cancel-token

Conversation

@vtjnash

@vtjnash vtjnash commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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#TBD and the OpenBLAS patch that adds openblas_set_thread_cancel_flag.

Assisted-by: Claude Code (Opus 5.5)

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

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.20690% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.56%. Comparing base (56f7246) to head (bc43993).

Files with missing lines Patch % Lines
src/blas.jl 85.71% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant