Repository navigation
Description
Hi, thank you for developing this library - I would like to write optimised kernels for common GPU algorithms such as reduce, scan, radix sort, etc. similar to CUB but available on all KernelAbstractions platforms. The resulting "KA standard library" (KALib? Caleb?) could be used as a benchmark for future KA development & optimisation - and I can use the lessons along the way to populate the "Writing Kernels" section in the documentation. Big plans, but...
I'm implementing the block-wise reduce following this tutorial with this simple-looking code:
using KernelAbstractions
using CUDA
using CUDAKernels
@kernel function block_reduce(out, in)
# Get block / workgroup size
bs = @uniform @groupsize()[1]
# Block / group index, thread index within block, global thread index
bi = @index(Group, Linear)
ti = @index(Local, Linear)
gi = @index(Global, Linear)
# Copy each thread's corresponding item from global to shared memory
cache = @localmem eltype(out) (bs,)
cache[ti] = in[gi]
@synchronize
# Reduce elements in shared memory using sequential addressing following
# https://developer.download.nvidia.com/assets/cuda/files/reduction.pdf
@private s = bs ÷ 2
while s > 0
if ti < s
cache[ti] += cache[ti + s]
end
@synchronize
s = s >> 1
end
# Copy result back to global memory
if ti == 1
out[bi] = cache[1]
end
end
num_blocks = 10
block_size = 32
num_elements = num_blocks * block_size
in = rand(1:10, num_elements) |> CuArray
out = zeros(num_blocks) |> CuArray
kernel_reduce = block_reduce(CUDADevice(), block_size)
ev = kernel_reduce(out, in, ndrange=num_elements)
wait(ev)
println(out)
It shouldn't be more exotic than the example code in the docs - however, these two lines:
@private s = bs ÷ 2
while s > 0Produce the following errors:
Reason: unsupported use of an undefined name (use of 'bs')
Stacktrace:
[1] macro expansion
@ ~/Prog/Julia/KALib/prototype/reduce.jl:31
[2] gpu_block_reduce
@ ~/.julia/packages/KernelAbstractions/DqITC/src/macros.jl:81
[3] gpu_block_reduce
@ ./none:0
Reason: unsupported dynamic function invocation (call to div)
Stacktrace:
[1] macro expansion
@ ~/Prog/Julia/KALib/prototype/reduce.jl:31
[2] gpu_block_reduce
@ ~/.julia/packages/KernelAbstractions/DqITC/src/macros.jl:81
[3] gpu_block_reduce
@ ./none:0
Reason: unsupported use of an undefined name (use of 's')
Stacktrace:
[1] macro expansion
@ ~/Prog/Julia/KALib/prototype/reduce.jl:32
[2] gpu_block_reduce
@ ~/.julia/packages/KernelAbstractions/DqITC/src/macros.jl:81
[3] gpu_block_reduce
@ ./none:0
Reason: unsupported dynamic function invocation (call to >)
[...Stacktrace...]I tried following the code using Cthulhu.jl, but the errors appear simple: it's calling div(::Any, ::Int64) and >(::Any, ::Int64), so I assume the bs = @uniform @groupsize()[1] and @private s = bs ÷ 2 are not inferred as being integers.
If I switch the arrays and device to CPU() I get the following error:
nested task error: MethodError: no method matching isless(::Int64, ::NTuple{32, Int64})Would you know why these errors appear or how I could investigate (and fix..) them?
Thanks,
Leonard
Activity
vchuravy commentedon Nov 16, 2022
Hm I need to think through the semantics of while loops on the CPU... #262
You can use
@macroexpandto debug the lowering of the kernel. You should see two functions being generated one for the GPU the other for the CPUThis looks like a scope issue.
In general I was thinking that instead of impliementing block reduce in KA we would provide a
reducefunction or macro that the backends need to implement.anicusan commentedon Nov 16, 2022
On the CPU the while loop misses an index into the thread-local
@privatevariable:I notice that the
@private sis normally used ass[(KernelAbstractions.__index_Local_Linear)(__ctx__, var"##I#485")](the NTuple GPU semantics simulation is really clever!) but thewhile s > 0condition doesn't.Does the
length((KernelAbstractions.__workitems_iterspace)(__ctx__))need to be redefined so many times? Could it be defined only once outside the user-code loops?Also - would a
breakthen work correctly in a@kernel?anicusan commentedon Nov 16, 2022
On the GPU I see:
Why is
bsthe only variable to be accessed asKernelAbstractions.bsrather than the locally-definedbs? That explains theundefined nameandunsupported dynamic function (div).Analog for
var"#177#s"ands. I find themacros.jlsource a bit hard to follow - I need to wrap my head around macros better - but would you know where to go next to fix these transformations?Thanks for the quick reply, I'll be using
@macroexpandin the future! This could go into a "Debugging Kernels" section in the docs..vchuravy commentedon Nov 16, 2022
So one thing to think through is what a
whileloop with a@synchronizeinside should look like.vchuravy commentedon Nov 16, 2022
Yeah the
@synchronizemakes while loops hard....But we certainly couldn't support
breakvchuravy commentedon Nov 16, 2022
We could solve break through introducing a mask...
I like this direction, but it is something that the current architecture doesn't easily support.
Moelf commentedon Jun 8, 2025
looks like it also doesn't work for
for-loop?For example, in #597, we calculate
tidmanually and the CPU() backend would throwtid UndefVarErrorvchuravy commentedon Jun 9, 2025
Yes You can't use
synchronizeinside while/for. Especially on the CPU that doesn't work and on the GPU you would need to useusnafe_indicies=true.The move to POCL is motivated by this #600
maleadt commentedon Oct 19, 2026
Fixed by #556 (released in 0.10), which made the
CPUbackend PoCL-based so that@synchronizeworks inwhileloops; thebs/sscoping problem on CUDA is gone as well. The kernel works on bothCPU()andCUDABackend()(with the reduction condition written asti <= s, since the indices are 1-based).