刚刚配好opencl的环境,本来想找网上找一个hello opencl实例来测试一试,结果网上的代码有好几个错误,最后基本等于是自己写的,所以发布一下正确的代码:
此代码支持系统:
linux
支持GPU:intel,AMD,NVIDA
我是在linux上实现的,CPU和GPU都是intel的:
注意opencl头文件的引用,因为cl.h是通过usr/include/CL指向/etc/alternatives/opencl-headers/文件夹的,所以引用的时候一定要用<CL/cl.h>
已表示引用的cl.h在CL文件夹下(其实是在CL指向的文件夹下)
#include <stdio.h>
#include <stdlib.h>
#include <CL/cl.h>
int main()
{
cl_uint NumPlatforms;
clGetPlatformIDs(0,NULL,&NumPlatforms);
cl_platform_id PlatformIDs[NumPlatforms];
clGetPlatformIDs(NumPlatforms,PlatformIDs,NULL);
char platformName[64];
size_t nameLen;
cl_int res = clGetPlatformInfo(PlatformIDs[0],CL_PLATFORM_NAME,64,platformName,&nameLen);
if (res != CL_SUCCESS) {
fprintf(stderr,"Err:1 \n");
exit(1);
}
platformName[nameLen]=0;
// printf("%s\n",platformName);
char openclVersion[128];
res = clGetPlatformInfo(PlatformIDs[0],CL_PLATFORM_VERSION,128,openclVersion,&nameLen);
if(res!=CL_SUCCESS){
fprintf(stderr,"Err:2 \n");
exit(1);
}
openclVersion[nameLen] = 0;
printf("hello,%s's %s\n",platformName,openclVersion);
return 0;
}
写好.c文件后,输入指令:
gcc hello.c -lOpenCL
然后会得到一个文件
a.out
输入指令:
./a.out
可以得到输出:
hello,Intel(R) OpenCL's OpenCL 1.2 LINUX
截图如下: