华为升腾算子开发(一) 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;
}
相关推荐
Flittly4 分钟前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring
RainCity9 分钟前
Java Swing 自定义组件库分享(十二)
java·笔记·后端
Muen15 分钟前
iOS设计模式-外观Facade
前端
Cobyte38 分钟前
21.Vue Vapor 组件的实现原理
前端·javascript·vue.js
前端双越老师38 分钟前
我从 0 开发的 AI Agent 智语项目发布了
前端·node.js·agent
橙某人41 分钟前
LogicFlow 工作流撤销与重做:从「全量快照」到「命令模式」🎯
前端·vue.js
铁皮饭盒1 小时前
Rust版Bun1.4之前, 盘点Bun1.3新特性
前端·javascript·后端
恋猫de小郭1 小时前
如何让 AI 快速搭建一套生产 Agent ?全面理解 Agent 架构。
前端·人工智能·ai编程
Csvn1 小时前
Vite 构建缓存优化:二次构建从 15s 降到 2s 的实战方案
前端