跳到正文
cccskills
免费在 GitHub 公开

cuda

Use when writing CUDA kernels, managing the thread, block, and grid hierarchy, tiling shared memory, using streams, setting nvcc flags, or using Thrust. Not for kernel debugging: use cuda-debugging.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(2)

  • SKILL.md7.1 KB
  • agents/openai.yaml210 B

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

CUDA

Contract

FieldBound contract
TriggerCUDA C or C++ kernel code is being written, optimized, or reviewed: launch configuration, memory hierarchy, streams, nvcc flags, Thrust, or known performance traps such as warp divergence and uncoalesced access.
AuthorityRead-only. The skill emits guidance and code; it writes no files. Nothing to roll back. No remote mutation.
Side effectGuidance, code samples, and flag recommendations in chat output.
DoneA launch configuration, memory plan, build command, and pitfall review exist for the kernel in question, each grounded in the toolkit version the project pins.

Inputs

  1. Kernel goal (required): the operation to move to the GPU, or the existing kernel under review.
  2. GPU model (required if code must run on it): compute capability from nvidia-smi --query-gpu=compute_cap.
  3. Toolkit version (required if not inferrable): nvcc --version. The grounded current stable is CUDA Toolkit 13.3 Update 1.
  4. Build system (optional): existing nvcc or CMake CUDA flags.

Procedure

  1. Identify the GPU compute capability and toolkit version. CUDA 13.x supports Turing (sm_75) and later; older targets require an older toolkit. Done when: the sm_XX target and toolkit line are recorded, or the mismatch is reported.
  2. Write or review the launch: threads grouped into blocks, blocks into a grid. Cap a block at 1024 threads. Compute blocks = (n + threads - 1) / threads and guard every load and store with if (i < n). Check each launch with cudaGetLastError() and each sync with a second check. Done when: the launch math covers every element and both error checks are present.
  3. Place data at the right level of the hierarchy. Registers hold per-thread scalars. Shared memory holds tiles reused across the block. Global memory holds large arrays, read coalesced. Constant memory holds broadcast read-only data. Latency numbers below are order-of-magnitude guides on current parts, not constants:
MemoryScopeTypical latencyUse
RegistersPer thread~1 cycleScalars, indices
Shared (__shared__)Per blocklow tens of cyclesTiled data
GlobalDevicehundreds of cyclesLarge arrays, coalesced
Constant (__constant__)Device, cachedfast broadcastParameters, lookup tables

Done when: every buffer has an assigned level and tiles reuse shared memory.

  1. Tile matrix work through shared memory and __syncthreads():
#define TILE 16

__global__ void matmul_tiled(const float *A, const float *B, float *C, int N) {
    __shared__ float As[TILE][TILE];
    __shared__ float Bs[TILE][TILE];

    int row = blockIdx.y * TILE + threadIdx.y;
    int col = blockIdx.x * TILE + threadIdx.x;
    float sum = 0.0f;

    for (int t = 0; t < (N + TILE - 1) / TILE; t++) {
        As[threadIdx.y][threadIdx.x] = (row < N && t * TILE + threadIdx.x < N)
            ? A[row * N + t * TILE + threadIdx.x] : 0.0f;
        Bs[threadIdx.y][threadIdx.x] = (col < N && t * TILE + threadIdx.y < N)
            ? B[(t * TILE + threadIdx.y) * N + col] : 0.0f;
        __syncthreads();

        for (int k = 0; k < TILE; k++)
            sum += As[threadIdx.y][k] * Bs[k][threadIdx.x];
        __syncthreads();
    }
    if (row < N && col < N)
        C[row * N + col] = sum;
}

Done when: each tile load is bounds-checked and the sync points separate load from use.

  1. Move copies off the critical path with streams and pinned memory. cudaMallocHost pins host buffers so cudaMemcpyAsync overlaps compute with DMA. Assign each stage its own cudaStream_t and pass the stream as the fourth launch parameter. Synchronize with cudaStreamSynchronize, not the whole device, when streams run independently. Done when: copies and kernels overlap across streams or a single-stream design is justified.
  2. Select nvcc flags for the build purpose:
FlagEffect
-O3Optimize device code
-arch=sm_XXTarget one architecture
-gencode arch=compute_XX,code=sm_XXFat binary for several GPUs
-GDisable optimizations for cuda-gdb
-lineinfoSource-line correlation in profiles
--use_fast_mathFaster, less precise math intrinsics
-maxrregcount=NCap registers per thread
-Xcompiler -fopenmpPass host flags to the host compiler

Done when: the build command targets the recorded sm_XX and omits -G outside debugging.

  1. Estimate occupancy before tuning by hand. Call cudaOccupancyMaxActiveBlocksPerMultiprocessor, or read sm__warps_active.avg.pct_of_peak_sustained_active in Nsight Compute. Treat a low achieved-occupancy reading as a signal to check register pressure, shared memory size, and block size against the measured stall reasons, not as a failure on its own. A compute-bound kernel with enough instruction-level parallelism can run well at a low occupancy; a latency-bound streaming kernel usually cannot. Done when: occupancy is measured and any change made to registers, shared memory, or block size is tied to a named limiting factor.
  2. Use Thrust for sort, scan, and reduce instead of hand-written kernels:
#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <thrust/reduce.h>

thrust::device_vector<int> d_vec(1000000);
thrust::sort(d_vec.begin(), d_vec.end());
int sum = thrust::reduce(d_vec.begin(), d_vec.end());

Write custom kernels for fused, domain-specific operations. Done when: every primitive that Thrust covers is delegated or the custom kernel is justified.

  1. Review the kernel against the two common traps. Divergence: threads in a warp of 32 execute in lockstep, so a branch on threadIdx serializes the paths. Uncoalesced access: consecutive threads must read consecutive addresses; a strided read like data[threadIdx.x * stride] defeats the memory system. Done when: no hot branch splits a warp and every hot load is coalesced.

Failure and recovery

Failure classBehavior
GPU or toolkit version unknownAsk for nvidia-smi --query-gpu=compute_cap and nvcc --version output. Guidance stays generic until both are known.
cudaErrorIllegalAddress (700)Out-of-bounds device pointer. Re-run under compute-sanitizer --tool memcheck and add bounds checks.
cudaErrorLaunchTimeout (702)Kernel exceeded the watchdog limit, primarily Windows TDR. Split the kernel or shrink the work per launch.
no kernel image availableBinary lacks this GPU's sm_XX. Rebuild with the recorded compute capability.
Copies dominate the timelineHost buffers are pageable. Pin them with cudaMallocHost.
CPU and GPU results differRace on shared or global memory. Audit __syncthreads() placement and use atomics for cross-thread reductions.

Output

Guidance in chat: the launch configuration, the memory plan per buffer, the nvcc build command for the recorded target, the occupancy measurement with its limiting factor, and the pitfall review. Each recommendation names the toolkit and architecture it assumes.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Use when explaining System V AMD64, ARM AAPCS, RISC-V psABI, stack frames, variadic calls, or FFI register rules. Not for the Rust FFI binding layer: use rust-ffi.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when configuring ADC sampling time, DMA-driven ADC, calibration, or DAC channel setup on bare-metal MCUs. Not for the DMA stream itself: use dma-baremetal.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

af-xdp

免费

Use when creating AF_XDP sockets, configuring UMEM and XSK rings, writing an XDP redirect program, or choosing copy versus zero-copy mode. Not for full kernel bypass: use dpdk.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when a completed session needs an agent-environment retrospective. Not for an engineering retrospective from telemetry: use engineering-retrospective.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

Use when a redacted, trimmed agent transcript must be appended to a GitHub PR or issue body, with human approval and preview. Not for automated or model-initiated insertion.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

agents-md

免费

Use when a repo needs agent setup, AGENTS.md added or made lean, CLAUDE.md audited, or agent instructions scored or pruned. Not for remote, credential, publish, deploy, or irreversible changes.

中文简介正在准备中。当前显示原文说明。

OutlineDriven/odin-claude-plugin372026年9月29日 更新

查看 OutlineDriven 的全部技能

报告此技能的问题