直接上代码
OpenCl的函数基本都是根据功能命名代码注释,还是比较容易理解的
查询平台信息
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include<CL/cl.h>
using namespace std;
/*displayprogram 传入了平台id,平台信息,保存平台信息的字符串*/
void displayprogram(cl_platform_id id, cl_platform_info param_name, const char* paramnameastr)
{
cl_int error = 0;
size_t parasize = 0;
error = clGetPlatformInfo(id, param_name, 0, nullptr, ¶size);
char* moreinfo = (char*)malloc(sizeof(char) * parasize);
error = clGetPlatformInfo(id, param_name, parasize, moreinfo, nullptr);
if (error != CL_SUCCESS)
{
perror("unable to find any opencl platform information");
exit(1);
}
cout << paramnameastr << " : " << moreinfo << endl;
}
int main()
{
cl_platform_id* platforms = nullptr;
cl_uint numofplatforms = 0;
cl_int error;
// Get the number of available platforms
error = clGetPlatformIDs(0, nullptr, &numofplatforms);
if (error != CL_SUCCESS) {
perror("Unable to find any platform");
exit(1);
}
platforms = (cl_platform_id*)malloc(sizeof(cl_platform_id) * numofplatforms);
cout << "number of opencl platforms find : " << numofplatforms << endl;
//初始化paltforms
error = clGetPlatformIDs(numofplatforms, platforms, nullptr);
for (int i = 0; i < numofplatforms; i++)
{
displayprogram(platforms[i], CL_PLATFORM_PROFILE, "CL_PLATFORM_PROFILE");
displayprogram(platforms[i], CL_PLATFORM_VERSION, "CL_PLATFORM_VERSION");
displayprogram(platforms[i], CL_PLATFORM_NAME, "CL_PLATFORM_NAME");
displayprogram(platforms[i], CL_PLATFORM_VENDOR, "CL_PLATFORM_VENDOR");
displayprogram(platforms[i], CL_PLATFORM_EXTENSIONS, "CL_PLATFORM_EXTENSIONS");
}
/*CL_PLATFORM_PROFILE这种只是想要获取平台信息类型的一种参数*/
return 0;
}