Cuda code generator

From wenyi-li/awesome-agent-kernel-skills · View interactive version

Cuda code generator is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Generates optimized CUDA kernel code based on performance analysis reports or algorithm type. Reads NCU analysis reports (e.g. *_analysis.md) and optionally existing kernel code, then produces high-quality compilable .cu files with applied optimizations. Use when the user provides an NCU analysis re

How to use this skill

Skill file

CUDA Code Generator

Reads performance analysis reports and generates optimized CUDA kernel code (.cu files). Does not handle compilation, execution, or profiling.

Execution Workflow

Progress Tracking

Task Progress:
- [ ] Step 1: Requirement Analysis
- [ ] Step 2: Read Input Files
- [ ] Step 3: Determine Optimization Strategy
- [ ] Step 4: Generate Code and Write to File

Step 1: Requirement Analysis

Confirm the following information (can be obtained from the report or user input):

| Item | Source |

| ---------------------------------------------------------- | ------------------ |

| Algorithm Type (MatMul / Reduction / Convolution / Custom) | Report Kernel Name |

| Data Scale (M, N, K, or other dimensions) | Provided by User |

| Precision (FP32 / FP16 / INT8) | Report or User |

| GPU Architecture (sm_XX) | Report Device CC |


Step 2: Read Input Files

  1. Read NCU Analysis Report (*_analysis.md, preferred)
  1. Read Existing Kernel Code (If user provides path)

If no analysis report is available, select a default strategy based on the algorithm type, see cuda-optimization-strategies.md.


Step 3: Determine Optimization Strategy

Find the corresponding strategy combination in cuda-optimization-strategies.md based on the bottleneck type from the report, then apply them in order of priority from P0 → Pn.

Quick reference for strategies corresponding to each bottleneck type (see reference documentation for detailed explanations and code templates):

| Bottleneck Type | Characteristic Condition | Priority Strategy |

| ----------------- | ------------------------------ | ------------------------------------------------------------ |

| DRAM_MEMORY_BOUND | DRAM > 70%, SM < 30% | Block Tiling → Vectorized Load → Prefetching |

| L1_PRESSURE_BOUND | L1 > 80%, DRAM < 30% | Shared Memory Tiling → Padding → Data Transpose |

| LATENCY_BOUND | SM Busy < 50%, Occupancy > 60% | Double Buffering → ILP → Loop Unrolling |

| COMPUTE_BOUND | SM > 60%, SM Busy > 80% | FMA → FP16/TF32 → Tensor Core |

| OCCUPANCY_BOUND | Occupancy < 30%, SM Busy > 70% | Adjust Block Size → __launch_bounds__ → Reduce smem |

| MIXED_BOUND | No single dominant bottleneck | Profile with narrower section sets; address top metric first |


Step 4: Generate Code and Write to File

Output File Path
  1. Execute date +%Y%m%d_%H%M%S to get the timestamp (e.g., 20260316_153045)
  2. Append the timestamp suffix based on the original .cu file path, always create a new file, do not overwrite the original file:
File Header Comment Template
/*
 * Optimized CUDA Kernel - <Algorithm Name>
 *
 * Generation Time: <YYYY-MM-DD HH:MM:SS>
 *
 * Optimization Measures (from NCU Analysis Report):
 *   [P0] Shared Memory Tiling (TILE_SIZE=16)
 *   [P1] Shared Memory Padding (+1 to eliminate Bank Conflict)
 *   [P2] Vectorized Load (float4)
 *
 * Compilation Command:
 *   nvcc -O3 -arch=sm_89 -o kernel solution_opt_<timestamp>.cu
 *
 * Target Device: Ada Lovelace (sm_89)
 * Precision: FP32
 */
Code Quality Requirements

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.