diff --git a/Project.toml b/Project.toml index 6d28d556e..33712e2a7 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/docs/src/kernelinterface.md b/docs/src/kernelinterface.md index 2a93f00c8..c1838fb9d 100644 --- a/docs/src/kernelinterface.md +++ b/docs/src/kernelinterface.md @@ -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) ``` diff --git a/examples/histogram.jl b/examples/histogram.jl index 3b7572e29..82396ee93 100644 --- a/examples/histogram.jl +++ b/examples/histogram.jl @@ -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 @@ -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] diff --git a/examples/performant_matmul.jl b/examples/performant_matmul.jl index ebfdd43e8..f6d669d26 100644 --- a/examples/performant_matmul.jl +++ b/examples/performant_matmul.jl @@ -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)) @@ -57,7 +57,7 @@ function coalesced_matmul_kernel!( end outval += out - KI.barrier() + KI.barrier(KI.LOCAL_MEM_FENCE) end # save if inbounds diff --git a/lib/KernelInterface/Project.toml b/lib/KernelInterface/Project.toml index 5289c5148..ae059408d 100644 --- a/lib/KernelInterface/Project.toml +++ b/lib/KernelInterface/Project.toml @@ -1,7 +1,7 @@ name = "KernelInterface" uuid = "4ee993da-d684-4d17-a7dd-4e58e78d92bf" authors = ["Valentin Churavy and contributors"] -version = "0.4.6" +version = "0.4.7" [compat] julia = "1.10" diff --git a/lib/KernelInterface/src/KernelInterface.jl b/lib/KernelInterface/src/KernelInterface.jl index 0c83b74fa..fb19077ef 100644 --- a/lib/KernelInterface/src/KernelInterface.jl +++ b/lib/KernelInterface/src/KernelInterface.jl @@ -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, diff --git a/lib/KernelInterface/src/device.jl b/lib/KernelInterface/src/device.jl index 15528ddf9..321361000 100644 --- a/lib/KernelInterface/src/device.jl +++ b/lib/KernelInterface/src/device.jl @@ -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() @@ -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() diff --git a/lib/KernelInterface/test/interface.jl b/lib/KernelInterface/test/interface.jl index 1e891d782..69cda14db 100644 --- a/lib/KernelInterface/test/interface.jl +++ b/lib/KernelInterface/test/interface.jl @@ -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)) @@ -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 @@ -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 @@ -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) diff --git a/lib/KernelInterface/test/runtests.jl b/lib/KernelInterface/test/runtests.jl index 4978c67c1..984f6628d 100644 --- a/lib/KernelInterface/test/runtests.jl +++ b/lib/KernelInterface/test/runtests.jl @@ -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 diff --git a/src/groupreduction.jl b/src/groupreduction.jl index 5fa103eba..e209d2db9 100644 --- a/src/groupreduction.jl +++ b/src/groupreduction.jl @@ -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] @@ -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 @@ -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 @@ -274,7 +274,7 @@ 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] @@ -282,7 +282,7 @@ end 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 @@ -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 diff --git a/src/pocl/backend.jl b/src/pocl/backend.jl index a1a53ca07..b44d768f8 100644 --- a/src/pocl/backend.jl +++ b/src/pocl/backend.jl @@ -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