DDeepin Developerfeat: Init commit
430163a1创建于 2022年10月19日历史提交

Hello World

本入门示例基于Ascend C SIMD实现Hello World算子,带你快速上手实践,涵盖Device端核函数实现、Host端调用以及编译运行的完整流程,帮助开发者建立整体认知。

开始前请参考环境准备安装所需的CANN软件包,完整样例请参考hello_world

  • Hello World功能介绍

    在NPU上打印Hello World!!!

  • Device端Kernel实现

    后缀名为*.asc的代码文件包含Host端与Device端代码,其Device端部分示例如下:

    __global__ __vector__ void hello_world()
    {
        printf("Hello World!!!\n");
    }
    

    说明

    • SIMD算子的Kernel函数需要额外的修饰符,如__vector__修饰符说明该算子仅在向量计算单元上执行。
  • Host端代码实现

    Host端通过<<<>>>语法糖调用Device端代码片段。

    int main(int argc, char const* argv[])
    {
        ...
        constexpr uint32_t numBlocks = 8;
        ...
        
        aclrtStream stream = nullptr;
        aclrtCreateStream(&stream);
        // Launch kernel <<<numBlocks, dynUBufSize, stream>>>
        // numBlocks : Number of blocks. Default to 8 in this example.
        // dynUBufSize : Dynamic unified buffer size. Default to 0 in this example.
        // stream : Runtime stream. Uses stream created by aclrtCreateStream API in this example.
        hello_world<<<numBlocks, 0, stream>>>();
        ...
    }
    
  • 算子编译与运行

    bisheng hello_world.asc --npu-arch=dav-2201 -o demo
    ./demo
    

    执行本样例,将打印了核号和Hello World!!!信息。

    说明

    • 该样例支持如下型号:
      • Ascend 950PR/Ascend 950DT
      • Atlas A3 训练系列产品/Atlas A3 推理系列产品
      • Atlas A2 训练系列产品/Atlas A2 推理系列产品
    • 编译选项--npu-arch用于指定NPU架构版本,dav-后为架构版本号,请替换为您实际使用的版本。各AI处理器型号对应的架构版本号请通过AI处理器型号和 __NPU_ARCH__ 的对应关系查询。

如需进一步了解Ascend C的SIMD与SIMT编程模型,请参阅Ascend C编程模型概述