diff --git a/src/ROCKernels.jl b/src/ROCKernels.jl index 4d6c05e05..6263fa05f 100644 --- a/src/ROCKernels.jl +++ b/src/ROCKernels.jl @@ -172,14 +172,16 @@ end # queried on every automatically-sized launch, so use the limits cached in the device KI.max_work_group_dims(::ROCBackend)::NTuple{3, Int} = HIP.max_workgroup_dims(AMDGPU.device()) # HIP takes the grid size in workgroups, but the dispatch packet holds it in work-items -# (as a UInt32 per dimension, which HIP checks), and the device code assumes workgroup -# indices fit in an Int32 (see `Device._max_groups`). Report the number of workgroups -# that can be launched with any valid workgroup size. HIP's `maxGridSize` isn't usable: -# depending on the ROCm version it holds CUDA's block limits or the work-item limits. +# (as a UInt32 per dimension, which HIP checks), the device code assumes workgroup indices +# fit in an Int32 (see `Device._max_groups`), and since ROCm 7.0 HIP rejects more than 65536 +# workgroups along y and z, like CUDA. Report the number of workgroups that can be launched +# with any valid workgroup size. HIP's `maxGridSize` isn't usable: depending on the ROCm +# version it holds CUDA's block limits or the work-item limits. +const _max_launch_groups = (typemax(Int32), typemax(UInt16), typemax(UInt16)) function KI.max_num_groups(backend::ROCBackend)::NTuple{3, Int} dims = KI.max_work_group_dims(backend) return ntuple(Val(3)) do d - Int(min(Device._max_groups[d], Device._max_grid_size[d] ÷ dims[d])) + Int(min(_max_launch_groups[d], Device._max_groups[d], Device._max_grid_size[d] ÷ dims[d])) end end # `KI.kernel_function` compiles for the device's wavefront size diff --git a/test/kernelabstractions_tests.jl b/test/kernelabstractions_tests.jl index f77c2b6a9..2d7603d12 100644 --- a/test/kernelabstractions_tests.jl +++ b/test/kernelabstractions_tests.jl @@ -8,6 +8,11 @@ include(joinpath(pkgdir(KernelAbstractions), "test", "testsuite.jl")) AMDGPU.allowscalar(false) +KA.@kernel function store_ones!(A) + I = KA.@index(Global, Cartesian) + @inbounds A[I] = 1 +end + KA.@kernel function store_global_linear!(A) I = KA.@index(Global, Linear) @inbounds A[I] = I @@ -90,3 +95,12 @@ end end end + +@testset "N-d ndrange past the grid's y/z limits" begin + # folded onto the grid's z-dimension if `KI.max_num_groups` allows it, which HIP + # limits to 65536 workgroups (AMDGPU.jl#1191: a 4-d `getindex` failed to launch) + A = AMDGPU.zeros(Int32, 4, 4, 4, 70000) + store_ones!(ROCBackend())(A; ndrange = size(A)) + KA.synchronize(ROCBackend()) + @test all(isone, Array(A)) +end diff --git a/test/kernelinterface_tests.jl b/test/kernelinterface_tests.jl index 84352115e..c03eddb8f 100644 --- a/test/kernelinterface_tests.jl +++ b/test/kernelinterface_tests.jl @@ -136,3 +136,11 @@ end end end + +@testset "grid limits" begin + # HIP rejects more than 65536 workgroups along y or z (since ROCm 7.0), and + # KernelAbstractions folds N-d ndranges onto z when these limits allow it (#1191) + groups = KI.max_num_groups(ROCBackend()) + @test groups[2] <= typemax(UInt16) + @test groups[3] <= typemax(UInt16) +end