-
Notifications
You must be signed in to change notification settings - Fork 17
Name CUDA kernels based on stack trace in auto_launch
#2376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
78043ba
687365e
26e4ad5
3a94f7c
f3542d4
cf830bf
e1285ae
06c44e2
7411a8d
9dd3116
93fcf68
25f23ee
9f0fa87
31ec263
eafbe54
52ff458
46c1f6c
fca1ad1
05a5d5b
e7bca7d
a541746
7af8d16
cec2ef7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,10 @@ import CUDA | |
| import ClimaCore.Fields | ||
| import ClimaCore.DataLayouts | ||
| import ClimaCore.DataLayouts: empty_kernel_stats | ||
| import ClimaCore.DebugOnly: name_kernels_from_stack_trace | ||
|
|
||
| const reported_stats = Dict() | ||
| const kernel_names = Dict{String, AbstractString}() | ||
| # Call via ClimaCore.DataLayouts.empty_kernel_stats() | ||
| empty_kernel_stats(::ClimaComms.CUDADevice) = empty!(reported_stats) | ||
| collect_kernel_stats() = false | ||
|
|
@@ -39,19 +41,60 @@ function auto_launch!( | |
| always_inline = true, | ||
| caller = :unknown, | ||
| ) where {F!} | ||
| # If desired, compute a kernel name from the stack trace and store in | ||
| # a global Dict, which serves as an in memory cache | ||
| kernel_name = nothing | ||
| if name_kernels_from_stack_trace() | ||
| # Create a key from the function and types of the args | ||
| key = string(objectid(f!)) | ||
| kernel_name_exists = key in keys(kernel_names) | ||
| if !kernel_name_exists | ||
| # Construct the kernel name, ignoring modules we don't care about | ||
| uninteresting_modules = [ | ||
| :Base, | ||
| :Core, | ||
| :GPUCompiler, | ||
| :CUDA, | ||
| :NVTX, | ||
| :ClimaCoreCUDAExt, | ||
| :ClimaCore, | ||
| ] | ||
| stack = stacktrace() | ||
| first_relevant_index = findfirst(stack) do frame | ||
| frame.linfo isa Core.MethodInstance && ( | ||
| fullname(frame.linfo.def.module)[1] ∉ uninteresting_modules | ||
| ) | ||
| end | ||
| if !isnothing(first_relevant_index) | ||
| frame = stack[first_relevant_index] | ||
| name_str = | ||
| string(frame.func) * | ||
| "_" * | ||
| string(frame.linfo.def.file) * | ||
petebachant marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "_" * | ||
| string(frame.line) | ||
|
||
| kernel_name = replace(name_str, r"[^A-Za-z0-9]" => "_") | ||
| end | ||
| @debug "Using kernel name: $kernel_name" | ||
| kernel_names[key] = kernel_name | ||
| end | ||
| kernel_name = kernel_names[key] | ||
| end | ||
|
|
||
| if auto | ||
| @assert !isnothing(nitems) | ||
| if nitems ≥ 0 | ||
| kernel = CUDA.@cuda always_inline = true launch = false f!(args...) | ||
| kernel = CUDA.@cuda name = kernel_name always_inline = true launch = | ||
imreddyTeja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| false f!(args...) | ||
| config = CUDA.launch_configuration(kernel.fun) | ||
| threads = min(nitems, config.threads) | ||
| blocks = cld(nitems, threads) | ||
| kernel(args...; threads, blocks) # This knows to use always_inline from above. | ||
| end | ||
| else | ||
| kernel = | ||
| CUDA.@cuda always_inline = always_inline threads = threads_s blocks = | ||
| blocks_s f!(args...) | ||
| CUDA.@cuda name = kernel_name always_inline = always_inline threads = | ||
| threads_s blocks = blocks_s f!(args...) | ||
| end | ||
|
|
||
| if collect_kernel_stats() # only for development use | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,4 +171,6 @@ function allow_mismatched_spaces_unsafe() | |
| return false | ||
| end | ||
|
|
||
| name_kernels_from_stack_trace() = false | ||
|
|
||
| end | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These syntax changes may be less clear, so feel free to ignore
or