Merged
SIMT新增memory_fence样例 #1923
shaonaiteAdd on Apr 30
SIMT新增memory_fence样例 #1923
Merged
shaonaiteAdd on Apr 30
Total: 8 files changed+578-0
@@ -0,0 +1,11 @@
1+# Sync Instruction样例介绍
2+ 
3+## 概述
4+ 
5+基于Ascend C编程的基础样例,展示SIMT同步与内存栅栏接口的使用方法。
6+ 
7+## 样例列表
8+ 
9+| 目录名称 | 功能描述 |
10+| ------------------------------------------------------------ | ---------------------------------------------------- |
11+| [memory_fence](./memory_fence/) | 基于SIMT编程方式实现归约求和样例,展示asc_syncthreads()和asc_threadfence()接口的使用方法。|
@@ -0,0 +1,38 @@
1+# ----------------------------------------------------------------------------------------------------------
2+# This program is free software, you can redistribute it and/or modify it.
3+# Copyright (c) 2026 Huawei Technologies Co., Ltd.
4+# This file is a part of the CANN Open Software.
5+# Licensed under CANN Open Software License Agreement Version 2.0 (the "License").
6+# Please refer to the License for details. You may not use this file except in compliance with the License.
7+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
8+# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
9+# See LICENSE in the root of the software repository for the full text of the License.
10+# ----------------------------------------------------------------------------------------------------------
11+ 
12+cmake_minimum_required(VERSION 3.16)
13+ 
14+find_package(ASC REQUIRED)
15+ 
16+project(kernel_samples LANGUAGES ASC CXX)
17+ 
18+set(SCENARIO_NUM "1" CACHE STRING "Scene number (1-2)")
19+ 
20+if(SCENARIO_NUM EQUAL 1)
21+ set(SCENARIO_SRC sync_barrier.asc)
22+elseif(SCENARIO_NUM EQUAL 2)
23+ set(SCENARIO_SRC memory_fence.asc)
24+endif()
25+ 
26+add_executable(demo
27+ ${SCENARIO_SRC}
28+)
29+ 
30+# ======================================================================================
31+# NPU 编译选项配置
32+#
33+# 说明:
34+# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。
35+# ======================================================================================
36+target_compile_options(demo PRIVATE
37+ $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510 --enable-simt>
38+)
@@ -0,0 +1,168 @@
1+# SIMT编程实现归约求和样例
2+ 
3+## 概述
4+ 
5+本样例基于 Ascend C SIMT 编程方式实现一维 `float` 输入的归约求和,通过 2 个递进场景展示线程块内同步与线程块间内存顺序控制的典型用法。
6+ 
7+2 个场景分别对应小 shape 输入、大 shape 输入下的归约求和,重点说明 `asc_syncthreads()` 和 `asc_threadfence()` 在不同归约规模下的使用方法。
8+ 
9+## 支持的产品
10+ 
11+- Ascend 950PR/Ascend 950DT
12+ 
13+## 支持的CANN软件版本
14+- \> CANN 9.0.0
15+ 
16+## 目录结构
17+ 
18+```text
19+├── memory_fence
20+│ ├── sync_barrier.asc # 场景1:小shape归约样例,展示 asc_syncthreads() 的线程块内同步用法
21+│ ├── memory_fence.asc # 场景2:大shape归约样例,展示 asc_threadfence() 与原子计数的线程块间协作用法
22+│ ├── CMakeLists.txt # 编译工程文件
23+│ └── README.md
24+```
25+ 
26+## 样例描述
27+ 
28+### 样例功能
29+ 
30+<table border="1" align="center">
31+ <tr>
32+ <td align="center">SCENARIO_NUM 取值</td>
33+ <td align="center">功能场景</td>
34+ <td align="center">场景说明</td>
35+ <td align="center">对应文件</td>
36+ </tr>
37+ <tr>
38+ <td align="center">1</td>
39+ <td align="center">小shape归约场景</td>
40+ <td align="center">使用 asc_syncthreads() 完成线程块内归约同步</td>
41+ <td align="center">sync_barrier.asc</td>
42+ </tr>
43+ <tr>
44+ <td align="center">2</td>
45+ <td align="center">大shape归约场景</td>
46+ <td align="center">使用 asc_threadfence() 完成多线程块间局部和的合并求和</td>
47+ <td align="center">memory_fence.asc</td>
48+ </tr>
49+</table>
50+ 
51+本样例通过 `SCENARIO_NUM` 控制构建分支,2 个场景按输入规模大小分类:场景1演示小shape输入下的归约求和与 `asc_syncthreads()` 同步用法。当输入元素较少时,仅需1个block 即可覆盖全部数据,无需多block间同步;场景2扩展到大 shape 输入的多 block 分段归约,引入 `asc_threadfence()` 完成 block 间合并。
52+ 
53+### 样例规格
54+ 
55+#### SCENARIO_NUM=1(小shape归约场景)
56+ 
57+- 样例功能:
58+ 
59+ 对 1024 个 `float` 输入元素在单个线程块内执行**归约求和**。
60+ 
61+- 样例规格:
62+ 
63+ <table border="1" align="center">
64+ <tr><td rowspan="1" align="center">样例类型(OpType)</td><td colspan="4" align="center">SyncBarrierSingleBlock</td></tr>
65+ <tr><td rowspan="2" align="center">样例输入</td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td><td align="center">format</td></tr>
66+ <tr><td align="center">input</td><td align="center">[1024]</td><td align="center">float</td><td align="center">ND</td></tr>
67+ <tr><td rowspan="1" align="center">样例输出</td><td align="center">output</td><td align="center">[1]</td><td align="center">float</td><td align="center">ND</td></tr>
68+ </table>
69+ 
70+- 数据切分:
71+ 
72+ - gridDim:(1, 1, 1)
73+ - blockDim:(1024, 1, 1)
74+ - 单线程处理:1 个输入元素
75+ 
76+#### SCENARIO_NUM=2(大shape归约场景)
77+ 
78+- 样例功能:
79+ 
80+ 对 `1024 * 1024` 个 `float` 输入元素执行**分段归约求和**,每个线程块先完成本线程块负责片段的**局部求和**,再由单个线程块对所有线程块的局部和做第二次归约求和,得到最终总和。
81+ 
82+- 样例规格:
83+ 
84+ <table border="1" align="center">
85+ <tr><td rowspan="1" align="center">样例类型(OpType)</td><td colspan="4" align="center">ThreadFenceMultiBlock</td></tr>
86+ <tr><td rowspan="2" align="center">样例输入</td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td><td align="center">format</td></tr>
87+ <tr><td align="center">input</td><td align="center">[1024 * 1024]</td><td align="center">float</td><td align="center">ND</td></tr>
88+ <tr><td rowspan="1" align="center">样例输出</td><td align="center">output</td><td align="center">[1]</td><td align="center">float</td><td align="center">ND</td></tr>
89+ </table>
90+ 
91+- 数据切分:
92+ 
93+ - gridDim:(1024, 1, 1)
94+ - blockDim:(1024, 1, 1)
95+ - 单线程处理:1 个输入元素
96+ 
97+### 样例实现
98+ 
99+#### 1:小shape归约场景
100+ 
101+单个线程块内的每个线程先读取一个输入元素,通过**两阶段归约求和**得到最终结果:
102+ 
103+- **阶段 1(warp 内归约)**:调用 `asc_reduce_add()` 接口对当前warp内所有线程的值做归约,得到warp内数据的和,写入共享内存。
104+- **阶段 2(block 级顺序累加)**:跨warp同步后,0号线程按顺序将各warp的部分和累加,得到最终结果。
105+ 
106+以 128 个元素(4 个 warp)为例,两阶段归约过程如图 1 所示:
107+ 
108+<p align="center">
109+ <img src="./figure/两阶段归约求和.png" width="50%">
110+ </p>
111+<p align="center">
112+图1:两阶段归约过程示意图
113+</p>
114+ 
115+阶段1中,各warp通过 `asc_reduce_add()` 完成warp内求和并将结果写入共享内存;阶段2中,0号线程依次读取各warp的部分和进行累加,得到最终结果。两个阶段之间需调用 `asc_syncthreads()` 同步。
116+ 
117+`asc_syncthreads()` 用于阻塞当前线程块的所有线程,直到所有线程都执行到该同步点位置。在本场景中,`asc_syncthreads()` 保证所有warp的首线程已将warp内归约结果写入共享内存,后续block级顺序累加才能读取完整的共享内存数据。
118+ 
119+#### 2:大shape归约场景
120+ 
121+当输入元素总数较大时,需要**多个线程块**分段处理。
122+ 
123+每个线程块先在块内完成上述两阶段局部归约,再由 `tid = 0` 的线程将本线程块的局部和写入 `block_sums[blockIdx.x]`。写入后执行 `asc_threadfence()`,再将全局计数器加 1。最后一个对计数器加 1 的线程块(即 `ticket = gridDim.x - 1` 的线程块)会读取 `block_sums` 并执行第二次两阶段归约,输出最终结果。
124+ 
125+以 4 个线程块为例,跨线程块协作过程如图2所示:
126+ 
127+<p align="center">
128+ <img src="./figure/跨线程块协作.png" width="75%">
129+ </p>
130+<p align="center">
131+图2:跨线程块协作过程示意图
132+</p>
133+ 
134+上述流程中,多个线程块需要读写同一块全局内存 `block_sums`,可能造成数据竞争。
135+ 
136+`asc_threadfence()` 作为核间内存屏障,强制保证其前后的内存写操作对其他核可见且顺序不被重排序。因此各线程块必须按以下顺序执行:将局部和写入 `block_sums` → 递增原子计数器。这一顺序确保了最后一个线程块在读取 `block_sums` 执行全局归约时,一定能看到所有其他线程块已写入的部分和;若省略 `asc_threadfence()`,则可能读到脏数据导致结果错误。
137+ 
138+## 编译运行
139+ 
140+在本样例根目录下执行如下步骤,编译并执行算子。
141+- 配置环境变量
142+ 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。
143+ - 默认路径,root用户安装CANN软件包
144+ ```bash
145+ source /usr/local/Ascend/cann/set_env.sh
146+ ```
147+ 
148+ - 默认路径,非root用户安装CANN软件包
149+ ```bash
150+ source $HOME/Ascend/cann/set_env.sh
151+ ```
152+ 
153+ - 指定路径install_path,安装CANN软件包
154+ ```bash
155+ source ${install_path}/cann/set_env.sh
156+ ```
157+ 
158+- 样例执行
159+```bash
160+ SCENARIO_NUM=1 #取值为1、2
161+ mkdir -p build && cd build; # 创建并进入build目录
162+ cmake .. -DSCENARIO_NUM=$SCENARIO_NUM; make -j; # 编译工程
163+ ./demo # 执行样例
164+ ```
165+ 执行结果如下,说明精度对比成功。
166+ ```
167+ [Success] Case accuracy is verification passed.
168+ ```
@@ -0,0 +1,3 @@
1+version https://git-lfs.github.com/spec/v1
2+oid sha256:1bc99a495187b07c62217b2371c176fac1ba5f8c253b4a3f532ae95ccfb924d8
3+size 82488
@@ -0,0 +1,3 @@
1+version https://git-lfs.github.com/spec/v1
2+oid sha256:586b66b31af48a97454f34af73edc383e44850c23841cea794abdf7238fa349d
3+size 132119
@@ -0,0 +1,216 @@
1+/**
wangxiaozhi

在02_features目录下README.md新增api的介绍索引

likedislike
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+/* !
12+ * \file memory_fence.asc
13+ * \brief
14+ */
15+ 
16+#include <iostream>
17+#include <vector>
18+#include "acl/acl.h"
19+#include "simt_api/asc_simt.h"
20+ 
21+namespace {
22+ constexpr uint32_t THREAD_COUNT = 1024;
qixingkaizhangshunwei2
qixingkaiqixingkaiApr 30

这里加namespace是不是没啥用 是不是可以去掉

likedislike
shaonaite
shaonaite
May 6 Comments:
likedislike

参考下之前的样例,常量用namespace放置

likedislike
23+}
24+ 
25+__aicore__ inline void block_reduce(float* smem, uint32_t tid, float val) {
26+ uint32_t lane_id = laneid();
27+ uint32_t warp_id = tid / warpSize;
28+ 
29+ // warp内归约,asc_reduce_add返回当前warp所有线程val的总和
30+ val = asc_reduce_add(val);
31+ // 仅首线程写入共享内存
32+ if (lane_id == 0) {
33+ smem[warp_id] = val;
34+ }
35+ 
36+ asc_syncthreads(); // 跨warp同步
37+ 
38+ // block级顺序累加
39+ if (tid == 0) {
40+ for (uint32_t i = 1; i < blockDim.x / warpSize; i++) {
41+ val += smem[i];
42+ }
43+ smem[0] = val;
44+ }
45+}
46+ 
47+__global__ void __launch_bounds__(THREAD_COUNT) kernel_memory_fence(float* input, float* block_sums, float* output, uint32_t* counter, uint32_t input_total_length) {
48+ extern __ubuf__ float smem[];
49+ __ubuf__ uint32_t is_last;
50+ uint32_t tid = threadIdx.x;
51+ uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
52+ 
53+ float val = (idx < input_total_length) ? input[idx] : 0.0f;
54+ 
55+ // 每个block内部归约,得到该block的和
56+ block_reduce(smem, tid, val);
57+ 
58+ if (tid == 0) {
59+ block_sums[blockIdx.x] = smem[0];
60+ }
61+ 
62+ // 确保当前block对block_sums的写入对其他block可见
63+ asc_threadfence();
64+ 
65+ if (tid == 0) {
66+ uint32_t ticket = asc_atomic_add(counter, 1);
67+ is_last = (ticket == gridDim.x - 1);
68+ }
69+ 
70+ // block内同步:将is_last标志从0号线程广播到该block的所有线程
71+ asc_syncthreads();
72+ 
73+ // 最后一个block负责全局归约:将所有block的部分和汇总为最终结果
74+ if (is_last) {
75+ float my_sum = 0; // 当前线程负责累加的block部分和
76+ // 每个线程以blockDim.x为步长遍历block_sums数组,
77+ for (uint32_t i = tid; i < gridDim.x; i += blockDim.x) {
78+ my_sum += block_sums[i];
79+ }
80+ // 对my_sum做block内部归约,得到所有block部分和的总和
81+ block_reduce(smem, tid, my_sum);
82+ 
83+ if (tid == 0) {
84+ output[0] = smem[0];
85+ }
86+ }
87+}
88+ 
89+uint32_t verify_result(float output, float golden)
90+{
91+ std::cout << "Output: " << output << std::endl;
92+ std::cout << "Golden: " << golden << std::endl;
93+ 
94+ float abs_err = std::abs(output - golden);
95+ float rel_err = abs_err / std::abs(golden);
96+ 
97+ if (abs_err < 1e-4f && rel_err < 1e-4f) {
98+ std::cout << "[Success] Case accuracy verification passed." << std::endl;
99+ return 0;
100+ } else {
101+ std::cout << "[Failed] Case accuracy verification failed!" << std::endl;
102+ return 1;
103+ }
104+}
105+ 
106+ 
107+float host_block_reduce(std::vector<float>& data, uint32_t WARP_SIZE, uint32_t thread_count)
108+{
109+ uint32_t warp_count = thread_count / WARP_SIZE;
110+ 
111+ // warp内直接求和
112+ std::vector<float> warp_sums(warp_count, 0.0f);
113+ for (uint32_t w = 0; w < warp_count; w++) {
114+ uint32_t base = w * WARP_SIZE;
115+ for (uint32_t lane = 0; lane < WARP_SIZE; lane++) {
116+ warp_sums[w] += data[base + lane];
117+ }
118+ }
119+ 
120+ // 顺序累加
121+ float result = warp_sums[0];
pengjiahao96

warpCount可能等于0么?等于0 的话访问会溢出

likedislike
shaonaite
shaonaite
May 6 Comments:
likedislike
122+ for (uint32_t i = 1; i < warp_count; i++) {
123+ result += warp_sums[i];
124+ }
125+ 
126+ return result;
127+}
128+ 
129+int32_t main(int32_t argc, char* argv[])
130+{
131+ constexpr uint32_t input_total_length = 1024 * 1024;
132+ 
133+ uint32_t thread_num_per_block = THREAD_COUNT;
134+ uint32_t dyn_ubuf_size = thread_num_per_block / 32 * sizeof(float);
135+ uint32_t block_num = (input_total_length + thread_num_per_block - 1) / thread_num_per_block;
136+ 
137+ size_t input_total_byte_size = input_total_length * sizeof(float);
138+ size_t block_sums_total_byte_size = block_num * sizeof(float);
139+ size_t output_total_byte_size = 1 * sizeof(float);
140+ 
141+ std::vector<float> input(input_total_length);
142+ for (uint32_t i = 0; i < input_total_length; i++) {
143+ input[i] = i % 10;
144+ }
145+ 
146+ int32_t device_id = 0;
147+ aclrtStream stream = nullptr;
148+ 
149+ uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data());
150+ uint8_t* output_host = nullptr;
151+ float* input_device = nullptr;
152+ float* block_sums_device = nullptr;
153+ float* output_device = nullptr;
154+ uint32_t* counter_device = nullptr; // 原子计数器,用于判断最后一个完成的block
155+ 
156+ aclInit(nullptr);
157+ aclrtSetDevice(device_id);
158+ aclrtCreateStream(&stream);
159+ 
160+ aclrtMallocHost((void **)&output_host, output_total_byte_size);
161+ aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
162+ aclrtMalloc((void **)&block_sums_device, block_sums_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
163+ aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
164+ aclrtMalloc((void **)&counter_device, sizeof(uint32_t), ACL_MEM_MALLOC_HUGE_FIRST);
165+ 
166+ // 将counter初始化为0
167+ uint32_t init_count = 0;
168+ aclrtMemcpy(counter_device, sizeof(uint32_t), &init_count, sizeof(uint32_t), ACL_MEMCPY_HOST_TO_DEVICE);
169+ aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
170+ 
171+ kernel_memory_fence<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(
172+ input_device, block_sums_device, output_device, counter_device, input_total_length);
173+ aclrtSynchronizeStream(stream);
174+ 
175+ aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
176+ float output = *reinterpret_cast<float *>(output_host);
177+ 
178+ aclrtFree(input_device);
179+ aclrtFree(block_sums_device);
180+ aclrtFree(output_device);
181+ aclrtFree(counter_device);
182+ 
183+ const char* err = aclGetRecentErrMsg();
184+ if (err != nullptr) {
185+ fprintf(stderr, "%s\n", err);
186+ }
187+ 
188+ aclrtDestroyStream(stream);
189+ aclrtResetDevice(device_id);
190+ aclFinalize();
191+ 
192+ 
193+ // Host侧校验
194+ uint32_t WARP_SIZE = 32;
195+ 
196+ std::vector<float> host_block_sums(block_num);
197+ for (uint32_t b = 0; b < block_num; b++) {
198+ uint32_t base = b * thread_num_per_block;
199+ std::vector<float> reduce_buf(input.begin() + base,
200+ input.begin() + base + thread_num_per_block);
201+ host_block_sums[b] = host_block_reduce(reduce_buf, WARP_SIZE, thread_num_per_block);
202+ }
203+ 
204+ std::vector<float> grid_partial(thread_num_per_block, 0.0f);
205+ for (uint32_t tid = 0; tid < thread_num_per_block; tid++) {
206+ float my_sum = 0;
207+ for (uint32_t i = tid; i < block_num; i += thread_num_per_block) {
208+ my_sum += host_block_sums[i];
209+ }
210+ grid_partial[tid] = my_sum;
211+ }
212+ 
213+ float golden = host_block_reduce(grid_partial, WARP_SIZE, thread_num_per_block);
214+ 
215+ return verify_result(output, golden);
216+}
@@ -0,0 +1,138 @@
1+/**
2+ * Copyright (c) 2026 Huawei Technologies Co., Ltd.
3+ * This program is free software; you can redistribute it and/or modify it under the terms and conditions of
4+ * CANN Open Software License Agreement Version 2.0 (the "License").
5+ * Please refer to the License for details. You may not use this file except in compliance with the License.
6+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+ * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+ * See LICENSE in the root of the software repository for the full text of the License.
9+ */
10+ 
11+/* !
12+ * \file sync_barrier.asc
13+ * \brief
14+ */
15+ 
16+#include <iostream>
17+#include <vector>
18+#include "acl/acl.h"
19+#include "simt_api/asc_simt.h"
20+ 
21+namespace {
zhangshunwei2

同参考之前的样例看下是否需要namespace

likedislike
22+ constexpr uint32_t THREAD_COUNT = 1024;
qixingkai
qixingkaiqixingkaiApr 30

这里同样是不是namespace可以去掉

likedislike
23+}
24+ 
25+__global__ __launch_bounds__(THREAD_COUNT) void kernel_sync_barrier(float* input, float* output, uint32_t input_total_length) {
26+ extern __ubuf__ float smem[];
27+ uint32_t tid = threadIdx.x;
28+ uint32_t lane_id = laneid();
29+ uint32_t warp_id = tid / warpSize;
30+ 
31+ float val = (tid < input_total_length) ? input[tid] : 0.0f;
32+ 
33+ // warp内归约,asc_reduce_add返回当前warp所有线程val的总和
34+ val = asc_reduce_add(val);
35+ // 仅首线程写入共享内存
36+ if (lane_id == 0) {
37+ smem[warp_id] = val;
38+ }
39+ 
40+ asc_syncthreads(); // 跨warp同步
41+ 
42+ if (tid == 0) {
43+ for (int i = 1; i < blockDim.x / warpSize; i++) {
44+ val += smem[i];
45+ }
46+ *output = val;
47+ }
48+}
49+ 
50+uint32_t verify_result(float output, float golden)
51+{
52+ std::cout << "Output: " << output << std::endl;
53+ std::cout << "Golden: " << golden << std::endl;
54+ 
55+ float abs_err = std::abs(output - golden);
56+ float rel_err = abs_err / std::abs(golden);
57+ 
58+ if (abs_err < 1e-4f && rel_err < 1e-4f) {
59+ std::cout << "[Success] Case accuracy verification passed." << std::endl;
60+ return 0;
61+ } else {
62+ std::cout << "[Failed] Case accuracy verification failed!" << std::endl;
63+ return 1;
64+ }
65+}
66+ 
67+int32_t main(int32_t argc, char* argv[])
68+{
69+ constexpr uint32_t input_total_length = 1024;
70+ uint32_t block_num = 1;
71+ size_t input_total_byte_size = input_total_length * sizeof(float);
72+ size_t output_total_byte_size = block_num * sizeof(float);
73+ 
74+ std::vector<float> input(input_total_length);
75+ for (uint32_t i = 0; i < input_total_length; i++) {
76+ input[i] = i % 10;
77+ }
78+ 
79+ int32_t device_id = 0;
80+ aclrtStream stream = nullptr;
81+ 
82+ uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data());
83+ uint8_t* output_host = nullptr;
84+ float* input_device = nullptr;
85+ float* output_device = nullptr;
86+ 
87+ aclInit(nullptr);
88+ aclrtSetDevice(device_id);
89+ aclrtCreateStream(&stream);
90+ 
91+ aclrtMallocHost((void **)&output_host, output_total_byte_size);
92+ aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
93+ aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
94+
95+ aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
96+ 
97+ uint32_t thread_num_per_block = THREAD_COUNT;
98+ uint32_t dyn_ubuf_size = thread_num_per_block * sizeof(float) / 32;
99+ kernel_sync_barrier<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(
100+ input_device, output_device, input_total_length);
101+ aclrtSynchronizeStream(stream);
102+ 
103+ aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
104+ float output = *reinterpret_cast<float *>(output_host);
105+ 
106+ aclrtFree(input_device);
107+ aclrtFree(output_device);
108+ 
109+ const char* err = aclGetRecentErrMsg();
110+ if (err != nullptr) {
111+ fprintf(stderr, "%s\n", err);
112+ }
113+ 
114+ aclrtDestroyStream(stream);
115+ aclrtResetDevice(device_id);
116+ aclFinalize();
117+ 
118+ // Host侧校验
119+ constexpr uint32_t WARP_SIZE = 32;
120+ constexpr uint32_t warp_count = THREAD_COUNT / WARP_SIZE;
121+ std::vector<float> reduce_buf(input);
122+ 
123+ for (uint32_t w = 0; w < warp_count; w++) {
124+ uint32_t base = w * WARP_SIZE;
125+ for (uint32_t s = 1; s < WARP_SIZE; s <<= 1) {
126+ for (uint32_t i = 0; i < WARP_SIZE; i += 2 * s) {
127+ reduce_buf[base + i] += reduce_buf[base + i + s];
128+ }
129+ }
130+ }
131+ 
132+ float golden = reduce_buf[0];
133+ for (uint32_t i = 1; i < warp_count; i++) {
134+ golden += reduce_buf[i * WARP_SIZE];
135+ }
136+ 
137+ return verify_result(output, golden);
138+}
@@ -8,4 +8,5 @@
8 8 
9| 目录名称 | 功能描述 |9| 目录名称 | 功能描述 |
10|-----------------------------------------------------|------------------------|10|-----------------------------------------------------|------------------------|
11+| [01_sync_instruction](./01_sync_instruction/) | 展示SIMT同步与内存栅栏接口的相关样例。 |
11| [02_atomic_operation](./02_atomic_operation/) | 展示SIMT中atomic相关的API样例。 |12| [02_atomic_operation](./02_atomic_operation/) | 展示SIMT中atomic相关的API样例。 |