1、环境搭建
去NVIDIA官网下载CUDA,只需要下载CUDA一项即可,里面包含有opencl库。
2、获取平台设备信息
OpenCL的api文档可以在OpenCL官网找到,不过比较难找,这里我放个链接
由于intel cpu的信息比较长,有41个字符,所以device字符串指针分配了50个字符空间,如果你的设备信息较长,可以再多分配点。
#include <CL/cl.h>
#include <iostream>
using namespace std;
int main()
{
cl_uint numPlatforms = 0;
cl_platform_id * platforms = nullptr;
// 第一次调用clGetPlatfromIDs,获取平台数量
cl_int status = clGetPlatformIDs(0, nullptr, &numPlatforms);
if(status != CL_SUCCESS)
{
cout << "error : getting platforms failed";
return 1;
}
cout << "FIND " << numPlatforms << " PLATFORM(S)" << endl;
if(numPlatforms == 0)
return -1;
platforms = new cl_platform_id[numPlatforms];
status = clGetPlatformIDs(numPlatforms, platforms, nullptr);
for(int i = 0; i < numPlatforms; ++i)
{
// 打印平台信息
cl_char * param = new cl_char[30];
cout << "PLATFORM " << i << " INFOMATION :" << endl;
clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 30, param, nullptr);
cout << "\tName : " << param << endl;
clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 30, param, nullptr);
cout << "\tVendor : " << param << endl;
clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 30, param, nullptr);
cout << "\tVersion : " << param << endl;
clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, 30, param, nullptr);
cout << "\tProfile : " << param << endl;
delete [] param;
// 获取设备
cl_uint numDevices = 0;
cl_device_id * devices;
status = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &numDevices);
cout << "PLATFORM " << i << " HAS " << numDevices << " DEVICE(S) : " << endl;
devices = new cl_device_id[numDevices];
clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_ALL, numDevices, devices, nullptr);
// 打印设备信息
for(int j = 0; j < numDevices; ++j)
{
cl_char * device_param = new cl_char[50];
cout << "DEVICE " << j << " INFOMATION :" << endl;
clGetDeviceInfo(devices[j], CL_DEVICE_NAME, 50, device_param, nullptr);
cout << "\tName : " << device_param << endl;
clGetDeviceInfo(devices[j], CL_DEVICE_VENDOR, 50, device_param, nullptr);
cout << "\tVendor : " << device_param << endl;
clGetDeviceInfo(devices[j], CL_DEVICE_VERSION, 50, device_param, nullptr);
cout << "\tVersion : " << device_param << endl;
clGetDeviceInfo(devices[j], CL_DEVICE_PROFILE, 50, device_param, nullptr);
cout << "\tProfile : " << device_param << endl;
delete [] device_param;
}
delete [] devices;
cout << "---------------------------------------" << endl;
}
return 0;
}
我使用vscode和gcc编译器,编译命令如下
g++ test.cpp OpenCL.lib -I E:\SOFTWARE\CUDA_11.5\include
-I 后面跟的就是cuda路径下的include目录,里面有CL的头文件
Opencl.lib可以在cuda路径下的lib目录找到,我的是64位系统,所以在x64里面找,可以将其复制一份到源文件目录,免得编译命令过长,如果不复制OpenCL.lib的话,编译命令如下:
g++ test.cpp -I E:\SOFTWARE\CUDA_11.5\include -L E:\SOFTWARE\CUDA_11.5\lib\x64 -lOpenCL
编译结束后,获得a.exe可执行文件,即可运行:
可以看到,我的电脑有两个平台:Intel(R) OpenCL 和 NVIDIA CUDA
Intel(R) OpenCL 平台下有两个设备:核心显卡 HD 630 和 CPU i7-7700HQ
NVIDIA CUDA 平台下有一个设备:GTX 1050Ti 显卡