Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Atomix = "1.5"
EnzymeCore = "0.8.25"
GPUCompiler = "2.23"
GPUToolbox = "3.6"
KernelInterface = "0.4"
KernelInterface = "0.4.7"
LLVM = "10"
LinearAlgebra = "1.6"
MacroTools = "0.5"
Expand Down
4 changes: 4 additions & 0 deletions docs/src/kernelinterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ get_sub_group_local_id

```@docs
barrier
barrier(::MemFenceFlags)
MemFenceFlags
LOCAL_MEM_FENCE
GLOBAL_MEM_FENCE
sub_group_barrier
sub_group_barrier(::Integer)
```
Expand Down
4 changes: 2 additions & 2 deletions examples/histogram.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function histogram_kernel!(histogram_output, input, ::Val{gs}) where {gs}

# Setting shared_histogram to 0
@inbounds shared_histogram[lid] = 0
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)

max_element = min_element + gs
if max_element > N
Expand All @@ -47,7 +47,7 @@ function histogram_kernel!(histogram_output, input, ::Val{gs}) where {gs}
@atomic shared_histogram[bin] += 1
end

KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)

if ((lid + min_element - 1) <= N)
@atomic histogram_output[lid + min_element - 1] += shared_histogram[lid]
Expand Down
4 changes: 2 additions & 2 deletions examples/performant_matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function coalesced_matmul_kernel!(
end

# wait for all tiles to be loaded
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)

# calculate value of spot in output, use temporary value to allow for vectorization
out = zero(eltype(output))
Expand All @@ -57,7 +57,7 @@ function coalesced_matmul_kernel!(
end
outval += out

KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
end

# save if inbounds
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.6"
version = "0.4.7"

[compat]
julia = "1.10"
1 change: 1 addition & 0 deletions lib/KernelInterface/src/KernelInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ include("host.jl")
:sub_group_any, :sub_group_all, :sub_group_ballot,
:sub_group_reduce, :sub_group_scan, :sub_group_exclusive_scan,
:barrier, :sub_group_barrier, :_print,
:MemFenceFlags, :LOCAL_MEM_FENCE, :GLOBAL_MEM_FENCE,
# compilation and launch
:Kernel, :kernel_function, :argconvert, :launch, Symbol("@launch"),
:launch_configuration, :max_work_group_size, :max_work_group_dims,
Expand Down
54 changes: 54 additions & 0 deletions lib/KernelInterface/src/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,33 @@ end

## synchronization

"""
MemFenceFlags

The memory that a [`barrier(flags)`](@ref barrier(::MemFenceFlags)) orders:
[`LOCAL_MEM_FENCE`](@ref), [`GLOBAL_MEM_FENCE`](@ref), or both combined with `|`.
"""
struct MemFenceFlags
bits::UInt32
end

"""
LOCAL_MEM_FENCE

Makes a [`barrier`](@ref barrier(::MemFenceFlags)) order accesses to local memory (see
[`localmemory`](@ref)), also called shared or threadgroup memory.
"""
const LOCAL_MEM_FENCE = MemFenceFlags(0x00000001)

"""
GLOBAL_MEM_FENCE

Makes a [`barrier`](@ref barrier(::MemFenceFlags)) order accesses to global memory.
"""
const GLOBAL_MEM_FENCE = MemFenceFlags(0x00000002)

@inline Base.:|(a::MemFenceFlags, b::MemFenceFlags) = MemFenceFlags(a.bits | b.bits)

"""
barrier()

Expand All @@ -955,6 +982,33 @@ function barrier()
error("Group barrier used outside kernel or not captured")
end

"""
barrier(flags::MemFenceFlags)

Like [`barrier()`](@ref barrier), but only the memory in `flags` has to be visible to the
work-group afterwards: the writes to local memory with [`LOCAL_MEM_FENCE`](@ref), and to
global memory with [`GLOBAL_MEM_FENCE`](@ref). `barrier(LOCAL_MEM_FENCE)` suffices when the
work-items only communicate through [`localmemory`](@ref), and can be cheaper than
`barrier()`.

```julia
tile = KI.localmemory(Float32, 256)
tile[i] = x
KI.barrier(KI.LOCAL_MEM_FENCE)
y = tile[257 - i]
```

`flags` are a minimum: a backend may order more memory than requested.

!!! note
Backends **may** implement:
```
@device_override barrier(flags::MemFenceFlags)
```
The fallback is [`barrier()`](@ref barrier), which orders both.
"""
@inline barrier(flags::MemFenceFlags) = barrier()

"""
sub_group_barrier()

Expand Down
50 changes: 30 additions & 20 deletions lib/KernelInterface/test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ function test_interface_kernel(results)
end
return
end
# `barrier()`, or `barrier(flags)` for a `Val(flags)`
sync(::Nothing) = KI.barrier()
sync(::Val{flags}) where {flags} = KI.barrier(flags)

# Local arrays, written by one work-item and read back by another one after a barrier.
# `c` has the same type and size as `a`, but is a different call site, so different memory.
function localmem_kernel(out, ::Val{N}) where {N}
function localmem_kernel(out, ::Val{N}, flags = nothing) where {N}
i = KI.get_local_id().x
a = KI.localmemory(Int32, N)
b = KI.localmemory(Int32, (2, N))
Expand All @@ -52,7 +56,7 @@ function localmem_kernel(out, ::Val{N}) where {N}
b[2, i] = 2i
c[i] = 3i
end
KI.barrier()
sync(flags)
j = N - i + 1
gid = KI.get_global_id().x
@inbounds begin
Expand All @@ -65,12 +69,12 @@ function localmem_kernel(out, ::Val{N}) where {N}
end

# Every work-item writes global memory, and reads another work-item's write after a barrier.
function global_barrier_kernel(scratch, out)
function global_barrier_kernel(scratch, out, flags = nothing)
n = KI.get_local_size().x
base = (KI.get_group_id().x - 1) * n
i = KI.get_local_id().x
@inbounds scratch[base + i] = base + i
KI.barrier()
sync(flags)
@inbounds out[base + i] = scratch[base + n - i + 1]
return
end
Expand Down Expand Up @@ -1297,22 +1301,28 @@ function interface_testsuite(backend::KI.Backend, AT)
@testset "Local memory and barriers" begin
N = 32
groups = 3
out = KI.zeros(backend, Int32, N * groups, 4)
KI.@launch backend numgroups = groups workgroupsize = N localmem_kernel(out, Val(N))
KI.synchronize(backend)
out = Array(out)
# each work-item sees what its mirror image wrote before the barrier, in both arrays
expected = repeat(N:-1:1, groups)
@test out[:, 1] == expected
@test out[:, 2] == -expected
@test out[:, 3] == 2 .* expected
@test out[:, 4] == 3 .* expected

scratch = KI.zeros(backend, Int, N * groups)
out = KI.zeros(backend, Int, N * groups)
KI.@launch backend numgroups = groups workgroupsize = N global_barrier_kernel(scratch, out)
KI.synchronize(backend)
@test Array(out) == vcat([(g * N) .+ (N:-1:1) for g in 0:(groups - 1)]...)
local_flags = (nothing, Val(KI.LOCAL_MEM_FENCE), Val(KI.LOCAL_MEM_FENCE | KI.GLOBAL_MEM_FENCE))
@testset "barrier($(something(flags, Val(:default))))" for flags in local_flags
out = KI.zeros(backend, Int32, N * groups, 4)
KI.@launch backend numgroups = groups workgroupsize = N localmem_kernel(out, Val(N), flags)
KI.synchronize(backend)
out = Array(out)
# each work-item sees what its mirror image wrote before the barrier, in both arrays
expected = repeat(N:-1:1, groups)
@test out[:, 1] == expected
@test out[:, 2] == -expected
@test out[:, 3] == 2 .* expected
@test out[:, 4] == 3 .* expected
end

global_flags = (nothing, Val(KI.GLOBAL_MEM_FENCE), Val(KI.LOCAL_MEM_FENCE | KI.GLOBAL_MEM_FENCE))
@testset "barrier($(something(flags, Val(:default))))" for flags in global_flags
scratch = KI.zeros(backend, Int, N * groups)
out = KI.zeros(backend, Int, N * groups)
KI.@launch backend numgroups = groups workgroupsize = N global_barrier_kernel(scratch, out, flags)
KI.synchronize(backend)
@test Array(out) == vcat([(g * N) .+ (N:-1:1) for g in 0:(groups - 1)]...)
end
end

if KI.supports_subgroups(backend)
Expand Down
2 changes: 2 additions & 0 deletions lib/KernelInterface/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ end
@testset "host fallbacks" begin
# Barriers are meaningless off-device and must say so rather than no-op.
@test_throws "used outside kernel" KI.barrier()
@test_throws "used outside kernel" KI.barrier(KI.LOCAL_MEM_FENCE)
@test_throws "used outside kernel" KI.sub_group_barrier()
@test (KI.LOCAL_MEM_FENCE | KI.GLOBAL_MEM_FENCE).bits == 0x00000003

# Conservative defaults: a backend only implements these if it can do better.
@test KI.multiprocessor_count(StubBackend()) == 0
Expand Down
18 changes: 9 additions & 9 deletions src/groupreduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,20 @@ end
res = __groupreduce_tree(op, convert(T, val), storage, __index_Local_Linear(ctx), n)
end
# all work-items have to read the result before the storage can be reused
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
return res
end

# Tree reduction in local memory, folding the upper half of the values onto the lower half.
@inline function __groupreduce_tree(op, val, storage, lid, n)
@inbounds storage[lid] = val
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
s = __prevpow2(n)
while s > 0
if lid <= s && lid + s <= n
@inbounds storage[lid] = op(storage[lid], storage[lid + s])
end
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
s >>= 1
end
return @inbounds storage[1]
Expand All @@ -242,7 +242,7 @@ end
if lane == 1
@inbounds storage[sg] = val
end
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)

# every sub-group reduces the results of all sub-groups, which avoids running sub-group
# operations in a branch that only some sub-groups take
Expand All @@ -254,12 +254,12 @@ end
i += width
end
acc = KI.sub_group_reduce(op, acc)
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
# the sub-groups can combine the values in different orders, so broadcast one result
if sg == 1 && lane == 1
@inbounds storage[1] = acc
end
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
return @inbounds storage[1]
end

Expand All @@ -274,15 +274,15 @@ end
# (up to) `2d` values ending at its own
src = 0
@inbounds storage[lid] = convert(T, val)
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
d = 1
while d < n
x = @inbounds storage[src + lid]
if lid > d
x = op(@inbounds(storage[src + lid - d]), x)
end
@inbounds storage[(N - src) + lid] = x
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
src = N - src
d <<= 1
end
Expand All @@ -293,7 +293,7 @@ end
res = lid == 1 ? neutral : @inbounds storage[src + lid - 1]
end
# all work-items have to read their result before the storage can be reused
KI.barrier()
KI.barrier(KI.LOCAL_MEM_FENCE)
return res
end

Expand Down
5 changes: 5 additions & 0 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,11 @@ end
work_group_barrier(POCL.LOCAL_MEM_FENCE | POCL.GLOBAL_MEM_FENCE)
end

# `KI.LOCAL_MEM_FENCE` and `KI.GLOBAL_MEM_FENCE` have the bits of OpenCL's flags
@device_override @inline function KI.barrier(flags::KI.MemFenceFlags)
work_group_barrier(flags.bits)
end

@device_override @inline function KI.sub_group_barrier()
sub_group_barrier(POCL.LOCAL_MEM_FENCE | POCL.GLOBAL_MEM_FENCE)
end
Expand Down
Loading