华为升腾算子开发(一) 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;
}
相关推荐
烈风逍遥5 小时前
第五篇:通用 LLM 流式对话:前后端联接的完整实现
前端·后端·架构
aixingpan5 小时前
aixingpan.cn API开发文档:api_docs_trichart_natal_progression_sec_transit2接口指南
前端·php
江华森5 小时前
Web 安全实战:验证机制、会话管理、SQL 注入、XSS 与 CSRF
前端
youyin5 小时前
HarmonyOS ArkTS 与智能融合案例之实时语音翻译应用 实验指导书和代码
华为·harmonyos
需要8265 小时前
Arthas 一个命令排查线上问题
java·jvm·spring·spring cloud
江华森5 小时前
HTTPS 实战:TLS 握手成本、自制 CA 与证书、三大常见故障
前端
江华森5 小时前
HTTP 再邂逅:报文结构、请求方法、状态码与 Cookie/Session
前端
Kyrie_kk5 小时前
Java--ProcessBuilder操作系统进程
java·后端
szephyr5 小时前
前端错误监控实战:用 Sentry 把线上报错从“用户反馈“变成“主动发现“
前端·sentry·稳定性·前端监控·错误监控