Merged
simt and simd best practice #2363
zc1110Add on May 21
simt and simd best practice #2363
Merged
Total: 10 files changed+1137-14
Mexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/README.md+10-6File content is under review, please try again later
Mexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/README_en.md+11-6File content is under review, please try again later
Mexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/matmul_gelu_high_performance/README.md+1-2File content is under review, please try again later
Aexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/simt_and_simd_high_performance/CMakeLists.txt+36-0File content is under review, please try again later
Aexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/simt_and_simd_high_performance/README.md+450-0File content is under review, please try again later
| @@ -0,0 +1,93 @@ | |||
| 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 data_utils.h | ||
| 13 | + * \brief Utility functions for reading and writing binary files | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#ifndef DATA_UTILS_H | ||
| 17 | +#define DATA_UTILS_H | ||
| 18 | +#include <fcntl.h> | ||
| 19 | +#include <sys/stat.h> | ||
| 20 | +#include <unistd.h> | ||
| 21 | +#include <fstream> | ||
| 22 | + | ||
| 23 | +#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) | ||
| 24 | + | ||
| 25 | +bool ReadFile(const std::string& filePath, size_t& fileSize, void* buffer, size_t bufferSize) | ||
| 26 | +{ | ||
| 27 | + struct stat sBuf; | ||
| 28 | + int fileStatus = stat(filePath.data(), &sBuf); | ||
| 29 | + if (fileStatus == -1) { | ||
| 30 | + ERROR_LOG("failed to get file"); | ||
| 31 | + return false; | ||
| 32 | + } | ||
| 33 | + if (S_ISREG(sBuf.st_mode) == 0) { | ||
| 34 | + ERROR_LOG("%s is not a file, please enter a file", filePath.c_str()); | ||
| 35 | + return false; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + std::ifstream file; | ||
| 39 | + file.open(filePath, std::ios::binary); | ||
| 40 | + if (!file.is_open()) { | ||
| 41 | + ERROR_LOG("Open file failed. path = %s", filePath.c_str()); | ||
| 42 | + return false; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + std::filebuf* buf = file.rdbuf(); | ||
| 46 | + size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); | ||
| 47 | + if (size == 0) { | ||
| 48 | + ERROR_LOG("file size is 0"); | ||
| 49 | + file.close(); | ||
| 50 | + return false; | ||
| 51 | + } | ||
| 52 | + if (size > bufferSize) { | ||
| 53 | + ERROR_LOG("file size is larger than buffer size"); | ||
| 54 | + file.close(); | ||
| 55 | + return false; | ||
| 56 | + } | ||
| 57 | + buf->pubseekpos(0, std::ios::in); | ||
| 58 | + buf->sgetn(static_cast<char*>(buffer), size); | ||
| 59 | + fileSize = size; | ||
| 60 | + file.close(); | ||
| 61 | + return true; | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +/** | ||
| 65 | + * @brief Write data to file | ||
| 66 | + * @param [in] filePath: file path | ||
| 67 | + * @param [in] buffer: data to write to file | ||
| 68 | + * @param [in] size: size to write | ||
| 69 | + * @return write result | ||
| 70 | + */ | ||
| 71 | +bool WriteFile(const std::string& filePath, const void* buffer, size_t size) | ||
| 72 | +{ | ||
| 73 | + if (buffer == nullptr) { | ||
| 74 | + ERROR_LOG("Write file failed. buffer is nullptr"); | ||
| 75 | + return false; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + int fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE); | ||
| 79 | + if (fd < 0) { | ||
| 80 | + ERROR_LOG("Open file failed. path = %s", filePath.c_str()); | ||
| 81 | + return false; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + size_t writeSize = write(fd, buffer, size); | ||
| 85 | + (void)close(fd); | ||
| 86 | + if (writeSize != size) { | ||
| 87 | + ERROR_LOG("Write file Failed."); | ||
| 88 | + return false; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + return true; | ||
| 92 | +} | ||
| 93 | +#endif // DATA_UTILS_H | ||
| @@ -0,0 +1,3 @@ | |||
| 1 | +version https://git-lfs.github.com/spec/v1 | ||
| 2 | +oid sha256:4d4525d47ced6b0ed67aea861d0f852fa0a26a8af75fc9b4e976ef7d49427f82 | ||
| 3 | +size 159719 | ||
Aexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/simt_and_simd_high_performance/floor_mod.asc+437-0File content is under review, please try again later
| @@ -0,0 +1,50 @@ | |||
| 1 | +#!/usr/bin/python3 | ||
| 2 | +# coding=utf-8 | ||
| 3 | + | ||
| 4 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 6 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 7 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 8 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 9 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 10 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 11 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 12 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +import os | ||
| 16 | +import numpy as np | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +def gen_golden_data_int(): | ||
| 20 | + input_total_length = 8192 * 8192 | ||
| 21 | + rng = np.random.default_rng(20260516) | ||
| 22 | + self_data = rng.integers(-100000, 100001, input_total_length, dtype=np.int32) | ||
| 23 | + other_data = rng.integers(-127, 128, input_total_length, dtype=np.int32) | ||
| 24 | + other_data[other_data == 0] = 7 | ||
| 25 | + | ||
| 26 | + divisible_indexes = np.arange(0, input_total_length, 64) | ||
| 27 | + multipliers = rng.integers(-2048, 2049, divisible_indexes.size, dtype=np.int32) | ||
| 28 | + self_data[divisible_indexes] = other_data[divisible_indexes] * multipliers | ||
| 29 | + | ||
| 30 | + golden = np.zeros(input_total_length, dtype=np.int32) | ||
| 31 | + chunk_size = 1 << 20 | ||
| 32 | + for start in range(0, input_total_length, chunk_size): | ||
| 33 | + end = min(start + chunk_size, input_total_length) | ||
| 34 | + self_chunk = self_data[start:end].astype(np.int64) | ||
| 35 | + other_chunk = other_data[start:end].astype(np.int64) | ||
| 36 | + quotient = np.abs(self_chunk) // np.abs(other_chunk) | ||
| 37 | + quotient = np.where((self_chunk >= 0) == (other_chunk >= 0), quotient, -quotient) | ||
| 38 | + rem = self_chunk - quotient * other_chunk | ||
| 39 | + signs_differ = (rem < 0) != (other_chunk < 0) | ||
| 40 | + golden[start:end] = np.where((signs_differ) & (rem != 0), rem + other_chunk, rem).astype(np.int32) | ||
| 41 | + | ||
| 42 | + os.makedirs("input", exist_ok=True) | ||
| 43 | + os.makedirs("output", exist_ok=True) | ||
| 44 | + self_data.tofile("./input/input_self.bin") | ||
| 45 | + other_data.tofile("./input/input_other.bin") | ||
| 46 | + golden.tofile("./output/golden.bin") | ||
| 47 | + | ||
| 48 | + | ||
| 49 | +if __name__ == "__main__": | ||
| 50 | + gen_golden_data_int() | ||
Aexamples/01_simd_cpp_api/04_best_practices/03_fusion_compute_practices/simt_and_simd_high_performance/scripts/verify_result.py+46-0File content is under review, please try again later
俩python文件,缺License头