mindspore-ai/akg/cuda-c-api
Claude Code and agent skill by mindspore-ai/akg
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
Install
npx vskill@latest install mindspore-ai/akg/cuda-c-apiSKILL.md
The same SKILL.md was published earlier as mindspore-ai/akg/cuda-c-api.
CUDA C API 参考手册
本文档提供 CUDA C 核心编程接口的详细参考,包括函数签名、参数说明和使用示例。
1. 函数修饰符
__global__
- 功能: 标记为可在 GPU 上执行的内核函数
- 调用: 只能从主机代码调用,使用
<<<>>>语法启动 - 返回值: 必须为
void
__device__
- 功能: 标记为在 GPU 上执行的设备函数
- 调用: 只能从其他
__device__或__global__函数调用 - 用途: 内核内部的辅助函数
__host__
- 功能: 标记为在 CPU 上执行的函数(默认)
- 调用: 只能从主机代码调用
__host__ __device__
- 功能: 同时在 CPU 和 GPU 上可用
- 用途: 通用工具函数
2. 内存类型修饰符
__shared__
- 功能: 声明线程块内共享的内存
- 生命周期: 与线程块相同
- 访问: 块内所有线程可读写
- 容量: 通常 48-164 KB/SM
__constant__
- 功能: 声明只读的常量内存
- 特点: 缓存优化,适合广播读取
- 设置: 通过
cudaMemcpyToSymbol从主机端设置
extern __shared__
- 功能: 动态分配的共享内存
- 大小: 在内核启动时通过第三个参数指定
- 启动:
kernel<<<grid, block, shared_mem_bytes>>>(args)
3. 内核启动语法
基本语法
- grid_size: 网格大小(
int或dim3) - block_size: 线程块大小(
int或dim3) - shared_mem_bytes: 动态共享内存大小(可选,默认 0)
- stream: CUDA 流(可选,默认 0)
dim3 类型
- 用途: 多维网格和线程块配置
- 默认: 未指定的维度默认为 1
4. 线程和块索引系统
块索引变量
- 类型:
uint3 - 用途: 确定当前线程块在网格中的位置
线程索引变量
- 类型:
uint3 - 用途: 确定当前线程在线程块中的位置
块维度信息
- 类型:
dim3 - 用途: 获取线程块的维度信息
网格维度信息
- 类型:
dim3 - 用途: 获取网格的维度信息
5. 内存管理 API
cudaMalloc(devPtr, size)
- 参数: 设备指针的地址, 分配字节数
- 返回:
cudaError_t错误状态码 - 功能: 在 GPU 上分配全局内存
cudaFree(devPtr)
- 参数: 设备内存指针
- 返回:
cudaError_t错误状态码 - 功能: 释放 GPU 全局内存
cudaMemcpy(dst, src, count, kind)
- 参数: 目标指针, 源指针, 字节数, 传输方向
- 返回:
cudaError_t错误状态码 - 传输方向:
cudaMemcpyHostToDevice: H2DcudaMemcpyDeviceToHost: D2HcudaMemcpyDeviceToDevice: D2D
cudaMemset(devPtr, value, count)
- 参数: 设备指针, 填充值(按字节), 字节数
- 功能: 将设备内存设为指定值
6. 线程同步机制
__syncthreads()
- 功能: 等待块内所有线程到达同步点
- 用途: 确保共享内存数据一致性
- ⚠️ 注意: 块内所有线程都必须执行到此同步点,否则导致死锁
__threadfence()
- 功能: 确保当前线程的内存写入对所有线程全局可见
- 用途: 跨线程块的数据可见性保证
__threadfence_block()
- 功能: 确保当前线程的内存写入对同块线程可见
- 用途: 块内的内存可见性保证
7. 原子操作 API
atomicAdd(address, val)
- 功能: 线程安全的加法操作
- 支持类型:
int,unsigned int,float,double(Compute Capability 6.0+)
atomicMax / atomicMin
- 功能: 线程安全的最大值/最小值更新
- 支持类型:
int,unsigned int
atomicCAS(address, compare, val)
- 功能: 比较并交换(Compare-And-Swap)
- 返回: 原始值
atomicExch(address, val)
- 功能: 原子交换操作
- 返回: 原始值
8. 数学运算函数
标准数学函数
快速数学函数(精度略低但速度更快)
类型转换
9. Warp 级操作(Compute Capability 3.0+)
__shfl_sync / __shfl_down_sync
- 功能: Warp 内线程间直接数据交换
- 参数: mask(参与线程掩码), 源值, 源 lane / 偏移量
10. PyTorch 集成 API
torch::Tensor 操作
常用 torch::Tensor 方法
使用建议
- 边界检查: 所有数组访问前必须检查边界
- 原子操作: 仅在必要时使用,有性能开销
- 快速数学函数: 精度要求不高时使用
__expf等加速 - 共享内存: 使用前后必须
__syncthreads()同步 - 错误检查: 每个 CUDA API 调用后检查返回值
- 内存对齐: 确保数据按合适边界对齐(128 字节)
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingakg_agents/python/akg_agents/op/resources/skills/cuda-c/guides/cuda-c-api/SKILL.md
Popularity
- Repository stars
- 259
- Repository forks
- 48
- 7-day trend score
- 28
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/mindspore-ai/akg/cuda-c-api)