当前位置: 首页 > news >正文

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封装了核函数的调用程序,通过<<<>>>内核调用符对核函数进行调用。

#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进行调用。

#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);// 设置参与运算的核数为8constexpr 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命令查询。

# 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 STATIChello_world.cpp
)add_executable(main main.cpp)target_link_libraries(main PRIVATEkernels
)

2.4 编译运行

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

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

http://www.lryc.cn/news/483211.html

相关文章:

  • PostgreSQL中的COPY命令:高效数据导入与导出
  • 【HAL库】STM32F105VCTx多通道ADC+DMA方式的【STM32CubeMX】配置及代码实现
  • [SaaS] 数禾科技 AIGC生成营销素材
  • vue3中查找字典列表中某个元素的值对应的列表索引值
  • 爱普生机器人EPSON RC
  • Linux探秘坊-------1.系统核心的低语:基础指令的奥秘解析(1)
  • ❤React-JSX语法认识和使用
  • 51单片机应用开发(进阶)---定时器应用(电子时钟)
  • JavaScript中的对象-栈内存和堆内存以及this指向的两种情况(后续会出进阶)
  • shell脚本使用curl上传FTP
  • 【漏洞分析】Fastjson最新版本RCE漏洞
  • 【项目开发 | 跨域认证】JSON Web Token(JWT)
  • 杨中科 .Net Core 笔记 DI 依赖注入2
  • 微信版产品目录如何制作?
  • 使用HTML、CSS和JavaScript创建动态圣诞树
  • 机器学习-35-提取时间序列信号的特征
  • 【软件测试】设计测试用例的万能公式
  • 【MySQL 保姆级教学】事务的自动提交和手动提交(重点)--上(13)
  • CUDA 核心与科学计算 :NVIDIA 计算核心在计算服务器的价值
  • 架构师之路-学渣到学霸历程-58
  • qq相册为啥越来越糊
  • <有毒?!> 诺顿检测:这篇 CSDN 文章有病毒
  • matlab实现主成分分析方法图像压缩和传输重建
  • 18.UE5怪物视野、AI感知、攻击范围、散弹技能
  • 【 ElementUI 组件Steps 步骤条使用新手详细教程】
  • MQTT从入门到精通之 MQTT 客户端编程
  • 数据结构-集合
  • 前端 JS面向对象 原型 prototype
  • Java中的不可变集合:性能与安全并重的最佳实践
  • RandomWords随机生成单词