Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
echo "--- :julia: Developing CUDA"
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/CUDA.jl main \
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/CUDA.jl tb/ki-0.5 \
CUDACore CUDATools lib/cusolver lib/curand lib/cublas lib/cufft lib/cusparse lib/cupti lib/nvml lib/custatevec lib/cudnn lib/cutensor lib/cutensornet || exit 3

julia -e 'println("+++ :julia: Running tests")
Expand Down Expand Up @@ -79,9 +81,11 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
echo "--- :julia: Developing CUDA"
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/CUDA.jl main \
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/CUDA.jl tb/ki-0.5 \
CUDACore CUDATools lib/cusolver lib/curand lib/cublas lib/cufft lib/cusparse lib/cupti lib/nvml lib/custatevec lib/cudnn lib/cutensor lib/cutensornet || exit 3
julia -e 'println("--- :julia: Adding Enzyme")
using Pkg
Expand Down Expand Up @@ -134,11 +138,13 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
julia -e 'println("--- :julia: Developing AMDGPU")
using Pkg
Pkg.add(url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="main")' || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/AMDGPU.jl main || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/AMDGPU.jl tb/ki-0.5 || exit 3
julia -e 'println("--- :julia: Adding Enzyme")
using Pkg
Pkg.add(["Enzyme", "Test"])' || exit 3
Expand Down Expand Up @@ -190,9 +196,11 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
echo "--- :julia: Developing Metal"
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/Metal.jl main || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/Metal.jl tb/ki-0.5 || exit 3
julia -e 'println("+++ :julia: Running tests")
using Pkg
check = joinpath(pwd(), ".buildkite", "check_sources.jl")
Expand Down Expand Up @@ -237,11 +245,13 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
julia -e 'println("--- :julia: Developing oneAPI")
using Pkg
Pkg.add(url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="main")' || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/oneAPI.jl main || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/oneAPI.jl tb/ki-0.5 || exit 3

julia -e 'println("+++ :julia: Running tests")
using Pkg
Expand Down Expand Up @@ -288,11 +298,13 @@ steps:
using Pkg
println("--- :julia: Instantiating project")
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
julia -e 'println("--- :julia: Developing AMDGPU")
using Pkg
Pkg.add(url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="main")' || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/AMDGPU.jl main || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/AMDGPU.jl tb/ki-0.5 || exit 3

julia -e 'println("+++ :julia: Running tests")
using Pkg
Expand Down Expand Up @@ -339,9 +351,11 @@ steps:
julia -e 'println("--- :julia: Instantiating project")
using Pkg
Pkg.develop([PackageSpec(; name="KernelAbstractions", path=pwd()),
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])' || exit 3
PackageSpec(; name="KernelInterface", path=joinpath(pwd(), "lib", "KernelInterface"))])
Pkg.add([PackageSpec(; url="https://github.com/JuliaGPU/AcceleratedKernels.jl", rev="tb/ki-0.5"),
PackageSpec(; url="https://github.com/JuliaGPU/GPUArrays.jl", rev="tb/ki-0.5")])' || exit 3
echo "--- :julia: Developing OpenCL"
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/OpenCL.jl main lib/intrinsics || exit 3
julia .buildkite/develop_backend.jl https://github.com/JuliaGPU/OpenCL.jl tb/ki-0.5 lib/intrinsics || exit 3

julia -e 'println("+++ :julia: Running tests")
using Pkg
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "KernelAbstractions"
uuid = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
version = "0.10.9"
version = "0.10.10"
authors = ["Valentin Churavy <v.churavy@gmail.com> and contributors"]

[workspace]
Expand Down Expand Up @@ -55,7 +55,7 @@ Atomix = "1.5"
EnzymeCore = "0.8.25"
GPUCompiler = "2.24"
GPUToolbox = "3.6"
KernelInterface = "0.4.7"
KernelInterface = "0.5"
LLVM = "10"
LinearAlgebra = "1.6"
MacroTools = "0.5"
Expand Down
9 changes: 9 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ end
compiled once). This lets PoCL vectorize kernels better: an axpy over 10⁵ elements on one
thread took about 15% longer than a serial loop, rather than 3× (x86) to 7× (Apple M1) as
long. `muladd` is computed with fused multiply-adds on CPUs that have them.
- KernelAbstractions now requires KernelInterface 0.5, which only guarantees a sub-group
width to kernels that request one with `sub_group_size = W`, for a `W` in
`KI.sub_group_sizes(backend)`, and removes `KI.sub_group_size(backend)`. KernelAbstractions'
own collectives, such as `@groupreduce`, read the width on the device and need no change, but
code that sizes anything in a `@kernel` from the host width has to request one. See
[Upgrading from KernelInterface 0.4](@ref).
- A kernel can be constructed with a `sub_group_size` keyword, e.g.
`my_kernel(backend, 256; sub_group_size = 16)`, to compile it for a sub-group width from
`KernelInterface.sub_group_sizes(backend)`. See [Launching kernels](@ref).

## Semantic differences

Expand Down
67 changes: 58 additions & 9 deletions docs/src/kernelinterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ What a backend implements, at a glance. The docstrings below have the details.
| **Capabilities** | | [`supports_float64`](@ref), [`supports_atomics`](@ref), [`supports_unified`](@ref), [`supports_subgroups`](@ref), [`supports_linear_subgroups`](@ref), [`supports_independent_subgroups`](@ref), [`supports_partitionable_subgroups`](@ref) (all `false`), [`supports_shuffle`](@ref) (`false` unless derived from the natively supported types, see its docstring) |
| **Compilation** | [`argconvert`](@ref), [`kernel_function`](@ref), [`launch`](@ref) | |
| **Device** | [`get_local_id`](@ref), [`get_group_id`](@ref), [`get_local_size`](@ref), [`get_num_groups`](@ref), [`localmemory`](@ref), [`barrier`](@ref) | [`get_global_id`](@ref), [`get_global_size`](@ref) (derived from the primitive queries), [`_print`](@ref KernelInterface._print) (host `print`) |
| **Sub-groups** | if `supports_subgroups`: [`sub_group_size`](@ref), the sub-group queries, [`sub_group_barrier`](@ref), [`sub_group_any`](@ref), [`sub_group_all`](@ref), and [`sub_group_ballot`](@ref) for widths of at most 64; if `supports_shuffle(backend, T)`: [`shfl`](@ref), [`shfl_down`](@ref), [`shfl_up`](@ref), [`shfl_xor`](@ref) for the primitive `T` supported natively, including `UInt32` | [`sub_group_reduce`](@ref), [`sub_group_scan`](@ref), [`sub_group_exclusive_scan`](@ref) (built on the shuffles); the functions with a `width` (built on those without, or on the ballot); shuffles of other primitive types (as `UInt32` or `UInt64` words) and of structs (packed into `UInt32` words) |
| **Sub-groups** | if `supports_subgroups`: the sub-group queries, [`sub_group_barrier`](@ref), [`sub_group_any`](@ref), [`sub_group_all`](@ref), and [`sub_group_ballot`](@ref) for widths of at most 64; if `supports_shuffle(backend, T)`: [`shfl`](@ref), [`shfl_down`](@ref), [`shfl_up`](@ref), [`shfl_xor`](@ref) for the primitive `T` supported natively, including `UInt32`; if kernels can request a width: [`sub_group_sizes`](@ref), and `kernel_function(...; sub_group_size = W)` for every `W` in it | [`sub_group_reduce`](@ref), [`sub_group_scan`](@ref), [`sub_group_exclusive_scan`](@ref) (built on the shuffles); the functions with a `width` (built on those without, or on the ballot); shuffles of other primitive types (as `UInt32` or `UInt64` words) and of structs (packed into `UInt32` words) |

Everything else, such as [`zeros`](@ref KernelInterface.zeros), [`ones`](@ref KernelInterface.ones),
the launch-keyword handling of [`Kernel`](@ref) and [`@launch`](@ref KernelInterface.@launch),
Expand All @@ -102,6 +102,26 @@ include(joinpath(pkgdir(KernelInterface), "test", "testsuite.jl"))
Testsuite.testsuite(MyBackend(), MyArray)
```

### Upgrading from KernelInterface 0.4

KernelInterface 0.5 no longer promises a sub-group width to kernels that don't request one,
and removes `KI.sub_group_size(backend)`: it throws an error that explains how to migrate,
until KernelInterface 1.0 removes the method too.

- **Kernel authors**: code that read `W = KI.sub_group_size(backend)` on the host, e.g. for a
`Val(W)` or to size local memory, picks a width from
[`sub_group_sizes(backend)`](@ref sub_group_sizes) instead (`first` of it, if any width
will do), and compiles the kernel with it: `KI.@launch backend sub_group_size=W ...`. If
the list is empty, it needs a variant that reads the width on the device with
[`get_max_sub_group_size`](@ref), as code that only needs it there does anyway.
- **Backend authors**: replace `sub_group_size(backend)` with
[`sub_group_sizes(backend)`](@ref sub_group_sizes), the widths of at most 64 that the
backend can guarantee, the one to suggest first, and implement `kernel_function`'s `sub_group_size`
keyword for them, throwing an `ArgumentError` for other widths. `sub_group_size = nothing`
has to be accepted as no request. Without a request, kernels may now run at a width the
backend chooses per launch. A device that has sub-groups but can't guarantee a width can
now report them, with an empty `sub_group_sizes`.

## Backend hierarchy

Backends subtype [`Backend`](@ref), and everything else in the interface dispatches on
Expand Down Expand Up @@ -149,9 +169,12 @@ get_global_size
### [Sub-groups](@id ki_subgroups)

Sub-groups are optional ([`supports_subgroups`](@ref)). A work-group is divided into
sub-groups of at most `W = `[`sub_group_size(backend)`](@ref sub_group_size) work-items, the
sub-group width. What KernelInterface guarantees, and backends that report sub-group support
have to ensure:
sub-groups of at most `W = `[`get_max_sub_group_size()`](@ref get_max_sub_group_size)
work-items, the sub-group width of the kernel. It is the width the kernel requested when it
was compiled (see [below](@ref ki_subgroup_width)), or else one that the backend chooses for
each launch: the same for all work-items of the launch, but not known on the host, and not
necessarily the same for different kernels or launches. What KernelInterface guarantees, and backends
that report sub-group support have to ensure:

- every work-item has a unique `(get_sub_group_id(), get_sub_group_local_id())` pair in its
work-group, which doesn't change during the kernel;
Expand Down Expand Up @@ -208,6 +231,28 @@ of its lanes take. Without independent sub-groups, all sub-groups of the work-gr
execute the same communication functions in the same order. The id and size queries can be
used anywhere.

#### [Choosing the width](@id ki_subgroup_width)

A kernel compiled without a request runs at the width the backend picks for the launch, and
reads it on the device with [`get_max_sub_group_size`](@ref). Code that needs the width on the
host, e.g. to size local memory or to pick a `Val` parameter, has to request one from
[`sub_group_sizes(backend)`](@ref sub_group_sizes) with `kernel_function`'s `sub_group_size`
keyword (also accepted by [`KI.@launch`](@ref KernelInterface.@launch)). The docstring of
`sub_group_sizes` has the guarantee and an example.

Some hardware supports several widths: Intel GPUs run sub-groups of 8, 16 or 32 work-items,
AMD's RDNA GPUs wavefronts of 32 or 64. Which one is fastest depends on the kernel and the
device. A narrower sub-group gives each work-item more registers, so a register-heavy kernel
that spills at 32 can be several times faster at 16, while a kernel that does little work per
work-item can be faster at 32. Without a request, the backend is free to make that choice per
kernel, or even per launch. `sub_group_sizes` can also be empty while sub-groups are
supported, on devices that can't guarantee any width; kernels then have to work with the
width they read on the device.

A `@kernel` requests a width when it is constructed, `my_kernel(backend, 256; sub_group_size = W)`.
KernelAbstractions' collectives, such as [`@groupreduce`](@ref KernelAbstractions.@groupreduce),
read the width on the device, and work without a request.

!!! note "Kernel languages"
The sub-group functions are meant for kernels written against KernelInterface, and for
kernel languages built on it. A kernel is either a KernelInterface kernel or a
Expand Down Expand Up @@ -331,7 +376,7 @@ max_work_group_size
launch_configuration
max_work_group_dims
max_num_groups
sub_group_size
sub_group_sizes
multiprocessor_count
```

Expand Down Expand Up @@ -375,10 +420,14 @@ optional methods where it can do better than the fallback. In particular:
```
4. Compute the typed index queries with `% T`, not `T(x)`: a checked conversion leaves
an error branch in every kernel.
5. Sub-groups are optional. To support them, report [`supports_subgroups`](@ref) and
[`sub_group_size`](@ref), and implement the device functions the
[contract](@ref Contract) requires. If the backend has shuffles, implement them only for the
primitive types the hardware shuffles natively, including `UInt32`, and report those with
5. Sub-groups are optional. To support them, report [`supports_subgroups`](@ref) and the
widths a kernel can request with [`sub_group_sizes`](@ref), and implement the device
functions the [contract](@ref Contract) requires. `kernel_function` has to compile a
kernel with exactly the width given by its `sub_group_size` keyword, and throw an
`ArgumentError` for widths that aren't offered. Kernels compiled without the keyword may
run at any width, chosen per launch; `sub_group_size = nothing` means no request. A
backend that can't guarantee a width keeps the fallback `sub_group_sizes` of `()`. If the
backend has shuffles, implement them only for the primitive types the hardware shuffles natively, including `UInt32`, and report those with
[`supports_shuffle`](@ref); KernelInterface shuffles other types as words. It also
provides the reductions, scans and functions with a `width`; override those where a
native implementation helps, keeping their documented results.
Expand Down
14 changes: 14 additions & 0 deletions docs/src/kernels.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ kernel = my_kernel(backend, 32, size(A))
kernel(A)
```

Any of these also takes a `sub_group_size` keyword, which compiles the kernel for that
sub-group width. It has to be one of the widths
[`KernelInterface.sub_group_sizes(backend)`](@ref KernelInterface.sub_group_sizes) offers,
and `nothing` (the default) requests none; other widths throw an `ArgumentError` when the
kernel is compiled. Without a request, the backend chooses the width, possibly per
launch. KernelAbstractions' collectives work at any width, so a width only needs
to be requested for performance, e.g. a narrower one on Intel GPUs for a kernel that uses
many registers, or by code that needs to know it on the host:

```julia
sizes = KernelInterface.sub_group_sizes(backend)
kernel = my_kernel(backend, 256; sub_group_size = 16 in sizes ? 16 : nothing)
```

### Index ranges

Each entry of `ndrange` is either an extent (indices `1:n`) or a range of indices, so a kernel
Expand Down
2 changes: 1 addition & 1 deletion lib/KernelInterface/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KernelInterface"
uuid = "4ee993da-d684-4d17-a7dd-4e58e78d92bf"
authors = ["Valentin Churavy <v.churavy@gmail.com> and contributors"]
version = "0.4.7"
version = "0.5.0"

[compat]
julia = "1.10"
2 changes: 1 addition & 1 deletion lib/KernelInterface/src/KernelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ include("host.jl")
# compilation and launch
:Kernel, :kernel_function, :argconvert, :launch, Symbol("@launch"),
:launch_configuration, :max_work_group_size, :max_work_group_dims,
:max_num_groups, :sub_group_size, :multiprocessor_count,
:max_num_groups, :sub_group_sizes, :multiprocessor_count,
# host side
:allocate, :zeros, :ones, :copyto!, :pagelock!, :unsafe_free!,
:synchronize, :record_event, :wait_event, :priority!,
Expand Down
Loading
Loading