From 8619befb0078ceb46739d66ee92d2d1f6f30c3d0 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 28 Oct 2026 13:34:58 +0100 Subject: [PATCH] Precompile common operations in the oneAPI extension oneAPI can't precompile these itself: this extension isn't loaded while oneAPI precompiles, so the kernels it would cache use the default tunings rather than the ones set here. When oneAPI can use the GPU while precompiling, run `sum` of a vector and along each dimension of a matrix, `cumsum`, `any` and `all` in a `@compile_workload` of the extension, at sizes that use the same kernels as arrays of millions of elements. On an Arc A770 with Julia 1.13, the first `sum` of a million elements takes 0.17 s rather than 1.2 s, and the first `cumsum` 0.41 s rather than 2.2 s, for an extension image that is 11 MB larger and about 80 ms more load time. --- Project.toml | 2 ++ ext/oneAPIExt.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Project.toml b/Project.toml index 1777ec22..52ff225d 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197" Atomix = "a9b6321e-bd34-4604-b9c9-b65b8de01458" GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527" KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c" +PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a" [weakdeps] CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" @@ -33,5 +34,6 @@ GPUArraysCore = "0.2.1" KernelAbstractions = "0.9.43, 0.10" Metal = "1.10.1" OpenCL = "0.10, 1" +PrecompileTools = "1" julia = "1.10" oneAPI = "1, 2" diff --git a/ext/oneAPIExt.jl b/ext/oneAPIExt.jl index ad57e343..53e730a4 100644 --- a/ext/oneAPIExt.jl +++ b/ext/oneAPIExt.jl @@ -3,6 +3,7 @@ module oneAPIExt using oneAPI import AcceleratedKernels as AK +using PrecompileTools: @setup_workload, @compile_workload # Some Intel GPUs (reportedly Intel UHD Graphics) hang when many threads write one global location, @@ -29,4 +30,29 @@ AK.sort_tuning(::oneAPIBackend, ::Type{T}) where {T} = AK.SortTuning(bitonic_max_len=128, bitonic_max_slice_len=128) +# oneAPI's own workload can't cache these operations: this extension isn't loaded while oneAPI +# precompiles, so the kernels it would compile use the default tunings, not the ones above. The +# sizes are large enough for the kernels that arrays of millions of elements use. Before Julia +# 1.12, compiling GPU code while precompiling can leak device code into the image. +@static if VERSION >= v"1.12-" +@setup_workload begin + # (`can_precompile` checks that the GPU can be used while precompiling) + if isdefined(oneAPI, :can_precompile) && oneAPI.can_precompile() + @compile_workload begin + m = oneArray(ones(Float32, 4096, 64)) + v = vec(m) + sum(v) + sum(m; dims=1) + sum(m; dims=2) + cumsum(v) + any(iszero, v) + all(iszero, v) + sum(oneArray(ones(Int, 4096))) + oneAPI.synchronize() + end + end +end +end + + end # module oneAPIExt