Merged
add simt sample hash map #1021
louyuxuanAdd on Mar 19
add simt sample hash map #1021
Merged
Merged fromlouyuxuan/asc-devkitsampleintocann/asc-devkitmaster
Total: 5 files changed+476-0
| @@ -0,0 +1,40 @@ | |||
| 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 | +cmake_minimum_required(VERSION 3.16) | ||
| 12 | + | ||
| 13 | +find_package(ASC REQUIRED) | ||
| 14 | + | ||
| 15 | +project(kernel_samples LANGUAGES ASC CXX) | ||
| 16 | + | ||
| 17 | +add_executable(demo | ||
| 18 | + insert_hash_table.asc | ||
| 19 | +) | ||
| 20 | + | ||
| 21 | +# ====================================================================================== | ||
| 22 | +# NPU 编译选项配置 | ||
| 23 | +# | ||
| 24 | +# 说明: | ||
| 25 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 26 | +# ====================================================================================== | ||
| 27 | +target_compile_options(demo PRIVATE | ||
| 28 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510 --enable-simt> | ||
| 29 | + $<BUILD_INTERFACE:$<$<STREQUAL:${RUN_MODE},sim>:-g -O0>> | ||
| 30 | +) | ||
| 31 | + | ||
| 32 | +target_link_directories(demo PRIVATE | ||
| 33 | + $<BUILD_INTERFACE:$<$<STREQUAL:${RUN_MODE},sim>:$ENV{ASCEND_HOME_PATH}/tools/simulator/${SOC_VERSION}/lib>> | ||
| 34 | +) | ||
| 35 | + | ||
| 36 | +target_link_libraries(demo PRIVATE | ||
| 37 | + m | ||
| 38 | + $<BUILD_INTERFACE:$<$<STREQUAL:${RUN_MODE},sim>:runtime_camodel>> | ||
| 39 | + $<BUILD_INTERFACE:$<$<STREQUAL:${RUN_MODE},sim>:npu_drv>> | ||
| 40 | +) | ||
| @@ -0,0 +1,114 @@ | |||
| 1 | +# SIMT实现InsertHashTable算子样例 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例介绍InsertHashTable算子,展示基于SIMT实现的大规模线程并发访问Global Memory内存的算子实践。 | ||
| 6 | + | ||
| 7 | +## 支持的产品 | ||
| 8 | + | ||
| 9 | +- Ascend 950PR/Ascend 950DT | ||
| 10 | + | ||
| 11 | +## 目录结构 | ||
| 12 | + | ||
| 13 | +``` | ||
| 14 | +├── 00_simt_insert_hash_table | ||
| 15 | +│ ├── CMakeLists.txt # cmake编译文件 | ||
| 16 | +│ ├── insert_hash_table.asc # Ascend C算子实现 & 调用样例 | ||
| 17 | +| └── README.md | ||
| 18 | +``` | ||
| 19 | + | ||
参考其他样例结构 算子描述(算子功能、算子规格、算子实现)补充 ![]() ![]() | |||
| 20 | +## 背景知识 | ||
| 21 | + | ||
| 22 | +Hash Table是一种高效的数据结构,它通过一个哈希函数将“键”(key)映射到一个固定大小的数组(桶数组)的特定位置上,从而实现快速地查找、插入和删除操作。 | ||
| 23 | + | ||
| 24 | +- 哈希函数:一个函数,它接受一个键作为输入,计算出一个整数(哈希值),该整数通过某种运算被映射为数组的一个索引。 | ||
| 25 | +- 桶数组:一个数组,每个元素(称为“桶”,Bucket)用于存储键值对(key-value pair) | ||
| 26 | + | ||
| 27 | +向hash table 中插入一个键值对的基本流程是: | ||
| 28 | + | ||
| 29 | +- 使用哈希函数计算出key的哈希值 | ||
| 30 | +- 将哈希值映射为桶数组的索引index(通常是对哈希表容量取余) | ||
| 31 | +- 将键值对存储到数组的bucket[index]位置 | ||
| 32 | + | ||
| 33 | +哈希冲突:由于hash table的容量(桶数组大小)远小于哈希函数的输出范围,不同的key可能会被映射到同一个索引上。 | ||
| 34 | + | ||
| 35 | +常见的哈希冲突解决方法是开放寻址法:当发生冲突时,算法会按照线性探查寻找下一个“空”桶来存储数据。 | ||
| 36 | + | ||
| 37 | +由于每个键值对的存储位置由计算出的哈希值决定,这些值通常是随机且离散的,加上哈希冲突的存在,导致在实际写入数据前需要进行多个条件判断,因此,哈希表不适合基于SIMD编程模型来实现。相比之下,SIMT编程中每个线程都能独立处理分支判断,并支持离散访存,因此在实现哈希表方面更具优势。 | ||
| 38 | + | ||
| 39 | +## 算法分析 | ||
| 40 | + | ||
| 41 | +SIMT能够通过大量线程的并发执行实现高效处理大批数据,但同时也带来了两个问题:多线程写冲突和核间数据同步问题。 | ||
| 42 | + | ||
| 43 | +### 多线程写冲突问题 | ||
| 44 | + | ||
| 45 | +在多线程操作同一块内存时,资源冲突问题在所难免。当两个线程插入的key产生哈希冲突时,多个线程会尝试向桶数组的同一位置写入数据,因此需要确保仅有一个线程能够获得桶的写入权限。程序实现时,在Bucket结构体中添加一个标记位“flag”,用于标记当前桶的写入权限,线程通过原子指令asc_atomic_cas()修改flag来获取桶的写入权限。 | ||
| 46 | + | ||
| 47 | +### 核间数据同步问题 | ||
| 48 | + | ||
| 49 | +当发生哈希冲突时,线程需要判断当前桶中存储的key是否与待插入的key相同,这要求当前线程能够读取其他线程写入的数据,并确保数据的完整性。程序实现时,在Bucket结构体中添加一个标记位“state”,用于标识key值的写入状态。写线程中在写入key后将state标记位置1,并在二者之间调用asc_threadfence()接口,确保当state被设置为1时,key的写入操作已经完成。读线程中,通过while循环轮询state值,直到state被设置为1,然后再读取key值。 | ||
| 50 | + | ||
| 51 | +```C++ | ||
| 52 | +struct Bucket { | ||
| 53 | + int64_t key; // 键 | ||
| 54 | + uint32_t state; // key值写入状态标记 | ||
| 55 | + uint32_t flag; // 原子操作标志位 | ||
| 56 | + float value[32]; // 值 | ||
| 57 | +}; | ||
| 58 | +``` | ||
| 59 | + | ||
| 60 | +## 算子描述 | ||
| 61 | + | ||
| 62 | +- 算子功能: | ||
| 63 | + InsertHashTable算子实现将N个键值对插入容量为Z的哈希表中,其中key为int64_t类型数,value为长度N的float类型的Tensor。 | ||
| 64 | + | ||
| 65 | +- 算子规格: | ||
| 66 | + <table> | ||
| 67 | + <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">insert_hash_table</td></tr> | ||
| 68 | + </tr> | ||
| 69 | + <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> | ||
| 70 | + <tr><td align="center">keys</td><td align="center">1, N</td><td align="center">int64_t</td><td align="center">ND</td></tr> | ||
| 71 | + <tr><td align="center">values</td><td align="center">N, M</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 72 | + </tr> | ||
| 73 | + </tr> | ||
| 74 | + <tr><td rowspan="1" align="center">算子输出</td><td align="center">table_addr</td><td align="center">1, Z</td><td align="center">Bucket</td><td align="center">ND</td></tr> | ||
| 75 | + </tr> | ||
| 76 | + <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">insert_hash_table</td></tr> | ||
| 77 | + </table> | ||
| 78 | + | ||
| 79 | +- 算子实现: | ||
| 80 | + InsertHashTable算子的实现流程为,每个warp处理一个键值对,warp中线程0负责根据key的哈希值寻找可用桶,线程0-31负责将value存入桶中。线程0寻找可用桶时,通过开放寻址法解决哈希冲突问题,并使用asc_atomic_cas()接口解决多线程冲突问题,使用asc_threadfence()接口解决核间数据同步问题。 | ||
| 81 | + | ||
| 82 | +- 调用实现: | ||
| 83 | + 使用内核调用符<<<>>>调用核函数。 | ||
| 84 | + | ||
| 85 | +## 编译运行 | ||
| 86 | + | ||
| 87 | +在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 88 | +- 配置环境变量 | ||
| 89 | + 请根据当前环境上CANN开发套件包的[安装方式](../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 90 | + - 默认路径,root用户安装CANN软件包 | ||
| 91 | + ```bash | ||
| 92 | + source /usr/local/Ascend/cann/set_env.sh | ||
| 93 | + ``` | ||
| 94 | + | ||
| 95 | + - 默认路径,非root用户安装CANN软件包 | ||
| 96 | + ```bash | ||
| 97 | + source $HOME/Ascend/cann/set_env.sh | ||
| 98 | + ``` | ||
| 99 | + | ||
| 100 | + - 指定路径install_path,安装CANN软件包 | ||
| 101 | + ```bash | ||
| 102 | + source ${install_path}/cann/set_env.sh | ||
| 103 | + ``` | ||
| 104 | + | ||
| 105 | +- 样例执行 | ||
| 106 | + ```bash | ||
| 107 | + mkdir -p build && cd build; # 创建并进入build目录 | ||
| 108 | + cmake ..; make -j; # 编译工程 | ||
| 109 | + ./demo # 执行样例 | ||
| 110 | + ``` | ||
| 111 | + 执行结果如下,说明精度对比成功。 | ||
| 112 | + ``` | ||
| 113 | + [Success] find all key-value in hash table. | ||
| 114 | + ``` | ||
| @@ -0,0 +1,310 @@ | |||
| 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 insert_hash_table.asc | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <cstdint> | ||
| 17 | +#include <cstddef> | ||
| 18 | +#include <cstring> | ||
| 19 | +#include <limits> | ||
| 20 | +#include <random> | ||
| 21 | +#include <cstring> | ||
| 22 | +#include <iostream> | ||
| 23 | +#include <array> | ||
| 24 | +#include <vector> | ||
| 25 | +#include "acl/acl.h" | ||
| 26 | +#include "simt_api/asc_simt.h" | ||
| 27 | + | ||
| 28 | +namespace { | ||
| 29 | +constexpr uint32_t VALUE_DIM = 32; | ||
| 30 | +constexpr uint32_t HASH_TABLE_CAPACITY = 16 * 1024; // must be 2^n | ||
| 31 | +constexpr uint32_t ALIGN_BYTES = 8; | ||
| 32 | +constexpr uint32_t KEY_NUM = 8 * 1024; | ||
| 33 | +constexpr uint32_t THREAD_COUNT = 1024; | ||
| 34 | +constexpr uint32_t INT64_TYPE_BYTES = 8; | ||
| 35 | +constexpr uint32_t DEFALUT_FLAG = 0; | ||
| 36 | +constexpr uint32_t HOLD_FLAG = 0x57277275; | ||
| 37 | +constexpr uint32_t WRITE_FLAG = 0x39555593; | ||
| 38 | +} // namespace | ||
| 39 | + | ||
| 40 | +// Bucket need 8 bytes align | ||
| 41 | +struct Bucket { | ||
| 42 | + int64_t key; | ||
| 43 | + uint32_t state; | ||
| 44 | + uint32_t flag; | ||
| 45 | + float value[VALUE_DIM]; | ||
| 46 | +}; | ||
| 47 | + | ||
| 48 | +__aicore__ inline uint32_t rotl32(uint32_t x, uint32_t r) | ||
| 49 | +{ | ||
| 50 | + return (x << r) | (x >> (32 - r)); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +__aicore__ inline uint32_t murmur_hash_3(int64_t key, uint32_t len, uint32_t seed) | ||
| 54 | +{ | ||
| 55 | + constexpr uint32_t c1 = 0xcc9e2d51; | ||
| 56 | + constexpr uint32_t c2 = 0x1b873593; | ||
| 57 | + constexpr uint32_t r1 = 15; | ||
| 58 | + constexpr uint32_t r2 = 13; | ||
| 59 | + constexpr uint32_t m = 5; | ||
| 60 | + constexpr uint32_t n = 0xe6546b64; | ||
| 61 | + uint32_t hash = seed; | ||
| 62 | + uint32_t k = 0; | ||
| 63 | + uint32_t* data = reinterpret_cast<uint32_t*>(&key); | ||
| 64 | + | ||
| 65 | + const uint32_t nblocks = len / 4; | ||
| 66 | +#pragma unroll 2 | ||
| 67 | + for (uint32_t i = 0; i < nblocks; i++) { | ||
| 68 | + k = data[i]; | ||
| 69 | + k *= c1; | ||
| 70 | + k = rotl32(k, r1); | ||
| 71 | + k *= c2; | ||
| 72 | + hash ^= k; | ||
| 73 | + hash = rotl32(hash, r2); | ||
| 74 | + hash = hash * m + n; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + hash ^= len; | ||
| 78 | + hash ^= hash >> 16; | ||
| 79 | + hash *= 0x85ebca6b; | ||
| 80 | + hash ^= hash >> 13; | ||
| 81 | + hash *= 0xc2b2ae35; | ||
| 82 | + hash ^= hash >> 16; | ||
| 83 | + return hash; | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +__global__ __launch_bounds__(THREAD_COUNT) void insert_hash_table(uint8_t* table_addr, uint32_t capacity, | ||
| 87 | + uint32_t value_dim, int64_t* keys, float* values, | ||
| 88 | + uint32_t key_num, uint32_t bucket_size) | ||
| 89 | +{ | ||
| 90 | + uint32_t idx = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 91 | + idx = idx / warpSize; | ||
| 92 | + if (idx >= key_num) { | ||
| 93 | + return; | ||
| 94 | + } | ||
| 95 | + int32_t lane_id = threadIdx.x % warpSize; | ||
| 96 | + Bucket* bucket_addr = nullptr; | ||
| 97 | + int32_t is_insert_succ = 0; | ||
| 98 | + uint32_t bucket_idx = 0; | ||
| 99 | + | ||
| 100 | + if (lane_id == 0) { | ||
| 101 | + int64_t insert_key = keys[idx]; | ||
| 102 | + uint32_t hash = murmur_hash_3(insert_key, INT64_TYPE_BYTES, 0); | ||
| 103 | + bucket_idx = hash & (capacity - 1); | ||
| 104 | + uint32_t bucket_offset = 0; | ||
| 105 | + for (uint32_t i = 0; i < capacity; i++) { | ||
| 106 | + bucket_offset = bucket_idx * bucket_size; | ||
| 107 | + bucket_addr = reinterpret_cast<Bucket*>(table_addr + bucket_offset); | ||
| 108 | + uint32_t old_flag = asc_atomic_cas(&bucket_addr->flag, DEFALUT_FLAG, WRITE_FLAG); | ||
| 109 | + if (old_flag == DEFALUT_FLAG) { | ||
| 110 | + bucket_addr->key = insert_key; | ||
| 111 | + asc_threadfence(); | ||
| 112 | + bucket_addr->state = 1; | ||
| 113 | + is_insert_succ = 1; | ||
| 114 | + break; | ||
| 115 | + } else { | ||
| 116 | + while (*reinterpret_cast<volatile uint32_t*>(&bucket_addr->state) != 1) {} | ||
| 117 | + if (insert_key == *reinterpret_cast<volatile int64_t*>(&bucket_addr->key)) { | ||
| 118 | + old_flag = asc_atomic_cas(&bucket_addr->flag, HOLD_FLAG, WRITE_FLAG); | ||
| 119 | + if (old_flag == HOLD_FLAG) { | ||
| 120 | + is_insert_succ = 1; | ||
| 121 | + } | ||
| 122 | + break; | ||
| 123 | + } else { | ||
| 124 | + bucket_idx = (bucket_idx + 1) & (capacity - 1); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + is_insert_succ = asc_shfl(is_insert_succ, 0, warpSize); | ||
| 131 | + if (is_insert_succ == 1) { | ||
| 132 | + bucket_idx = asc_shfl(bucket_idx, 0, warpSize); | ||
| 133 | + bucket_addr = reinterpret_cast<Bucket*>(table_addr + bucket_idx * bucket_size); | ||
| 134 | + for (uint32_t j = lane_id; j < value_dim; j += warpSize) { | ||
| 135 | + bucket_addr->value[j] = values[idx * value_dim + j]; | ||
| 136 | + } | ||
| 137 | + asc_threadfence(); | ||
| 138 | + if (lane_id == 0) { | ||
| 139 | + bucket_addr->flag = HOLD_FLAG; | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | +} | ||
| 143 | + | ||
| 144 | +int64_t random_key() | ||
| 145 | +{ | ||
| 146 | + static std::random_device rd; | ||
| 147 | + static std::mt19937_64 gen(rd()); | ||
| 148 | + static std::uniform_int_distribution<int64_t> dist(0, std::numeric_limits<int64_t>::max()); | ||
| 149 | + return dist(gen); | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +float random_value() | ||
| 153 | +{ | ||
| 154 | + static std::random_device rd; | ||
| 155 | + static std::mt19937_64 gen(rd()); | ||
| 156 | + static std::uniform_real_distribution<float> dist(std::numeric_limits<float>::min(), | ||
| 157 | + std::numeric_limits<float>::max()); | ||
| 158 | + return dist(gen); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +void generate_test_data(std::vector<int64_t>& keys, std::vector<std::array<float, VALUE_DIM>>& values, uint32_t num) | ||
| 162 | +{ | ||
| 163 | + for (int32_t i = 0; i < num; i++) { | ||
| 164 | + keys[i] = random_key(); | ||
| 165 | + values[i] = std::array<float, VALUE_DIM>{random_value(), random_value(), random_value(), random_value()}; | ||
| 166 | + } | ||
| 167 | +} | ||
| 168 | + | ||
| 169 | +std::vector<uint8_t> launch_kernel(std::vector<int64_t>& keys, std::vector<std::array<float, VALUE_DIM>>& values) | ||
| 170 | +{ | ||
| 171 | + size_t key_total_byte_size = KEY_NUM * sizeof(int64_t); | ||
| 172 | + size_t value_total_byte_size = KEY_NUM * sizeof(float) * VALUE_DIM; | ||
| 173 | + | ||
| 174 | + size_t bucket_byte_size = sizeof(Bucket); | ||
| 175 | + bucket_byte_size = (bucket_byte_size + ALIGN_BYTES - 1) & ~(ALIGN_BYTES - 1); | ||
| 176 | + size_t hash_map_total_byte_size = bucket_byte_size * HASH_TABLE_CAPACITY; | ||
| 177 | + | ||
| 178 | + uint32_t warp_per_block = THREAD_COUNT / 32; | ||
| 179 | + uint32_t num_blocks = (KEY_NUM + warp_per_block - 1) / warp_per_block; | ||
| 180 | + | ||
| 181 | + int32_t device_id = 0; | ||
| 182 | + aclrtStream stream = nullptr; | ||
| 183 | + void* key_host = reinterpret_cast<void*>(keys.data()); | ||
| 184 | + void* key_device = nullptr; | ||
| 185 | + void* value_host = reinterpret_cast<void*>(values.data()); | ||
| 186 | + void* value_device = nullptr; | ||
| 187 | + void* hash_map_host = nullptr; | ||
| 188 | + void* hash_table_device = nullptr; | ||
| 189 | + | ||
| 190 | + aclInit(nullptr); | ||
| 191 | + aclrtSetDevice(device_id); | ||
| 192 | + aclrtCreateStream(&stream); | ||
| 193 | + aclrtMallocHost(&hash_map_host, hash_map_total_byte_size); | ||
| 194 | + aclrtMalloc(&key_device, key_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 195 | + aclrtMalloc(&value_device, value_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 196 | + aclrtMalloc(&hash_table_device, hash_map_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 197 | + | ||
| 198 | + aclrtMemcpy(key_device, key_total_byte_size, key_host, key_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 199 | + aclrtMemcpy(value_device, value_total_byte_size, value_host, value_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 200 | + | ||
| 201 | + insert_hash_table<<<num_blocks, THREAD_COUNT, 0, stream>>>( | ||
| 202 | + static_cast<uint8_t*>(hash_table_device), HASH_TABLE_CAPACITY, VALUE_DIM, static_cast<int64_t*>(key_device), | ||
| 203 | + static_cast<float*>(value_device), KEY_NUM, static_cast<uint32_t>(bucket_byte_size)); | ||
| 204 | + aclrtSynchronizeStream(stream); | ||
| 205 | + | ||
| 206 | + aclrtMemcpy(hash_map_host, hash_map_total_byte_size, hash_table_device, hash_map_total_byte_size, | ||
| 207 | + ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 208 | + std::vector<uint8_t> output((uint8_t*)hash_map_host, (uint8_t*)hash_map_host + hash_map_total_byte_size); | ||
| 209 | + | ||
| 210 | + aclrtFree(key_device); | ||
| 211 | + aclrtFree(value_device); | ||
| 212 | + aclrtFree(hash_table_device); | ||
| 213 | + aclrtFreeHost(hash_map_host); | ||
| 214 | + | ||
| 215 | + aclrtDestroyStream(stream); | ||
| 216 | + aclrtResetDevice(device_id); | ||
| 217 | + aclFinalize(); | ||
| 218 | + | ||
| 219 | + return output; | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +inline uint32_t rotl32_cpu(uint32_t x, uint32_t r) | ||
| 223 | +{ | ||
| 224 | + return (x << r) | (x >> (32 - r)); | ||
| 225 | +} | ||
| 226 | + | ||
| 227 | +uint32_t murmur_hash_3_cpu(int64_t key, uint32_t len, uint32_t seed) | ||
| 228 | +{ | ||
| 229 | + constexpr uint32_t c1 = 0xcc9e2d51; | ||
| 230 | + constexpr uint32_t c2 = 0x1b873593; | ||
| 231 | + constexpr uint32_t r1 = 15; | ||
| 232 | + constexpr uint32_t r2 = 13; | ||
| 233 | + constexpr uint32_t m = 5; | ||
| 234 | + constexpr uint32_t n = 0xe6546b64; | ||
| 235 | + uint32_t hash = seed; | ||
| 236 | + uint32_t k = 0; | ||
| 237 | + uint32_t* data = reinterpret_cast<uint32_t*>(&key); | ||
| 238 | + | ||
| 239 | + const uint32_t nblocks = len / 4; | ||
| 240 | + for (uint32_t i = 0; i < nblocks; i++) { | ||
| 241 | + k = data[i]; | ||
| 242 | + k *= c1; | ||
| 243 | + k = rotl32_cpu(k, r1); | ||
| 244 | + k *= c2; | ||
| 245 | + hash ^= k; | ||
| 246 | + hash = rotl32_cpu(hash, r2); | ||
| 247 | + hash = hash * m + n; | ||
| 248 | + } | ||
| 249 | + | ||
| 250 | + hash ^= len; | ||
| 251 | + hash ^= hash >> 16; | ||
| 252 | + hash *= 0x85ebca6b; | ||
| 253 | + hash ^= hash >> 13; | ||
| 254 | + hash *= 0xc2b2ae35; | ||
| 255 | + hash ^= hash >> 16; | ||
| 256 | + return hash; | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +void check_result(std::vector<int64_t>& keys, std::vector<std::array<float, VALUE_DIM>>& values, | ||
| 260 | + std::vector<uint8_t>& hash_map) | ||
| 261 | +{ | ||
| 262 | + constexpr float epsilon = 1e-5f; | ||
| 263 | + size_t bucket_byte_size = sizeof(Bucket); | ||
| 264 | + bucket_byte_size = (bucket_byte_size + ALIGN_BYTES - 1) & ~(ALIGN_BYTES - 1); | ||
| 265 | + size_t hash_map_total_byte_size = bucket_byte_size * HASH_TABLE_CAPACITY; | ||
| 266 | + const Bucket* bucket_addr = reinterpret_cast<const Bucket*>(hash_map.data()); | ||
| 267 | + uint32_t count = 0; | ||
| 268 | + for (int32_t i = 0; i < KEY_NUM; i++) { | ||
| 269 | + int64_t insert_key = keys[i]; | ||
| 270 | + uint32_t hash = murmur_hash_3_cpu(insert_key, INT64_TYPE_BYTES, 0); | ||
| 271 | + uint32_t pos = hash % HASH_TABLE_CAPACITY; | ||
| 272 | + for (uint32_t j = 0; j < HASH_TABLE_CAPACITY; j++) { | ||
| 273 | + uint32_t idx = (pos + j) % HASH_TABLE_CAPACITY; | ||
| 274 | + if (bucket_addr[idx].key == insert_key) { | ||
| 275 | + if (bucket_addr[idx].state != 1 || bucket_addr[idx].flag != HOLD_FLAG) { | ||
| 276 | + std::cout << "check value failed, key: " << bucket_addr[idx].key | ||
| 277 | + << ", state: " << bucket_addr[idx].state << ", flag" << bucket_addr[idx].flag | ||
| 278 | + << std::endl; | ||
| 279 | + std::cout << "expect key: " << insert_key << ", state: 1" << ", flag" << HOLD_FLAG | ||
| 280 | + << std::endl; | ||
| 281 | + break; | ||
| 282 | + } | ||
| 283 | + for (uint32_t k = 0; k < VALUE_DIM; k++) { | ||
| 284 | + if (std::abs(values[i][k] - bucket_addr[idx].value[k]) > epsilon) { | ||
| 285 | + std::cout << "check value failed, key: " << insert_key | ||
| 286 | + << ", value: " << bucket_addr[idx].value[k] << std::endl; | ||
| 287 | + std::cout << " expect, key: " << insert_key << ", value: " << values[i][k] | ||
| 288 | + << std::endl; | ||
| 289 | + count++; | ||
| 290 | + break; | ||
| 291 | + } | ||
| 292 | + } | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | + } | ||
| 296 | + if (count == 0) { | ||
| 297 | + std::cout << "[Success] find all key-value in hash map." << std::endl; | ||
| 298 | + } else { | ||
| 299 | + std::cout << "[Failed] total: " << count << " not find." << std::endl; | ||
| 300 | + } | ||
| 301 | +} | ||
| 302 | + | ||
| 303 | +int32_t main(int32_t argc, char* argv[]) | ||
| 304 | +{ | ||
| 305 | + std::vector<int64_t> keys(KEY_NUM); | ||
| 306 | + std::vector<std::array<float, VALUE_DIM>> values(KEY_NUM); | ||
| 307 | + generate_test_data(keys, values, KEY_NUM); | ||
| 308 | + std::vector<uint8_t> hash_map = launch_kernel(keys, values); | ||
| 309 | + check_result(keys, values, hash_map); | ||
| 310 | +} | ||
| @@ -0,0 +1,11 @@ | |||
| 1 | +# BestPractices样例介绍 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +基于Ascend C SIMT的算子开发实践,展示SIMT特性在实际算子开发中的运用。 | ||
| 6 | + | ||
| 7 | +## 算子开发样例 | ||
| 8 | + | ||
| 9 | +| 目录名称 | 功能描述 | | ||
| 10 | +| --------- | --------- | | ||
| 11 | +| [00_simt_insert_hash_table](./00_simt_insert_hash_table/) | 样例基于Ascend C SIMT编程方式实现InsertHashTable算子,展示SIMT算子处理复杂业务逻辑能力。 | | ||
| @@ -8,6 +8,7 @@ | |||
| 8 | | [00_introduction](./00_introduction) | 基于Ascend C SIMT编程的简单的示例,通过Ascend C编程语言实现了自定义算子,分别给出对应的<<<>>>直调实现,适合初学者 | | 8 | | [00_introduction](./00_introduction) | 基于Ascend C SIMT编程的简单的示例,通过Ascend C编程语言实现了自定义算子,分别给出对应的<<<>>>直调实现,适合初学者 | |
| 9 | | [01_utilities](./01_utilities) | 基于Ascend C SIMT的调试调优样例,通过printf、assert、clock等API介绍上板打印、打点、CPU孪生调试等系统工具使用方法,适用于调试阶段 | | 9 | | [01_utilities](./01_utilities) | 基于Ascend C SIMT的调试调优样例,通过printf、assert、clock等API介绍上板打印、打点、CPU孪生调试等系统工具使用方法,适用于调试阶段 | |
| 10 | | [02_features](./02_features) | 展示Ascend C SIMT编程的重点特性的样例,包含基础编程、内存资源管理、线程同步、原子操作等特性。 | | 10 | | [02_features](./02_features) | 展示Ascend C SIMT编程的重点特性的样例,包含基础编程、内存资源管理、线程同步、原子操作等特性。 | |
| 11 | +| [03_best_practices](./03_best_practices) | 展示Ascend C SIMT编程的最佳实践。 | | ||
| 11 | 12 | ||
| 12 | ## npu-arch编译选项说明 | 13 | ## npu-arch编译选项说明 |
| 13 | 14 | ||


外层未增加readme