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
12 changes: 7 additions & 5 deletions src/ROCKernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/kernelabstractions_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions test/kernelinterface_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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