Skip to content

Commit 25fd692

Browse files
simonbyrnemaleadt
andauthored
Detect and explain when parameters exceed the launch limit (#2180)
Co-authored-by: Tim Besard <tim.besard@gmail.com>
1 parent b6a4efb commit 25fd692

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/compiler/compilation.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,53 @@ function compile(@nospecialize(job::CompilerJob))
273273
push!(ptxas_opts, "--compile-only")
274274
end
275275

276+
ptx = job.config.params.ptx
276277
cap = job.config.params.cap
277278
arch = "sm_$(cap.major)$(cap.minor)"
278279

280+
# validate use of parameter memory
281+
argtypes = filter([KernelState, job.source.specTypes.parameters...]) do dt
282+
!isghosttype(dt) && !Core.Compiler.isconstType(dt)
283+
end
284+
param_usage = sum(sizeof, argtypes)
285+
param_limit = 4096
286+
if cap >= v"7.0" && ptx >= v"8.1"
287+
param_limit = 32764
288+
end
289+
if param_usage > param_limit
290+
msg = """Kernel invocation uses too much parameter memory.
291+
$(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)."""
292+
293+
try
294+
details = "\n\nRelevant parameters:"
295+
296+
source_types = job.source.specTypes.parameters
297+
source_argnames = Base.method_argnames(job.source.def)
298+
while length(source_argnames) < length(source_types)
299+
# this is probably due to a trailing vararg; repeat its name
300+
push!(source_argnames, source_argnames[end])
301+
end
302+
303+
for (i, typ) in enumerate(source_types)
304+
if isghosttype(typ) || Core.Compiler.isconstType(typ)
305+
continue
306+
end
307+
name = source_argnames[i]
308+
details *= "\n [$(i-1)] $name::$typ uses $(Base.format_bytes(sizeof(typ)))"
309+
end
310+
details *= "\n"
311+
312+
if cap >= v"7.0" && ptx < v"8.1" && param_usage < 32764
313+
details *= "\nNote: use a newer CUDA to support more parameters on your device.\n"
314+
end
315+
316+
msg *= details
317+
catch err
318+
@error "Failed to analyze kernel parameter usage; please file an issue with a reproducer."
319+
end
320+
error(msg)
321+
end
322+
279323
# compile to machine code
280324
# NOTE: we use tempname since mktemp doesn't support suffixes, and mktempdir is slow
281325
ptx_input = tempname(cleanup=false) * ".ptx"

test/core/execution.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,11 @@ end
599599
@test f(2) == 2
600600
end
601601

602+
@testset "parameter space" begin
603+
kernel(x) = nothing
604+
@test_throws "Kernel invocation uses too much parameter memory" @cuda kernel(ntuple(_->UInt64(1), 2^13))
605+
end
606+
602607
end
603608

604609
############################################################################################

0 commit comments

Comments
 (0)