如何查找系统上的所有 opencl 设备? [英] How to find all opencl devices on a system?

查看:112
本文介绍了如何查找系统上的所有 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 的笔记本电脑上运行它.

and I am running it on a laptop that has 2 gpu, one HD4400 and GForce 750.

当我运行它时,我得到两个平台,每个平台都有该特定制造商的设备,例如在平台 0 上,我得到 i7 和 HD4400,而在平台 1 上,我得到 GeForce 750.

When I run it, I am getting two platform and each platform has the device for that specific manufacturer, for example on platform 0, I am getting i7 and HD4400 and on platform 1, I am getting GeForce 750.

我以为我可以从一个平台获取所有设备?

I thought that I can get all devices from one platform?

我认为找到合适的设备是否正确,我需要遍历所有平台并找到适用于 GPU 的设备,然后我才能获得所有设备的列表?

Am I correct to believe to find a suitable device, I need to go over all platforms and find devices which are for GPU and then I have the list of all devices?

找到适合任务的设备的正确方法是什么?

What is the correct way to find suitable device for a task?

说我想找到具有最大内存或最大工人数的 GPU?

Say I want to find GPU with maximum memory or maximum worker?

是否有任何图书馆可以帮助我解决这个问题?

Is there any library that can help me on this?

推荐答案

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

The OpenCL platform basically denotes the manufacturer. If you would have two (can be different models) Nvidia GPUs, they would be on the same platform. But Intel and Nvidia are different platforms.

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

Yes, you need to specifically select one device for your OpenCL computations. Therefore you iterate over all platforms and for every platform over all of its devices in order to get a list of all available OpenCL devices. Then from this list you can select the best/fastest one (in your case the GForce 750 as it is both faster than the HD4400 and has more video memory).

下面是一些代码,它会为您提供 devices 向量中所有可用设备的列表.选择带有 devices[1] 的 GeForce 750.

Here is some code that will give you a list of all available devices in the devices vector. Select the GeForce 750 with devices[1].

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 设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆