理论知识

什么是AscendC

Ascend C是CANN针对算子开发场景推出的编程语言,原生支持C和C++标准规范,兼具开发效率和运行性能。基于Ascend C编写的算子程序,通过编译器编译和运行时调度,运行在昇腾AI处理器上。使用Ascend C,开发者可以基于昇腾AI硬件,高效的实现自定义的创新算法。您可以通过Ascend C主页了解更详细的内容。

Ascend C提供多层级API,满足多维场景算子开发诉求。

  • 基础API:基于Tensor进行编程的C++类库API,实现单指令级抽象,为底层算子开发提供灵活控制能力。
  • 高阶API:封装单核公共算法,涵盖一些常见的计算算法(如卷积、矩阵运算等),显著降低复杂算法开发门槛。
  • 算子模板库:基于模板提供算子完整实现参考,简化Tiling(切分算法)开发,支撑用户自定义扩展。
  • Python前端:PyAsc编程语言基于Python原生接口,提供芯片底层完备编程能力,支持基于Python接口开发高性能Ascend C算子。

毕昇编译器是什么

毕昇编译器是一款专为昇腾AI处理器设计的编译器,支持异构编程扩展,可以将用户编写的昇腾算子代码编译成二进制可执行文件和动态库等形式。

毕昇编译器的可执行程序命名为bisheng,支持x86、aarch64等主机系统,并且原生支持设备侧AI Core架构指令集编译。

通过使用毕昇编译器,用户可以更加高效地进行针对昇腾AI处理器的编程和开发工作。

动手实践

下载安装CANN8.5.alpha002

CANN下载

wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C25B800TP028/Ascend-cann-toolkit_8.5.0.alpha002_linux-aarch64.run?response-content-type=application/octet-stream -O Ascend-cann-toolkit_8.5.0.alpha002_linux-aarch64.run

CANN安装

bash Ascend-cann-toolkit_8.5.0.alpha002_linux-aarch64.run --full

理论上来说使用bash Ascend-cann-toolkit_8.5.0.alpha002_linux-aarch64.run --install命令也是可以完成安装CANN的,但是使用full参数可以安装的更全一点,新版本的CANN其实也提供了很多新的功能其实。

激活环境

 source /home/service/Ascend/ascend-toolkit/set_env.sh

编写helloworld代码

创建hello_world.asc代码

#// Host侧应用程序需要包含的头文件
#include "acl/acl.h"
// Kernel侧需要包含的头文件
#include "kernel_operator.h"
__global__ __aicore__ void hello_world()
{
    // 设置Kernel类型,控制算子执行时仅启动Vector核  
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIC_ONLY);  
    AscendC::printf("Hello World!!!\n");
}

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

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

编译

编译可以使用两种方法一种使用毕昇编译器,另一种就是以前传统的CMake编译,作为初学者两种都尝试一下,毕昇编译器其实以前也只是听过,今天就先从一个Hello World开始了解使用他。

毕昇编译器编译

编译并且运行

bisheng hello_world.asc --npu-arch=dav-2201 -o demo
./demo

程序输出

opType=hello_world, DumpHead: AIC-0, CoreType=AIC, block dim=1, total_block_num=1, block_remain_len=1048416, block_initial_space=1048576, rsv=0, magic=5aa5bccd
CANN Version: 8.5.0.alpha002, TimeStamp: 20251209133531043
Hello World!!!

使用CMake编译

创建CMakeLists.txt文件脚本内容如下:

cmake_minimum_required(VERSION 3.16)
# find_package(ASC)是CMake中用于查找和配置Ascend C编译工具链的命令
find_package(ASC REQUIRED)
# 指定项目支持的语言包括ASC和CXX,ASC表示支持使用毕昇编译器对Ascend C编程语言进行编译
project(kernel_samples LANGUAGES ASC CXX)
add_executable(demo
    hello_world.asc
)
# 通过编译选项设置NPU架构
target_compile_options(demo PRIVATE   
   $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201>
)

编译命令

mkdir -p build && cd build;
cmake ..;make -j;
./demo

这一步在CANN8.5.Alpha002上出现以下报错

cmake ..;make -j;
./demo
CMake Error at CMakeLists.txt:3 (find_package):
  By not providing "FindASC.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "ASC", but
  CMake did not find one.

  Could not find a package configuration file provided by "ASC" with any of
  the following names:

    ASCConfig.cmake
    asc-config.cmake

  Add the installation prefix of "ASC" to CMAKE_PREFIX_PATH or set "ASC_DIR"
  to a directory containing one of the above files.  If "ASC" provides a
  separate development package or SDK, be sure it has been installed.


-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found.  Stop.
bash: ./demo: No such file or directory

解决方法:

根据ops-math代码仓的快速开始中的CANN链接下载CANN8.5-beta.1的安装包进行版本更换

wget https://ascend.devcloud.huaweicloud.com/artifactory/cann-run/software/8.5.0-beta.1/aarch64/Ascend-cann-toolkit_8.5.0-beta.1_linux-aarch64.run

安装过程同一开始安装CANN8.5.Alpha002一样,切换到CANN8.5-beta.1版本后即可正常出现运行结果。

编译输出

cmake ..;make -j;
./demo

输出结果

-- System processer: aarch64
-- CMAKE_ASC_COMPILER: /home/service/Ascend/cann-8.5.0-beta.1/bin/bisheng
-- ASCEND_CANN_PACKAGE_LINUX_PATH: /home/service/Ascend/cann-8.5.0-beta.1/aarch64-linux
-- CMAKE_ASC_LLD_LINKER: /home/service/Ascend/cann-8.5.0-beta.1/aarch64-linux/ccec_compiler/bin/ld.lld
-- The CXX compiler identification is GNU 11.4.0
-- 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
-- Generating done
-- Build files have been written to: /opt/huawei/edu-apaas/src/init/work_dir/build
[ 50%] Building ASC object CMakeFiles/demo.dir/hello_world.asc.o
[100%] Linking ASC executable demo
[100%] Built target demo
Hello World!!!

编译完成之后和毕昇编译器一样会自动生成一个demo文件,一个菜鸟的AscendC初体验也就算完成了

参考文档

https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/850alpha002/opdevg/ascendcbestP/atlas_ascendc_best_practices_10_0001.html

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐