Cuda programming

From j4flmao/agent-skills · View interactive version

Cuda programming is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about a CUDA kernel, nvcc, shared memory tiling, coalesced access, warp divergence, occupancy, CUDA streams or __syncthreads. Produces CUDA C++ kernels with launch config, error checking and an Nsight Compute profiling plan. Not for Triton kernels (use triton-kernels) or PTX/S

How to use this skill

Skill file

CUDA Programming

Purpose

Write correct, fast CUDA C++ kernels for NVIDIA GPUs. Most kernels are limited by memory bandwidth or latency, not arithmetic, so the work is mostly about access patterns, data reuse in shared memory/registers, and enough parallelism to hide latency. Every optimization must be confirmed with Nsight Compute, not intuition.

Agent Protocol

Trigger

"cuda", "cuda kernel", "nvcc", "gpu programming", "thread block", "shared memory", "warp divergence", "coalesced", "occupancy", "cuda streams", "__syncthreads".

Input Context

Output Artifact

Kernel code (.cu) with launch code, error checking, and a profiling plan. No file unless requested.

Response Format

Bound: {memory|compute|latency} (evidence: {ncu metric})
Launch: grid={...} block={...} smem={bytes}
Change: {what and why}
Expected effect: {metric to re-check}

Completion Criteria

Execution Model

| Level | Facts |

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

| Thread | Has private registers; spills go to "local" memory (device DRAM, cached in L1/L2). |

| Warp | 32 threads. Scheduler issues one instruction per warp per cycle. Since Volta, threads have independent program counters but divergent paths still execute serially. |

| Block (CTA) | Up to 1024 threads. Shares shared memory; synchronizes with __syncthreads(). Runs on one SM. |

| Cluster (sm_90+) | Group of blocks on a GPC that can access each other's shared memory (distributed shared memory). |

| Grid | All blocks of a launch. No grid-wide barrier except cooperative launch. |

Memory Hierarchy

| Space | Scope | Notes |

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

| Registers | thread | Fastest. Per-thread limit 255. High usage lowers occupancy. |

| Shared memory | block | On-chip SRAM, 32 banks of 4 bytes. Default static limit 48 KB per block; larger requires cudaFuncSetAttribute(..., cudaFuncAttributeMaxDynamicSharedMemorySize, bytes) (up to ~227 KB on H100, ~163 KB on A100). |

| L1 / L2 | SM / device | Hardware caches; L1 shares capacity with shared memory. |

| Global (HBM/GDDR) | device | Hundreds of cycles latency. Bandwidth is the usual limit. |

| Constant | device, cached | Fast only when all threads of a warp read the same address. |

Rules

  1. Coalesce global access. Consecutive threads of a warp should touch consecutive addresses so a warp load maps to few 32-byte sectors. Structure-of-arrays beats array-of-structures.
  2. Reuse via shared memory. Load a tile once, use it many times (tiled GEMM/stencils). Pad tiles ([TILE][TILE+1]) to avoid bank conflicts on column access.
  3. Avoid divergence inside a warp. Branching on threadIdx.x % 2 serializes both paths; branching on warp-uniform values (threadIdx.x / 32) does not.
  4. Occupancy is a means, not a goal. Enough warps to hide latency; higher occupancy past that point rarely helps. Use cudaOccupancyMaxPotentialBlockSize as a starting point.
  5. Block size a multiple of 32, usually 128-256.
  6. Grid-stride loops make kernels correct for any n and reusable across GPUs.
  7. Minimize host-device transfers; use pinned memory (cudaMallocHost) and cudaMemcpyAsync on streams to overlap copy and compute.
  8. Check errors after launches: cudaGetLastError() for launch errors, a sync (in debug) for execution errors.

Example: Error Checking + Grid-Stride SAXPY

#include <cstdio>
#include <cstdlib>
#include <cuda_runtime.h>

#define CUDA_CHECK(call)                                                     \
  do {                                                                       \
    cudaError_t err_ = (call);                                               \
    if (err_ != cudaSuccess) {                                               \
      std::fprintf(stderr, "CUDA error %s at %s:%d: %s\n", #call, __FILE__,  \
                   __LINE__, cudaGetErrorString(err_));                      \
      std::exit(EXIT_FAILURE);                                               \
    }                                                                        \
  } while (0)

__global__ void saxpy(int n, float a, const float* __restrict__ x,
                      float* __restrict__ y) {
  for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < n;
       i += blockDim.x * gridDim.x) {
    y[i] = a * x[i] + y[i];
  }
}

void run_saxpy(int n, float a, const float* d_x, float* d_y, cudaStream_t s) {
  int device = 0, sms = 0;
  CUDA_CHECK(cudaGetDevice(&device));
  CUDA_CHECK(cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, device));
  const int block = 256;
  const int grid = sms * 8;  // enough blocks to fill the GPU; loop covers the rest
  saxpy<<<grid, block, 0, s>>>(n, a, d_x, d_y);
  CUDA_CHECK(cudaGetLastError());
}

Example: Shared-Memory Tiled Transpose (bank-conflict free)

constexpr int TILE = 32;
constexpr int ROWS = 8;

__global__ void transpose(float* out, const float* in, int width, int height) {
  __shared__ float tile[TILE][TILE + 1];  // +1 pad removes bank conflicts
  int x = blockIdx.x * TILE + threadIdx.x;
  int y = blockIdx.y * TILE + threadIdx.y;
  for (int j = 0; j < TILE; j += ROWS)
    if (x < width && y + j < height)
      tile[threadIdx.y + j][threadIdx.x] = in[(y + j) * width + x];
  __syncthreads();
  x = blockIdx.y * TILE + threadIdx.x;  // swapped block indices
  y = blockIdx.x * TILE + threadIdx.y;
  for (int j = 0; j < TILE; j += ROWS)
    if (x < height && y + j < width)
      out[(y + j) * height + x] = tile[threadIdx.x][threadIdx.y + j];
}
// launch: dim3 block(TILE, ROWS); dim3 grid((width+TILE-1)/TILE, (height+TILE-1)/TILE);

Both reads and writes are coalesced; the column read from shared memory is conflict-free because of the padding.

Warp-Level Reduction

__inline__ __device__ float warp_sum(float v) {
  for (int offset = 16; offset > 0; offset >>= 1)
    v += __shfl_down_sync(0xffffffffu, v, offset);
  return v;  // lane 0 holds the warp total
}

Shuffles exchange registers without shared memory. Use CUB (cub::BlockReduce, cub::DeviceReduce) instead of hand-rolling in production.

Build & Profile

nvcc -O3 -arch=sm_90 -lineinfo -o app app.cu        # -lineinfo maps SASS to source in Nsight
compute-sanitizer --tool memcheck ./app            # out-of-bounds, races (--tool racecheck)
nsys profile -o timeline ./app                     # timeline: transfers, launches, overlap
ncu --set full -o report ./app                     # per-kernel: throughput, stalls, occupancy

Read in Nsight Compute: "GPU Speed Of Light" (memory vs compute % of peak), "Memory Workload Analysis" (sectors per request), "Warp State Statistics" (stall reasons), "Occupancy".

Decision Table

| Symptom (ncu) | Likely cause | Fix |

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

| DRAM throughput near peak, SM low | memory-bound | Fuse kernels, reuse via smem, lower precision, vectorized loads (float4) |

| Many sectors per request | uncoalesced access | Change layout (SoA), transpose via smem |

| Shared-memory bank conflicts | strided smem access | Padding or swizzled layout |

| Low achieved occupancy | registers/smem per block too high | __launch_bounds__, smaller tiles |

| Stall "Long Scoreboard" dominant | waiting on global loads | More ILP, prefetch, cp.async pipelining (sm_80+) |

| Tensor-heavy math on FP32 cores | missed tensor cores | Use cuBLAS/CUTLASS or WMMA/MMA with FP16/BF16/TF32 |

Anti-Patterns

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.