Merged
添加SIMT编译样例 #2356
shaonaiteAdd on May 20
添加SIMT编译样例 #2356
Merged
Merged fromshaonaite/asc-devkitprofilingintocann/asc-devkitmaster
Total: 25 files changed+1528-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 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 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 14 | + | ||
| 15 | +find_package(ASC REQUIRED) | ||
| 16 | + | ||
| 17 | +project(basic_compile LANGUAGES ASC CXX) | ||
| 18 | + | ||
| 19 | +add_executable(demo | ||
| 20 | + gather_kernel.asc | ||
| 21 | + main.cpp | ||
| 22 | +) | ||
| 23 | + | ||
| 24 | +set_target_properties(demo PROPERTIES | ||
| 25 | + LINKER_LANGUAGE ASC | ||
| 26 | +) | ||
| 27 | + | ||
| 28 | +# ====================================================================================== | ||
| 29 | +# NPU 编译选项配置 | ||
| 30 | +# | ||
| 31 | +# 说明: | ||
| 32 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 33 | +# ====================================================================================== | ||
| 34 | +target_compile_options(demo PRIVATE | ||
| 35 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 36 | +) | ||
| 37 | + | ||
| 38 | +target_include_directories(demo PRIVATE | ||
| 39 | + $ENV{ASCEND_HOME_PATH}/include | ||
| 40 | +) | ||
Aexamples/03_simt_api/02_features/06_compile/00_basic_compile/README.md+144-0File content is under review, please try again later
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:60a260cbfecf0a85336b028787bee46386eb64af58433effbbba890367ba4251 | ||
| 3 | +size 64928 | ||
| @@ -0,0 +1,50 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file gather_kernel.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "acl/acl.h" | ||
| 18 | + | ||
| 19 | +namespace { | ||
| 20 | + constexpr uint32_t THREAD_COUNT = 2048; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +__global__ __launch_bounds__(THREAD_COUNT) void gather_custom( | ||
| 24 | + float* input, | ||
| 25 | + int32_t* index, | ||
| 26 | + float* output, | ||
| 27 | + uint32_t input_total_length, | ||
| 28 | + uint32_t index_total_length) | ||
| 29 | +{ | ||
| 30 | + uint32_t begin = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 31 | + uint32_t stride = gridDim.x * blockDim.x; | ||
| 32 | + | ||
| 33 | + for (uint32_t i = begin; i < index_total_length; i += stride) { | ||
| 34 | + int32_t input_idx = index[i]; | ||
| 35 | + if (input_idx >= 0 && input_idx < input_total_length) { | ||
| 36 | + output[i] = input[input_idx]; | ||
| 37 | + } else { | ||
| 38 | + output[i] = 0.0f; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 44 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 45 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 46 | + aclrtStream stream) | ||
| 47 | +{ | ||
| 48 | + gather_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>( | ||
| 49 | + input, index, output, input_total_length, index_total_length); | ||
| 50 | +} | ||
| @@ -0,0 +1,142 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file main.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <iostream> | ||
| 18 | +#include <iterator> | ||
| 19 | +#include <vector> | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | + | ||
| 22 | +extern void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 23 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 24 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 25 | + aclrtStream stream); | ||
| 26 | + | ||
| 27 | +std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index) | ||
| 28 | +{ | ||
| 29 | + if (input.empty() || index.empty()) { | ||
| 30 | + std::cout << "[ERROR] Empty input tensors." << std::endl; | ||
| 31 | + return {}; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + uint32_t input_total_length = input.size(); | ||
| 35 | + uint32_t index_total_length = index.size(); | ||
| 36 | + | ||
| 37 | + size_t input_total_byte_size = input_total_length * sizeof(float); | ||
| 38 | + size_t index_total_byte_size = index_total_length * sizeof(int32_t); | ||
| 39 | + size_t output_total_byte_size = index_total_length * sizeof(float); | ||
| 40 | + | ||
| 41 | + uint32_t thread_num_per_block = 2048; | ||
| 42 | + uint32_t block_num = (index_total_length + thread_num_per_block - 1) / thread_num_per_block; | ||
| 43 | + | ||
| 44 | + std::cout << "[INFO] Block number is " << block_num << "." << std::endl; | ||
| 45 | + std::cout << "[INFO] Thread number in a block is " << thread_num_per_block << "." << std::endl; | ||
| 46 | + | ||
| 47 | + int32_t device_id = 0; | ||
| 48 | + aclrtStream stream = nullptr; | ||
| 49 | + | ||
| 50 | + uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data()); | ||
| 51 | + uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data()); | ||
| 52 | + uint8_t* output_host = nullptr; | ||
| 53 | + float* input_device = nullptr; | ||
| 54 | + int32_t* index_device = nullptr; | ||
| 55 | + float* output_device = nullptr; | ||
| 56 | + | ||
| 57 | + aclInit(nullptr); | ||
| 58 | + aclrtSetDevice(device_id); | ||
| 59 | + aclrtCreateStream(&stream); | ||
| 60 | + | ||
| 61 | + aclrtMallocHost((void **)(&output_host), output_total_byte_size); | ||
| 62 | + aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 63 | + aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 64 | + aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 65 | + | ||
| 66 | + aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 67 | + aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 68 | + | ||
| 69 | + uint32_t dyn_ubuf_size = 0; | ||
| 70 | + run_gather_custom(input_device, index_device, output_device, input_total_length, index_total_length, | ||
| 71 | + block_num, thread_num_per_block, dyn_ubuf_size, stream); | ||
| 72 | + | ||
| 73 | + aclrtSynchronizeStream(stream); | ||
| 74 | + aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 75 | + std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size)); | ||
| 76 | + | ||
| 77 | + aclrtFree(input_device); | ||
| 78 | + aclrtFree(index_device); | ||
| 79 | + aclrtFree(output_device); | ||
| 80 | + aclrtFreeHost(output_host); | ||
| 81 | + | ||
| 82 | + const char* err = aclGetRecentErrMsg(); | ||
| 83 | + if (err != nullptr) { | ||
| 84 | + fprintf(stderr, "%s\n", err); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + aclrtDestroyStream(stream); | ||
| 88 | + aclrtResetDevice(device_id); | ||
| 89 | + aclFinalize(); | ||
| 90 | + return output; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden) | ||
| 94 | +{ | ||
| 95 | + auto print_tensor = [](std::vector<float>& tensor, const char* name) { | ||
| 96 | + constexpr size_t max_print_size = 20; | ||
| 97 | + std::cout << name << ": "; | ||
| 98 | + std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size), | ||
| 99 | + std::ostream_iterator<float>(std::cout, " ")); | ||
| 100 | + if (tensor.size() > max_print_size) { | ||
| 101 | + std::cout << "..."; | ||
| 102 | + } | ||
| 103 | + std::cout << std::endl; | ||
| 104 | + }; | ||
| 105 | + print_tensor(output, "Output"); | ||
| 106 | + print_tensor(golden, "Golden"); | ||
| 107 | + if (std::equal(output.begin(), output.end(), golden.begin())) { | ||
| 108 | + std::cout << "[Success] Case accuracy is verification passed." << std::endl; | ||
| 109 | + return 0; | ||
| 110 | + } else { | ||
| 111 | + std::cout << "[Failed] Case accuracy is verification failed!" << std::endl; | ||
| 112 | + return 1; | ||
| 113 | + } | ||
| 114 | + return 0; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +int32_t main(int32_t argc, char* argv[]) | ||
| 118 | +{ | ||
| 119 | + constexpr uint32_t input_total_length = 100000; | ||
| 120 | + constexpr uint32_t index_total_length = 12288; | ||
| 121 | + | ||
| 122 | + std::vector<float> input(input_total_length); | ||
| 123 | + for (uint32_t i = 0; i < input_total_length; i++) { | ||
| 124 | + input[i] = i * 1.2f; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + std::vector<int32_t> index(index_total_length); | ||
| 128 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 129 | + index[i] = static_cast<int32_t>((i * 97 + 13) % input_total_length); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + std::vector<float> golden(index_total_length); | ||
| 133 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 134 | + golden[i] = input[index[i]]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + std::vector<float> output = gather_1d(input, index); | ||
| 138 | + if (output.empty()) { | ||
| 139 | + return 1; | ||
| 140 | + } | ||
| 141 | + return verify_result(output, golden); | ||
| 142 | +} | ||
| @@ -0,0 +1,41 @@ | |||
| 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 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 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 14 | + | ||
| 15 | +find_package(ASC REQUIRED) | ||
| 16 | + | ||
| 17 | +project(separate_compile LANGUAGES ASC CXX) | ||
| 18 | + | ||
| 19 | +add_executable(demo | ||
| 20 | + gather_compute.asc | ||
| 21 | + gather_kernel.asc | ||
| 22 | + main.cpp | ||
| 23 | +) | ||
| 24 | + | ||
| 25 | +set_target_properties(demo PROPERTIES | ||
| 26 | + LINKER_LANGUAGE ASC | ||
| 27 | +) | ||
| 28 | + | ||
| 29 | +# ====================================================================================== | ||
| 30 | +# NPU 编译选项配置 | ||
| 31 | +# | ||
| 32 | +# 说明: | ||
| 33 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 34 | +# ====================================================================================== | ||
| 35 | +target_compile_options(demo PRIVATE | ||
| 36 | + $<$<COMPILE_LANGUAGE:ASC>:-dc --npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 37 | +) | ||
| 38 | + | ||
| 39 | +target_include_directories(demo PRIVATE | ||
| 40 | + $ENV{ASCEND_HOME_PATH}/include | ||
| 41 | +) | ||
Aexamples/03_simt_api/02_features/06_compile/01_separate_compile/README.md+150-0File content is under review, please try again later
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:cba3b137064a10cb78bcfe66d27ff9d187bb0c7b79951c714b7c94cf50549dfd | ||
| 3 | +size 85811 | ||
| @@ -0,0 +1,24 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file gather_compute.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +__aicore__ float gather_element(float input_val, int32_t input_idx, uint32_t input_total_length) | ||
| 18 | +{ | ||
| 19 | + if (input_idx >= 0 && input_idx < input_total_length) { | ||
| 20 | + return input_val; | ||
| 21 | + } else { | ||
| 22 | + return 0.0f; | ||
| 23 | + } | ||
| 24 | +} | ||
| @@ -0,0 +1,48 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file gather_kernel.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "acl/acl.h" | ||
| 18 | + | ||
| 19 | +namespace { | ||
| 20 | + constexpr uint32_t THREAD_COUNT = 2048; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +extern __aicore__ float gather_element(float input_val, int32_t input_idx, uint32_t input_total_length); | ||
| 24 | + | ||
| 25 | +__global__ __launch_bounds__(THREAD_COUNT) void gather_custom( | ||
| 26 | + float* input, | ||
| 27 | + int32_t* index, | ||
| 28 | + float* output, | ||
| 29 | + uint32_t input_total_length, | ||
| 30 | + uint32_t index_total_length) | ||
| 31 | +{ | ||
| 32 | + uint32_t begin = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 33 | + uint32_t stride = gridDim.x * blockDim.x; | ||
| 34 | + | ||
| 35 | + for (uint32_t i = begin; i < index_total_length; i += stride) { | ||
| 36 | + int32_t input_idx = index[i]; | ||
| 37 | + output[i] = gather_element(input[input_idx], input_idx, input_total_length); | ||
| 38 | + } | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 42 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 43 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 44 | + aclrtStream stream) | ||
| 45 | +{ | ||
| 46 | + gather_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>( | ||
| 47 | + input, index, output, input_total_length, index_total_length); | ||
| 48 | +} | ||
| @@ -0,0 +1,142 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file main.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <iostream> | ||
| 18 | +#include <iterator> | ||
| 19 | +#include <vector> | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | + | ||
| 22 | +extern void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 23 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 24 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 25 | + aclrtStream stream); | ||
| 26 | + | ||
| 27 | +std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index) | ||
| 28 | +{ | ||
| 29 | + if (input.empty() || index.empty()) { | ||
| 30 | + std::cout << "[ERROR] Empty input tensors." << std::endl; | ||
| 31 | + return {}; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + uint32_t input_total_length = input.size(); | ||
| 35 | + uint32_t index_total_length = index.size(); | ||
| 36 | + | ||
| 37 | + size_t input_total_byte_size = input_total_length * sizeof(float); | ||
| 38 | + size_t index_total_byte_size = index_total_length * sizeof(int32_t); | ||
| 39 | + size_t output_total_byte_size = index_total_length * sizeof(float); | ||
| 40 | + | ||
| 41 | + uint32_t thread_num_per_block = 2048; | ||
| 42 | + uint32_t block_num = (index_total_length + thread_num_per_block - 1) / thread_num_per_block; | ||
| 43 | + | ||
| 44 | + std::cout << "[INFO] Block number is " << block_num << "." << std::endl; | ||
| 45 | + std::cout << "[INFO] Thread number in a block is " << thread_num_per_block << "." << std::endl; | ||
| 46 | + | ||
| 47 | + int32_t device_id = 0; | ||
| 48 | + aclrtStream stream = nullptr; | ||
| 49 | + | ||
| 50 | + uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data()); | ||
| 51 | + uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data()); | ||
| 52 | + uint8_t* output_host = nullptr; | ||
| 53 | + float* input_device = nullptr; | ||
| 54 | + int32_t* index_device = nullptr; | ||
| 55 | + float* output_device = nullptr; | ||
| 56 | + | ||
| 57 | + aclInit(nullptr); | ||
| 58 | + aclrtSetDevice(device_id); | ||
| 59 | + aclrtCreateStream(&stream); | ||
| 60 | + | ||
| 61 | + aclrtMallocHost((void **)(&output_host), output_total_byte_size); | ||
| 62 | + aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 63 | + aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 64 | + aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 65 | + | ||
| 66 | + aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 67 | + aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 68 | + | ||
| 69 | + uint32_t dyn_ubuf_size = 0; | ||
| 70 | + run_gather_custom(input_device, index_device, output_device, input_total_length, index_total_length, | ||
| 71 | + block_num, thread_num_per_block, dyn_ubuf_size, stream); | ||
| 72 | + | ||
| 73 | + aclrtSynchronizeStream(stream); | ||
| 74 | + aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 75 | + std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size)); | ||
| 76 | + | ||
| 77 | + aclrtFree(input_device); | ||
| 78 | + aclrtFree(index_device); | ||
| 79 | + aclrtFree(output_device); | ||
| 80 | + aclrtFreeHost(output_host); | ||
| 81 | + | ||
| 82 | + const char* err = aclGetRecentErrMsg(); | ||
| 83 | + if (err != nullptr) { | ||
| 84 | + fprintf(stderr, "%s\n", err); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + aclrtDestroyStream(stream); | ||
| 88 | + aclrtResetDevice(device_id); | ||
| 89 | + aclFinalize(); | ||
| 90 | + return output; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden) | ||
| 94 | +{ | ||
| 95 | + auto print_tensor = [](std::vector<float>& tensor, const char* name) { | ||
| 96 | + constexpr size_t max_print_size = 20; | ||
| 97 | + std::cout << name << ": "; | ||
| 98 | + std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size), | ||
| 99 | + std::ostream_iterator<float>(std::cout, " ")); | ||
| 100 | + if (tensor.size() > max_print_size) { | ||
| 101 | + std::cout << "..."; | ||
| 102 | + } | ||
| 103 | + std::cout << std::endl; | ||
| 104 | + }; | ||
| 105 | + print_tensor(output, "Output"); | ||
| 106 | + print_tensor(golden, "Golden"); | ||
| 107 | + if (std::equal(output.begin(), output.end(), golden.begin())) { | ||
| 108 | + std::cout << "[Success] Case accuracy is verification passed." << std::endl; | ||
| 109 | + return 0; | ||
| 110 | + } else { | ||
| 111 | + std::cout << "[Failed] Case accuracy is verification failed!" << std::endl; | ||
| 112 | + return 1; | ||
| 113 | + } | ||
| 114 | + return 0; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +int32_t main(int32_t argc, char* argv[]) | ||
| 118 | +{ | ||
| 119 | + constexpr uint32_t input_total_length = 100000; | ||
| 120 | + constexpr uint32_t index_total_length = 12288; | ||
| 121 | + | ||
| 122 | + std::vector<float> input(input_total_length); | ||
| 123 | + for (uint32_t i = 0; i < input_total_length; i++) { | ||
| 124 | + input[i] = i * 1.2f; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + std::vector<int32_t> index(index_total_length); | ||
| 128 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 129 | + index[i] = static_cast<int32_t>((i * 97 + 13) % input_total_length); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + std::vector<float> golden(index_total_length); | ||
| 133 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 134 | + golden[i] = input[index[i]]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + std::vector<float> output = gather_1d(input, index); | ||
| 138 | + if (output.empty()) { | ||
| 139 | + return 1; | ||
| 140 | + } | ||
| 141 | + return verify_result(output, golden); | ||
| 142 | +} | ||
| @@ -0,0 +1,47 @@ | |||
| 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 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 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 14 | + | ||
| 15 | +find_package(ASC REQUIRED) | ||
| 16 | + | ||
| 17 | +project(dynamic_library_compile LANGUAGES ASC CXX) | ||
| 18 | + | ||
| 19 | +add_library(gather_kernel SHARED | ||
| 20 | + gather_kernel.asc | ||
| 21 | +) | ||
| 22 | + | ||
| 23 | +add_executable(demo | ||
| 24 | + main.cpp | ||
| 25 | +) | ||
| 26 | + | ||
| 27 | +set_target_properties(demo PROPERTIES | ||
| 28 | + LINKER_LANGUAGE ASC | ||
| 29 | +) | ||
| 30 | + | ||
| 31 | +# ====================================================================================== | ||
| 32 | +# NPU 编译选项配置 | ||
| 33 | +# | ||
| 34 | +# 说明: | ||
| 35 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 36 | +# ====================================================================================== | ||
| 37 | +target_compile_options(gather_kernel PRIVATE | ||
| 38 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 39 | +) | ||
| 40 | + | ||
| 41 | +target_include_directories(demo PRIVATE | ||
| 42 | + $ENV{ASCEND_HOME_PATH}/include | ||
| 43 | +) | ||
| 44 | + | ||
| 45 | +target_link_libraries(demo PRIVATE | ||
| 46 | + gather_kernel | ||
| 47 | +) | ||
Aexamples/03_simt_api/02_features/06_compile/02_dynamic_library_compile/README.md+114-0File content is under review, please try again later
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:5ad2d4356381e236f3e896a57a738c098d586989e4b460bde5d803e4f514b9de | ||
| 3 | +size 65455 | ||
| @@ -0,0 +1,50 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file gather_kernel.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "acl/acl.h" | ||
| 18 | + | ||
| 19 | +namespace { | ||
| 20 | + constexpr uint32_t THREAD_COUNT = 2048; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +__global__ __launch_bounds__(THREAD_COUNT) void gather_custom( | ||
| 24 | + float* input, | ||
| 25 | + int32_t* index, | ||
| 26 | + float* output, | ||
| 27 | + uint32_t input_total_length, | ||
| 28 | + uint32_t index_total_length) | ||
| 29 | +{ | ||
| 30 | + uint32_t begin = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 31 | + uint32_t stride = gridDim.x * blockDim.x; | ||
| 32 | + | ||
| 33 | + for (uint32_t i = begin; i < index_total_length; i += stride) { | ||
| 34 | + int32_t input_idx = index[i]; | ||
| 35 | + if (input_idx >= 0 && input_idx < input_total_length) { | ||
| 36 | + output[i] = input[input_idx]; | ||
| 37 | + } else { | ||
| 38 | + output[i] = 0.0f; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 44 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 45 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 46 | + aclrtStream stream) | ||
| 47 | +{ | ||
| 48 | + gather_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>( | ||
| 49 | + input, index, output, input_total_length, index_total_length); | ||
| 50 | +} | ||
| @@ -0,0 +1,142 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file main.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <iostream> | ||
| 18 | +#include <iterator> | ||
| 19 | +#include <vector> | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | + | ||
| 22 | +extern void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 23 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 24 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 25 | + aclrtStream stream); | ||
| 26 | + | ||
| 27 | +std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index) | ||
| 28 | +{ | ||
| 29 | + if (input.empty() || index.empty()) { | ||
| 30 | + std::cout << "[ERROR] Empty input tensors." << std::endl; | ||
| 31 | + return {}; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + uint32_t input_total_length = input.size(); | ||
| 35 | + uint32_t index_total_length = index.size(); | ||
| 36 | + | ||
| 37 | + size_t input_total_byte_size = input_total_length * sizeof(float); | ||
| 38 | + size_t index_total_byte_size = index_total_length * sizeof(int32_t); | ||
| 39 | + size_t output_total_byte_size = index_total_length * sizeof(float); | ||
| 40 | + | ||
| 41 | + uint32_t thread_num_per_block = 2048; | ||
| 42 | + uint32_t block_num = (index_total_length + thread_num_per_block - 1) / thread_num_per_block; | ||
| 43 | + | ||
| 44 | + std::cout << "[INFO] Block number is " << block_num << "." << std::endl; | ||
| 45 | + std::cout << "[INFO] Thread number in a block is " << thread_num_per_block << "." << std::endl; | ||
| 46 | + | ||
| 47 | + int32_t device_id = 0; | ||
| 48 | + aclrtStream stream = nullptr; | ||
| 49 | + | ||
| 50 | + uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data()); | ||
| 51 | + uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data()); | ||
| 52 | + uint8_t* output_host = nullptr; | ||
| 53 | + float* input_device = nullptr; | ||
| 54 | + int32_t* index_device = nullptr; | ||
| 55 | + float* output_device = nullptr; | ||
| 56 | + | ||
| 57 | + aclInit(nullptr); | ||
| 58 | + aclrtSetDevice(device_id); | ||
| 59 | + aclrtCreateStream(&stream); | ||
| 60 | + | ||
| 61 | + aclrtMallocHost((void **)(&output_host), output_total_byte_size); | ||
| 62 | + aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 63 | + aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 64 | + aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 65 | + | ||
| 66 | + aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 67 | + aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 68 | + | ||
| 69 | + uint32_t dyn_ubuf_size = 0; | ||
| 70 | + run_gather_custom(input_device, index_device, output_device, input_total_length, index_total_length, | ||
| 71 | + block_num, thread_num_per_block, dyn_ubuf_size, stream); | ||
| 72 | + | ||
| 73 | + aclrtSynchronizeStream(stream); | ||
| 74 | + aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 75 | + std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size)); | ||
| 76 | + | ||
| 77 | + aclrtFree(input_device); | ||
| 78 | + aclrtFree(index_device); | ||
| 79 | + aclrtFree(output_device); | ||
| 80 | + aclrtFreeHost(output_host); | ||
| 81 | + | ||
| 82 | + const char* err = aclGetRecentErrMsg(); | ||
| 83 | + if (err != nullptr) { | ||
| 84 | + fprintf(stderr, "%s\n", err); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + aclrtDestroyStream(stream); | ||
| 88 | + aclrtResetDevice(device_id); | ||
| 89 | + aclFinalize(); | ||
| 90 | + return output; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden) | ||
| 94 | +{ | ||
| 95 | + auto print_tensor = [](std::vector<float>& tensor, const char* name) { | ||
| 96 | + constexpr size_t max_print_size = 20; | ||
| 97 | + std::cout << name << ": "; | ||
| 98 | + std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size), | ||
| 99 | + std::ostream_iterator<float>(std::cout, " ")); | ||
| 100 | + if (tensor.size() > max_print_size) { | ||
| 101 | + std::cout << "..."; | ||
| 102 | + } | ||
| 103 | + std::cout << std::endl; | ||
| 104 | + }; | ||
| 105 | + print_tensor(output, "Output"); | ||
| 106 | + print_tensor(golden, "Golden"); | ||
| 107 | + if (std::equal(output.begin(), output.end(), golden.begin())) { | ||
| 108 | + std::cout << "[Success] Case accuracy is verification passed." << std::endl; | ||
| 109 | + return 0; | ||
| 110 | + } else { | ||
| 111 | + std::cout << "[Failed] Case accuracy is verification failed!" << std::endl; | ||
| 112 | + return 1; | ||
| 113 | + } | ||
| 114 | + return 0; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +int32_t main(int32_t argc, char* argv[]) | ||
| 118 | +{ | ||
| 119 | + constexpr uint32_t input_total_length = 100000; | ||
| 120 | + constexpr uint32_t index_total_length = 12288; | ||
| 121 | + | ||
| 122 | + std::vector<float> input(input_total_length); | ||
| 123 | + for (uint32_t i = 0; i < input_total_length; i++) { | ||
| 124 | + input[i] = i * 1.2f; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + std::vector<int32_t> index(index_total_length); | ||
| 128 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 129 | + index[i] = static_cast<int32_t>((i * 97 + 13) % input_total_length); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + std::vector<float> golden(index_total_length); | ||
| 133 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 134 | + golden[i] = input[index[i]]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + std::vector<float> output = gather_1d(input, index); | ||
| 138 | + if (output.empty()) { | ||
| 139 | + return 1; | ||
| 140 | + } | ||
| 141 | + return verify_result(output, golden); | ||
| 142 | +} | ||
| @@ -0,0 +1,47 @@ | |||
| 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 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 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 14 | + | ||
| 15 | +find_package(ASC REQUIRED) | ||
| 16 | + | ||
| 17 | +project(static_library_compile LANGUAGES ASC CXX) | ||
| 18 | + | ||
| 19 | +add_library(gather_kernel STATIC | ||
| 20 | + gather_kernel.asc | ||
| 21 | +) | ||
| 22 | + | ||
| 23 | +add_executable(demo | ||
| 24 | + main.cpp | ||
| 25 | +) | ||
| 26 | + | ||
| 27 | +set_target_properties(demo PROPERTIES | ||
| 28 | + LINKER_LANGUAGE ASC | ||
| 29 | +) | ||
| 30 | + | ||
| 31 | +# ====================================================================================== | ||
| 32 | +# NPU 编译选项配置 | ||
| 33 | +# | ||
| 34 | +# 说明: | ||
| 35 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 36 | +# ====================================================================================== | ||
| 37 | +target_compile_options(gather_kernel PRIVATE | ||
| 38 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 39 | +) | ||
| 40 | + | ||
| 41 | +target_include_directories(demo PRIVATE | ||
| 42 | + $ENV{ASCEND_HOME_PATH}/include | ||
| 43 | +) | ||
| 44 | + | ||
| 45 | +target_link_libraries(demo PRIVATE | ||
| 46 | + gather_kernel | ||
| 47 | +) | ||
Aexamples/03_simt_api/02_features/06_compile/03_static_library_compile/README.md+113-0File content is under review, please try again later
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:6ccae239cfa7c70d770e4ee68a7706e129060df6cc2933102f6054a173980608 | ||
| 3 | +size 65301 | ||
| @@ -0,0 +1,50 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file gather_kernel.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include "acl/acl.h" | ||
| 18 | + | ||
| 19 | +namespace { | ||
| 20 | + constexpr uint32_t THREAD_COUNT = 2048; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +__global__ __launch_bounds__(THREAD_COUNT) void gather_custom( | ||
| 24 | + float* input, | ||
| 25 | + int32_t* index, | ||
| 26 | + float* output, | ||
| 27 | + uint32_t input_total_length, | ||
| 28 | + uint32_t index_total_length) | ||
| 29 | +{ | ||
| 30 | + uint32_t begin = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 31 | + uint32_t stride = gridDim.x * blockDim.x; | ||
| 32 | + | ||
| 33 | + for (uint32_t i = begin; i < index_total_length; i += stride) { | ||
| 34 | + int32_t input_idx = index[i]; | ||
| 35 | + if (input_idx >= 0 && input_idx < input_total_length) { | ||
| 36 | + output[i] = input[input_idx]; | ||
| 37 | + } else { | ||
| 38 | + output[i] = 0.0f; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 44 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 45 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 46 | + aclrtStream stream) | ||
| 47 | +{ | ||
| 48 | + gather_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>( | ||
| 49 | + input, index, output, input_total_length, index_total_length); | ||
| 50 | +} | ||
| @@ -0,0 +1,142 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file main.cpp | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <iostream> | ||
| 18 | +#include <iterator> | ||
| 19 | +#include <vector> | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | + | ||
| 22 | +extern void run_gather_custom(float* input, int32_t* index, float* output, | ||
| 23 | + uint32_t input_total_length, uint32_t index_total_length, | ||
| 24 | + uint32_t block_num, uint32_t thread_num_per_block, uint32_t dyn_ubuf_size, | ||
| 25 | + aclrtStream stream); | ||
| 26 | + | ||
| 27 | +std::vector<float> gather_1d(std::vector<float>& input, std::vector<int32_t>& index) | ||
| 28 | +{ | ||
| 29 | + if (input.empty() || index.empty()) { | ||
| 30 | + std::cout << "[ERROR] Empty input tensors." << std::endl; | ||
| 31 | + return {}; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + uint32_t input_total_length = input.size(); | ||
| 35 | + uint32_t index_total_length = index.size(); | ||
| 36 | + | ||
| 37 | + size_t input_total_byte_size = input_total_length * sizeof(float); | ||
| 38 | + size_t index_total_byte_size = index_total_length * sizeof(int32_t); | ||
| 39 | + size_t output_total_byte_size = index_total_length * sizeof(float); | ||
| 40 | + | ||
| 41 | + uint32_t thread_num_per_block = 2048; | ||
| 42 | + uint32_t block_num = (index_total_length + thread_num_per_block - 1) / thread_num_per_block; | ||
| 43 | + | ||
| 44 | + std::cout << "[INFO] Block number is " << block_num << "." << std::endl; | ||
| 45 | + std::cout << "[INFO] Thread number in a block is " << thread_num_per_block << "." << std::endl; | ||
| 46 | + | ||
| 47 | + int32_t device_id = 0; | ||
| 48 | + aclrtStream stream = nullptr; | ||
| 49 | + | ||
| 50 | + uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data()); | ||
| 51 | + uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data()); | ||
| 52 | + uint8_t* output_host = nullptr; | ||
| 53 | + float* input_device = nullptr; | ||
| 54 | + int32_t* index_device = nullptr; | ||
| 55 | + float* output_device = nullptr; | ||
| 56 | + | ||
| 57 | + aclInit(nullptr); | ||
| 58 | + aclrtSetDevice(device_id); | ||
| 59 | + aclrtCreateStream(&stream); | ||
| 60 | + | ||
| 61 | + aclrtMallocHost((void **)(&output_host), output_total_byte_size); | ||
| 62 | + aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 63 | + aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 64 | + aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 65 | + | ||
| 66 | + aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 67 | + aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 68 | + | ||
| 69 | + uint32_t dyn_ubuf_size = 0; | ||
| 70 | + run_gather_custom(input_device, index_device, output_device, input_total_length, index_total_length, | ||
| 71 | + block_num, thread_num_per_block, dyn_ubuf_size, stream); | ||
| 72 | + | ||
| 73 | + aclrtSynchronizeStream(stream); | ||
| 74 | + aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 75 | + std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size)); | ||
| 76 | + | ||
| 77 | + aclrtFree(input_device); | ||
| 78 | + aclrtFree(index_device); | ||
| 79 | + aclrtFree(output_device); | ||
| 80 | + aclrtFreeHost(output_host); | ||
| 81 | + | ||
| 82 | + const char* err = aclGetRecentErrMsg(); | ||
| 83 | + if (err != nullptr) { | ||
| 84 | + fprintf(stderr, "%s\n", err); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + aclrtDestroyStream(stream); | ||
| 88 | + aclrtResetDevice(device_id); | ||
| 89 | + aclFinalize(); | ||
| 90 | + return output; | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden) | ||
| 94 | +{ | ||
| 95 | + auto print_tensor = [](std::vector<float>& tensor, const char* name) { | ||
| 96 | + constexpr size_t max_print_size = 20; | ||
| 97 | + std::cout << name << ": "; | ||
| 98 | + std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size), | ||
| 99 | + std::ostream_iterator<float>(std::cout, " ")); | ||
| 100 | + if (tensor.size() > max_print_size) { | ||
| 101 | + std::cout << "..."; | ||
| 102 | + } | ||
| 103 | + std::cout << std::endl; | ||
| 104 | + }; | ||
| 105 | + print_tensor(output, "Output"); | ||
| 106 | + print_tensor(golden, "Golden"); | ||
| 107 | + if (std::equal(output.begin(), output.end(), golden.begin())) { | ||
| 108 | + std::cout << "[Success] Case accuracy is verification passed." << std::endl; | ||
| 109 | + return 0; | ||
| 110 | + } else { | ||
| 111 | + std::cout << "[Failed] Case accuracy is verification failed!" << std::endl; | ||
| 112 | + return 1; | ||
| 113 | + } | ||
| 114 | + return 0; | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +int32_t main(int32_t argc, char* argv[]) | ||
| 118 | +{ | ||
| 119 | + constexpr uint32_t input_total_length = 100000; | ||
| 120 | + constexpr uint32_t index_total_length = 12288; | ||
| 121 | + | ||
| 122 | + std::vector<float> input(input_total_length); | ||
| 123 | + for (uint32_t i = 0; i < input_total_length; i++) { | ||
| 124 | + input[i] = i * 1.2f; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + std::vector<int32_t> index(index_total_length); | ||
| 128 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 129 | + index[i] = static_cast<int32_t>((i * 97 + 13) % input_total_length); | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + std::vector<float> golden(index_total_length); | ||
| 133 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 134 | + golden[i] = input[index[i]]; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + std::vector<float> output = gather_1d(input, index); | ||
| 138 | + if (output.empty()) { | ||
| 139 | + return 1; | ||
| 140 | + } | ||
| 141 | + return verify_result(output, golden); | ||
| 142 | +} | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | +# Compile样例介绍 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +基于Ascend C SIMT的编译样例,展示SIMT编程的各类编译模式。 | ||
| 6 | + | ||
| 7 | +## 算子开发样例 | ||
| 8 | + | ||
| 9 | +| 目录名称 | 功能描述 | | ||
| 10 | +|-----------------------------------------------------|------------------------| | ||
| 11 | +| [00_basic_compile](./00_basic_compile) | 展示Host侧.cpp与Device侧.asc混合文件的基本异构编译流程。 | | ||
| 12 | +| [01_separate_compile](./01_separate_compile) | 展示将Device侧代码拆分到多个.asc文件中,通过单独编译实现跨文件设备函数调用的流程。 | | ||
| 13 | +| [02_dynamic_library_compile](./02_dynamic_library_compile) | 展示将Device侧.asc编译为动态共享库(.so)的编译流程。 | | ||
| 14 | +| [03_static_library_compile](./03_static_library_compile) | 展示将Device侧.asc编译为静态库(.a)的编译流程。 | | ||
| @@ -0,0 +1,14 @@ | |||
| 1 | +# Compile Sample Introduction | ||
| 2 | + | ||
| 3 | +## Overview | ||
| 4 | + | ||
| 5 | +This document presents compile samples based on Ascend C SIMT, demonstrating various SIMT compilation modes. | ||
| 6 | + | ||
| 7 | +## Operator Development Samples | ||
| 8 | + | ||
| 9 | +| Directory Name | Description | | ||
| 10 | +|-----------------------------------------------------|------------------------| | ||
| 11 | +| [00_basic_compile](./00_basic_compile) | Demonstrates the basic heterogeneous compilation workflow of Host-side .cpp and Device-side .asc mixed files. | | ||
| 12 | +| [01_separate_compile](./01_separate_compile) | Demonstrates splitting Device-side code into multiple .asc files with separate compilation, enabling cross-file device function calls. | | ||
| 13 | +| [02_dynamic_library_compile](./02_dynamic_library_compile) | Demonstrates compiling Device-side .asc files into dynamic shared libraries (.so). | | ||
| 14 | +| [03_static_library_compile](./03_static_library_compile) | Demonstrates compiling Device-side .asc files into static libraries (.a). | | ||
| @@ -9,3 +9,4 @@ This document presents feature samples based on Ascend C SIMT, demonstrating var | |||
| 9 | | Directory Name | Description | | 9 | | Directory Name | Description | |
| 10 | |----------------------------------------------------|--------------------| | 10 | |----------------------------------------------------|--------------------| |
| 11 | | [01_api_features](./01_api_features/) | Samples demonstrating API-related features in SIMT. | | 11 | | [01_api_features](./01_api_features/) | Samples demonstrating API-related features in SIMT. | |
| 12 | +| [06_compile](./06_compile) | Samples demonstrating SIMT compile-related features. | | ||
新增样例 可以使用set(CMAKE_ASC_ENABLE_SIMT, ON) 使能simt 编译