cuda-profiling — Claude Code skill: profile CUDA kernels
Walks your agent through CUDA profiling: Nsight Systems timelines, Nsight Compute kernel metrics, roofline reading and bottleneck diagnosis.
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a claude-codeWhat cuda-profiling does
The skill helps you figure out why a CUDA kernel runs slower than expected. Your agent picks the right tool: nsys for the CPU+GPU timeline, ncu for per-kernel metrics. Then it works through the diagnosis — is the kernel bound by memory bandwidth or by compute?
Inside is a working order: build with -lineinfo, capture a timeline, then drill into the hot kernel with ncu --kernel-name. There is NVTX range annotation for phase visibility, a metric set for CI, and a table of common failures. ERR_NVGPUCTRPERM, for instance, means you lack profiling permissions — run with sudo or lift the restriction via NVreg_RestrictProfilingToAdminUsers=0. Worth checking before you kick off long runs.
The skill leans on NVIDIA's Nsight Systems and Nsight Compute, so without the CUDA Toolkit and those tools it does nothing. Occupancy limits from registers or shared memory get their own ncu commands.
What it helps with
- find the bottleneck in a CUDA kernel that runs slower than expected
- compare kernel variants with different block sizes and tiling
- build CI performance regression checks from
ncumetrics - annotate application phases with NVTX ranges for the timeline
- tell whether a kernel is memory-bound or compute-bound
Example prompts for the agent
- profile my CUDA kernel with nsys and show me the gaps between launches
- pull ncu metrics for the matmul_tiled kernel and tell me if it's memory-bound or compute-bound
- add NVTX ranges around the H2D copy and the kernel launch
How to install cuda-profiling
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a claude-code2Manually: put the files in place
~/.claude/skills/cuda-profiling/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/cuda-profiling/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a codex2Manually: put the files in place
.agents/skills/cuda-profiling/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a cursor2Manually: put the files in place
.cursor/skills/cuda-profiling/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install mohitmishra786/low-level-dev-skills cuda-profiling2Manually: put the files in place
.github/skills/cuda-profiling/SKILL.md1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a gemini-cli2Platform CLI
gemini skills install https://github.com/mohitmishra786/low-level-dev-skills --path skills/gpu/cuda-profilingManually: put the files in place
.devin/skills/cuda-profiling/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about cuda-profiling
How do I install cuda-profiling in Claude?
Run npx skills add mohitmishra786/low-level-dev-skills --skill cuda-profiling -a claude-code. The skill lands in ~/.claude/skills/cuda-profiling/SKILL.md.
What does cuda-profiling do?
It guides your agent through CUDA profiling: Nsight Systems timelines, Nsight Compute kernel metrics, roofline interpretation, and memory-bound vs compute-bound diagnosis.
What do I need for it to work?
Nsight Systems and Nsight Compute from NVIDIA. Profiling also needs permissions: on ERR_NVGPUCTRPERM, run with sudo or lift the restriction via NVreg_RestrictProfilingToAdminUsers=0.
Source text
SKILL.md · MIT
---
name: cuda-profiling
description: CUDA profiling skill for NVIDIA GPU performance analysis. Use when profiling kernels with Nsight Systems or Nsight Compute, interpreting roofline models, diagnosing memory-bound vs compute-bound kernels, or annotating code with NVTX ranges. Activates on queries about Nsight, NCU, ncu CLI, GPU roofline, occupancy metrics, or CUDA profiling workflow.
---
# CUDA Profiling
## Purpose
Guide agents through profiling CUDA applications with Nsight Systems (timeline-level) and Nsight Compute (kernel-level metrics), using the NCU CLI for automated metric collection, interpreting roofline models, and diagnosing whether kernels are memory-bound or compute-bound.
## When to Use
- A CUDA kernel is slower than expected and you need bottleneck identification
- Comparing kernel variants (tiling strategies, block sizes)
- Building CI performance regression checks with `ncu` metrics
- Correlating CPU and GPU activity in multi-stream pipelines
- Annotating application phases with NVTX for timeline visibility
- Interpreting occupancy, memory throughput, and SM utilization metrics
## Workflow
### 1. Choose profiling tool
```
What do you need?
├── System-wide timeline (CPU+GPU+CUDA API) → Nsight Systems (nsys)
├── Per-kernel deep metrics (occupancy, memory) → Nsight Compute (ncu)
└── Quick metric from CLI in CI → ncu --metrics ...
```
### 2. Nsight Systems — timeline profiling
```bash
# Profile entire application
nsys profile --trace=cuda,nvtx,osrt --output=report ./my_cuda_app
# Open report
nsys-ui report.nsys-rep
# CLI summary
nsys stats report.nsys-rep
```
What to look for in the timeline:
- Gaps between kernel launches (CPU bottleneck or sync points)
- `cudaDeviceSynchronize` stalls
- Overlap between H2D copies and kernel execution across streams
- CUDA API call overhead
```bash
# Capture with CUDA graph info
nsys profile --capture-range=cudaProfilerApi ./my_cuda_app
```
### 3. NVTX range annotations
```cpp
#include <nvtx3/nvToolsExt.h>
void pipeline(void) {
nvtxRangePushA("H2D copy");
cudaMemcpyAsync(d_in, h_in, size, cudaMemcpyHostToDevice, stream);
nvtxRangePop();
nvtxRangePushA("kernel");
my_kernel<<<grid, block, 0, stream>>>(d_in, d_out, n);
nvtxRangePop();
nvtxRangePushA("D2H copy");
cudaMemcpyAsync(h_out, d_out, size, cudaMemcpyDeviceToHost, stream);
nvtxRangePop();
}
```
Compile with `-lnvToolsExt` or link nvtx3 header-only. Ranges appear as colored bands in Nsight Systems.
### 4. Nsight Compute — kernel analysis
```bash
# Profile all kernels, save report
ncu -o kernel_report ./my_cuda_app
# Profile specific kernel by name
ncu --kernel-name regex:matmul_tiled ./my_cuda_app
# Launch UI
ncu-ui kernel_report.ncu-rep
```
Key sections in NCU report:
- **Speed of Light**: SM throughput vs memory throughput vs peak
- **Occupancy**: Active warps vs hardware limit
- **Memory Workload Analysis**: L1/L2 hit rates, coalescing efficiency
- **Warp State Statistics**: Stall reasons (memory, barrier, dispatch)
### 5. NCU CLI metrics
```bash
# Essential metrics set
ncu --metrics \
sm__throughput.avg.pct_of_peak_sustained_elapsed,\
dram__throughput.avg.pct_of_peak_sustained_elapsed,\
sm__warps_active.avg.pct_of_peak_sustained_active,\
l1tex__t_sectors_pipe_lsu_mem_global_op_ld.sum,\
smsp__sass_thread_inst_executed_op_ffma_pred_on.sum \
./my_cuda_app
# CSV export for CI
ncu --csv --metrics dram__bytes_read.sum,dram__bytes_write.sum ./my_cuda_app
# Set kernel replay mode for accurate counters
ncu --kernel-replay-mode application ./my_cuda_app
```
### 6. Memory-bound vs compute-bound diagnosis
```
Roofline interpretation
├── dram__throughput near peak AND sm__throughput low → memory-bound
│ └── Fix: coalescing, shared mem tiling, reduce traffic
├── sm__throughput near peak AND dram low → compute-bound
│ └── Fix: tensor cores, loop unrolling, ILP
└── Both low → launch config, occupancy, or sync overhead
```
Roofline model (conceptual):
```
Performance (GFLOP/s)
| /\ compute roof
| / \
| / \____ memory roof (bandwidth-limited region)
| /
+------------------ Arithmetic Intensity (FLOP/byte)
```
Measure arithmetic intensity: `smsp__sass_thread_inst_executed_op_ffma_pred_on.sum * 2 / dram__bytes.sum`
### 7. Occupancy analysis
```bash
ncu --metrics sm__warps_active.avg.pct_of_peak_sustained_active,\
launch__occupancy_limit_registers,\
launch__occupancy_limit_shared_mem,\
launch__occupancy_limit_block_size \
./my_cuda_app
```
| Limiting factor | Typical fix |
|-----------------|-------------|
| Registers | `-maxrregcount`, simplify kernel |
| Shared memory | Reduce tile size, split phases |
| Block size | Try 128 or 256 instead of 512+ |
### 8. Profiling workflow checklist
```bash
# 1. Build with line info (not -G unless debugging)
nvcc -lineinfo -O3 -arch=sm_80 -o app main.cu
# 2. Timeline first
nsys profile --trace=cuda,nvtx -o timeline ./app
# 3. Deep dive on hot kernel
ncu --kernel-name regex:hot_kernel --set full ./app
# 4. Compare before/after
ncu --csv --metrics sm__throughput.avg.pct_of_peak_sustained_elapsed ./app_v1 > v1.csv
ncu --csv --metrics sm__throughput.avg.pct_of_peak_sustained_elapsed ./app_v2 > v2.csv
```
## Common Problems
| Symptom | Cause | Fix |
|---------|-------|-----|
| `ERR_NVGPUCTRPERM` | Insufficient profiling permissions | Run with sudo or set `NVreg_RestrictProfilingToAdminUsers=0` |
| All metrics show zero | Profiling disabled or wrong GPU | Check `CUDA_VISIBLE_DEVICES`; use `--target-processes all` |
| NCU report empty | Kernel too short or not launched | Increase workload; verify `cudaGetLastError()` |
| Huge profiling overhead | Full metric sets on many kernels | Use `--kernel-name` filter; `--launch-skip` |
| Timeline shows no overlap | Single default stream | Create multiple streams; use async copies |
| Occupancy looks fine but kernel slow | Memory latency not hidden | Check memory coalescing; increase active warps |
## Related Skills
- `skills/gpu/cuda` — kernel writing, occupancy tuning, nvcc flags
- `skills/gpu/gpu-memory-model` — coalescing, bank conflicts, SIMT model
- `skills/gpu/cuda-debugging` — correctness before performance tuning
- `skills/profilers/intel-vtune-amd-uprof` — CPU-side roofline and hotspot analysis
- `skills/profilers/flamegraphs` — CPU flamegraphs complementary to nsys timeline
- `skills/profilers/hardware-counters` — general perf stat concepts
© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills