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

相关推荐
冷de猫2 分钟前
AI API 网关的四层架构与代码实现:以 Aegisy 为例
人工智能·架构·大模型·ai编程·ai网关·api代理·多模型接入
不羁的木木7 分钟前
HarmonyOS APP实战-画图APP - 第7篇:文本绘制功能
华为·harmonyos
qq_355357029 分钟前
SKC智能知识管理系统-设计分享 之 知识聚类图谱
机器学习·数据挖掘·聚类
●VON15 分钟前
HarmonyKit | 鸿蒙新特性:HDS 沉浸光感与空间化视觉设计
华为·性能优化·单元测试·harmonyos·鸿蒙
武子康16 分钟前
🔥 vLLM / SGLang / TGI / TensorRT-LLM / Triton 真实分工:分层定位 + 5 步决策路径
人工智能·llm·gpu
雪隐22 分钟前
用Flutter做背单词APP-03为了给单词库塞满数据,我让AI给我打了上万份工
前端·人工智能·后端
weixin_4462608526 分钟前
WebSwarm:面向深度+广度混合网页搜索的递归多智能体编排框架
人工智能
网易云信27 分钟前
抢先剧透!网易智企即将亮相 2026 WAIC,这次我们带来了什么?
人工智能·aigc·线下活动
解局易否结局27 分钟前
鸿蒙原生开发实战:用 Native C++ 与 OpenGL ES 构建高性能图形渲染管线
c++·elasticsearch·华为·harmonyos
学究天人29 分钟前
数学公理体系大全:第六章 选择公理的等价形式及证明
人工智能·线性代数·算法·机器学习·数学建模·概率论·原型模式