我有这个代码:
// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetPlatformIDs.html
cl_uint platformIdCount = 0;
clGetPlatformIDs(0, nullptr, &platformIdCount);
if (platformIdCount == 0) {
std::cerr << "No OpenCL platform found" << std::endl;
return 1;
}
else {
std::cout << "Found " << platformIdCount << " platform(s)" << std::endl;
}
std::vector<cl_platform_id> platformIds(platformIdCount);
clGetPlatformIDs(platformIdCount, platformIds.data(), nullptr);
for (cl_uint i = 0; i < platformIdCount; ++i) {
std::cout << "\t (" << (i + 1) << ") : " << GetPlatformName(platformIds[i]) << std::endl;
}
// http://www.khronos.org/registry/cl/sdk/1.1/docs/man/xhtml/clGetDeviceIDs.html
cl_uint deviceIdCount = 0;
clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_ALL, 0, nullptr,
&deviceIdCount);
if (deviceIdCount == 0) {
std::cerr << "No OpenCL devices found" << std::endl;
return 1;
}
else {
std::cout << "Found " << deviceIdCount << " device(s)" << std::endl;
}
std::vector<cl_device_id> deviceIds(deviceIdCount);
clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_ALL, deviceIdCount,
deviceIds.data(), nullptr);
for (cl_uint i = 0; i < deviceIdCount; ++i) {
std::cout << "\t (" << (i + 1) << ") : " << GetDeviceName(deviceIds[i]) << std::endl;
}
我在一台配备 2 个 GPU、一个 HD4400 和 GForce 750 的笔记本电脑上运行它。
当我运行它时,我得到了两个平台,每个平台都有该特定制造商的设备,例如,在平台 0 上,我得到 i7 和 HD4400,在平台 1 上,我得到 GeForce 750。
我以为我可以从一个平台获取所有设备?
我认为要找到合适的设备是否正确,我需要遍历所有平台并找到适用于 GPU 的设备,然后我得到所有设备的列表?
为任务找到合适设备的正确方法是什么?
假设我想找到具有最大内存或最大工作线程的 GPU?
有没有图书馆可以帮我解决这个问题?
最佳答案
OpenCL 平台基本上表示制造商。如果您有两个(可以是不同型号)Nvidia GPU,它们将位于同一平台上。但 Intel 和 Nvidia 是不同的平台。
是的,您需要专门为 OpenCL 计算选择一个设备。因此,您遍历所有平台并针对每个平台的所有设备进行迭代,以获得所有可用 OpenCL 设备的列表。然后从这个列表中您可以选择最好/最快的一个(在您的情况下是 GForce 750,因为它比 HD4400 更快并且具有更多的视频内存)。
下面是一些代码,可以为您提供 devices
vector 中所有可用设备的列表。选择带有 devices[1]
的 GeForce 750。
std::vector<Device> devices;
int find_devices() {
std::vector<Platform> platforms; // get all platforms
std::vector<Device> devices_available;
int n = 0; // number of available devices
Platform::get(&platforms);
for(int i=0; i<(int)platforms.size(); i++) {
devices_available.clear();
platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices_available);
if(devices_available.size()==0) continue; // no device found in plattform i
for(int j=0; j<(int)devices_available.size(); j++) {
n++;
devices.push_back(devices_available[j]);
}
}
if(platforms.size()==0||devices.size()==0) {
std::cout << "Error: There are no OpenCL devices available!" << std::endl;
return -1;
}
for(int i=0; i<n; i++) std::cout << "ID: " << i << ", Device: " << devices[i].getInfo<CL_DEVICE_NAME>() << std::endl;
return n; // return number of available devices
}
为了轻松开始使用 OpenCL,我创建了一个轻量级包装器,它极大地简化了 OpenCL C++ 绑定(bind)并消除了随之而来的整个代码开销。您可以使用 get_devices()
获取所有可用设备的列表,并使用 select_device_with_most_flops()
自动找到最快的设备:https://github.com/ProjectPhysX/OpenCL-Wrapper
关于c++ - 如何查找系统上的所有 opencl 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28546439/