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
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
572

被折叠的 条评论
为什么被折叠?



