Skip to content

Commit

Permalink
Detect and explain when parameters exceed the launch limit (#2180)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Besard <[email protected]>
  • Loading branch information
simonbyrne and maleadt authored Jan 4, 2024
1 parent b6a4efb commit 25fd692
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,53 @@ function compile(@nospecialize(job::CompilerJob))
push!(ptxas_opts, "--compile-only")
end

ptx = job.config.params.ptx
cap = job.config.params.cap
arch = "sm_$(cap.major)$(cap.minor)"

# validate use of parameter memory
argtypes = filter([KernelState, job.source.specTypes.parameters...]) do dt
!isghosttype(dt) && !Core.Compiler.isconstType(dt)
end
param_usage = sum(sizeof, argtypes)
param_limit = 4096
if cap >= v"7.0" && ptx >= v"8.1"
param_limit = 32764
end
if param_usage > param_limit
msg = """Kernel invocation uses too much parameter memory.
$(Base.format_bytes(param_usage)) exceeds the $(Base.format_bytes(param_limit)) limit imposed by sm_$(cap.major)$(cap.minor) / PTX v$(ptx.major).$(ptx.minor)."""

try
details = "\n\nRelevant parameters:"

source_types = job.source.specTypes.parameters
source_argnames = Base.method_argnames(job.source.def)
while length(source_argnames) < length(source_types)
# this is probably due to a trailing vararg; repeat its name
push!(source_argnames, source_argnames[end])
end

for (i, typ) in enumerate(source_types)
if isghosttype(typ) || Core.Compiler.isconstType(typ)
continue
end
name = source_argnames[i]
details *= "\n [$(i-1)] $name::$typ uses $(Base.format_bytes(sizeof(typ)))"
end
details *= "\n"

if cap >= v"7.0" && ptx < v"8.1" && param_usage < 32764
details *= "\nNote: use a newer CUDA to support more parameters on your device.\n"
end

msg *= details
catch err
@error "Failed to analyze kernel parameter usage; please file an issue with a reproducer."
end
error(msg)
end

# compile to machine code
# NOTE: we use tempname since mktemp doesn't support suffixes, and mktempdir is slow
ptx_input = tempname(cleanup=false) * ".ptx"
Expand Down
5 changes: 5 additions & 0 deletions test/core/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ end
@test f(2) == 2
end

@testset "parameter space" begin
kernel(x) = nothing
@test_throws "Kernel invocation uses too much parameter memory" @cuda kernel(ntuple(_->UInt64(1), 2^13))
end

end

############################################################################################
Expand Down

0 comments on commit 25fd692

Please sign in to comment.