You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Significant perf drop when using dynamic ranges in GPU kernel #470
Running the CUDA benchmarks from the HPCBenchmarks.jl tests returns significant performance drop using KA with dynamic range definition. The blow tests are performed on GH200 using local CUDA 12.4 install and Julia 10.2.
@inlinefunctionexpand(ndrange::NDRange{N}, groupidx::CartesianIndex{N}, idx::CartesianIndex{N}) where N
So if we don't know the ndrange the code here won't be optimized away and we do execute quite a few integer operations more. Which is particular costly for small kernels.
One avenue I have been meaning to try, but never got around to is to ensure that most of the index calculation occur using Int32
We have a call to div there which does a check for 0 and otherwise will throw an error. div on it's own is bad enough and I was trying to avoid those in the happy path...
Fixed by #797: with a dynamic ndrange, kernels now launch on an N-d grid and compute indices in 32 bits from the hardware indices, so the udiv and its exception branch are gone. On main the dynamic-ndrange version of this kernel has no division and runs as fast as the static-ndrange and plain CUDA.jl versions (RTX 5080, N = 256: 311 µs for all three).
Activity
vchuravy commentedon Apr 3, 2024
Yeah this is due to KA allowing for arbitrary dimensions instead of just limiting the user to
3.You end up in https://github.com/JuliaGPU/CUDA.jl/blob/7f725c0a117c2ba947015f48833630605501fb3a/src/CUDAKernels.jl#L178
and thereafter in
KernelAbstractions.jl/src/nditeration.jl
Line 73 in c5fe83c
So if we don't know the ndrange the code here won't be optimized away and we do execute quite a few integer operations more. Which is particular costly for small kernels.
One avenue I have been meaning to try, but never got around to is to ensure that most of the index calculation occur using
Int32vchuravy commentedon Apr 3, 2024
Can you use
CUDA.@device_code dir="out"for both cases kernels? In particular the optimized.llwould be of interest.luraess commentedon Apr 4, 2024
Here are the outputs from the
device_codefor dynamic (dyn) and static (stat) expressions.out_dyn.zip
out_stat.zip
vchuravy commentedon Apr 4, 2024
There is a performance pitfall that I didn't expect...
KernelAbstractions.jl/src/nditeration.jl
Line 83 in c5fe83c
We have a call to
divthere which does a check for0and otherwise will throw an error.divon it's own is bad enough and I was trying to avoid those in the happy path...vchuravy commentedon Apr 4, 2024
x-ref: JuliaGPU/GPUArrays.jl#520
vchuravy commentedon Apr 4, 2024
In contrast with constant a ndrange:
The division is turned into a
lshrluraess commentedon Apr 4, 2024
Should one do more globally what was done for Metal in there?
vchuravy commentedon Apr 4, 2024
I am not sure right now.
udivor the exception branch. (The exception branch we could get remove)maleadt commentedon Aug 23, 2024
Just a pointer to the relevant Metal implementation of using hardware indices when available: https://github.com/JuliaGPU/Metal.jl/blob/28576b3f4601ed0b32ccc74485cddf9a6f56249c/src/broadcast.jl#L82-L147
maleadt commentedon Oct 19, 2026
Fixed by #797: with a dynamic
ndrange, kernels now launch on an N-d grid and compute indices in 32 bits from the hardware indices, so theudivand its exception branch are gone. On main the dynamic-ndrangeversion of this kernel has no division and runs as fast as the static-ndrangeand plain CUDA.jl versions (RTX 5080, N = 256: 311 µs for all three).