Ascendc helloworld编译问题

1 入门ascendc 写HelloWorld

https://www.hiascend.com/document/detail/zh/canncommercial/81RC1/developmentguide/opdevg/Ascendcopdevg/atlas_ascendc_10_0004.html
下面是一个简单的Ascend C的"Hello World"样例,展示了一个Ascend C核函数(设备侧实现的入口函数)的基本写法,及其如何被调用的流程。

包含核函数的实现文件hello_world.cpp代码如下:核函数hello_world的核心逻辑为打印"Hello World!!!"字符串。hello_world_do封装了核函数的调用程序,通过<<<>>>内核调用符对核函数进行调用。

#include "kernel_operator.h"
extern "C" __global__ __aicore__ void hello_world()
{
    AscendC::printf("Hello World!!!\n");
}

void hello_world_do(uint32_t blockDim, void* stream)
{
    hello_world<<<blockDim, nullptr, stream>>>();
}

调用核函数的应用程序main.cpp代码如下(您可以通过代码注释了解其主要的流程):

#include "acl/acl.h"
extern void hello_world_do(uint32_t coreDim, void* stream);

int32_t main(int argc, char const *argv[])
{
    // AscendCL初始化
    aclInit(nullptr);
    // 运行管理资源申请
    int32_t deviceId = 0;
    aclrtSetDevice(deviceId);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    // 设置参与运算的核数为8(核数可根据实际需求设置)
    constexpr uint32_t blockDim = 8;
    // 用内核调用符<<<>>>调用核函数,hello_world_do中封装了<<<>>>调用
    hello_world_do(blockDim, stream);
    aclrtSynchronizeStream(stream);
    // 资源释放和AscendCL去初始化
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

通过如下的代码工程对上述文件进行组织,您可以通过LINK获取样例工程,并参考README完成CMakeLists中的AI处理器的型号、软件包安装路径配置。

|-- CMakeLists.txt                                        // CMake编译配置文件
|-- hello_world.cpp                                       // Kernel实现
|-- main.cpp                                              // 调用核函数的主程序
|-- run.sh                                                // 一键式编译运行脚本    

通过执行如下脚本实现样例程序的编译和运行:

bash run.sh -v <soc_version>

2 环境准备

1、安装CANN
CANN安装

2、安装依赖的软件

  • Debian系列(Ubuntu、Debian、UOS20、UOS20 SP1):
apt-get install -y gcc g++ make net-tools cmake python3 python3-dev python3-pip
  • openEuler系列(openEuler、CentOS、Kylin、BCLinux、BC-Linux-for-Euler、UOS201050e、UOS20 1020e、UOSV20、AntOS、CTyunOS、CULinux、Tlinux):
yum install -y gcc make net-tools cmake python3 python3-devel python3-pip

也可以参考:
https://www.hiascend.com/document/detail/zh/canncommercial/81RC1/developmentguide/opdevg/Ascendcopdevg/atlas_ascendc_10_0002.html

3 编译遇到的问题

问题1:

Current compile soc version is Ascend910B3
-- The C compiler identification is GNU 12.3.1
-- The CXX compiler identification is unknown
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:7 (project):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
gmake: Makefile: No such file or directory
gmake: *** No rule to make target 'Makefile'.  Stop.

解决:

yum install g++

问题2:

In file included from /usr/local/Ascend/ascend-toolkit/latest/tools/tikcpp/tikcfw/impl/utils/kernel_utils_macros.h:33:
/usr/local/Ascend/ascend-toolkit/latest/tools/tikcpp/tikcfw/impl/kernel_macros.h:24:10: fatal error: 'cstdint' file not found
#include <cstdint>
         ^~~~~~~~~
1 error generated.
gmake[5]: *** [CMakeFiles/precompile_obj.dir/build.make:76: CMakeFiles/precompile_obj.dir/home/ascendc/samples/operator/ascendc/0_introduction/0_helloworld/hello_world.cpp.o] Error 1

解决:
首先查找cstdint的位置,如下:

root@hostname-bnq0f:/home/ascendc/samples/operator/ascendc/0_introduction/0_helloworld# find / -name cstdint
/usr/local/Ascend/ascend-toolkit/8.1.RC1/toolkit/toolchain/hcc/aarch64-target-linux-gnu/include/c++/7.3.0/cstdint
/usr/local/Ascend/ascend-toolkit/8.1.RC1/toolkit/toolchain/hcc/aarch64-target-linux-gnu/include/c++/7.3.0/tr1/cstdint
/usr/include/c++/12/cstdint
/usr/include/c++/12/tr1/cstdint

然后设置CPLUS_INCLUDE_PATH变量

export CPLUS_INCLUDE_PATH=/usr/include/c++/12/:$CPLUS_INCLUDE_PATH

问题3:

In file included from /usr/local/Ascend/ascend-toolkit/latest/tools/tikcpp/tikcfw/impl/kernel_macros.h:24:
/usr/include/c++/12/cstdint:38:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
         ^~~~~~~~~~~~~~~~~~
1 error generated.
gmake[5]: *** [CMakeFiles/precompile_obj.dir/build.make:76: CMakeFiles/precompile_obj.dir/home/ascendc/samples/operator/ascendc/0_introduction/0_helloworld/hello_world.cpp.o] Error 1

bits/c++config.h处在目录下面/usr/include/c++/12/aarch64-openEuler-linux,需要将bits目录添加到变量CPLUS_INCLUDE_PATH中。

export CPLUS_INCLUDE_PATH=/usr/include/c++/12:/usr/include/c++/12/aarch64-openEuler-linux:$CPLUS_INCLUDE_PATH
root@fwb-VMware-Virtual-Platform:/home/fwb/Downloads/samples/operator/ascendc/0_introduction/0_helloworld# bash run.sh cpu Current compile soc version is Ascend310P3 -- The C compiler identification is GNU 13.3.0 -- The CXX compiler identification is GNU 13.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (0.6s) -- Generating done (0.0s) -- Build files have been written to: /home/fwb/Downloads/samples/operator/ascendc/0_introduction/0_helloworld/build [ 2%] Creating directories for 'kernels_precompile' [ 4%] No download step for 'kernels_precompile' [ 6%] No update step for 'kernels_precompile' [ 9%] No patch step for 'kernels_precompile' [ 11%] Performing configure step for 'kernels_precompile' -- The C compiler identification is GNU 13.3.0 -- The CXX compiler identification is GNU 13.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/cc - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done (0.5s) -- Generating done (0.0s) CMake Warning: Manually-specified variables were not used by the project: DYNAMIC_MODE INCLUDE_DIR -- Build files have been written to: /home/fwb/Downloads/samples/operator/ascendc/0_introduction/0_helloworld/build/kernels_precompile-prefix/src/kernels_precompile-build [ 13%] Performing build step for 'kernels_precompile' [100%] Build
03-31
### Ascend算子开发认证指南与开发流程 Ascend算子开发是基于华为昇腾(Ascend)AI处理器的算子开发技术,旨在为开发者提供高效的AI计算能力。以下是Ascend算子开发认证的相关信息及开发流程: #### 1. 算子开发环境搭建 在开始Ascend算子开发之前,需要先搭建开发环境。开发环境的搭建包括安装Ascend CANN(Compute Architecture for Neural Networks)软件栈,以及相关的工具包和SDK。CANN软件栈提供了完整的AI计算框架支持,包含驱动、算子开发库、推理引擎等组件[^1]。 #### 2. 开发流程概述 Ascend算子开发的流程可以分为以下几个阶段: - **需求分析**:明确需要开发算子类型及其功能要求。 - **算子设计**:根据需求设计算子的输入输出格式、数据类型及计算逻辑。 - **代码实现**:使用C/C++语言编写算子代码,并调用Ascend提供的API进行开发。 - **编译与调试**:将编写好的算子代码编译成二进制文件,并在Ascend平台上进行测试与调试。 - **性能优化**:对算子的性能进行评估,并根据需要进行优化。 #### 3. 认证指南 Ascend算子开发认证主要面向希望掌握昇腾AI处理器算子开发技能的开发者。认证内容包括但不限于以下方面: - **Ascend架构基础**:了解昇腾AI处理器的硬件架构及工作原理。 - **CANN软件栈**:熟悉CANN软件栈的组成及各组件的功能。 - **算子开发工具**:掌握Ascend提供的算子开发工具,如`communitysdk`社区算子开发工具包。 - **开发实践**:通过实际项目练习,掌握算子开发的全流程。 #### 4. 示例代码 以下是一个简单的Ascend算子开发示例代码,展示如何实现一个加法算子: ```cpp #include "ascend_kernel.h" using namespace std; class AddKernel : public AscendKernel { public: AddKernel() {} ~AddKernel() {} virtual void Compute(const vector<Tensor>& inputs, vector<Tensor>& outputs) override { // 获取输入张量 const Tensor& input1 = inputs[0]; const Tensor& input2 = inputs[1]; // 获取输出张量 Tensor& output = outputs[0]; // 获取张量数据 float* data1 = (float*)input1.GetData(); float* data2 = (float*)input2.GetData(); float* out_data = (float*)output.GetData(); // 计算加法 int size = input1.GetSize(); for (int i = 0; i < size; ++i) { out_data[i] = data1[i] + data2[i]; } } }; ``` #### 5. 开发资源推荐 - **官方文档**:Ascend CANN官方文档提供了详细的算子开发指导和技术规范[^1]。 - **ModelArts平台**:作为一站式AI开发平台,ModelArts不仅支持模型训练与部署,还集成了Ascend算子开发的相关工具和资源[^2]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值