diff --git a/docs/src/api/devices.md b/docs/src/api/devices.md index ab2b33f86..922ba2d2d 100644 --- a/docs/src/api/devices.md +++ b/docs/src/api/devices.md @@ -57,6 +57,7 @@ AMDGPU.device_id! AMDGPU.HIP.name AMDGPU.HIP.wavefrontsize AMDGPU.HIP.max_workgroup_dims +AMDGPU.HIP.max_workgroup_size AMDGPU.HIP.gcn_arch AMDGPU.HIP.device_id AMDGPU.HIP.properties diff --git a/src/ROCKernels.jl b/src/ROCKernels.jl index 6263fa05f..f76d4b937 100644 --- a/src/ROCKernels.jl +++ b/src/ROCKernels.jl @@ -150,26 +150,19 @@ end # these queries are compiled for every kernel, so they forward to functions of the # `HIPFunction` that are compiled only once KI.max_work_group_size(kernel::KI.Kernel{ROCBackend})::Int = - max_threads_per_block(kernel.kern.kernel.fun) + HIP.max_threads_per_block(kernel.kern.kernel.fun) KI.launch_configuration(kernel::KI.Kernel{ROCBackend}; nitems::Union{Integer,Nothing}=nothing, max_work_group_size::Integer=typemax(Int)) = (; workgroupsize=launch_threads(kernel.kern.kernel.fun, nitems, max_work_group_size)) -@noinline function max_threads_per_block(fun::HIP.HIPFunction) - max_items = Ref{Cint}() - HIP.hipFuncGetAttribute(max_items, HIP.HIP_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, fun) - return Int(max_items[]) -end @noinline function launch_threads(fun::HIP.HIPFunction, nitems, max_work_group_size) max_items = min(max_work_group_size, something(nitems, typemax(Int)), - max_threads_per_block(fun)) + HIP.max_threads_per_block(fun)) (; groupsize) = HIP.launch_configuration(fun; max_block_size=max_items) return Int(min(groupsize, max_items)) end -function KI.max_work_group_size(::ROCBackend)::Int - Int(HIP.attribute(AMDGPU.device(), HIP.hipDeviceAttributeMaxThreadsPerBlock)) -end -# queried on every automatically-sized launch, so use the limits cached in the device +# queried on every launch, so use the limits cached in the device +KI.max_work_group_size(::ROCBackend)::Int = HIP.max_workgroup_size(AMDGPU.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), the device code assumes workgroup indices diff --git a/src/hip/device.jl b/src/hip/device.jl index 6cfb62e42..8f90e9315 100644 --- a/src/hip/device.jl +++ b/src/hip/device.jl @@ -4,6 +4,7 @@ struct HIPDevice gcn_arch::String wavefrontsize::Cint max_workgroup_dims::NTuple{3, Int} + max_workgroup_size::Int end const DEFAULT_DEVICE = Ref{Union{Nothing, HIPDevice}}(nothing) @@ -20,7 +21,9 @@ function HIPDevice(device_id::Integer) wavefrontsize = props.warpSize max_workgroup_dims = Int.(props.maxThreadsDim) - HIPDevice(device_ref[], device_id, gcn_arch, wavefrontsize, max_workgroup_dims) + max_workgroup_size = Int(props.maxThreadsPerBlock) + HIPDevice(device_ref[], device_id, gcn_arch, wavefrontsize, max_workgroup_dims, + max_workgroup_size) end """ @@ -45,6 +48,13 @@ Get the maximum number of work-items along each dimension of a workgroup. """ max_workgroup_dims(d::HIPDevice)::NTuple{3, Int} = d.max_workgroup_dims +""" + max_workgroup_size(d::HIPDevice)::Int + +Get the maximum number of work-items in a workgroup. +""" +max_workgroup_size(d::HIPDevice)::Int = d.max_workgroup_size + """ gcn_arch(d::HIPDevice)::String diff --git a/src/hip/module.jl b/src/hip/module.jl index fbffab956..4240e042d 100644 --- a/src/hip/module.jl +++ b/src/hip/module.jl @@ -31,15 +31,20 @@ function module_global(mod::HIPModule, name::AbstractString) return ptr_ref[], Int(bytes_ref[]) end -struct HIPFunction - handle::hipFunction_t - mod::HIPModule - global_hostcalls::Vector{Symbol} +mutable struct HIPFunction + const handle::hipFunction_t + const mod::HIPModule + const global_hostcalls::Vector{Symbol} + + # queried for every KernelAbstractions launch, so cached; see `max_threads_per_block` + # and `launch_configuration` + @atomic max_threads::Int # 0 until queried + @atomic launch_config::NTuple{2, Cint} # (gridsize, groupsize), (0, 0) until queried function HIPFunction(mod::HIPModule, name::String, global_hostcalls::Vector{Symbol}) fun_ref = Ref{hipFunction_t}() hipModuleGetFunction(fun_ref, mod, name) - new(fun_ref[], mod, global_hostcalls) + new(fun_ref[], mod, global_hostcalls, 0, (Cint(0), Cint(0))) end end @@ -47,9 +52,57 @@ Base.unsafe_convert(::Type{hipFunction_t}, fun::HIPFunction) = fun.handle Base.:(==)(a::HIPFunction, b::HIPFunction) = a.handle == b.handle Base.hash(fun::HIPFunction, h::UInt) = hash(fun.handle, h) +""" + max_threads_per_block(fun::HIPFunction)::Int + +The maximum number of work-items in a workgroup that `fun` can be launched with. +""" +function max_threads_per_block(fun::HIPFunction) + # fixed when the function is loaded + threads = @atomic :monotonic fun.max_threads + if threads == 0 + threads_ref = Ref{Cint}() + hipFuncGetAttribute(threads_ref, HIP_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, fun) + threads = Int(threads_ref[]) + @atomic :monotonic fun.max_threads = threads + end + return threads +end + +""" + launch_configuration(fun::HIPFunction; shmem=0, max_block_size=0) + +The workgroup size with the best occupancy for `fun`, using `shmem` bytes of dynamic shared +memory, and the number of workgroups that reach that occupancy, as a named tuple +`(; gridsize, groupsize)`. A positive `max_block_size` limits the workgroup size; HIP rounds +it up to the wavefront size. + +Without dynamic shared memory, the result is cached in `fun`. +""" function launch_configuration(fun::HIPFunction; shmem::Integer = 0, max_block_size::Integer = 0) + if shmem == 0 + # the occupancy only depends on the resources the function uses, which are fixed + # when it is loaded + config = @atomic :monotonic fun.launch_config + if config[2] == 0 + config = query_launch_configuration(fun, 0, 0) + @atomic :monotonic fun.launch_config = config + end + # HIP picks the largest block size the occupancy allows, capped by the limit rounded + # up to the wavefront size, so a limit that admits the unconstrained choice doesn't + # change the result + gridsize, groupsize = config + if max_block_size <= 0 || max_block_size >= groupsize + return (; gridsize, groupsize) + end + end + gridsize, groupsize = query_launch_configuration(fun, shmem, max_block_size) + return (; gridsize, groupsize) +end + +function query_launch_configuration(fun::HIPFunction, shmem::Integer, max_block_size::Integer) grid_size_ref, block_size_ref = Ref{Cint}(), Ref{Cint}() hipModuleOccupancyMaxPotentialBlockSize( grid_size_ref, block_size_ref, fun, shmem, max_block_size) - return (; gridsize=grid_size_ref[], groupsize=block_size_ref[]) + return (grid_size_ref[], block_size_ref[]) end diff --git a/test/core/codegen.jl b/test/core/codegen.jl index 319705d3d..0ed97bc4e 100644 --- a/test/core/codegen.jl +++ b/test/core/codegen.jl @@ -133,6 +133,7 @@ end AMDGPU.HIP.hipFuncGetAttribute( maxthreads, AMDGPU.HIP.HIP_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, k.fun.handle) @test maxthreads[] == 256 + @test AMDGPU.HIP.max_threads_per_block(k.fun) == 256 k(; groupsize=256) AMDGPU.synchronize() diff --git a/test/core/core_tests.jl b/test/core/core_tests.jl index 235e931f4..4188e8f1c 100644 --- a/test/core/core_tests.jl +++ b/test/core/core_tests.jl @@ -69,6 +69,8 @@ end @test occursin("gfx", HIP.gcn_arch(device)) @test HIP.wavefrontsize(device) in (32, 64) + @test HIP.max_workgroup_size(device) == + HIP.attribute(device, HIP.hipDeviceAttributeMaxThreadsPerBlock) end end end diff --git a/test/device/launch.jl b/test/device/launch.jl index f8628fb8a..11de51f80 100644 --- a/test/device/launch.jl +++ b/test/device/launch.jl @@ -167,6 +167,15 @@ end # This kernel has no occupancy constraints @test occ.groupsize == AMDGPU.Device._max_group_size + # the configuration without dynamic shared memory is cached, which must not change it + for kern in (kern, @roc launch=false maxthreads=256 f(x)) + for max_block_size in (0, 1, 31, 32, 33, 100, 256, 257, 1000, 1024, 2048) + @test AMDGPU.HIP.launch_configuration(kern.fun; max_block_size) == + NamedTuple{(:gridsize, :groupsize)}( + AMDGPU.HIP.query_launch_configuration(kern.fun, 0, max_block_size)) + end + end + # TODO # @testset "Automatic groupsize selection" begin # function groupsize_kernel(A)