Skip to content
Draft
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
10 changes: 7 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "AcceleratedKernels"
uuid = "6a4ca0a5-0e36-4168-a932-d9be78d558f1"
version = "0.5.14"
version = "0.6.0"
authors = ["Andrei-Leonard Nicusan <leonard@evophase.co.uk> and contributors"]

[deps]
Expand All @@ -27,6 +27,10 @@ MetalExt = "Metal"
OpenCLExt = "OpenCL"
oneAPIExt = "oneAPI"

[sources]
KernelAbstractions = {url = "https://github.com/JuliaGPU/KernelAbstractions.jl", rev = "tb/sub-group-request"}
KernelInterface = {url = "https://github.com/JuliaGPU/KernelAbstractions.jl", rev = "tb/sub-group-request", subdir = "lib/KernelInterface"}

[compat]
AMDGPU = "2.9"
Adapt = "4"
Expand All @@ -35,8 +39,8 @@ Atomix = "1.7"
CUDA = "6.5"
CUDACore = "6.5"
GPUArraysCore = "0.2.1"
KernelAbstractions = "0.9.43, 0.10"
KernelInterface = "0.4"
KernelAbstractions = "0.10.10"
KernelInterface = "0.5"
Metal = "1.12"
OpenCL = "1"
PrecompileTools = "1"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ Start Julia with multiple threads to run the tests on a multithreaded CPU backen
$> julia --threads=4 -e 'import Pkg; Pkg.test("AcceleratedKernels")'
```

With KernelAbstractions 0.10, whose host backend runs kernels on PoCL, `--cpu-ka` tests
As the host backend runs kernels on PoCL, `--cpu-ka` tests
AcceleratedKernels' GPU kernels on host arrays instead of the threaded CPU algorithms:
```bash
$> julia -e 'import Pkg; Pkg.test("AcceleratedKernels"; test_args=["--cpu-ka"])'
Expand Down
6 changes: 3 additions & 3 deletions docs/src/api/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ Base's views and reshapes have. MappedArrays.jl does not define such rules yet,
#### The host backend

Arrays in host memory (`Array` and views of it) are on the host backend. `Auto` processes them on
Julia threads with the `CPUThreads` algorithms. On KernelAbstractions 0.10 the host backend also
runs AcceleratedKernels' GPU kernels (on PoCL), so an explicit kernel algorithm such as
`MergeSort()` works on host arrays there; on KernelAbstractions 0.9 it is an `ArgumentError`.
Julia threads with the `CPUThreads` algorithms. The host backend also runs AcceleratedKernels'
GPU kernels (on PoCL), so an explicit kernel algorithm such as `MergeSort()` works on host arrays
too.
2 changes: 1 addition & 1 deletion docs/src/api/sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ never replaced:
| [`BitonicSort`](@ref AcceleratedKernels.BitonicSort) | no | yes | no | no | all | kernels |
| [`CPUThreads.SampleSort`](@ref AcceleratedKernels.CPUThreads.SampleSort) | yes | yes | yes | yes | all | host |

"Kernels" means GPU backends, and the host backend on KernelAbstractions 0.10, which runs
"Kernels" means GPU backends, and the host backend, which runs
AcceleratedKernels' kernels on PoCL. See [Algorithms and backends](@ref) for how `Auto` chooses.

```@docs
Expand Down
5 changes: 1 addition & 4 deletions src/AcceleratedKernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ import Adapt
using GPUArraysCore: AnyGPUArray, @allowscalar, neutral_element
using KernelAbstractions
using KernelAbstractions: @context
# Sub-group operations, with KernelAbstractions 0.10
@static if isdefined(KernelAbstractions, :KernelInterface)
const KI = KernelAbstractions.KernelInterface
end
import KernelInterface as KI


# Exposed functions from upstream packages
Expand Down
9 changes: 5 additions & 4 deletions src/accumulate/accumulate_1d_gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function accumulate_1d_gpu!(
start = scan_start(v, init, seed)

subgroups = scan_subgroup_width(backend, typeof(seed), block_size)
kernel! = scan_tile_kernel!(backend, block_size)
sub_group_size = sub_group_request(subgroups)
kernel! = scan_tile_kernel!(backend, block_size; sub_group_size)
if num_blocks == 1
kernel!(op, v, src, load, start, seed, inclusive, nothing, nothing, items,
subgroups, ndrange=block_size)
Expand All @@ -160,9 +161,9 @@ function accumulate_1d_gpu!(
if tuning(scan_tuning, backend, eltype(v)).reduce_then_scan
# Reduce the tiles, then scan them from their prefix: this reads the source twice and
# writes the result once
tile_aggregates_kernel!(backend, block_size)(op, totals, src, load, start, seed,
items, subgroups,
ndrange=num_blocks * block_size)
tile_aggregates_kernel!(backend, block_size; sub_group_size)(
op, totals, src, load, start, seed, items, subgroups,
ndrange=num_blocks * block_size)
scan_totals!()
kernel!(op, v, src, load, start, seed, inclusive, nothing, totals, items,
subgroups, ndrange=num_blocks * block_size)
Expand Down
7 changes: 4 additions & 3 deletions src/accumulate/accumulate_nd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,11 @@ function slice_pass!(kind, backend, block_size, nslices, v, src, load, op::OP, s
kernel!(v, src, load, op, start, seed, carries, totals, inclusive, n_inner, len,
nchunks, per_chunk, nthreads; ndrange=(block_size * cld(nthreads, block_size),))
else
kernel! = slice_tiles_kernel!(backend, block_size)
subgroups = scan_subgroup_width(backend, typeof(seed), block_size)
kernel! = slice_tiles_kernel!(backend, block_size;
sub_group_size=sub_group_request(subgroups))
kernel!(v, src, load, op, start, seed, carries, totals, inclusive, n_inner, len,
nchunks, per_chunk, Val(items),
scan_subgroup_width(backend, typeof(seed), block_size);
nchunks, per_chunk, Val(items), subgroups;
ndrange=(block_size * nslices * nchunks,))
end
return nothing
Expand Down
2 changes: 1 addition & 1 deletion src/accumulate/tuning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ returned by [`scan_tuning`](@ref).
adds their prefix, which reads and writes the result twice. GPUs are faster with the former,
CPUs, whose caches hold the result, with the latter.
- `subgroups`: whether the kernel algorithms scan the values of a block with sub-group
operations, where the backend has them (from KernelAbstractions 0.10), rather than in local
operations, where the backend has them, rather than in local
memory. CPUs, whose sub-group operations synchronize the whole block, scan faster without.
- `wide_optional_flag`: as for [`ReduceTuning`](@ref).

Expand Down
8 changes: 3 additions & 5 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ end # module CPUThreads
# Capabilities: correctness facts about a backend, checked for `Auto` and explicit algorithms
# alike. Unlike tunings, they cannot be changed per device.

# The host backend: `KernelAbstractions.CPU` on KernelAbstractions 0.9, its PoCL backend on 0.10.
# The host backend: KernelAbstractions' PoCL backend
const HOST_BACKEND = get_backend(Int[])
const HostBackend = typeof(HOST_BACKEND)

Expand All @@ -143,11 +143,9 @@ runs_threads(::HostBackend) = true
runs_kernels(backend)

Whether AK's kernels (`@kernel cpu=false`) run on `backend`: every GPU backend, and the host
backend of KernelAbstractions 0.10, which compiles kernels for PoCL. KernelAbstractions 0.9's
`CPU` backend cannot run them.
backend, which compiles kernels for PoCL.
"""
runs_kernels(::Backend) = true
runs_kernels(::HostBackend) = nameof(HostBackend) !== :CPU


# Tunings: values chosen per device by the `*_tuning(backend, T)` hooks, to which AK's package
Expand Down Expand Up @@ -214,7 +212,7 @@ fill_threads(a::A, t) where {A} =
function require_kernels(a, backend)
runs_kernels(backend) || throw(ArgumentError(
"$(algname(a)) runs AcceleratedKernels' GPU kernels, which " *
"$(backend_name(backend)) cannot run (on the host, this needs KernelAbstractions 0.10); " *
"$(backend_name(backend)) cannot run; " *
"use `Auto()` or a `CPUThreads` algorithm"))
nothing
end
Expand Down
31 changes: 20 additions & 11 deletions src/block.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,33 @@ end

# The sub-group width `W` for a block scan or reduction of `T` in blocks of `block_size` threads,
# as a `Val`, or `Val(0)` for the local-memory one. Sub-groups must be enabled by the tuning,
# linear (from KernelAbstractions 0.10), able to shuffle `T`, and `W ≤ block_size ≤ W²`, as one
# sub-group combines the sub-groups' values.
@static if isdefined(KernelAbstractions, :KernelInterface)
function subgroup_width(backend, ::Type{T}, block_size) where {T}
KI.supports_linear_subgroups(backend) && KI.supports_shuffle(backend, T) || return Val(0)
W = KI.sub_group_size(backend)
return ispow2(W) && W <= block_size <= W * W ? Val(W) : Val(0)
# linear, able to shuffle `T`, and `W ≤ block_size ≤ W²`, as one sub-group combines the
# sub-groups' values. `W` is the first such width the backend offers (its suggestion first), and
# the kernels that take it must be constructed with `sub_group_size = sub_group_request(Val(W))`,
# as a kernel is only guaranteed the width it requests.
function subgroup_width(backend, ::Type{T}, block_size) where {T}
KI.supports_linear_subgroups(backend) && KI.supports_shuffle(backend, T) || return Val(0)
for W in KI.sub_group_sizes(backend)
ispow2(W) && W <= block_size <= W * W && return Val(W)
end
else
subgroup_width(backend, T, block_size) = Val(0)
return Val(0)
end
scan_subgroup_width(backend, ::Type{T}, block_size) where {T} =
tuning(scan_tuning, backend, T).subgroups ? subgroup_width(backend, T, block_size) : Val(0)
reduce_subgroup_width(backend, ::Type{T}, block_size) where {T} =
tuning(reduce_tuning, backend, T).subgroups ? subgroup_width(backend, T, block_size) : Val(0)

# The sub-group width a kernel taking the sub-group widths `subgroups` requests: none for the
# local-memory path. A kernel can only request one width, so those of its block operations that
# use sub-groups must agree; widths chosen for the same block size do, as only the operations'
# types, not the widths, decide whether a backend's width suits them.
@inline sub_group_request(::Val{W}) where {W} = W == 0 ? nothing : W
function sub_group_request(::Val{A}, ::Val{B}) where {A, B}
A == 0 || B == 0 || A == B ||
throw(ArgumentError("a kernel cannot use sub-groups of widths $A and $B"))
return sub_group_request(Val(max(A, B)))
end

# The local memory of a block reduction or scan of `N` values (see `block_reduce` and
# `block_exclusive_scan`): a value per sub-group, or per thread without sub-groups
@inline block_slots(N, ::Val{W}) where {W} = W == 0 ? N : N ÷ W
Expand All @@ -51,7 +62,6 @@ reduce_subgroup_width(backend, ::Type{T}, block_size) where {T} =
@inline reduce_group!(@context, op, sdata, ithread)
return sdata[0x1]
end
@static if isdefined(KernelAbstractions, :KernelInterface)
@inline function block_reduce(@context, op, sdata, x, ithread, ::Val{W}) where {W}
N = @groupsize()[1]
x = KI.sub_group_reduce(op, x)
Expand All @@ -65,7 +75,6 @@ end
# sub-groups of a block to run the same sub-group operations; thread 0's is the result)
return KI.sub_group_reduce(op, sdata[ithread % nsg + 0x1], nsg)
end
end


# Exclusive scan of the values `x` of the block's threads, seeded with `seed`, with `sdata` of
Expand Down
8 changes: 4 additions & 4 deletions src/findall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,23 +137,23 @@ function compact(
scan_subgroup_width(backend, Int32, block_size), bufs, like)
end

# (a function barrier on the block size: from KernelAbstractions 0.10, constructing the kernels
# then infers, which saved 0.5 µs per kernel and call on CUDA)
# (a function barrier on the block size: constructing the kernels then infers, which saved 0.5 µs per kernel and call on CUDA)
function compact_tiles(bools, ::Type{I}, output_indices, backend, ::Val{B}, items::Val{N},
subgroups, bufs, like) where {I, B, N}
num_blocks = cld(length(bools), B * N)
block_counts = bufs.counts

# Count each tile's selected elements, as the scans reduce their tiles, then scan the counts
tile_aggregates_kernel!(backend, B)(
sub_group_size = sub_group_request(subgroups)
tile_aggregates_kernel!(backend, B; sub_group_size)(
+, block_counts, one_based(bools), b -> Int32(b), Int32(0), Int32(0), items, subgroups;
ndrange=num_blocks * B)
accumulate_nested!(+, block_counts, bufs.scan; backend, init=0)
n = @allowscalar block_counts[end]

out = similar_on(backend, like, I, (n,))
if n > 0
compact_kernel!(backend, B)(
compact_kernel!(backend, B; sub_group_size)(
out, bools, block_counts, eachindex(bools), output_indices, items, subgroups;
ndrange=num_blocks * B)
end
Expand Down
7 changes: 4 additions & 3 deletions src/reduce/mapreduce_1d_gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ end
function mapreduce_1d_pass!(commutative::Bool, backend, block_size, items_per_thread, src, dst,
f::F, op::OP, seed, init, blocks; buffer=nothing) where {F, OP}
if commutative
kernel! = mapreduce_1d_kernel!(backend, block_size)
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
kernel! = mapreduce_1d_kernel!(backend, block_size;
sub_group_size=sub_group_request(subgroups))
Base.inferencebarrier(kernel!)(src, dst, f, op, seed, init, blocks, Val(block_size),
Val(items_per_thread),
reduce_subgroup_width(backend, typeof(seed), block_size);
Val(items_per_thread), subgroups;
ndrange=(block_size * blocks,))
else
len = length(src)
Expand Down
29 changes: 15 additions & 14 deletions src/reduce/mapreduce_nd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ function launch!(shape::ByLanes, f::F, op::OP, dst, src, kind, backend, alg, buf
(; base_offset, outer_strides, outer_sizes, dst_size, reduce_size) = ix
block_size = alg.block_size
blocks = cld(dst_size, block_size ÷ shape.lanes)
by_lanes_kernel!(backend, block_size)(
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
by_lanes_kernel!(backend, block_size; sub_group_size=sub_group_request(subgroups))(
kernel_buffer(src, kind), dst, f, op, seed, init,
base_offset, outer_strides, outer_sizes, dst_size, reduce_size, Val(shape.lanes),
reduce_subgroup_width(backend, typeof(seed), block_size),
subgroups,
ndrange=(block_size * blocks,),
)
end
Expand All @@ -149,20 +150,20 @@ function launch!(shape::ByBlock, f::F, op::OP, dst, src, kind, backend, alg, buf
block_size, blocks = alg.block_size, shape.blocks
W = isdense_buffer(kind) ? vector_width(buffer, base_offset, outer_strides, reduce_strides,
reduce_size, block_size) : 0
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
sub_group_size = sub_group_request(subgroups)
if W > 0
kernel! = by_block_vector_kernel!(backend, block_size)
kernel! = by_block_vector_kernel!(backend, block_size; sub_group_size)
Base.inferencebarrier(kernel!)(
buffer, dst, f, op, seed, init, base_offset, outer_strides, outer_sizes,
dst_size, reduce_size, blocks, Val(W),
reduce_subgroup_width(backend, typeof(seed), block_size),
dst_size, reduce_size, blocks, Val(W), subgroups,
ndrange=(block_size * blocks,),
)
else
kernel! = by_block_kernel!(backend, block_size)
kernel! = by_block_kernel!(backend, block_size; sub_group_size)
Base.inferencebarrier(kernel!)(
buffer, dst, f, op, seed, init, base_offset, outer_strides, outer_sizes,
reduce_strides, reduce_sizes, dst_size, reduce_size, blocks,
reduce_subgroup_width(backend, typeof(seed), block_size),
reduce_strides, reduce_sizes, dst_size, reduce_size, blocks, subgroups,
ndrange=(block_size * blocks,),
)
end
Expand All @@ -174,11 +175,11 @@ function launch!(shape::ByBlocks, f::F, op::OP, dst, src, kind, backend, alg, bu
reduce_size) = ix
block_size, groups = alg.block_size, shape.groups
partials = bufs.partials
multigroup_kernel!(backend, block_size)(
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
multigroup_kernel!(backend, block_size; sub_group_size=sub_group_request(subgroups))(
kernel_buffer(src, kind), partials, f, op, seed,
base_offset, outer_strides, outer_sizes, reduce_strides, reduce_sizes,
Val(dst_size), reduce_size, groups,
reduce_subgroup_width(backend, typeof(seed), block_size),
Val(dst_size), reduce_size, groups, subgroups,
ndrange=(block_size * dst_size * groups,),
)
combine_groups!(backend, partials, dst, op, seed, init, dst_size, groups, false,
Expand Down Expand Up @@ -242,10 +243,10 @@ function combine_groups!(backend, partials, dst, op::OP, seed, init, dst_size, g
ordered_pass!(backend, block_size, 1, partials, dst, PartialInputs(), op, seed, init,
0, (1,), (dst_size,), (dst_size,), (groups,), dst_size, groups, 1)
else
by_block_kernel!(backend, block_size)(
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
by_block_kernel!(backend, block_size; sub_group_size=sub_group_request(subgroups))(
partials, dst, PartialInputs(), op, seed, init, 0, (1,), (dst_size,), (dst_size,),
(groups,), dst_size, groups, dst_size,
reduce_subgroup_width(backend, typeof(seed), block_size),
(groups,), dst_size, groups, dst_size, subgroups,
ndrange=(block_size * dst_size,))
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/reduce/mapreduce_ordered.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ function ordered_pass!(
) where {F, OP}
W = dense_buffer ? ordered_vector_width(src, base_offset, outer_strides, reduce_strides,
reduce_size, block_size, items_per_thread) : 0
kernel! = ordered_kernel!(backend, block_size)
subgroups = reduce_subgroup_width(backend, typeof(seed), block_size)
kernel! = ordered_kernel!(backend, block_size; sub_group_size=sub_group_request(subgroups))
Base.inferencebarrier(kernel!)(
src, dst, f, op, seed, init,
base_offset, outer_strides, outer_sizes, reduce_strides, reduce_sizes,
output_size, reduce_size, groups, Val(items_per_thread), Val(W),
reduce_subgroup_width(backend, typeof(seed), block_size),
output_size, reduce_size, groups, Val(items_per_thread), Val(W), subgroups,
ndrange=(block_size * output_size * groups,),
)
return nothing
Expand Down
2 changes: 1 addition & 1 deletion src/reduce/tuning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Values that fill unset algorithm fields for reductions on one device, as returne
(so that Compute Sanitizer's initcheck doesn't see reads of unwritten bytes there), or with a
`Bool`, which some compilers handle better in local memory. The results are the same.
- `subgroups`: whether the kernels combine the values of a block with sub-group operations,
where the backend has them (from KernelAbstractions 0.10), keeping a value per sub-group
where the backend has them, keeping a value per sub-group
rather than per thread in local memory. CPUs, whose sub-group operations synchronize the
whole block, reduce faster without.

Expand Down
8 changes: 4 additions & 4 deletions src/select.jl
Original file line number Diff line number Diff line change
Expand Up @@ -780,8 +780,8 @@ function radix_select!(a::RadixSelect, dst, src, rk::NTuple{NS, Int}, backend, d
B = len <= 2048 ? 256 : 512
items = nextpow(2, cld(len, B))
W = scan_subgroup_width(backend, UInt32, B)
select_block_kernel!(backend, B)(dst, src, rev, rk, src_layout, dst_layout, Val(items),
W; ndrange=B * nslices)
select_block_kernel!(backend, B; sub_group_size=sub_group_request(W))(
dst, src, rev, rk, src_layout, dst_layout, Val(items), W; ndrange=B * nslices)
else
select_global!(a, dst, src, rk, backend, src_layout, dst_layout, rev, bufs)
end
Expand All @@ -806,8 +806,8 @@ function select_global!(a, dst, src, ranks::NTuple{NR}, backend, src_layout, dst
WS = scan_subgroup_width(backend, UInt32, 256)
(; hist, kst, ust, pmin, pmax, comp) = bufs
select_init_kernel!(backend, 256)(hist, kst, ust, rk, len; ndrange=256 * nslices)
hist! = select_hist_kernel!(backend, B)
resolve! = select_resolve_kernel!(backend, 256)
hist! = select_hist_kernel!(backend, B; sub_group_size=sub_group_request(W))
resolve! = select_resolve_kernel!(backend, 256; sub_group_size=sub_group_request(WS, WR))
for pass in 0:sizeof(K) - 1
hist!(hist, comp, pmin, pmax, kst, ust, src, rev, src_layout, pass, bps, cap, Val(NS),
Val(a.items_per_thread), W; ndrange=B * bps * nslices)
Expand Down
Loading
Loading