Skip to content

oneMKL: return from calls without waiting for the queue - #791

Open
maleadt wants to merge 2 commits into
mainfrom
tb/onemkl-nonblocking
Open

maleadt wants to merge 2 commits into
mainfrom
tb/onemkl-nonblocking

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

Every oneMKL wrapper in the support library waited for the queue after submitting its work, so each call was a host round trip and a sequence of small calls couldn't overlap (point 3 of #766). The wait was added in #518 to fix norm returning 0, before oneAPI.jl ordered oneMKL work with the rest of its operations. Today memory passed to oneMKL as a ZePtr records the task's stream as its user, so host reads, other tasks and the next kernel already wait for the oneMKL work they depend on.

The generated wrappers now return once their work is submitted. The wait stays where that tracking doesn't apply: bindings that pass host memory (the scalar results of dot and nrm2, the parameter arrays of the grouped batch routines, and other bindings without ZePtr argument types), the sparse routines, whose matrix handles refer to memory the call doesn't see, the FFTs, whose plans use the queue of the task that created them, and potrf/getrf/sytrf, which return info to cholesky, lu and bunchkaufman. The generator decides from the binding types in res/support.toml, so a newly wrapped routine waits until its binding passes device memory as ZePtr. Two ordering gaps that the wait used to hide are closed: an FFT plan used by another task than the one that created it executes on the creator's queue, so it now first waits for the using task's oneMKL work; and a call submitted to the device of its arguments rather than the selected one (as the BLAS wrappers do since #534) is now ordered with, and waited for through, the selected device's stream, which is where its memory records its user.

Because calls are no longer serialized by the wait, the SYCL queue is now created in_order (the wrappers submit without dependencies). It also gets an async handler: errors that oneMKL reports after a call returned are thrown as a SYCLError by the next oneMKL call or by whatever waits for the work (synchronize, a host read, the next kernel). Without a handler, DPC++'s default one would have terminated the process.

Per call, back-to-back on an Arc A770, Float32, best of 5 × 500 calls with one synchronize at the end (the reproducer from #766):

before after
gemm! m=32 / 64 / 256 75 / 75 / 88 µs 11 / 10 / 15 µs
syrk! m=32 / 64 / 256 75 / 75 / 87 µs 10 / 10 / 15 µs
trsm! m=32 / 64 / 256 77 / 89 / 138 µs 11 / 17 / 61 µs
potrs! n=32 / 256 142 / 299 µs 36 / 177 µs
potrf! n=32 / 256 267 / 1048 µs 279 / 1063 µs

potrf! doesn't change because oneMKL itself waits for the GPU inside potrf, as it does in getrf, geqrf, ormqr, orgqr, potri, syevd, gesvd and gebrd. That is also why there is no check keyword like the one in JuliaGPU/CUDA.jl#3639 and JuliaGPU/AMDGPU.jl#1220: oneMKL reports info by throwing from the call, never at the wait. potrs, getrs, getri and trtrs return early and never report a singular matrix, waiting or not. So skipping the wait loses no error reporting, and a keyword to restore it would change nothing (measured on the A770 with oneMKL 2026.1; I couldn't check PVC).

The new tests keep the queue busy with large products and check that gemm!, mul!, syrk!, trsm! and potrs! return before it drains (they fail with the old library), that dot and potrf! still wait, that a chain of dependent calls reads back correctly from this task and from another one, and that cholesky still throws PosDefException behind queued work. The oneMKL, FFT and SYCL tests and the full suite pass locally. This changes deps/src, so CI builds the support library from source, and oneAPI_Support_jll needs a new build before a release.

The oneMKL wrappers submit without dependencies and discard the returned
events, relying on the SYCL queue to execute their work in submission
order. The queue was created without the in_order property; that went
unnoticed because every wrapper waits for the queue before returning.
Without an async handler, an asynchronous error would also have reached
DPC++'s default handler, which terminates the process.

Create the queue in order, with a handler that records errors. The SYCL
queue now lives on the task's stream rather than in task-local storage,
so that the points that wait for oneMKL work (the next Julia submission,
host accesses, synchronize) can wait for the SYCL queue and report what
it recorded as a SYCLError. The next oneMKL call also reports them.
Every generated wrapper waited for the queue after submitting its work,
which made each call a host round trip: back-to-back gemm! calls on 32x32
matrices took 75 µs each on an Arc A770. The wait dates from before
oneAPI.jl ordered oneMKL work with the rest: today, memory passed as a
ZePtr records the task's stream as its user, so host accesses, other
tasks and the next Julia submission wait for the oneMKL work as needed.

The generator now only keeps the wait where that tracking doesn't cover
the call: bindings that pass host pointers (the scalar results of dot and
nrm2, the parameter arrays of grouped batch routines, and other bindings
without ZePtr types), the sparse routines, whose matrix handles refer to
memory the call doesn't see, and potrf, getrf and sytrf, whose info is
returned to LinearAlgebra. Scratchpad size queries and the hand-written
batch wrappers no longer wait before the call. The FFT wrappers keep
waiting.

Two cases relied on the wait for ordering. An FFT plan executes on the
queue of the task that created it, so a plan used by another task now
first waits for that task's oneMKL work. And the BLAS wrappers submit to
the device of their arguments, while the memory records the selected
device's stream as its user; when the two differ, the call is now
ordered after that stream's work, and waiting for that stream also waits
for the call.

Small gemm!, syrk! and trsm! calls now take 10-17 µs. With oneMKL 2026.1
on the A770, the factorizations that report info (potrf, getrf, geqrf,
syevd, gesvd, ...) wait for the GPU internally and throw from the call,
while potrs, getrs, getri and trtrs report nothing either way, so
skipping the wait loses no error reporting.
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Documentation preview: https://juliagpu.github.io/oneAPI.jl/previews/PR791/

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