如何在系统中查找所有opencl设备

How to find all opencl devices on a system?

本文关键字:opencl 设备 查找 系统      更新时间:2023-10-16

我有这个代码:

// 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,它们将在同一平台上。但英特尔和英伟达是不同的平台。

是的,您需要为您的OpenCL计算专门选择一个设备。因此,您在所有平台上迭代,并在每个平台的所有设备上迭代,以获得所有可用OpenCL设备的列表。然后从这个列表中,你可以选择最好/最快的一个(在你的情况下是GForce 750,因为它比HD4400更快,而且有更多的视频内存)。

以下是一些代码,它将为您提供devices矢量中所有可用设备的列表。选择带有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++绑定,并消除了随之而来的整个代码开销。您可以获得所有具有get_devices()的可用设备的列表,并自动找到具有select_device_with_most_flops()的最快设备:https://github.com/ProjectPhysX/OpenCL-Wrapper

您无法在一个平台中获取所有设备。

最多,您只会看到来自同一供应商的设备组合在一起(例如,AMD CPU和AMD GPU,或Intel CPU和GPU)。在Windows(我推测是Linux)上,您可能会看到多个平台。在MacOSX上,我只见过一个(带CPU和所有GPU)。

您需要迭代所有平台和设备才能找到它们,这是正确的。您可以筛选到您将支持的内容,并根据功能进行排序。

clinfo[1]实用程序和clinfo[2]实用程序显示可用的平台和设备信息。它们适合当地使用。在分发软件时,khronos-clGetPlatformID和clGetDeviceID适用于运行时检测。

1:https://github.com/Oblomov/clinfo1:https://sf.net/p/clinfo