华为升腾算子开发(一) helloword

下面是一个简单的Ascend C的"Hello World"样例,展示了一个Ascend C核函数(设备侧实现的入口函数)的基本写法,及其如何被调用的流程。

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

go 复制代码
#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代码如下(您可以通过代码注释了解其主要的流程):

go 复制代码
#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;
}
相关推荐
Cooper259 小时前
三源热榜去重加 AI 摘要:URL 唯一键踩出的三个坑
前端·python
文丑颜不良啊9 小时前
LeetCode2901~2920题目解析
java·leetcode
码云社区9 小时前
能碳管理系统开发要关注什么?能源数据、统计口径与AI分析的衔接
java·人工智能·ai·能源·能源管理系统·能碳管理系统·能碳管理系统源码
Yk酱228409 小时前
A2UI探索随笔-技术选型
前端
前端繁华如梦9 小时前
# 从 73000 行压缩代码到 TypeScript 模块化:我把一个 Three.js FPS 游戏 1:1 复刻了一遍
前端
Penge6669 小时前
React Context 详解:从 Props 传递到跨组件状态共享
前端
Penge6669 小时前
React 事件处理:bind(this) 原理与实际开发常用写法
前端
Flynn Liang9 小时前
Agentic 工作流:子代理、Supervisor 与人工介入
java·langchain·多智能体·supervisor·agentic·langchain4j
xys_67810 小时前
JavaSE通关手册:运算符
java·java-ee
ZhengEnCi10 小时前
小白用 AI 做官网完全指南:不用框架的六格清单,双击就能打开
前端·html·ai编程