如何在系统上找到所有opencl设备?

时间:2015-02-16 16:54:14

标签: c++ opencl

我有这段代码:

// 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吗?

有没有可以帮助我的图书馆?

3 个答案:

答案 0 :(得分:2)

OpenCL平台基本上代表制造商。如果您有两个(可以是不同的型号)Nvidia GPU,它们将在同一平台上。但是英特尔和Nvidia是不同的平台。

是的,您需要为OpenCL计算专门选择一个设备。因此,您要遍历所有平台,并遍历所有平台的所有设备,以获取所有可用的OpenCL设备的列表。然后,从该列表中选择最佳/最快的硬盘(在您的情况下为GForce 750,因为它既比HD4400更快,又具有更多的视频内存)。

这是一些代码,将为您提供devices向量中所有可用设备的列表。使用devices[1]选择GeForce 750。

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
}

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

clinfo [1]实用程序和clinfo [2]实用程序显示可用的平台和设备信息。它们适合本地使用。 khronos clGetPlatformIDs和clGetDeviceIDs在分发软件时非常适合运行时检测。

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