Merged
simt入门样例修改 #2405
simt入门样例修改 #2405
Merged
liuyi424Add on May 21
Total: 27 files changed+284-189
@@ -83,7 +83,7 @@ graph TD
83 83 
84**示例**84**示例**
85- [SIMD Add 算子示例(带同步计算接口)](../examples/02_simd_c_api/00_introduction/01_add/c_api_sync_add/c_api_add.asc)85- [SIMD Add 算子示例(带同步计算接口)](../examples/02_simd_c_api/00_introduction/01_add/c_api_sync_add/c_api_add.asc)
86-- [SIMT Add 算子示例(匹配业界习惯)](../examples/03_simt_api/00_introduction/01_add/add.asc)86+- [SIMT Gather 算子示例(匹配业界习惯)](../examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d/gather_1d.asc)
87- 更多样例请参考 [examples 目录](https://gitcode.com/cann/asc-devkit/tree/master/examples)87- 更多样例请参考 [examples 目录](https://gitcode.com/cann/asc-devkit/tree/master/examples)
88 88 
89---89---
@@ -83,7 +83,7 @@ You can also refer to the following key dimensions for quick decision-making:
83 83 
84**Examples**84**Examples**
85- [SIMD Add Operator Example (with Synchronous Computation Interface)](../../examples/02_simd_c_api/00_introduction/01_add/c_api_sync_add/c_api_add.asc)85- [SIMD Add Operator Example (with Synchronous Computation Interface)](../../examples/02_simd_c_api/00_introduction/01_add/c_api_sync_add/c_api_add.asc)
86-- [SIMT Add Operator Example (Matching Industry Habits)](../../examples/03_simt_api/00_introduction/01_add/add.asc)86+- [SIMT Gather Operator Example (Matching Industry Habits)](../../examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d/gather_1d.asc)
87- For more examples, refer to [examples directory](https://gitcode.com/cann/asc-devkit/tree/master/examples)87- For more examples, refer to [examples directory](https://gitcode.com/cann/asc-devkit/tree/master/examples)
88 88 
89---89---
@@ -11,7 +11,7 @@
11 11 
12 - [基于SIMT编程](入门教程/快速入门/基于SIMT编程/基于SIMT编程.md)12 - [基于SIMT编程](入门教程/快速入门/基于SIMT编程/基于SIMT编程.md)
13 - [HelloWorld](入门教程/快速入门/基于SIMT编程/HelloWorld.md)13 - [HelloWorld](入门教程/快速入门/基于SIMT编程/HelloWorld.md)
14- - [Add算子快速入门](入门教程/快速入门/基于SIMT编程/Add算子快速入门.md)14+ - [Gather算子快速入门](入门教程/快速入门/基于SIMT编程/Gather算子快速入门.md)
15 15 
16- [编程指南](编程指南/本文档组织结构.md)16- [编程指南](编程指南/本文档组织结构.md)
17 - [本文档组织结构](编程指南/本文档组织结构.md)17 - [本文档组织结构](编程指南/本文档组织结构.md)
Rdocs/guide/入门教程/快速入门/基于SIMT编程/Add算子快速入门.md→docs/guide/入门教程/快速入门/基于SIMT编程/Gather算子快速入门.md+42-34
@@ -1,16 +1,18 @@
1-# Add算子快速入门1+# Gather算子快速入门
2 2 
3-本入门示例基于Ascend C SIMT实现Add算子,带你快速上手实践,涵盖Device端核函数实现、Host端调用以及编译运行的完整流程,帮助开发者建立整体认知。3+本入门示例基于Ascend C SIMT实现一维Gather算子,带你快速上手实践,涵盖Device端核函数实现、Host端调用以及编译运行的完整流程,帮助开发者建立整体认知。
4 4 
5-开始前请参考[环境准备](../../环境准备.md)安装所需的CANN软件包,完整样例请见[Add](https://gitcode.com/cann/asc-devkit/tree/master/examples/03_simt_api/00_introduction/01_add)。5+开始前请参考[环境准备](../../环境准备.md)安装所需的CANN软件包,完整样例请见[Gather](https://gitcode.com/cann/asc-devkit/tree/master/examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d)。
6 6 
7-- **算子功能介绍**:7+- **Gather算子功能介绍**:
8 8 
9- Add算子的数学表达式为:9+ Gather算子的数学表达式为:
10 10 
11- ![](../../../figures/zh-cn_formulaimage_0000002541636777.png)11+ ```
12+ output[i] = input[index[i]]
13+ ```
12 14 
13- 计算逻辑:逐元素完成 `z = x + y`。15+ 计算逻辑:根据索引张量`index`中的每个元素,从一维输入向量`input`中采集对应位置的数据,并写入输出张量`output`。
14 16 
15- **算子设计**17- **算子设计**
16 18 
ai_xin
ai_xinai_xinMay 28

相比原来内容,缺少算子设计内容

likedislike
@@ -18,7 +20,7 @@
18 - 核函数定义:通过 [\_\_global\_\_](../../../编程指南/语言扩展层/SIMT-BuiltIn关键字.md) 修饰符声明。20 - 核函数定义:通过 [\_\_global\_\_](../../../编程指南/语言扩展层/SIMT-BuiltIn关键字.md) 修饰符声明。
19 - 数据分块(Tiling):使用内置关键字 [threadIdx、blockIdx、blockDim](../../../编程指南/语言扩展层/SIMT-BuiltIn关键字.md) 确定每个线程负责处理的数据。21 - 数据分块(Tiling):使用内置关键字 [threadIdx、blockIdx、blockDim](../../../编程指南/语言扩展层/SIMT-BuiltIn关键字.md) 确定每个线程负责处理的数据。
20 - 数据搬入:无需额外接口,直接通过指针访问即可。22 - 数据搬入:无需额外接口,直接通过指针访问即可。
21- - 数据计算:无需额外接口,直接使用 `+` 运算符完成。23+ - 数据计算:根据`index`中的索引值及操作符`[]`读取输入数据。
22 - 数据搬出:无需额外接口,直接通过指针访问即可。24 - 数据搬出:无需额外接口,直接通过指针访问即可。
23 - **Host端运行时接口**25 - **Host端运行时接口**
24 - 内存分配:使用 `aclrtMallocHost`分配Host Memory,`aclrtMalloc`分配Device Memory。26 - 内存分配:使用 `aclrtMallocHost`分配Host Memory,`aclrtMalloc`分配Device Memory。
@@ -26,9 +28,6 @@
26 - 启动NPU计算任务:通过 `<<<...>>>`语法糖启动核函数。28 - 启动NPU计算任务:通过 `<<<...>>>`语法糖启动核函数。
27 - 同步等待:调用 `aclrtSynchronizeStream`或 `aclrtSynchronizeDevice`等待任务完成。29 - 同步等待:调用 `aclrtSynchronizeStream`或 `aclrtSynchronizeDevice`等待任务完成。
28 - 数据搬出:使用 `aclrtMemcpy`将计算结果从Device Memory拷贝回Host Memory。30 - 数据搬出:使用 `aclrtMemcpy`将计算结果从Device Memory拷贝回Host Memory。
29-
30- > [!NOTE] 说明
31- > - 请参见[Ascend-C概述与学习路径](../../Ascend-C概述与学习路径.md)技术附录章节,获取`Ascend C API 参考`和`CANN运行时接口`链接,以查阅更多接口信息。
32 31 
33- **算子代码实现**:32- **算子代码实现**:
34 33 
@@ -37,49 +36,53 @@
37 - **Device端Kernel实现**:36 - **Device端Kernel实现**:
38 37 
39 Device端部分示例如下:38 Device端部分示例如下:
39+ 
40 ```cpp40 ```cpp
41- __global__ void add_custom(float* x, float* y, float* z, uint64_t total_length)41+ __global__ void gather_1d_custom(float* input, int32_t* index, float* output, uint64_t index_total_length)
42 {42 {
43 // Calculate global thread ID43 // Calculate global thread ID
44 int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;44 int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
45- // Maps to the row index of output tensor45+ 
46- if (idx >= total_length) {46+ // Maps to the index of output tensor
47+ if (idx >= index_total_length) {
47 return;48 return;
48 }49 }
49- z[idx] = x[idx] + y[idx];50+ output[idx] = input[index[idx]];
50 }51 }
51 ```52 ```
52 53 
53 - **Host端代码实现**:54 - **Host端代码实现**:
54 55 
55 Host端通过<<<>>>语法糖调用Device端代码。56 Host端通过<<<>>>语法糖调用Device端代码。
57+ 
56 ```cpp58 ```cpp
57- int32_t main(int argc, char const *argv[])59+ std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index)
58 {60 {
59 ...61 ...
60 aclrtCreateStream(&stream);62 aclrtCreateStream(&stream);
61 // Allocate host and device memory, and copy input data from host to device63 // Allocate host and device memory, and copy input data from host to device
62- aclrtMallocHost((void **)(&z_host), total_byte_size);64+ aclrtMallocHost((void **)(&output_host), output_total_byte_size);
63- aclrtMalloc((void **)&x_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);65+ aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
64- aclrtMalloc((void **)&y_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);66+ aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
65- aclrtMalloc((void **)&z_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);67+ aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
66- aclrtMemcpy(x_device, total_byte_size, x_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);68+ aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
67- aclrtMemcpy(y_device, total_byte_size, y_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);69+ aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
68 70 
69 // Configure kernel launch parameters71 // Configure kernel launch parameters
70- uint32_t blocks_per_grid = 48; // Number of thread blocks (Grid size)72+ uint32_t blocks_per_grid = 48; // Number of thread blocks (Grid size)
71 uint32_t threads_per_block = 256; // Number of threads per block (Block size)73 uint32_t threads_per_block = 256; // Number of threads per block (Block size)
72- uint32_t dyn_ubuf_size = 0; // No dynamic memory required in this sample74+ uint32_t dyn_ubuf_size = 0; // No dynamic memory required in this sample
73 75 
74 // Launch kernel <<<grid_dim, block_dim, dynamic_memory_size, stream>>>76 // Launch kernel <<<grid_dim, block_dim, dynamic_memory_size, stream>>>
75- add_custom<<<blocks_per_grid, threads_per_block, dyn_ubuf_size, stream>>>(x_device, y_device, z_device, x.size());77+ gather_1d_custom<<<blocks_per_grid, threads_per_block, dyn_ubuf_size, stream>>>(
78+ input_device, index_device, output_device, index.size());
76 79 
77- // Wait for the add_custom kernel to complete80+ // Wait for the gather_1d_custom kernel to complete
78 aclrtSynchronizeStream(stream);81 aclrtSynchronizeStream(stream);
79 82 
80 // Copy the result from device memory to host memory83 // Copy the result from device memory to host memory
81- aclrtMemcpy(z_host, total_byte_size, z_device, total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);84+ aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
82- std::vector<float> output((float *)z_host, (float *)(z_host + total_byte_size)); 85+ std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size));
83 ...86 ...
84 }87 }
85 ```88 ```
@@ -87,28 +90,33 @@
87- **算子编译与运行**:90- **算子编译与运行**:
88 91 
89 CMake 配置文件示例:92 CMake 配置文件示例:
93+ 
90 ```94 ```
91 cmake_minimum_required(VERSION 3.16)95 cmake_minimum_required(VERSION 3.16)
92 # find_package(ASC)是CMake中用于查找和配置Ascend C编译工具链的命令96 # find_package(ASC)是CMake中用于查找和配置Ascend C编译工具链的命令
93 find_package(ASC REQUIRED)97 find_package(ASC REQUIRED)
94 # 指定项目支持的语言包括ASC和CXX,ASC表示支持使用毕昇编译器对Ascend C编程语言进行编译98 # 指定项目支持的语言包括ASC和CXX,ASC表示支持使用毕昇编译器对Ascend C编程语言进行编译
95 project(kernel_samples LANGUAGES ASC CXX)99 project(kernel_samples LANGUAGES ASC CXX)
96- 100+ 
97 add_executable(demo101 add_executable(demo
98- add.asc102+ gather_1d.asc
99 )103 )
100- 104+ 
101 # 通过编译选项设置NPU架构105 # 通过编译选项设置NPU架构
102- target_compile_options(demo PRIVATE 106+ set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")
103- $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510 --enable-simt>107+ target_compile_options(demo PRIVATE
108+ $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt>
104 )109 )
105 ```110 ```
111+ 
106 编译并执行:112 编译并执行:
113+ 
107 ```114 ```
108- mkdir -p build && cd build; 115+ mkdir -p build && cd build;
109 cmake ..;make -j;116 cmake ..;make -j;
110 ./demo117 ./demo
111 ```118 ```
119+ 
112 > [!NOTE] 说明120 > [!NOTE] 说明
113 > - 该样例支持以下型号:121 > - 该样例支持以下型号:
114 > - Ascend 950PR / Ascend 950DT122 > - Ascend 950PR / Ascend 950DT
Mdocs/guide/算子实践参考/SIMT算子实现/算子实现.md+1-2File content is under review, please try again later
@@ -30,21 +30,31 @@ output[i] = input[index[i]]
30 }30 }
31 ```31 ```
32 32 
33-核函数的实现参考如下代码。完整的样例请参考[简单gather算子样例](https://gitcode.com/cann/asc-devkit/tree/master/examples/03_simt_api/02_features/00_resource_management/basic_gather)。33+核函数的实现参考如下代码。完整的样例请参考[简单gather算子样例](https://gitcode.com/cann/asc-devkit/tree/master/examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_2d)。
34 34 
35```35```
36template <typename type_data, typename type_idx>36template <typename type_data, typename type_idx>
37-__global__ void gather_custom(type_data* input, type_idx* index, type_data* gather_output, uint32_t in_width, uint32_t index_total_length)37+__global__ void gather_2d_custom(
38+ type_data* input,
39+ type_idx* index,
40+ type_data* gather_output,
41+ uint32_t in_width,
42+ uint32_t index_total_length)
38{43{
39- // 计算计算索引偏移量44+ // Calculate global thread ID
40 int32_t out_row = blockIdx.x * blockDim.x + threadIdx.x;45 int32_t out_row = blockIdx.x * blockDim.x + threadIdx.x;
41- // 从index中取出需要处理的行索引46+ 
47+ // Maps to the row index of output tensor
48+ if (out_row >= index_total_length) {
49+ return;
50+ }
51+ 
52+ // Single thread processes an entire row (all columns).
42 uint32_t in_row = index[out_row];53 uint32_t in_row = index[out_row];
43- // 循环处理该行所有数据
44 for (int32_t col = 0; col < in_width; col++) {54 for (int32_t col = 0; col < in_width; col++) {
45 int input_idx = in_row * in_width + col;55 int input_idx = in_row * in_width + col;
46 int output_idx = out_row * in_width + col;56 int output_idx = out_row * in_width + col;
47- gather_output[output_idx] = input[input_idx]; // 将输入数据拷贝到输出中57+ gather_output[output_idx] = input[input_idx];
48 }58 }
49}59}
50```60```
@@ -52,10 +62,15 @@ __global__ void gather_custom(type_data* input, type_idx* index, type_data* gath
52算子需要处理总共12288行数据,每行数据由核函数完成处理,因此需要12288个线程来完成对所有数据的处理。在Host侧通过<<<...\>\>\>调用核函数,同时设置启动48个线程块、每个线程块包含256个线程,示例代码如下。62算子需要处理总共12288行数据,每行数据由核函数完成处理,因此需要12288个线程来完成对所有数据的处理。在Host侧通过<<<...\>\>\>调用核函数,同时设置启动48个线程块、每个线程块包含256个线程,示例代码如下。
53 63 
54```64```
55-int main(int argc, char* argv[])65+int32_t main(int32_t argc, char* argv[])
56{66{
57- … 67+ ...
58- gather_custom<<<48, 256, 0, stream>>>(input_device, index_device, output_device, in_shape[1], index_total_length);68+ uint32_t numBlocks = 48;
59- … 69+ uint32_t thread_num_per_block = 256;
70+ uint32_t dyn_ubuf_size = 0; // No dynamic memory required in this sample
71+ // Call kernel function with <<<...>>>
72+ gather_2d_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>(
73+ input_device, index_device, output_device, in_shape[1], index_total_length);
74+ ...
60}75}
61```76```
@@ -0,0 +1,12 @@
1+# Gather样例介绍
2+ 
3+## 概述
4+ 
5+基于Ascend C SIMT编程的基础样例,包含简化场景和泛化场景的Gather算子,展示基于SIMT编程的算子开发接口、开发流程等。
6+ 
7+## 样例列表
8+ 
9+| 目录名称 | 功能描述 |
10+| --------- | --------- |
11+| [basic_gather](./basic_gather) | 样例基于Ascend C SIMT编程方式实现简化场景的Gather算子,展示SIMT编程的基本流程。 |
12+| [general_gather](./general_gather) | 样例基于Ascend C SIMT编程方式实现泛化场景的Gather算子,展示SIMT算子开发的端到端流程。 |
Rexamples/03_simt_api/02_features/00_resource_management/README_en.md→examples/03_simt_api/00_introduction/01_gather/README_en.md+2-2
@@ -8,5 +8,5 @@ This document presents basic samples based on Ascend C SIMT programming, demonst
8 8 
9| Directory Name | Description |9| Directory Name | Description |
10| --------- | --------- |10| --------- | --------- |
11-| [basic_gather](./basic_gather/) | This sample implements a Gather operator using Ascend C SIMT programming, demonstrating the development of operators with discrete memory access patterns in simplified scenarios. |11+| [basic_gather](./basic_gather) | This sample implements a Gather operator using Ascend C SIMT programming, demonstrating the development of operators with discrete memory access patterns in simplified scenarios. |
12-| [general_gather](./general_gather/) | This sample implements a Gather operator for generalized scenarios using Ascend C SIMT programming, demonstrating the end-to-end process of SIMT operator development. |12+| [general_gather](./general_gather) | This sample implements a Gather operator for generalized scenarios using Ascend C SIMT programming, demonstrating the end-to-end process of SIMT operator development. |
@@ -0,0 +1,12 @@
1+# Basic Gather样例介绍
2+ 
3+## 概述
4+ 
5+基于Ascend C SIMT编程的基础样例,包含简单场景(固定shape)的一维Gather算子和二维Gather算子,展示SIMT编程的基本流程。
6+ 
7+## 样例列表
8+ 
9+| 目录名称 | 功能描述 |
10+| --------- | --------- |
11+| [gather_1d](./gather_1d) | 样例基于Ascend C SIMT编程方式实现固定shape的一维Gather算子。 |
12+| [gather_2d](./gather_2d) | 样例基于Ascend C SIMT编程方式实现固定shape的二维Gather算子。 |
@@ -0,0 +1,12 @@
1+# Basic Gather Sample Introduction
2+ 
3+## Overview
4+ 
5+Basic samples based on Ascend C SIMT programming, including one-dimensional and two-dimensional Gather operators for simple scenarios with fixed shapes, demonstrating the basic SIMT programming flow.
6+ 
7+## Sample List
8+ 
9+| Directory Name | Description |
10+| --------- | --------- |
11+| [gather_1d](./gather_1d) | This sample implements a fixed-shape one-dimensional Gather operator using Ascend C SIMT programming. |
12+| [gather_2d](./gather_2d) | This sample implements a fixed-shape two-dimensional Gather operator using Ascend C SIMT programming. |
Rexamples/03_simt_api/00_introduction/01_add/CMakeLists.txt→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d/CMakeLists.txt+1-1
@@ -16,7 +16,7 @@ find_package(ASC REQUIRED)
16project(kernel_samples LANGUAGES ASC CXX)16project(kernel_samples LANGUAGES ASC CXX)
17 17 
18add_executable(demo18add_executable(demo
19- add.asc19+ gather_1d.asc
20)20)
21 21 
22set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")22set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")
Rexamples/03_simt_api/00_introduction/01_add/README.md→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d/README.md+30-27
@@ -1,80 +1,82 @@
1-# SIMT编程模式实现Add算子样例1+# SIMT编程模式实现一维Gather算子样例
2 2 
3## 概述3## 概述
4 4 
5-样例基于Ascend C SIMT编程方式实现Add算子,实现两个输入张量逐元素相加得到输出张量的功能,展示SIMT编程的基本流程。5+样例基于Ascend C SIMT编程方式实现简单场景(固定shape的)一维Gather算子,从一维输入中采集指定索引的元素,展示简化场景离散内存访问类算子的开发方法。
6 6 
7## 支持的产品7## 支持的产品
8 8 
9- Ascend 950PR/Ascend 950DT9- Ascend 950PR/Ascend 950DT
10 10 
11## 支持的CANN软件版本11## 支持的CANN软件版本
12+ 
12- \>= CANN 9.0.013- \>= CANN 9.0.0
13 14 
14-## 目录结构介绍15+## 目录结构
15 16 
16-```text17+```
17-├── 01_add18+├── gather_1d
18-│ ├── CMakeLists.txt // cmake编译文件19+│ ├── CMakeLists.txt # cmake编译文件
19-│ ├── add.asc // SIMT实现add调用样例20+│ ├── gather_1d.asc # SIMT实现一维gather调用样例
20-│ └── README.md21+| └── README.md
21```22```
22 23 
23## 算子描述24## 算子描述
24 25 
25- 算子功能: 26- 算子功能:
26- 本算子实现了形状为48 * 256的两个张量x,y相加得到算子输出z。第i个元素的计算公式为:27+ gather_1d算子实现了从长度为100000的一维输入向量中,根据index中每个元素获取1个数据的功能。算子输出output第i个元素计算公式为:
27- 28+ 
28 ```29 ```
29- z[i] = x[i] + y[i]30+ output[i] = input[index[i]]
30 ```31 ```
31 32 
32- 算子规格: 33- 算子规格:
33 <table>34 <table>
34- <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">add</td></tr>35+ <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">gather_1d</td></tr>
35- </tr>
36 <tr><td rowspan="3" 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>36 <tr><td rowspan="3" 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>
37- <tr><td align="center">x</td><td align="center">48 * 256</td><td align="center">float</td><td align="center">ND</td></tr>37+ <tr><td align="center">input</td><td align="center">100000</td><td align="center">float</td><td align="center">ND</td></tr>
38- <tr><td align="center">y</td><td align="center">48 * 256</td><td align="center">float</td><td align="center">ND</td></tr>38+ <tr><td align="center">index</td><td align="center">12288</td><td align="center">int32_t</td><td align="center">ND</td></tr>
39- </tr>39+ <tr><td rowspan="1" align="center">算子输出</td><td align="center">output</td><td align="center">12288</td><td align="center">float</td><td align="center">ND</td></tr>
40- </tr>40+ <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">gather_1d_custom</td></tr>
41- <tr><td rowspan="1" align="center">算子输出</td><td align="center">z</td><td align="center">48 * 256</td><td align="center">float</td><td align="center">ND</td></tr>
42- </tr>
43- <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">add_custom</td></tr>
44 </table>41 </table>
45 42 
46- 数据切分: 43- 数据切分:
47 * 核数:48核44 * 核数:48核
48 * 每核线程数:256线程45 * 每核线程数:256线程
49 * 单线程处理:1个元素46 * 单线程处理:1个元素
50- * 总处理能力:48×256=1228847+ * 总处理能力:48 * 256 = 12288个元素
48+ * 结果预期:输出第i个元素等于输入的第index[i]个元素
51 49 
52- 算子实现: 50- 算子实现:
53- 算子的实现流程为从输入x(Global Memory上的指针)中获取指定索引的数据。基于上述数据切分,首先计算线程应处理数据的索引,然后通过加法运算符计算得到输出值。51+ gather_1d算子的实现流程为从输入input(Global Memory)中获取指定索引的数据。基于上述数据切分,首先计算线程应处理数据的索引,然后根据index[i]读取1个元素,并写入output[i]。
54 52 
55- 调用实现: 53- 调用实现:
56- 使用内核调用符<<<>>>调用核函数。54+ 使用内核调用符<<<>>>启动Kernel。
57 55 
58## 编译运行56## 编译运行
59 57 
60在本样例根目录下执行如下步骤,编译并执行算子。58在本样例根目录下执行如下步骤,编译并执行算子。
61-- 配置环境变量 59+ 
62- 请根据当前环境上CANN开发套件包的[安装方式](../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。60+- 配置环境变量
61+ 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。
63 - 默认路径,root用户安装CANN软件包62 - 默认路径,root用户安装CANN软件包
63+ 
64 ```bash64 ```bash
65 source /usr/local/Ascend/cann/set_env.sh65 source /usr/local/Ascend/cann/set_env.sh
66 ```66 ```
67 67 
68 - 默认路径,非root用户安装CANN软件包68 - 默认路径,非root用户安装CANN软件包
69+ 
69 ```bash70 ```bash
70 source $HOME/Ascend/cann/set_env.sh71 source $HOME/Ascend/cann/set_env.sh
71 ```72 ```
72 73 
73 - 指定路径install_path,安装CANN软件包74 - 指定路径install_path,安装CANN软件包
75+ 
74 ```bash76 ```bash
75 source ${install_path}/cann/set_env.sh77 source ${install_path}/cann/set_env.sh
76 ```78 ```
77- 79+ 
78- 样例执行 80- 样例执行
79 81 
80 ```bash82 ```bash
@@ -90,6 +92,7 @@
90 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |92 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |
91 93 
92 执行结果如下,说明精度对比成功。94 执行结果如下,说明精度对比成功。
93- ```95+ 
96+ ```text
94 [Success] Case accuracy is verification passed.97 [Success] Case accuracy is verification passed.
95 ```98 ```
Rexamples/03_simt_api/00_introduction/01_add/add.asc→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_1d/gather_1d.asc+73-50
@@ -8,75 +8,94 @@
8* See LICENSE in the root of the software repository for the full text of the License.8* See LICENSE in the root of the software repository for the full text of the License.
9*/9*/
10 10 
11+ 
11/* !12/* !
12- * \file gather.asc13+ * \file gather_1d.asc
13 * \brief14 * \brief
14 */15 */
15 16 
17+#include <algorithm>
16#include <iostream>18#include <iostream>
17#include <iterator>19#include <iterator>
18#include <vector>20#include <vector>
19#include "acl/acl.h"21#include "acl/acl.h"
20-#include "tiling/platform/platform_ascendc.h"
21 22 
22- 23+__global__ void gather_1d_custom(float* input, int32_t* index, float* output, uint64_t index_total_length)
23-__global__ void add_custom(float* x, float* y, float* z, uint64_t total_length)
24{24{
25 // Calculate global thread ID25 // Calculate global thread ID
26 int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;26 int32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
27 27 
28- // Maps to the row index of output tensor28+ // Maps to the index of output tensor
29- if (idx >= total_length) {29+ if (idx >= index_total_length) {
30 return;30 return;
31 }31 }
32- z[idx] = x[idx] + y[idx];32+ output[idx] = input[index[idx]];
33}33}
34 34 
35-std::vector<float> add(std::vector<float>& x, std::vector<float>& y)35+std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index)
36{36{
37- size_t total_byte_size =x.size() * sizeof(float);37+ if (input.empty() || index.empty()) {
38+ std::cout << "[ERROR] Empty input tensors." << std::endl;
39+ return {};
40+ }
41+ 
42+ uint32_t input_total_length = input.size();
43+ uint32_t index_total_length = index.size();
44+ 
45+ size_t input_total_byte_size = input_total_length * sizeof(float);
46+ size_t index_total_byte_size = index_total_length * sizeof(int32_t);
47+ size_t output_total_byte_size = index_total_length * sizeof(float);
48+ 
38 int32_t device_id = 0;49 int32_t device_id = 0;
39 aclrtStream stream = nullptr;50 aclrtStream stream = nullptr;
40 51 
41- uint8_t* x_host = reinterpret_cast<uint8_t *>(x.data());52+ uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data());
42- uint8_t* y_host = reinterpret_cast<uint8_t *>(y.data());53+ uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data());
43- uint8_t* z_host = nullptr;54+ uint8_t* output_host = nullptr;
44- float* x_device = nullptr;55+ float* input_device = nullptr;
45- float* y_device = nullptr;56+ int32_t* index_device = nullptr;
46- float* z_device = nullptr;57+ float* output_device = nullptr;
47 // Init58 // Init
48 aclInit(nullptr);59 aclInit(nullptr);
49 aclrtSetDevice(device_id);60 aclrtSetDevice(device_id);
50 aclrtCreateStream(&stream);61 aclrtCreateStream(&stream);
51- // Malloc memory in host and device62+ 
52- aclrtMallocHost((void **)(&z_host), total_byte_size);63+ // Allocate host and device memory, and copy input data from host to device
53- aclrtMalloc((void **)&x_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);64+ aclrtMallocHost((void **)(&output_host), output_total_byte_size);
54- aclrtMalloc((void **)&y_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);65+ aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
55- aclrtMalloc((void **)&z_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);66+ aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
56- aclrtMemcpy(x_device, total_byte_size, x_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);67+ aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
57- aclrtMemcpy(y_device, total_byte_size, y_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);68+ aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
58- // Calc splite params69+ aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
59- uint32_t block_num = 48;70+ 
60- uint32_t thread_num_per_block = 256;71+ // Configure kernel launch parameters
61- uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory.72+ uint32_t blocks_per_grid = 48; // Number of thread blocks (Grid size)
62- // Call kernel funtion with <<<...>>>73+ uint32_t threads_per_block = 256; // Number of threads per block (Block size)
63- add_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(x_device, y_device, z_device, x.size());74+ uint32_t dyn_ubuf_size = 0; // No dynamic memory required in this sample
75+ 
76+ // Call kernel function with <<<...>>>
77+ gather_1d_custom<<<blocks_per_grid, threads_per_block, dyn_ubuf_size, stream>>>(
78+ input_device, index_device, output_device, index_total_length);
79+ 
80+ // Wait for the gather_1d_custom kernel to complete
64 aclrtSynchronizeStream(stream);81 aclrtSynchronizeStream(stream);
65- // Copy result from device to host82+ 
66- aclrtMemcpy(z_host, total_byte_size, z_device, total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);83+ // Copy the result from device memory to host memory
67- std::vector<float> output((float *)z_host, (float *)(z_host + total_byte_size));84+ aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST);
68- // Free memory85+ std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size));
69- aclrtFree(x_device);86+ 
70- aclrtFree(y_device);87+ // free memory
71- aclrtFree(z_device);88+ aclrtFree(input_device);
72- aclrtFreeHost(z_host);89+ aclrtFree(index_device);
90+ aclrtFree(output_device);
91+ aclrtFreeHost(output_host);
73 92 
74 const char* err = aclGetRecentErrMsg();93 const char* err = aclGetRecentErrMsg();
75 if (err != nullptr) {94 if (err != nullptr) {
76 fprintf(stderr, "%s\n", err);95 fprintf(stderr, "%s\n", err);
77 }96 }
78 97 
79- // DeInt98+ // DeInit
80 aclrtDestroyStream(stream);99 aclrtDestroyStream(stream);
81 aclrtResetDevice(device_id);100 aclrtResetDevice(device_id);
82 aclFinalize();101 aclFinalize();
@@ -109,23 +128,27 @@ uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden)
109 128 
110int32_t main(int32_t argc, char* argv[])129int32_t main(int32_t argc, char* argv[])
111{130{
112- constexpr uint32_t in_shape = 48 * 256;131+ constexpr uint32_t input_total_length = 100000;
113- std::vector<float> x(in_shape);132+ constexpr uint32_t index_total_length = 48 * 256;
114- for (uint32_t i = 0; i < in_shape; i++) {133+ 
115- x[i] = i * 1.1f;134+ std::vector<float> input(input_total_length);
135+ for (uint32_t i = 0; i < input_total_length; i++) {
136+ input[i] = i * 1.2f;
116 }137 }
117 138 
118- std::vector<float> y(in_shape);139+ std::vector<int32_t> index(index_total_length);
119- for (uint32_t i = 0; i < in_shape; i++) {140+ for (uint32_t i = 0; i < index_total_length; i++) {
120- y[i] = i + 3.4f;141+ index[i] = static_cast<int32_t>((i * 97 + 13) % input_total_length);
121 }142 }
122 143 
123- std::vector<float> golden(in_shape);144+ std::vector<float> golden(index_total_length);
124- for (uint32_t i = 0; i < in_shape; i++) {145+ for (uint32_t i = 0; i < index_total_length; i++) {
125- golden[i] = x[i] + y[i];146+ golden[i] = input[index[i]];
126 }147 }
127 148 
128- std::vector<float> output = add(x, y);149+ std::vector<float> output = gather_1d(input, index);
129- 150+ if (output.empty()) {
151+ return 1;
152+ }
130 return verify_result(output, golden);153 return verify_result(output, golden);
131-}154+}
Rexamples/03_simt_api/02_features/00_resource_management/basic_gather/CMakeLists.txt→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_2d/CMakeLists.txt+10-3
@@ -15,8 +15,8 @@ find_package(ASC REQUIRED)
15 15 
16project(kernel_samples LANGUAGES ASC CXX)16project(kernel_samples LANGUAGES ASC CXX)
17 17 
18-add_executable(gather18+add_executable(demo
19- gather.asc19+ gather_2d.asc
20)20)
21 21 
22set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")22set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")
@@ -27,6 +27,13 @@ set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510")
27# 说明:27# 说明:
28# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。28# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。
29# ======================================================================================29# ======================================================================================
30-target_compile_options(gather PRIVATE30+target_compile_options(demo PRIVATE
31 $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt>31 $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt>
32)32)
33+ 
34+target_link_libraries(demo PRIVATE
35+ m
36+ platform
37+ tiling_api
38+ dl
39+)
Rexamples/03_simt_api/02_features/00_resource_management/basic_gather/README.md→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_2d/README.md+21-19
@@ -1,29 +1,30 @@
1-# SIMT编程模式实现Gather算子样例1+# SIMT编程模式实现二维Gather算子样例
2 2 
3## 概述3## 概述
4 4 
5-样例基于Ascend C SIMT编程方式实现简单场景(固定shape的)Gather算子,从输入张量中采集指定的m行数据,展示简化场景离散内存访问类算子的开发方法。5+样例基于Ascend C SIMT编程方式实现简单场景(固定shape的)二维Gather算子,从输入张量中采集指定的m行数据,展示简化场景离散内存访问类算子的开发方法。
6 6 
7## 支持的产品7## 支持的产品
8 8 
9- Ascend 950PR/Ascend 950DT9- Ascend 950PR/Ascend 950DT
10 10 
11## 支持的CANN软件版本11## 支持的CANN软件版本
12+ 
12- \>= CANN 9.0.013- \>= CANN 9.0.0
13 14 
14## 目录结构介绍15## 目录结构介绍
15 16 
16-```text17+```
17-├── basic_gather18+├── gather_2d
18-│ ├── CMakeLists.txt // cmake编译文件19+│ ├── CMakeLists.txt # cmake编译文件
19-│ ├── gather.asc // SIMT实现gather调用样例20+│ ├── gather_2d.asc # SIMT实现二维gather调用样例
20-│ └── README.md21+| └── README.md
21```22```
22 23 
23## 算子描述24## 算子描述
24 25 
25- 算子功能: 26- 算子功能:
26- gather算子实现了从形状为100000 * 128的二维向量中获取指定索引的12288行数据的功能。算子输出output第i行数据计算公式为:27+ gather_2d算子实现了从形状为100000 * 128的二维向量中获取指定索引的12288行数据的功能。算子输出output第i行数据计算公式为:
27 28
28 ```29 ```
29 output[i] = input[index[i]]30 output[i] = input[index[i]]
@@ -31,16 +32,12 @@
31 32 
32- 算子规格: 33- 算子规格:
33 <table>34 <table>
34- <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">gather</td></tr>35+ <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">gather_2d</td></tr>
35- </tr>
36 <tr><td rowspan="3" 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>36 <tr><td rowspan="3" 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>
37 <tr><td align="center">input</td><td align="center">100000 * 128</td><td align="center">float</td><td align="center">ND</td></tr>37 <tr><td align="center">input</td><td align="center">100000 * 128</td><td align="center">float</td><td align="center">ND</td></tr>
38 <tr><td align="center">index</td><td align="center">12288</td><td align="center">uint32_t</td><td align="center">ND</td></tr>38 <tr><td align="center">index</td><td align="center">12288</td><td align="center">uint32_t</td><td align="center">ND</td></tr>
39- </tr>
40- </tr>
41 <tr><td rowspan="1" align="center">算子输出</td><td align="center">output</td><td align="center">12288 * 128</td><td align="center">float</td><td align="center">ND</td></tr>39 <tr><td rowspan="1" align="center">算子输出</td><td align="center">output</td><td align="center">12288 * 128</td><td align="center">float</td><td align="center">ND</td></tr>
42- </tr>40+ <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">gather_2d_custom</td></tr>
43- <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">gather_custom</td></tr>
44 </table>41 </table>
45 42 
46- 数据切分: 43- 数据切分:
@@ -50,7 +47,7 @@
50 * 总处理能力:48×256=12288行(覆盖索引长度)47 * 总处理能力:48×256=12288行(覆盖索引长度)
51 48 
52- 算子实现: 49- 算子实现:
53- gather算子的实现流程为从输入input(Global Memory)中获取指定索引的数据。基于上述数据切分,首先计算线程应处理数据的索引,然后通过赋值操作将一行数据存储到Global Memory上。50+ gather_2d算子的实现流程为从输入input(Global Memory)中获取指定索引的数据。基于上述数据切分,首先计算线程应处理数据的索引,然后通过赋值操作将一行数据存储到Global Memory上。
54 51 
55- 调用实现: 52- 调用实现:
56 使用内核调用符<<<>>>调用核函数。53 使用内核调用符<<<>>>调用核函数。
@@ -58,29 +55,33 @@
58## 编译运行55## 编译运行
59 56 
60在本样例根目录下执行如下步骤,编译并执行算子。57在本样例根目录下执行如下步骤,编译并执行算子。
58+ 
61- 配置环境变量 59- 配置环境变量
62- 请根据当前环境上CANN开发套件包的[安装方式](../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。60+ 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。
63 - 默认路径,root用户安装CANN软件包61 - 默认路径,root用户安装CANN软件包
62+ 
64 ```bash63 ```bash
65 source /usr/local/Ascend/cann/set_env.sh64 source /usr/local/Ascend/cann/set_env.sh
66 ```65 ```
67 66 
68 - 默认路径,非root用户安装CANN软件包67 - 默认路径,非root用户安装CANN软件包
68+ 
69 ```bash69 ```bash
70 source $HOME/Ascend/cann/set_env.sh70 source $HOME/Ascend/cann/set_env.sh
71 ```71 ```
72 72 
73 - 指定路径install_path,安装CANN软件包73 - 指定路径install_path,安装CANN软件包
74+ 
74 ```bash75 ```bash
75 source ${install_path}/cann/set_env.sh76 source ${install_path}/cann/set_env.sh
76 ```77 ```
77- 78+ 
78-- 样例执行79+- 样例执行
79 80 
80 ```bash81 ```bash
81 mkdir -p build && cd build; # 创建并进入build目录82 mkdir -p build && cd build; # 创建并进入build目录
82 cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j; # 编译工程83 cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j; # 编译工程
83- ./gather # 执行样例84+ ./demo # 执行样例
84 ```85 ```
85 86 
86 编译选项说明87 编译选项说明
@@ -90,6 +91,7 @@
90 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |91 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |
91 92 
92 执行结果如下,说明精度对比成功。93 执行结果如下,说明精度对比成功。
94+ 
93 ```95 ```
94 [Success] Case accuracy is verification passed.96 [Success] Case accuracy is verification passed.
95 ```97 ```
Rexamples/03_simt_api/02_features/00_resource_management/basic_gather/gather.asc→examples/03_simt_api/00_introduction/01_gather/basic_gather/gather_2d/gather_2d.asc+12-11
@@ -10,10 +10,11 @@
10 10 
11 11 
12/* !12/* !
13- * \file gather.asc13+ * \file gather_2d.asc
14 * \brief14 * \brief
15 */15 */
16 16 
17+#include <algorithm>
17#include <iostream>18#include <iostream>
18#include <iterator>19#include <iterator>
19#include <vector>20#include <vector>
@@ -22,7 +23,7 @@
22 23 
23 24 
24template <typename type_data, typename type_idx>25template <typename type_data, typename type_idx>
25-__global__ void gather_custom(26+__global__ void gather_2d_custom(
26 type_data* input,27 type_data* input,
27 type_idx* index,28 type_idx* index,
28 type_data* gather_output,29 type_data* gather_output,
@@ -37,7 +38,7 @@ __global__ void gather_custom(
37 return;38 return;
38 }39 }
39 40 
40- // Single thread processes entire row (all columns) - enables coalesced memory access41+ // Single thread processes an entire row (all columns).
41 uint32_t in_row = index[out_row];42 uint32_t in_row = index[out_row];
42 for (int32_t col = 0; col < in_width; col++) {43 for (int32_t col = 0; col < in_width; col++) {
43 int input_idx = in_row * in_width + col;44 int input_idx = in_row * in_width + col;
@@ -46,7 +47,7 @@ __global__ void gather_custom(
46 }47 }
47}48}
48 49 
49-std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, std::vector<uint32_t>& index)50+std::vector<float> gather_2d(std::vector<float>& input, const uint32_t* in_shape, std::vector<uint32_t>& index)
50{51{
51 std::vector<float> output_stub;52 std::vector<float> output_stub;
52 uint32_t input_total_length = input.size();53 uint32_t input_total_length = input.size();
@@ -80,12 +81,12 @@ std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, s
80 aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);81 aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
81 aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);82 aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
82 aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);83 aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
83- // Calc splite params84+ // Calc split params
84 uint32_t numBlocks = 48;85 uint32_t numBlocks = 48;
85 uint32_t thread_num_per_block = 256;86 uint32_t thread_num_per_block = 256;
86- uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory.87+ uint32_t dyn_ubuf_size = 0; // No dynamic memory required in this sample
87- // Call kernel funtion with <<<...>>>88+ // Call kernel function with <<<...>>>
88- gather_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>(89+ gather_2d_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>(
89 input_device, index_device, output_device, in_shape[1], index_total_length);90 input_device, index_device, output_device, in_shape[1], index_total_length);
90 aclrtSynchronizeStream(stream);91 aclrtSynchronizeStream(stream);
91 // Copy result from device to host92 // Copy result from device to host
@@ -101,7 +102,7 @@ std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, s
101 if (err != nullptr) {102 if (err != nullptr) {
102 fprintf(stderr, "%s\n", err);103 fprintf(stderr, "%s\n", err);
103 }104 }
104- // DeInt105+ // DeInit
105 aclrtDestroyStream(stream);106 aclrtDestroyStream(stream);
106 aclrtResetDevice(device_id);107 aclrtResetDevice(device_id);
107 aclFinalize();108 aclFinalize();
@@ -158,9 +159,9 @@ int32_t main(int32_t argc, char* argv[])
158 }159 }
159 }160 }
160 161 
161- std::vector<float> output = gather(input, in_shape, index);162+ std::vector<float> output = gather_2d(input, in_shape, index);
162 if (output.empty()) {163 if (output.empty()) {
163 return 1;164 return 1;
164 }165 }
165 return verify_result(output, golden);166 return verify_result(output, golden);
166-}167+}
Rexamples/03_simt_api/02_features/00_resource_management/general_gather/CMakeLists.txt→examples/03_simt_api/00_introduction/01_gather/general_gather/CMakeLists.txt+0-0
File renamed without changes.
Rexamples/03_simt_api/02_features/00_resource_management/general_gather/README.md→examples/03_simt_api/00_introduction/01_gather/general_gather/README.md+15-1
@@ -5,9 +5,11 @@
5本样例基于Ascend C SIMT编程方式实现支持泛化shape的Gather算子,包括基础版gather和增强版gather_v2。gather算子从二维输入张量中采集指定索引的行数据,gather_v2算子支持从多维输入张量中按指定维度收集数据,并支持batch_dims批量处理模式。样例展示了泛化场景下离散内存访问类算子的开发方法。5本样例基于Ascend C SIMT编程方式实现支持泛化shape的Gather算子,包括基础版gather和增强版gather_v2。gather算子从二维输入张量中采集指定索引的行数据,gather_v2算子支持从多维输入张量中按指定维度收集数据,并支持batch_dims批量处理模式。样例展示了泛化场景下离散内存访问类算子的开发方法。
6 6 
7## 支持的产品7## 支持的产品
8+ 
8- Ascend 950PR/Ascend 950DT9- Ascend 950PR/Ascend 950DT
9 10 
10## 支持的CANN软件版本11## 支持的CANN软件版本
12+ 
11- \>= CANN 9.0.013- \>= CANN 9.0.0
12 14 
13## 目录结构介绍15## 目录结构介绍
@@ -21,10 +23,12 @@
21```23```
22 24 
23## 算子描述25## 算子描述
26+ 
24### 1. gather算子27### 1. gather算子
25 28 
26- 算子功能: 29- 算子功能:
27 gather算子实现了从形状为M * N的二维输入张量input中获取指定索引的m行数据的功能,这m行的行索引由输入index指定。算子输出output第i行数据的计算公式为:30 gather算子实现了从形状为M * N的二维输入张量input中获取指定索引的m行数据的功能,这m行的行索引由输入index指定。算子输出output第i行数据的计算公式为:
31+ 
28 ```text32 ```text
29 output[i] = input[index[i]]33 output[i] = input[index[i]]
30 ```34 ```
@@ -52,10 +56,12 @@
52 使用内核调用符<<<>>>调用核函数。56 使用内核调用符<<<>>>调用核函数。
53 57 
54### 2. gather_v2算子58### 2. gather_v2算子
59+ 
55- 算子功能: 60- 算子功能:
56 gather_v2算子实现了从多维输入张量input中按照指定维度axis收集数据的功能,indices张量指定了要收集的索引位置。支持batch_dims批量处理模式,让不同的batch使用不同的索引集合。61 gather_v2算子实现了从多维输入张量input中按照指定维度axis收集数据的功能,indices张量指定了要收集的索引位置。支持batch_dims批量处理模式,让不同的batch使用不同的索引集合。
57- 处理流程: 62- 处理流程:
58 例如输入张量input的shape为(2, 2, 3, 2),索引张量indices的shape为(2, 2):63 例如输入张量input的shape为(2, 2, 3, 2),索引张量indices的shape为(2, 2):
64+ 
59 ```text65 ```text
60 input:66 input:
61 [[[[ 1, 2],67 [[[[ 1, 2],
@@ -79,9 +85,11 @@
79 [[1, 2],85 [[1, 2],
80 [0, 1]]86 [0, 1]]
81 ```87 ```
88+ 
82 axis=2, batch_dims=1表明收集维度为2,并且每个batch下使用不同的索引:89 axis=2, batch_dims=1表明收集维度为2,并且每个batch下使用不同的索引:
83 - batch=0: output[0, :, :, :] = input[0, :, [1, 2], :],即从input[0]的维度2上收集indices[0]对应的切片90 - batch=0: output[0, :, :, :] = input[0, :, [1, 2], :],即从input[0]的维度2上收集indices[0]对应的切片
84 - batch=1: output[1, :, :, :] = input[1, :, [0, 1], :],即从input[1]的维度2上收集indices[1]对应的切片91 - batch=1: output[1, :, :, :] = input[1, :, [0, 1], :],即从input[1]的维度2上收集indices[1]对应的切片
92+ 
85 ```text93 ```text
86 output:94 output:
87 [[[[ 3, 4],95 [[[[ 3, 4],
@@ -96,7 +104,7 @@
96 [[19, 20],104 [[19, 20],
97 [21, 22]]]]105 [21, 22]]]]
98 ```106 ```
99- 107+ 
100- 算子规格:108- 算子规格:
101 <table>109 <table>
102 <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">gather_v2</td></tr>110 <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">gather_v2</td></tr>
@@ -128,20 +136,25 @@
128 使用内核调用符<<<>>>调用核函数。136 使用内核调用符<<<>>>调用核函数。
129 137 
130## 编译运行138## 编译运行
139+ 
131在本样例根目录下执行如下步骤,编译并执行算子。140在本样例根目录下执行如下步骤,编译并执行算子。
141+ 
132- 配置环境变量 142- 配置环境变量
133 请根据当前环境上CANN开发套件包的[安装方式](../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。143 请根据当前环境上CANN开发套件包的[安装方式](../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。
134 - 默认路径,root用户安装CANN软件包144 - 默认路径,root用户安装CANN软件包
145+ 
135 ```bash146 ```bash
136 source /usr/local/Ascend/cann/set_env.sh147 source /usr/local/Ascend/cann/set_env.sh
137 ```148 ```
138 149 
139 - 默认路径,非root用户安装CANN软件包150 - 默认路径,非root用户安装CANN软件包
151+ 
140 ```bash152 ```bash
141 source $HOME/Ascend/cann/set_env.sh153 source $HOME/Ascend/cann/set_env.sh
142 ```154 ```
143 155 
144 - 指定路径install_path,安装CANN软件包156 - 指定路径install_path,安装CANN软件包
157+ 
145 ```bash158 ```bash
146 source ${install_path}/cann/set_env.sh159 source ${install_path}/cann/set_env.sh
147 ```160 ```
@@ -162,6 +175,7 @@
162 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |175 | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) |
163 176 
164 执行结果如下,说明精度对比成功:177 执行结果如下,说明精度对比成功:
178+ 
165 ```text179 ```text
166 [Success] Case accuracy is verification passed.180 [Success] Case accuracy is verification passed.
167 ```181 ```
Rexamples/03_simt_api/02_features/00_resource_management/general_gather/gather.asc→examples/03_simt_api/00_introduction/01_gather/general_gather/gather.asc+4-3File content is under review, please try again later
Rexamples/03_simt_api/02_features/00_resource_management/general_gather/gather_v2.asc→examples/03_simt_api/00_introduction/01_gather/general_gather/gather_v2.asc+0-0
File renamed without changes.
@@ -9,4 +9,4 @@
9| 目录名称 | 功能描述 |9| 目录名称 | 功能描述 |
10| ------------------------------------------------------------ | ---------------------------------------------------- |10| ------------------------------------------------------------ | ---------------------------------------------------- |
11| [00_quickstart](./00_quickstart) | 基于SIMT编程方式实现HelloWorld样例,是SIMT编程的核函数、<<<>>>调用等基础代码示例 |11| [00_quickstart](./00_quickstart) | 基于SIMT编程方式实现HelloWorld样例,是SIMT编程的核函数、<<<>>>调用等基础代码示例 |
12-| [01_add](./01_add) | 基于SIMT编程方式实现Add样例,展示了SIMT单指令多线程的编程方式完成加法计算 |12+| [01_gather](./01_gather) | 基于SIMT编程方式实现gather样例,展示了从简化场景到泛化场景的离散访存类算子开发方法 |
Mexamples/03_simt_api/00_introduction/README_en.md+1-1File content is under review, please try again later
@@ -21,7 +21,7 @@
21```21```
22 22 
23## 算子描述23## 算子描述
24- gather算子实现了从形状为100000 * 128的二维向量中获取指定索引的12288行数据的功能,具体功能描述可参考[Gather算子详情](../../../02_features/00_resource_management/basic_gather/README.md)章节。24+ gather算子实现了从形状为100000 * 128的二维向量中获取指定索引的12288行数据的功能,具体功能描述可参考[Gather算子详情](../../../00_introduction/01_gather/basic_gather/gather_2d/README.md)章节。
25 25 
26## msProf工具介绍26## msProf工具介绍
27msProf工具是单算子性能分析工具。包含msprof op和msprof op simulator两种使用方式。该工具协助用户定位算子内存、算子代码以及算子指令的异常,实现全方位的算子调优。当前支持基于不同运行模式(上板或仿真)和不同文件形式(可执行文件或算子二进制.o文件)进行性能数据的采集和自动解析。27msProf工具是单算子性能分析工具。包含msprof op和msprof op simulator两种使用方式。该工具协助用户定位算子内存、算子代码以及算子指令的异常,实现全方位的算子调优。当前支持基于不同运行模式(上板或仿真)和不同文件形式(可执行文件或算子二进制.o文件)进行性能数据的采集和自动解析。
@@ -80,11 +80,11 @@ std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, s
80 aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);80 aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST);
81 aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);81 aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
82 aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);82 aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE);
83- // Calc splite params83+ // Calc split params
shaonaite
shaonaiteshaonaiteMay 26

readme中有链接到gather的地址,需要同步修改

likedislike
84 uint32_t numBlocks = 48;84 uint32_t numBlocks = 48;
85 uint32_t thread_num_per_block = 256;85 uint32_t thread_num_per_block = 256;
86 uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory.86 uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory.
87- // Call kernel funtion with <<<...>>>87+ // Call kernel function with <<<...>>>
88 gather_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>(88 gather_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>(
89 input_device, index_device, output_device, in_shape[1], index_total_length);89 input_device, index_device, output_device, in_shape[1], index_total_length);
90 aclrtSynchronizeStream(stream);90 aclrtSynchronizeStream(stream);
@@ -101,7 +101,7 @@ std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, s
101 if (err != nullptr) {101 if (err != nullptr) {
102 fprintf(stderr, "%s\n", err);102 fprintf(stderr, "%s\n", err);
103 }103 }
104- // DeInt104+ // DeInit
105 aclrtDestroyStream(stream);105 aclrtDestroyStream(stream);
106 aclrtResetDevice(device_id);106 aclrtResetDevice(device_id);
107 aclFinalize();107 aclFinalize();
@@ -163,4 +163,4 @@ int32_t main(int32_t argc, char* argv[])
163 return 1;163 return 1;
164 }164 }
165 return verify_result(output, golden);165 return verify_result(output, golden);
166-}166+}
@@ -1,12 +0,0 @@
1-# 编程基础样例介绍
2- 
3-## 概述
4- 
5-基于Ascend C SIMT编程的基础样例,展示基于SIMT编程的算子开发用户界面、开发流程等。
6- 
7-## 样例列表
8- 
9-| 目录名称 | 功能描述 |
10-| --------- | --------- |
11-| [basic_gather](./basic_gather/) | 样例基于Ascend C SIMT编程方式实现Gather算子,展示简化场景离散内存访问类算子的开发方法。 |
12-| [general_gather](./general_gather/) | 样例基于Ascend C SIMT编程方式实现泛化场景的Gather算子,展示SIMT算子开发的端到端流程。 |
@@ -8,5 +8,4 @@
8 8 
9| 目录名称 | 功能描述 |9| 目录名称 | 功能描述 |
10|----------------------------------------------------|--------------------|10|----------------------------------------------------|--------------------|
11-| [00_resource_management](./00_resource_management/) | 展示SIMT资源管理相关的特性样例。 |11+| [01_api_features](./01_api_features/) | 展示SIMT中API相关的特性样例。 |
12-| [01_api_features](./01_api_features/) | 展示SIMT中API相关的特性样例。 |
@@ -8,5 +8,4 @@ This document presents feature samples based on Ascend C SIMT, demonstrating var
8 8 
9| Directory Name | Description |9| Directory Name | Description |
10|----------------------------------------------------|--------------------|10|----------------------------------------------------|--------------------|
11-| [00_resource_management](./00_resource_management/) | Samples demonstrating SIMT resource management related features. |11+| [01_api_features](./01_api_features/) | Samples demonstrating API-related features in SIMT. |
12-| [01_api_features](./01_api_features/) | Samples demonstrating API-related features in SIMT. |