Choose launch configuration

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

Choose launch configuration is an AI agent skill from the GitHub repository wenyi-li/awesome-agent-kernel-skills. Guide the agent through selecting the correct and efficient thread block dimensions and grid dimensions for a CUDA kernel, covering occupancy analysis, register and shared memory constraints, tail eff

How to use this skill

Skill file

Skill: Choose CUDA Launch Configuration

Purpose

Guide the agent through selecting the correct and efficient thread block dimensions and grid dimensions for a CUDA kernel, covering occupancy analysis, register and shared memory constraints, tail effects, persistent kernels, and when to use cudaOccupancyMaxActiveBlocksPerMultiprocessor as a decision tool.

Use this when

Do not use this when

Inputs the agent should gather first

Required reasoning process

  1. Determine the theoretical occupancy for candidate block sizes. Theoretical occupancy is the ratio of active warps to maximum possible warps per SM. The SM can support a fixed maximum number of threads (2048 on SM70+). For a block of 256 threads, the SM can hold at most 8 blocks (2048/256). But register and smem constraints may further limit this.
  1. Compute register-limited occupancy. Each SM has 65536 32-bit registers (SM70+). With R registers per thread and block size B threads:

For R=32 and B=256: registers per block = 32 * 256 = 8192. Max blocks = floor(65536 / 8192) = 8. Occupancy = 8 * 256 / 2048 = 100%.

For R=64 and B=256: registers per block = 64 * 256 = 16384. Max blocks = 4. Occupancy = 4 * 256 / 2048 = 50%.

For R=128 and B=256: registers per block = 32768. Max blocks = 2. Occupancy = 25%.

  1. Compute smem-limited occupancy. smem per SM varies (e.g., 96 KB for V100, 164 KB for A100). With S bytes of smem per block:

Combine with register limit: take the minimum of both max-block estimates.

  1. Account for the max blocks per SM hardware limit. Even if registers and smem allow more, the hardware has a maximum number of resident blocks per SM (32 for SM90, 32 for SM80, 32 for SM75, 32 for SM70, 16 for SM35). This limits small-block configurations: with B=32 (one warp per block) and max 32 blocks: maximum 32 * 32 = 1024 active threads, which is only 50% occupancy even with zero register/smem overhead.
  1. Compare occupancy across candidate block sizes. Typical candidates: 64, 128, 256, 512 threads. For each:

Select the block size with highest occupancy. If multiple are equal, prefer 128 or 256 as defaults (fewer blocks to schedule, simpler grid math).

  1. Use cudaOccupancyMaxActiveBlocksPerMultiprocessor at runtime for accurate values. This API accounts for register rounding, smem rounding, and architecture-specific limits accurately. Use it when autotuning or when register count is uncertain at compile time:

```c

int numBlocks;

cudaOccupancyMaxActiveBlocksPerMultiprocessor(&numBlocks, myKernel, blockSize, dynamicSmem);

float occupancy = (numBlocks * blockSize / 32.0f) / maxWarpsPerSM;

```

This is more reliable than manual calculation for production code.

  1. Compute the grid size. Grid size must ensure all work items are covered:

```

gridDim.x = ceil(total_work / blockDim.x) // for 1D

gridDim.x = ceil(N / blockDim.x), gridDim.y = ceil(M / blockDim.y) // for 2D

```

For grids larger than 2^31 - 1 in any dimension: use gridDim.y or gridDim.z to encode the extra dimension, or use a grid-stride loop in the kernel.

  1. Check for tail effects. When total_work % blockDim.x != 0, the last block is a "tail block" with fewer active threads than blockDim.x. The tail block occupies a full SM slot. For total_work / blockDim.x very small (e.g., 2–4 blocks per SM), tail effects cause significant underutilization. Mitigate by:
  1. Consider persistent kernels for latency-sensitive or irregular workloads. A persistent kernel launches exactly SM_count blocks (or a small multiple) and uses a grid-stride loop to consume work from a queue or array. Benefits: eliminates repeated kernel launch overhead, allows finer-grained load balancing for irregular work distributions. Costs: more complex, requires the work queue and termination logic.
  1. Handle __launch_bounds__ for register-heavy kernels. When register usage is high, annotate the kernel with __launch_bounds__(max_threads_per_block, min_blocks_per_SM):

Kernel design rules

Correctness requirements

Performance requirements

Output format

The final response must include:

  1. Occupancy analysis table: for candidate block sizes (64, 128, 256, 512), show the register-limited blocks, smem-limited blocks, hardware-limited blocks, and resulting occupancy.
  2. Selected configuration: block dimensions and grid dimensions with justification.
  3. Tail analysis: for the selected configuration, state the tail block size and its fraction of total work.
  4. __launch_bounds__ recommendation (if applicable): based on register usage and desired occupancy.
  5. Host launch code: kernel<<<grid, block, smem>>> with all parameters.
  6. Grid validity check: verify grid dimensions are within CUDA limits.
  7. Occupancy verification code: optionally include a call to cudaOccupancyMaxActiveBlocksPerMultiprocessor to validate the estimate.

Common failure modes

Review checklist

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.