Merged
修复simt样例中的asc_init不全等隐患问题 #7255
wulinyuAdd on 21 days ago
修复simt样例中的asc_init不全等隐患问题 #7255
Merged
wulinyuAdd on 21 days ago
Total: 7 files changed+40-31
@@ -1,10 +1,8 @@
1# SIMD与SIMT混合编程实现gather和adds计算1# SIMD与SIMT混合编程实现gather和adds计算
2 2 
3- 
4## 概述3## 概述
5本样例基于SIMD和SIMT混合编程模式实现gather和adds计算,以SIMT编程方式实现离散内存访问操作gather,以SIMD编程方式实现连续内存访问操作adds。4本样例基于SIMD和SIMT混合编程模式实现gather和adds计算,以SIMT编程方式实现离散内存访问操作gather,以SIMD编程方式实现连续内存访问操作adds。
6 5 
7- 
8## 本样例支持的产品及CANN软件版本6## 本样例支持的产品及CANN软件版本
9 7 
10| 产品 | CANN软件版本 |8| 产品 | CANN软件版本 |
@@ -43,19 +41,21 @@
43 <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">gather_and_adds_kernel</td></tr>41 <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">gather_and_adds_kernel</td></tr>
44 </table>42 </table>
45 43 
46-- 样例实现: 44+## 样例实现
47- Vector Core中SIMT单元和SIMD单元共享片上存储,可以使用片上存储完成SIMT和SIMD的混合编程。本例中样例输入index的shape为[8192],可设置核数为8,每个核处理数据量为1024,设置线程数`THREAD_COUNT`为1024,每个线程处理1个数据元素,单个核只需调用1次simt_gather函数即可完成gather运算。
48 45 
49- > ⚠️ **注意** 当单核处理数据量大于设置的线程数时,需要切分数据到多个线程块,可使用asc_vf_call多次调用simt_gather函数启动多个线程块完成获取指定索引数据的操作。46+Vector Core中SIMT单元和SIMD单元共享片上存储,可以使用片上存储完成SIMT和SIMD的混合编程。本例中样例输入index的shape为[8192],可设置核数为8,每个核处理数据量为1024,设置线程数`THREAD_COUNT`为1024,每个线程处理1个数据元素,单个核只需调用1次simt_gather函数即可完成gather运算。
50 47 
51- 基于上述数据拆分,在simd_adds函数中,处理1024个数据元素的加1操作。48+> ⚠️ **注意** 当单核处理数据量大于设置的线程数时,需要切分数据到多个线程块,可使用asc_vf_call多次调用simt_gather函数启动多个线程块完成获取指定索引数据的操作。
52 49 
53- > ⚠️ **注意** simd_adds中加1运算实际可以直接在simt_gather函数中快速实现,本例目的仅仅是通过一个简单用例展示SIMT和SIMD两种编程模式的混合编程方式,不是该样例最佳实践。50+基于上述数据拆分,在simd_adds函数中,处理1024个数据元素的加1操作。
54 51 
55- gather & adds样例的实现流程主要分为3个步骤:simt_gather,simd_adds和数据搬出。52+> ⚠️ **注意** simd_adds中加1运算实际可以直接在simt_gather函数中快速实现,本例目的仅仅是通过一个简单用例展示SIMT和SIMD两种编程模式的混合编程方式,不是该样例最佳实践。
56 53 
57- (1)simt_gather从GM(Global Memory)输入中获取指定索引的数据。54+gather & adds样例的实现流程主要分为3个步骤:simt_gather,simd_adds和数据搬出。
58- ```55+ 
56+- simt_gather从GM(Global Memory)输入中获取指定索引的数据。
57+ 
58+ ```cpp
59 uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;59 uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
60 ...60 ...
61 uint32_t gather_idx = index[idx];61 uint32_t gather_idx = index[idx];
@@ -63,8 +63,9 @@
63 local_output[threadIdx.x] = input[gather_idx];63 local_output[threadIdx.x] = input[gather_idx];
64 ```64 ```
65 65 
66- (2)simd_adds将UB(Unified Buffer)中数据做加1操作。调用asc_loadalign将数据从UB(Unified Buffer)搬运到寄存器上,调用asc_add_scalar完成加1运算并输出到目标寄存器,最后调用asc_storealign将数据从寄存器搬运到UB。重复上述操作即可完成1024个数据元素的加1运算。66+- simd_adds将UB(Unified Buffer)中数据做加1操作。调用asc_loadalign将数据从UB(Unified Buffer)搬运到寄存器上,调用asc_add_scalar完成加1运算并输出到目标寄存器,最后调用asc_storealign将数据从寄存器搬运到UB。重复上述操作即可完成1024个数据元素的加1运算。
67- ```67+ 
68+ ```cpp
68 for (uint16_t i = 0; i < repeat_times; i++) {69 for (uint16_t i = 0; i < repeat_times; i++) {
69 mask_reg = asc_update_mask_b32(count);70 mask_reg = asc_update_mask_b32(count);
70 asc_loadalign(src_reg0, input + i * one_repeat_size);71 asc_loadalign(src_reg0, input + i * one_repeat_size);
@@ -73,10 +74,9 @@
73 }74 }
74 ```75 ```
75 76 
76- (3)将输出数据从UB(Unified Buffer)搬出至GM(Global Memory)上。77+- 将输出数据从UB(Unified Buffer)搬出至GM(Global Memory)上。
77 78 
78-- 调用实现: 79+- Host侧使用内核调用符<<<>>>调用核函数。
79- 使用内核调用符<<<>>>调用核函数。
80 80 
81## 编译运行81## 编译运行
82 82 
@@ -3,7 +3,6 @@
3## Overview3## Overview
4This sample implements gather and adds computation based on SIMD and SIMT hybrid programming mode. It uses SIMT programming to implement discrete memory access operation gather, and SIMD programming to implement continuous memory access operation adds.4This sample implements gather and adds computation based on SIMD and SIMT hybrid programming mode. It uses SIMT programming to implement discrete memory access operation gather, and SIMD programming to implement continuous memory access operation adds.
5 5 
6- 
7## Supported Products and CANN Software Versions6## Supported Products and CANN Software Versions
8 7 
9| Product | CANN Software Version |8| Product | CANN Software Version |
@@ -42,19 +41,21 @@ This sample implements gather and adds computation based on SIMD and SIMT hybrid
42 <tr><td rowspan="1" align="center">Kernel Function Name</td><td colspan="4" align="center">gather_and_adds_kernel</td></tr>41 <tr><td rowspan="1" align="center">Kernel Function Name</td><td colspan="4" align="center">gather_and_adds_kernel</td></tr>
43 </table>42 </table>
44 43 
45-- Sample Implementation: 44+## Sample Implementation
46- The SIMT unit and SIMD unit in the Vector Core share on-chip storage, which can be used to complete SIMT and SIMD hybrid programming. In this sample, the shape of the sample input index is [8192], the number of cores can be set to 8, each core processes 1024 data elements, the number of threads THREAD_COUNT is set to 1024, each thread processes 1 data element, and a single core only needs to call the simt_gather function once to complete the gather operation.
47 45 
48- > ⚠️ **Note** When the amount of data processed by a single core is greater than the set number of threads, the data needs to be split into multiple thread blocks. You can use asc_vf_call to call the simt_gather function multiple times to start multiple thread blocks to complete the operation of obtaining specified index data.46+The SIMT unit and SIMD unit in the Vector Core share on-chip storage, which can be used to complete SIMT and SIMD hybrid programming. In this sample, the shape of the sample input index is [8192], the number of cores can be set to 8, each core processes 1024 data elements, the number of threads THREAD_COUNT is set to 1024, each thread processes 1 data element, and a single core only needs to call the simt_gather function once to complete the gather operation.
49 47 
50- Based on the above data partitioning, in the simd_adds function, the operation of adding 1 to 1024 data elements is performed.48+> ⚠️ **Note** When the amount of data processed by a single core is greater than the set number of threads, the data needs to be split into multiple thread blocks. You can use asc_vf_call to call the simt_gather function multiple times to start multiple thread blocks to complete the operation of obtaining specified index data.
51 49 
52- > ⚠️ **Note** The addition operation in simd_adds can actually be implemented directly in the simt_gather function quickly. The purpose of this sample is only to demonstrate the hybrid programming approach of SIMT and SIMD programming modes through a simple use case, which is not the best practice for this sample.50+Based on the above data partitioning, in the simd_adds function, the operation of adding 1 to 1024 data elements is performed.
53 51 
54- The implementation flow of the gather & adds sample is mainly divided into 3 steps: simt_gather, simd_adds, and copying data from UB to GM.52+> ⚠️ **Note** The addition operation in simd_adds can actually be implemented directly in the simt_gather function quickly. The purpose of this sample is only to demonstrate the hybrid programming approach of SIMT and SIMD programming modes through a simple use case, which is not the best practice for this sample.
55 53 
56- (1) simt_gather obtains data with specified indexes from GM (Global Memory) input.54+The implementation flow of the gather & adds sample is mainly divided into 3 steps: simt_gather, simd_adds, and copying data from UB to GM.
57- ```55+ 
56+- simt_gather obtains data with specified indexes from GM (Global Memory) input.
57+ 
58+ ```cpp
58 uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;59 uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x;
59 ...60 ...
60 uint32_t gather_idx = index[idx];61 uint32_t gather_idx = index[idx];
@@ -62,8 +63,9 @@ This sample implements gather and adds computation based on SIMD and SIMT hybrid
62 local_output[threadIdx.x] = input[gather_idx];63 local_output[threadIdx.x] = input[gather_idx];
63 ```64 ```
64 65 
65- (2) simd_adds performs the add 1 operation on the data in UB (Unified Buffer). Call asc_loadalign to move data from UB (Unified Buffer) to the register, call asc_add_scalar to complete the add 1 operation and output to the destination register, and finally call asc_storealign to move data from the register to UB. Repeat the above operation to complete the add 1 operation on 1024 data elements.66+- simd_adds performs the add 1 operation on the data in UB (Unified Buffer). Call asc_loadalign to move data from UB (Unified Buffer) to the register, call asc_add_scalar to complete the add 1 operation and output to the destination register, and finally call asc_storealign to move data from the register to UB. Repeat the above operation to complete the add 1 operation on 1024 data elements.
66- ```67+ 
68+ ```cpp
67 for (uint16_t i = 0; i < repeat_times; i++) {69 for (uint16_t i = 0; i < repeat_times; i++) {
68 mask_reg = asc_update_mask_b32(count);70 mask_reg = asc_update_mask_b32(count);
69 asc_loadalign(src_reg0, input + i * one_repeat_size);71 asc_loadalign(src_reg0, input + i * one_repeat_size);
@@ -72,10 +74,9 @@ This sample implements gather and adds computation based on SIMD and SIMT hybrid
72 }74 }
73 ```75 ```
74 76 
75- (3) Copy the output data from UB (Unified Buffer) to GM (Global Memory).77+- Copy the output data from UB (Unified Buffer) to GM (Global Memory).
76 78 
77-- Call Implementation: 79+- On the host side, use the kernel call operator <<<>>> to call the kernel function.
78- Use the kernel call operator <<<>>> to call the kernel function.
79 80 
80## Compilation and Execution81## Compilation and Execution
81 82 
@@ -98,6 +98,7 @@ __global__ __vector__ void gather_and_adds_kernel(
98 __gm__ float* input, __gm__ uint32_t* index, __gm__ float* output, uint32_t input_total_length,98 __gm__ float* input, __gm__ uint32_t* index, __gm__ float* output, uint32_t input_total_length,
99 uint32_t index_total_length)99 uint32_t index_total_length)
100{100{
101+ asc_init();
101 // 按启动核数均分index元素。本样例index_total_length为8192、核数为8,每个核处理1024个元素。102 // 按启动核数均分index元素。本样例index_total_length为8192、核数为8,每个核处理1024个元素。
102 uint32_t index_total_length_per_block = index_total_length / block_num;103 uint32_t index_total_length_per_block = index_total_length / block_num;
103 // 申请UB空间作为Gather结果和Adds结果的本地输出缓存。104 // 申请UB空间作为Gather结果和Adds结果的本地输出缓存。
@@ -259,6 +259,7 @@ __global__ __vector__ void insert_or_assign_warp_store_kernel(
259 __gm__ Bucket* table, __gm__ float* table_values, __gm__ const int64_t* keys, __gm__ const float* values,259 __gm__ Bucket* table, __gm__ float* table_values, __gm__ const int64_t* keys, __gm__ const float* values,
260 uint32_t key_num, uint32_t capacity, uint32_t dim)260 uint32_t key_num, uint32_t capacity, uint32_t dim)
261{261{
262+ asc_init();
262 asc_vf_call<insert_or_assign_warp_store_vf>(263 asc_vf_call<insert_or_assign_warp_store_vf>(
263 dim3(THREAD_COUNT), table, table_values, keys, values, key_num, capacity, dim, block_idx, BLOCK_DIM);264 dim3(THREAD_COUNT), table, table_values, keys, values, key_num, capacity, dim, block_idx, BLOCK_DIM);
264}265}
@@ -635,6 +636,7 @@ __global__ __vector__ void find_verify_kernel(
635 __gm__ const float* expected_values, __gm__ uint32_t* mismatch_count, uint32_t key_num, uint32_t capacity,636 __gm__ const float* expected_values, __gm__ uint32_t* mismatch_count, uint32_t key_num, uint32_t capacity,
636 uint32_t dim)637 uint32_t dim)
637{638{
639+ asc_init();
638 asc_vf_call<find_verify_vf>(640 asc_vf_call<find_verify_vf>(
639 dim3(THREAD_COUNT), table, table_values, keys, expected_values, mismatch_count, key_num, capacity, dim,641 dim3(THREAD_COUNT), table, table_values, keys, expected_values, mismatch_count, key_num, capacity, dim,
640 block_idx, BLOCK_DIM);642 block_idx, BLOCK_DIM);
@@ -77,6 +77,7 @@ template <int scenario>
77__global__ __vector__ void integer_div_kernel(77__global__ __vector__ void integer_div_kernel(
78 __gm__ uint32_t* input, __gm__ uint32_t* output, uint32_t divisor, uint32_t total_length)78 __gm__ uint32_t* input, __gm__ uint32_t* output, uint32_t divisor, uint32_t total_length)
79{79{
80+ asc_init();
80 uint32_t magic = 0;81 uint32_t magic = 0;
81 uint32_t shift = 0;82 uint32_t shift = 0;
82 if constexpr (scenario == 1) {83 if constexpr (scenario == 1) {
@@ -137,6 +137,7 @@ __simt_vf__ __launch_bounds__(THREADS_2048) inline void simt_transpose_naive(
137__global__ __vector__ void transpose_naive_kernel(137__global__ __vector__ void transpose_naive_kernel(
138 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height)138 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height)
139{139{
140+ asc_init();
140 uint32_t total = width * height;141 uint32_t total = width * height;
141 asc_vf_call<simt_transpose_naive>(dim3(THREADS_PER_BLOCK), output, input, width, height, total);142 asc_vf_call<simt_transpose_naive>(dim3(THREADS_PER_BLOCK), output, input, width, height, total);
142}143}
@@ -236,6 +237,7 @@ __simt_vf__ __launch_bounds__(THREADS_2048) inline void simt_transpose_2tile(
236__global__ __vector__ void transpose_ub_2tile_static_kernel(237__global__ __vector__ void transpose_ub_2tile_static_kernel(
237 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)238 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)
238{239{
240+ asc_init();
239 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];241 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
240 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];242 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
241 uint32_t tiles_x = width / TILE_DIM;243 uint32_t tiles_x = width / TILE_DIM;
@@ -275,6 +277,7 @@ void launch_case_kernel(float* output, float* input, const LaunchConfig& config)
275__global__ __vector__ void transpose_ub_2tile_loop_kernel(277__global__ __vector__ void transpose_ub_2tile_loop_kernel(
276 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)278 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)
277{279{
280+ asc_init();
278 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];281 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
279 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];282 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
280 uint32_t tiles_x = width / TILE_DIM;283 uint32_t tiles_x = width / TILE_DIM;
@@ -332,6 +335,7 @@ __simt_vf__ __launch_bounds__(THREADS_2048) inline void simt_transpose_2tile_pad
332__global__ __vector__ void transpose_ub_pad_loop_kernel(335__global__ __vector__ void transpose_ub_pad_loop_kernel(
333 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)336 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)
334{337{
338+ asc_init();
335 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_PAD_STRIDE];339 __ubuf__ float in_tile[TILES_PER_BLOCK][TILE_DIM][TILE_PAD_STRIDE];
336 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];340 __ubuf__ float out_tile[TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
337 uint32_t tiles_x = width / TILE_DIM;341 uint32_t tiles_x = width / TILE_DIM;
@@ -370,6 +374,7 @@ void launch_case_kernel(float* output, float* input, const LaunchConfig& config)
370__global__ __vector__ void transpose_ub_pad_db_kernel(374__global__ __vector__ void transpose_ub_pad_db_kernel(
371 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)375 __gm__ float* output, __gm__ float* input, uint32_t width, uint32_t height, uint32_t total_tiles)
372{376{
377+ asc_init();
373 // 两组UB buffer轮换使用:当前buffer进入SIMT VF/MTE3流水,下一组buffer由MTE2搬入。378 // 两组UB buffer轮换使用:当前buffer进入SIMT VF/MTE3流水,下一组buffer由MTE2搬入。
374 __ubuf__ float in_tile[2][TILES_PER_BLOCK][TILE_DIM][TILE_PAD_STRIDE];379 __ubuf__ float in_tile[2][TILES_PER_BLOCK][TILE_DIM][TILE_PAD_STRIDE];
375 __ubuf__ float out_tile[2][TILES_PER_BLOCK][TILE_DIM][TILE_DIM];380 __ubuf__ float out_tile[2][TILES_PER_BLOCK][TILE_DIM][TILE_DIM];
@@ -146,6 +146,7 @@ __global__ __vector__ void lambda_operator_hybrid_kernel(
146 __gm__ float* input, __gm__ float* y_leaky_affine, __gm__ float* y_clamp, __gm__ uint32_t* neg_count,146 __gm__ float* input, __gm__ float* y_leaky_affine, __gm__ float* y_clamp, __gm__ uint32_t* neg_count,
147 uint32_t total_length, float alpha, float scale, float bias, float lo, float hi)147 uint32_t total_length, float alpha, float scale, float bias, float lo, float hi)
148{148{
149+ asc_init();
149 // 混合编程核函数体内lambda:标记__aicore__,按核号计算本核分片(返回每核元素数)150 // 混合编程核函数体内lambda:标记__aicore__,按核号计算本核分片(返回每核元素数)
150 auto per_block = [total_length]() __aicore__ { return total_length / block_num; };151 auto per_block = [total_length]() __aicore__ { return total_length / block_num; };
151 uint32_t count = per_block();152 uint32_t count = per_block();
@@ -157,8 +158,6 @@ __global__ __vector__ void lambda_operator_hybrid_kernel(
157 __ubuf__ float* ub_clamp = dyn_ub + count * 2;158 __ubuf__ float* ub_clamp = dyn_ub + count * 2;
158 159 
159 // 将本核GM输入分片搬入UB:SIMT与SIMD两阶段均以该副本为输入,Clamp作用于原始输入。160 // 将本核GM输入分片搬入UB:SIMT与SIMD两阶段均以该副本为输入,Clamp作用于原始输入。
160- // MTE2搬运与Vector计算流水间以事件同步:先通知MTE2可搬运,搬运完成事件到达后再放行Vector计算。
161- asc_sync_notify(PIPE_V, PIPE_MTE2, EVENT_ID0);
162 asc_copy_gm2ub_align(161 asc_copy_gm2ub_align(
163 ub_in, input + block_idx * count, 1, count * sizeof(float), 0, 0, false, asc_load_l2_cache_mode::NOTALLOC_KEEP,162 ub_in, input + block_idx * count, 1, count * sizeof(float), 0, 0, false, asc_load_l2_cache_mode::NOTALLOC_KEEP,
164 0, 0);163 0, 0);