基于AMDGPU如何运行OpenCL测试用例
基于AMDGPU如何运行OpenCL测试用例
参考CPU算例的OpenCL的例子,写一个OpenCL的helloworld demo,这个并不太难,因为OpenCL是开源机构Khronos Group定义的标准,在这个标准下所有的头文件,运行时标准等都是定义好的,所以源码级没有太多改动,基本上拷贝过来就能跑。重点是分析基于AMDGPU的执行机制。
下面是一份简单的OpenCL的代码,基本上和C没有什么差别,除了需要提供设备端的代码,简单的测试用例可以只在主机上跑。
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <CL/cl.h>
void displayPlatformInfo(cl_platform_id id,
cl_platform_info param_name,
const char* paramNameAsStr)
{
cl_int error = 0;
size_t paramSize = 0;
error = clGetPlatformInfo( id, param_name, 0, NULL, ¶mSize );
char* moreInfo = (char*)alloca( sizeof(char) * paramSize);
error = clGetPlatformInfo( id, param_name, paramSize, moreInfo, NULL );
if (error != CL_SUCCESS ) {
perror("不能找到任何OpenCL平台信息");
return;
}
printf("%s: %s\n", paramNameAsStr, moreInfo);
}
int main(void) {
/* OpenCL 1.1数据结构 */
cl_platform_id* platforms;
/* OpenCL 1.1标量数据类型 */
cl_uint numOfPlatforms;
cl_int error;
/*
获取平台数目。
对于安装在计算机上的每个供应商的SDK,可用平台的数量也会增加。
*/
error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
if(error != CL_SUCCESS) {
perror("不能找到任何OpenCL平台");
exit(1);
}
// 为已安装的平台数量分配内存,alloca(…)会占用一些堆栈空间,
// 但在返回时会自动释放
platforms = (cl_platform_id*) alloca(sizeof(cl_platform_id) * numOfPlatforms);
printf("OpenCL找到平台数目: %d\n", numOfPlatforms);
error = clGetPlatformIDs(numOfPlatforms, platforms, NULL);
if(error != CL_SUCCESS) {
perror("没有找到OpenCL平台");
exit(1);
}
// 每个参数调用API clPlatformInfo两次,并使用返回值创建临时
// 数据结构(在堆栈上),以在第二次调用时存储返回的信息。
for(cl_uint i = 0; i < numOfPlatforms; ++i) {
displayPlatformInfo( platforms[i], CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE" );
displayPlatformInfo( platforms[i], CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION" );
displayPlatformInfo( platforms[i], CL_PLATFORM_NAME, "CL_PLATFORM_NAME" );
displayPlatformInfo( platforms[i], CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR" );
displayPlatformInfo( platforms[i], CL_PLATFORM_EXTENSIONS, "CL_PLATFORM_EXTENSIONS" );
}
return 0;
}
ROCm环境安装了OpenCL开发所需要的编译器,OpenCL运行时环境,以及标准的Khronos Group头文件,使用如下命令编译:
/opt/rocm/llvm/bin/clang OpenCL.c -I/opt/rocm/OpenCL/include -L/opt/rocm/OpenCL/lib -lOpenCL
编译,运行测试没有问题。
ROCm使用OpenCL编译运行时环境,如图1-28所示。

图1-28 ROCm使用OpenCL编译运行时环境
strace追踪系统调用,发现OpenCL的测试用例确实打开了/dev/kfd设备节点,并对
GPU进行IOCTL操作:
strace -tt -T -f -e trace=file, close, openat, ioctl -o strace.log./a.out
ROCm使用strace追踪系统调用,如图1-29所示。

图1-29 ROCm使用strace追踪系统调用
这个用例比较简单,调用的IOCTL 列表,如图1-30所示。

图1-30 ROCm调用IOCTL列表
人工智能芯片与自动驾驶

浙公网安备 33010602011771号