KernelAbstractions
KernelAbstractions.jl (KA) is a package that allows you to write GPU-like kernels targetting different execution backends. KA intends to be a minimal and performant library that explores ways to write heterogeneous code. Although parts of the package are still experimental, it has been used successfully as part of the Exascale Computing Project to run Julia code on pre-Frontier and pre-Aurora systems. Currently, profiling and debugging require backend-specific calls like, for example, in CUDA.jl.
While KernelAbstraction.jl is focused on performance portability, it emulates GPU semantics and therefore the kernel language has several constructs that are necessary for good performance on the GPU, but serve no purpose on the CPU. In these cases, we either ignore such statements entirely (such as with @synchronize) or swap out the construct for something similar on the CPU (such as using an MVector to replace @localmem). This means that CPU performance will still be fast, but might be performing extra work to provide a consistent programming model across GPU and CPU
Supported backends
All supported backends rely on their respective Julia interface to the compiler backend and depend on GPUArrays.jl and GPUCompiler.jl.
CUDA
import CUDA
using KernelAbstractionsCUDA.jl is currently the most mature way to program for GPUs. This provides a backend CUDABackend <: KA.Backend to CUDA.
Changelog
0.9
Major refactor of KernelAbstractions. In particular:
- Removal of the event system. Kernel are now implicitly ordered.
- Removal of backend packages, backends are now directly provided by CUDA.jl and similar
0.9.5
- adds
@kernel cpu=false
0.9.11
- adds
@kernel inbounds=true
0.9.22
- adds
KA.functional(::Backend)
0.9.32
- clarifies the semantics of
KA.copyto!and addsKA.pagelock! - adds support for multiple devices per backend
0.9.34
Restricts the semantics of @synchronize to require convergent execution. The OpenCL backend had several miss-compilations due to divergent execution of @synchronize. The CPU backend always had this limitation and upon investigation the CUDA backend similarly requires convergent execution, but allows for a wider set of valid kernels.
This highlighted a design flaw in KernelAbstractions. Most GPU implementations execute KernelAbstraction workgroups on static blocks This means a kernel with ndrange=(32, 30) might be executed on a static block of (32,32). In order to block these extra indices, KernelAbstraction would insert a dynamic boundscheck.
Prior to v0.9.34 a kernel like
@kernel function localmem(A)
N = @uniform prod(@groupsize())
I = @index(Global, Linear)
i = @index(Local, Linear)
lmem = @localmem Int (N,) # Ok iff groupsize is static
lmem[i] = i
@synchronize
A[I] = lmem[N - i + 1]
endwas lowered to GPU backends like this:
function localmem_gpu(A)
if __validindex(__ctx__)
N = @uniform prod(@groupsize())
I = @index(Global, Linear)
i = @index(Local, Linear)
lmem = @localmem Int (N,) # Ok iff groupsize is static
lmem[i] = i
@synchronize
A[I] = lmem[N - i + 1]
end
endThis would cause an implicit divergent execution of @synchronize.
With this release the lowering has been changed to:
function localmem_gpu(A)
__valid_lane__ __validindex(__ctx__)
N = @uniform prod(@groupsize())
lmem = @localmem Int (N,) # Ok iff groupsize is static
if __valid_lane__
I = @index(Global, Linear)
i = @index(Local, Linear)
lmem[i] = i
end
@synchronize
if __valid_lane__
A[I] = lmem[N - i + 1]
end
endNote that this follow the CPU lowering with respect to @uniform, @private, @localmem and @synchronize.
Since this transformation can be disruptive, user can now opt out of the implicit bounds-check, but users must avoid the use of @index(Global) and instead use their own derivation based on @index(Group) and @index(Local).
@kernel unsafe_indices=true function localmem(A)
N = @uniform prod(@groupsize())
gI = @index(Group, Linear)
i = @index(Local, Linear)
lmem = @localmem Int (N,) # Ok iff groupsize is static
lmem[i] = i
@synchronize
I = (gI - 1) * N + i
if i <= N && I <= length(A)
A[I] = lmem[N - i + 1]
end
end0.10
- KernelAbstractions requires Julia 1.10 or later.
- The
CPUbackend is an OpenCL backend:CPUis an alias ofPOCLBackend, which compiles kernels like the GPU backends do and runs them with PoCL on PoCL's own threads, and no longer on Julia tasks.CPU(; static=true)has been removed, as there is no dynamic task scheduling to opt out of anymore. - KernelAbstractions is built on KernelInterface, which defines
Backendand the host-side functions (allocate,synchronize, …) that KernelAbstractions re-exports. User code is unaffected, but backends have to implement KernelInterface; see the notes for backend implementations. - The Enzyme extension has been removed temporarily, and is planned to return in a later 0.10 release.
KernelAbstractions.@spawnlaunches kernels from a task likeThreads.@spawndoes, and additionally orders them after the work the spawning task has queued, and can select the device the task uses; see the Quickstart.KernelAbstractions.isgpuhas been removed. Like forGPUbelow, query a capability instead.ndrangeentries may be index ranges, given statically (kernel(backend, workgroupsize, (-2:N+3, 0:M+1))) or at launch (ndrange=(-2:N+3, 0:M+1), a single range, or aCartesianIndices).@index(Global, Cartesian)and@index(Global, NTuple)return the shifted indices.@ka_code_llvmand@ka_code_typedhave been removed. They reflected on the host-side lowering of a kernel, and@ka_code_llvmadditionally rejected GPU backends, which since the CPU backend became an OpenCL backend meant every backend. UseKernelAbstractions.@device_code_llvmandKernelAbstractions.@device_code_typedinstead, which report on the code a backend actually generates; see Reflection.KernelAbstractions.GPUis deprecated, and backends should subtypeKernelAbstractions.Backenddirectly.GPUis now an alias ofBackendand will be removed in a future release. Code dispatching on::GPUshould dispatch on::Backend, on concrete backend types, or on a capability such asKernelAbstractions.supports_float64.- An exception thrown in a kernel on the
CPUbackend is reported as aKernelAbstractions.POCL.KernelExceptionwhen the kernel has completed, like on GPU backends, and no longer as the original exception (e.g., aBoundsError). Depending on the debug level (julia -g), the kernel prints which exception it threw, on which work-item, and with-g2a backtrace. - The
CPUbackend runs kernels on as many threads as Julia was started with (julia -t), like the thread-basedCPUbackend of 0.9 did, and no longer on one thread per hardware thread. SetJULIA_KA_CPU_THREADSto use a different number of threads; seeCPU. - A
CPUlaunch without a workgroup size is split into about one workgroup per thread, like PoCL does when an OpenCL launch doesn't give a local size, with workgroups of up to 4096 work-items, instead of into workgroups of 1024 work-items. Small launches therefore use more threads, and large ones larger workgroups. The default assumes that the work is balanced across the work-items; see Quickstart. @privatearrays are aKernelAbstractions.PrivateArrayin stack storage on every backend, instead of aStaticArrays.MArrayon most GPU backends, which was heap-allocated when passed to a function that isn't inlined. It is still aStaticArray, but static-array arithmetic, slicing and fast whole-array reductions now need StaticArrays to be loaded, andcopyis not supported: writeSVector(Tuple(p))for an immutable copy. See@private. Backends no longer need to implementKernelAbstractions.Scratchpad: it is an overlay inGPUCompiler.SHARED_METHOD_TABLE, which backends that overrideGPUCompiler.method_table_viewhave to include, e.g., by implementingGPUCompiler.method_tablesinstead.@Constarguments are constified on the host when the kernel is launched, instead of at the start of the kernel. Arguments whose Adapt.jl rules don't compile for GPUs, e.g. aStepRangeLenorLinRange, can now be passed as@Const. Backends have to convert kernel arguments with Adapt.jl, both inKI.argconvertand inKI.launch, since that conversion now constifies them; see the notes for backend implementations.- Launching a kernel on the
CPUbackend costs more than in 0.9: the kernel is handed to PoCL's threads and the launching task waits for them, which takes about 5 µs per launch even for an empty kernel. Code that launches many small kernels suffers the most. For example, a 7-point stencil over a 4×4×4 range took about 3.5 µs per launch, against 0.2 µs with 0.9. Over a 16×16×16 range, which 0.9 also ran on all threads, both took about 7 µs. - On the
CPUbackend, don't pass a fixed workgroup size such as64. Omit it (kernel(CPU())instead ofkernel(CPU(), 64)), so that it is automatically chosen for every launch. A one-dimensional workgroup size pads a multidimensional range: with a workgroup size of 64 and a range of(16, 16, 16), each of the 256 workgroups has 64 work-items, of which 16 are inside the range. A 7-point stencil over that range took about 13 µs per launch with a workgroup size of 64, and 7 µs with the chosen one. Over a 128×128×128 range the chosen size was still 5 to 15% faster. - On the
CPUbackend, every work-item checks whether it is inside the range, even when the workgroups cover the range exactly. This can make kernels with a cheap body slightly slower than with 0.9. foreach_index(f, A)runsfonce per index of the arrayAwithout writing a kernel out, andforeach_index(f, backend, indices)once per index in a range orCartesianIndices.- KernelInterface 0.4 extends its sub-groups beyond the ids,
sub_group_barrierandshfl_downof 0.3:shfl,shfl_upandshfl_xorof anyisbitsvalue, the votessub_group_any,sub_group_allandsub_group_ballot, and reductions and scans, also within segments of a sub-group. They are the low-level API for backends, for kernels written against KernelInterface and for kernel languages built on it. KernelInterface no longer promises how work-items form sub-groups:supports_linear_subgroupssays whether they are consecutive work-items, andsupports_independent_subgroupsandsupports_partitionable_subgroupswhether sub-groups and their segments may diverge. See Sub-groups. @groupreduceand@groupscanreduce and scan a value over the workgroup, on every backend.@groupreduceuses sub-groups internally where the backend can shuffle the values. See Reductions and scans.
Semantic differences
To CUDA.jl/AMDGPU.jl
- The kernels are automatically bounds-checked against either the dynamic or statically provided
ndrange. - Kernels implicitly return
nothing
Contributing
Please file any bug reports through Github issues or fixes through a pull request. Any heterogeneous hardware or code aficionados is welcome to join us on our journey.