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
1 change: 1 addition & 0 deletions docs/src/api/devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 4 additions & 11 deletions src/ROCKernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/hip/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

"""
Expand All @@ -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

Expand Down
65 changes: 59 additions & 6 deletions src/hip/module.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,78 @@ 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

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
1 change: 1 addition & 0 deletions test/core/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions test/core/core_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/device/launch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading