AI CPU是位于Device侧ARM64架构的处理器,其具备与AI Core相同的内存访问能力,可直接访问Device侧内存资源;也可以与Host侧的CPU一样,进行类似的数据计算,通常作为AI Core的补充,主要承担非矩阵类、逻辑比较复杂的分支密集型计算。AI CPU的运行环境为基础的Linux环境,编程时可使用libc库,C++标准库,STL模板库等。其硬件架构图如下所示:
本节介绍的AI CPU编程仅支持如下产品型号:
在进行AI CPU编程时,与AI Core类似,同样需要定义设备侧函数入口(即核函数),该函数必须通过__aicpu__标识符进行声明,并且需与__global__标识符联合使用以表明其只能被Host侧调用。AI CPU的Device侧实现文件需要以.aicpu为后缀(或者以.cpp为后缀,在编译时增加-x aicpu选项)。该实现文件中包括上面介绍的核函数以及AI CPU普通函数定义,AI CPU普通函数无需添加执行空间标识符。
如下是一个AI CPU“Hello World”程序的示例,hello_world.aicpu文件内容如下:
1 2 3 4 5 6 7 8 |
// 调用printf接口需要包含的头文件 #include "aicpu_api.h" __global__ __aicpu__ uint32_t hello_world(void *args) { AscendC::printf("Hello World!!!\n"); return 0; } |
编程时需要遵循如下规范:
1
|
hello_world<<<numBlocks, nullptr, stream>>>(&args, sizeof(KernelArgs)); |
在编写调用代码时需要遵循如下规范:
加载和运行算子时,需要使用Runtime API,完成运行时管理和配置,详细内容请参考算子运行。AI CPU算子的编译请参考AI CPU算子编译。
template<typename T, int BUFF_SIZE>
__global__ __aicpu__ uint32_t hello_world(void *args)
{
AscendC::printf("Hello World!!!\n");
AscendC::printf("buffer_size is %d\n", BUFF_SIZE);
return 0;
}
template __global__ __aicpu__ uint32_t hello_world<KernelArgs, 4096>(void *args);
并在.asc文件中新增模板核函数实例化的extern声明:
template<typename T, int BUFF_SIZE> extern __global__ __aicpu__ uint32_t hello_world(void *args); template extern __global__ __aicpu__ uint32_t hello_world<KernelArgs, 4096>(void *args);