Skip to content
Back to skills

Ascendc Simt Best Practices

ASecurity

AscendC SIMT 最佳实践与 API 导航。提供 SIMT 算子开发的实践经验总结和专有 API 分类索引:VF函数声明与调用、线程排布模式、索引计算API、数据搬入搬出、计算指令映射、多核同步、核内共享内存、TensorList处理、调测接口、内置宏、编译运行。触发:开发SIMT算子kernel代码、设计SIMT编程模式、查询SIMT API分类或线程排布。

  • 21 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added November 7, 2026
code-qualityc++api

Works with

  • api

Security analysis

A100/100

Pro scans all 16 files and shows the line behind each finding

Scanned November 24, 2026

npx -y skills add hicann/cannbot-skills --skill ascendc-simt-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Ascendc Simt Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Ascendc Simt Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hicann-ascendc-simt-best-practices/badge)](https://www.skillsdirectory.com/skills/hicann-ascendc-simt-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: ascendc-simt-best-practices
description: "AscendC SIMT 最佳实践与 API 导航。提供 SIMT 算子开发的实践经验总结和专有 API 分类索引:VF函数声明与调用、线程排布模式、索引计算API、数据搬入搬出、计算指令映射、多核同步、核内共享内存、TensorList处理、调测接口、内置宏、编译运行。触发:开发SIMT算子kernel代码、设计SIMT编程模式、查询SIMT API分类或线程排布。"
---

# AscendC SIMT 最佳实践

> 本 skill 提供基于实际算子开发经验提炼的 SIMT 编程范式总结,以及 SIMT 专有 API 的分类索引和速查。SIMT 基础概念(核函数、线程架构、内存层级)请通过 `ascendc-docs-search` skill 查阅 `$ASC_DEVKIT_DIR/docs/zh/guide/programming_guide/programming_model/ai_core_simt_programming/`。

---

## API 分类索引表

| 类别 | $ASC_DEVKIT_DIR 目录 | 功能概述 | 头文件 |
|------|-----------------|----------|--------|
| 总览与核函数 | `SIMT-API/overview.md` | VF启动机制、头文件、混合编程辅助函数 | common_functions.h |
| 同步与内存栅栏 | `SIMT-API/sync_and_memory_fence/` | asc_syncthreads/asc_threadfence/asc_threadfence_block | device_sync_functions.h |
| 数学函数 | `SIMT-API/math_functions/` | 三角/指数/对数/幂/误差/特殊/基础算术/整数工具 | math_functions.h |
| 原子操作 | `SIMT-API/atomic_operations/` | add/sub/exch/max/min/inc/dec/cas/and/or/xor | device_atomic_functions.h |
| Warp函数 | `SIMT-API/Warp_functions/` | all/any/ballot/activemask/shfl/shfl_up/down/xor/reduce | device_warp_functions.h |
| 地址空间谓词 | `SIMT-API/address_space_predicate_functions/` | __isGlobal/__isShared/__isConstant/__isLocal | asc_simt.h |
| 地址空间转换 | `SIMT-API/address_space_conversion_functions/` | __cast_to_xxx 地址空间转换 | asc_simt.h |
| 访存函数 | `SIMT-API/memory_access_functions/` | asc_ldcg/asc_ldca/asc_stcg/asc_stwt | device_functions.h |
| 协作组 | `SIMT-API/cooperative_groups/` | 协作组编程模型 | asc_simt.h |
| SIMT编程简介 | `SIMT-API/SIMT_programming_intro/` | SIMT编程概念和入门 | asc_simt.h |
| 混合编程简介 | `SIMT-API/SIMD_SIMT_hybrid_programming_intro/` | SIMD/SIMT混合编程指导 | asc_simt.h |

> **查阅完整 API 文档**:使用 `ascendc-docs-search` skill 查阅 SIMT API 官方文档(`find "$ASC_DEVKIT_DIR/docs/zh/api/SIMT-API/" -name "{APIName}*.md"`)。

---

## 编程阶段分类索引

| 编程阶段 | 核心主题 | 参考文档 | 关键要点 |
|---------|---------|---------|---------|
| **函数定义** | VF 函数声明 | [vf-declaration.md](references/vf-declaration.md) | `__simt_vf__` 标记、LAUNCH_BOUND、参数类型、三层结构 |
| **函数调用** | VF_CALL 启动 | [vf-call.md](references/vf-call.md) | Dim3 线程数、Add 完整示例、参数传递 3 方式、GM_ADDR 直接转换 |
| **线程排布** | 两种排布模式 | [thread-stride-pattern.md](references/thread-stride-pattern.md) | 模式一全局 grid-stride、模式二核内分块+线程步进、索引分解、3 种线程↔数据映射粒度(含每 warp 一输出/warp 协作) |
| **索引计算** | API 与全局索引 | [index-calculation.md](references/index-calculation.md) | GetThreadIdx/GetBlockIdx 系列、全局索引与 stride、magic 快除与官方 magic/shift 公式、索引位宽 32/64 自适应 |
| **数据搬运** | GM 直接 / UB 中转 | [data-transfer.md](references/data-transfer.md) | 选型决策流、GM 直读/UB 中转/缓存复用;**首要判据是访存粒度而非是否逐元素**:连续 / 规则访问可直接 GM 直读(硬件合并),GM 经 Data Cache 以 **128B 扇区**为最小传输单位,读放大 = 128B/单元素字节(int8 128x、fp16/bf16 64x、int32 32x、int64 16x);单次随机访问连续字节 `e≥128B` 可直读、`e<128B` 读放大 `128/e`,**不应默认逐元素 GM 直读;调线程数/迭代序/缓存提示不能改变放大倍数** |
| **随机访存** | 写侧(散写)策略 | [random-access-patterns.md](references/random-access-patterns.md) | 同址写冲突(数据竞争,**无原子时可能丢更新**);**写侧首要目标是减少同址写次数而非优化单次写**;按 **dtype 原子能力 + 确定性需求** 选原子直写 / **workspace 升位宽三段式(目标 dtype 无原子指令或低精度:cast→宽类型原子→cast 回)** / 排序归并单写(**需确定性或重复度高时,每唯一目标只写一次**) / 按目标分区(按核/槽位切分且无跨核同址);**重复度高时不应裸用原子直写——原子在目标地址排队,重复度越高串行化越重**;可用**分层归约**(线程块私有 UB 局部归约 → 整块汇总 GM)进一步降竞争 |
| **计算指令** | C++ 运算符映射 | [compute-operators.md](references/compute-operators.md) | 优先运算符写法、显式 API 仅特殊需求 |
| **多核同步** | workspace + SyncAll | [multi-core-sync.md](references/multi-core-sync.md) | 多核 workspace 交换、VF 间同步、同步原语全景、event 依赖链、SetScheduleMode(1) |
| **核内共享** | UB buffer 共享 | [intra-core-shared.md](references/intra-core-shared.md) | TBuf VECCALC、__ubuf__/__local_mem__ 指针、线程间竞争与原子、UB 使用约束 |
| **动态输入** | TensorList 解析 | [tensorlist-handling.md](references/tensorlist-handling.md) | ListTensorDesc 布局、Host 侧获取、Device 侧两种解析方式、选型建议 |

---

## 核心编程范式速查

```cpp
// 1. VF 声明
template <typename T>
__simt_vf__ __aicore__ LAUNCH_BOUND(512) inline void OpSimt(
    uint64_t count, __gm__ T* x, __gm__ T* y);

// 2. VF 调用
AscendC::Simt::VF_CALL<OpSimt<T>>(AscendC::Simt::Dim3(512), count, (__gm__ T*)x, (__gm__ T*)y);

// 3. 线程排布 for 循环
for (uint64_t i = AscendC::Simt::GetBlockIdx() * AscendC::Simt::GetThreadNum() + AscendC::Simt::GetThreadIdx();
     i < count; i += AscendC::Simt::GetThreadNum() * AscendC::Simt::GetBlockNum()) { ... }
```

---

## SIMT vs SIMD 差异速查

| 维度 | SIMT | SIMD |
|------|------|------|
| **编程模型** | 多线程并行,每线程独立执行 | 单核向量指令,整块数据并行 |
| **函数标记** | `__simt_vf__` | 无特殊标记 |
| **启动方式** | `Simt::VF_CALL<VF>(Dim3(N), args)` | 直接在 kernel 内调用 |
| **线程索引** | GetThreadIdx / GetBlockIdx | 无线程概念,靠 tile 循环 |
| **数据访问** | GM 直接读写(`__gm__ T*`) | DataCopyPad 搬到 UB/L0C |
| **计算方式** | C++ 运算符(`y[i]=x[i]+1`) | 向量 API(`Add(xLocal, yLocal, zLocal)`) |
| **核内共享** | UB buffer + `__ubuf__` 指针 | TQue 流水线 queue |
| **多核同步** | `SyncAll()` + `SetScheduleMode(1)` | 流水线 set/wait flag |
| **适用场景** | 离散 / 不规则访存、**数据依赖的逐元素迭代**(循环轮数依赖元素自身值:GCD 收敛循环 / 位扫描 ctz·ffs 等——SIMD 无法提前确定向量指令循环轮数) | 规则向量、高吞吐归约(含单次数据依赖 Select / 掩码) |

### SIMT 适用场景

实现载体选型时按以下两个维度逐条评估,命中任一维度且目标芯片支持 SIMT(DAV_3510)时,建议推荐 SIMT:

**SIMT 适用场景**:
- **离散 / 不规则访存**:Gather/Scatter、Hash 插入、随机数、排序(含原子操作)
- **数据依赖的逐元素迭代**:每个元素的迭代轮数或分支路径依赖该元素自身的值(GCD 的 Stein 收敛循环、位扫描 ctz / ffs 等)——SIMD 无法提前确定向量指令的循环次数,标量线程天然适配

> 单次的数据依赖分支 / 选择(`?:`、除零特判 `Select` 等)SIMD 掩码即可表达,不构成命中;访存是否规则(连续 / 离散)本身也不构成排除 SIMT 的理由。

---

## 参考资料索引

`references/` 按需加载:

**编程最佳实践:**

- **vf-declaration.md** -- VF 函数声明规范:`__simt_vf__` 标记、`__simt_callee__`→`__simt_vf__`→`__aicore__` 三层结构、参数类型支持表、LAUNCH_BOUND 编译期常量约束(线程数分档见 tiling-design)
- **vf-call.md** -- VF_CALL 启动语法与完整示例:Add 算子模板、参数传递 3 方式、GM_ADDR 直接转换
- **thread-stride-pattern.md** -- 线程排布两种模式:模式一全局 grid-stride、模式二核内分块+线程步进(分核与对齐见 tiling-design)、索引分解、3 种线程↔数据映射粒度(一线程一元素 / 一线程一段一行 / 每 warp 一输出,含 warp 协作归约)、Select 示例
- **index-calculation.md** -- 索引计算 API:GetThreadIdx/GetThreadNum/GetBlockIdx/GetBlockNum、全局索引与 stride、Simt::UintDiv magic 快除与官方 magic/shift 公式、索引位宽选择(默认 32 位/按需 64 位;位宽判定与 tilingKey 编码见 tiling-design,kernel 侧 ADDR_T 模板化)
- **data-transfer.md** -- 数据搬入搬出方案选择(聚焦连续/规则访问):选型决策流、GM 直接计算、UB 中转与缓存复用(UB/DCache 分配见 tiling-design);含细粒度随机访存机理(读侧 / 写侧)与判据
- **random-access-patterns.md** -- 细粒度随机访存(写侧 / 散写):同址写冲突(数据竞争)机理,按 dtype 原子能力与确定性需求的策略选择(原子直写 / workspace 升位宽三段式 / 排序归并去重 / 按目标分区);重复度高或需确定性时以「先分组后单写者」消除原子串行化;分层归约(片上聚合 → 整块汇总)降低原子竞争;读侧处方边界(UB 驻留仅在工作块可规整且 ≤UB 时成立,全域无局部性随机读归设计级布局评估)
- **compute-operators.md** -- 计算指令映射:C++ 运算符优先写法、显式 API 适用场景
- **multi-core-sync.md** -- 多核同步机制:多核 workspace 交换、VF 间同步、同步原语全景、SIMT 与 Vector/DMA 的 event 依赖链、SetScheduleMode(1)
- **intra-core-shared.md** -- 核内共享内存:TBuf VECCALC 申请、`__ubuf__`/`__local_mem__` 指针传递与等价性、按索引写 UB 示例、线程间竞争与原子 dtype 注意事项
- **tensorlist-handling.md** -- TensorList 动态输入处理:ListTensorDesc 内存布局、Host 侧获取 shape/dtype、Device 侧两种解析方式、选型建议

**API 导航与速查:**

- **overview.md** -- SIMT API 总览:VF启动机制、头文件映射、核函数定义、混合编程辅助函数速查
- **debug-api.md** -- 调测接口速查:printf/assert/__trap 使用要点
- **macro-api.md** -- 内置宏速查:特殊值(INF/NAN/MAX/MIN)、数学常数(pi/e/ln2等)
- **编译与运行.md** -- 异构编译场景编译与运行速查:SIMT/AI CPU 算子的 bisheng 命令行与 CMake 编译命令、`<<<...>>>` 内核调用符

**完整 API 文档:**

- **$ASC_DEVKIT_DIR** -- 通过 `ascendc-docs-search` skill 查阅 SIMT API 文档

Files in this skill

  • SKILL.md9 KB
  • evals/evals.json4.4 KB
  • references/compute-operators.md1 KB
  • references/data-transfer.md6.6 KB
  • references/debug-api.md13.8 KB
  • references/index-calculation.md3.6 KB
  • references/intra-core-shared.md2.5 KB
  • references/macro-api.md1.6 KB
  • references/multi-core-sync.md2.5 KB
  • references/overview.md5 KB
  • references/random-access-patterns.md3.6 KB
  • references/tensorlist-handling.md3.4 KB
  • references/thread-stride-pattern.md6.6 KB
  • references/vf-call.md2.2 KB
  • references/vf-declaration.md2.2 KB
  • references/编译与运行.md7.8 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in code-quality →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.