欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > 华为升腾算子开发(一) helloword

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

2025/9/15 10:05:02 来源:https://blog.csdn.net/luoganttcc/article/details/145257875  浏览:    关键词:华为升腾算子开发(一) helloword

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

包含核函数的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>>>();
}

调用核函数的应用程序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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词