AscendC从入门到精通系列(一)初步感知AscendC

1 什么是AscendC

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

算子开发学习地图:

2 从helloworld出发感受AscendC

2.1 使用AscendC写核函数

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

cpp 复制代码
#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>>>();
}

2.2 通过main.cpp调用核函数

便携main.cpp进行调用。

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;
}

2.3 添加CMakeLists文件

注意修改:SOC_VERSION,一般是: Ascend310P3,Ascend910B3等,通过npu-sim info命令查询。

bash 复制代码
# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.

# CMake lowest version requirement
cmake_minimum_required(VERSION 3.16.0)

# project information
project(Ascend_C)
set(SOC_VERSION "Ascend310P3" CACHE STRING "system on chip type")
set(ASCEND_CANN_PACKAGE_PATH "/usr/local/Ascend/ascend-toolkit/latest" CACHE PATH "ASCEND CANN package installation directory")
set(RUN_MODE "npu" CACHE STRING "run mode: npu")
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type Release/Debug (default Debug)" FORCE)
set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/out" CACHE STRING "path for install()" FORCE)

if(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/tools/tikcpp/ascendc_kernel_cmake)
    set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/tools/tikcpp/ascendc_kernel_cmake)
elseif(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
    set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/compiler/tikcpp/ascendc_kernel_cmake)
elseif(EXISTS ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
    set(ASCENDC_CMAKE_DIR ${ASCEND_CANN_PACKAGE_PATH}/ascendc_devkit/tikcpp/samples/cmake)
else()
    message(FATAL_ERROR "ascendc_kernel_cmake does not exist, please check whether the cann package is installed.")
endif()

include(${ASCENDC_CMAKE_DIR}/ascendc.cmake)

# ascendc_library use to add kernel file to generate ascendc library
ascendc_library(kernels STATIC
    hello_world.cpp
)

add_executable(main main.cpp)

target_link_libraries(main PRIVATE
    kernels
)

2.4 编译运行

注意修改:SOC_VERSION,一般是: Ascend310P3, Ascend910B3等,通过npu-sim info命令查询。

bash 复制代码
source /usr/local/Ascend/ascend-toolkit/latest/bin/setenv.bash  // 注意修改为当前环境的路径
rm -rf build
mkdir -p build
cmake -B build \
    -DSOC_VERSION=${SOC_VERSION} \
    -DASCEND_CANN_PACKAGE_PATH=/usr/local/Ascend/ascend-toolkit/latest
cmake --build build -j
cmake --install build

./build/main 

注意:编译有报错,确认下CANN版本和Sample的版本是不是匹配的。

比如CANN是8.0RC2,那么Sample库的版本最好也切到8.0RC2。

如果CANN是8.0RC3这种,Sample中没有8.0RC2,那就直接用master。

详细可以Ascend官方gitee库:

operator/HelloWorldSample/run.sh · Ascend/samples - 码云 - 开源中国 (gitee.com)
​gitee.com/ascend/samples/tree/master/operator/Hello

相关推荐
广州智造9 小时前
Radioss中破坏模型FAILTAB1的详细介绍
数据库·人工智能·算法·机器学习·仿真软件·radioss
2501_944525769 小时前
开源鸿蒙平台 KMP_CMP 三方库「assertk」适配全流程
华为·开源·harmonyos
lisw059 小时前
导数与机器学习优化!
算法·机器学习·数学建模
方知我9 小时前
01FastAPI基础入门
人工智能·python
听风吹等浪起9 小时前
第25章:基于ConvNeXt实现肺部CT肺癌四分类:数据集、训练调优、网络结构与Streamlit网页部署
人工智能·分类·数据挖掘
艺杯羹9 小时前
Android无障碍自动化死循环全解:从动态角标伪匹配到单向状态机防御实战
人工智能·自动化·大语言模型
会编程的吕洞宾9 小时前
Spring AI 语义缓存:重复提问不重复花钱
java·人工智能·后端·spring
物联网老王9 小时前
第05篇-Agent 连不上工具?MCP 就是那根“USB-C”
开发语言·人工智能·ai·aigc·ai编程
镭封9 小时前
手机文字转语音怎么操作?新手从输入文案到导出音频
人工智能·智能手机·音视频·语音识别·媒体