如何访问 OpenCL C++ 中 cl_device_id 变量的数值

发布于 2025-01-19 07:25:05 字数 330 浏览 2 评论 0原文

我创建了一个自定义类型的向量,其大小等于平台中 GPU 设备的数量。现在的想法是在引用该向量的当前设备的索引处访问该向量中的元素。例如,如果 id 为 0 的设备当前正在引用此向量,那么我需要从此向量访问索引 0 处的元素。
实际的问题在于设备id类型,它似乎是cl_Device_id类型。由于要使用[]访问向量,我们需要一个int类型,我们需要设备ID数值。由于我是初学者,我不知道如何访问该变量的数值,即实际的设备 ID。
查看头文件(cl.h)后,它似乎是一个struct,但找不到它的定义。

I have created a vector of custom type whose size is equal to number of GPU devices in the platform. Now the idea is to access the element from this vector at an index of current device referring to this vector. For example, if device with id 0 is currently referring to this vector, then I need to access the element at index 0 from this vector.
The actual problem is in the device id type, which seems to be cl_Device_id type. Since to access the vector using [], we need an int type, we need the device id numerical value. As I am a beginner, I don't know how can we access the numerical value of this variable i.e. the actual device id.
After looking at the header file (cl.h), it seems it's a struct but its definition is nowhere to be found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2025-01-26 07:25:05

OpenCL对象是不透明的类型,这意味着您无法在运行时查看其内部结构。您可以放弃指针并尝试读取其字节,但是您可能不应该不应该,因为如果其内容甚至在主机内存中分配了(不保证!),它不会造成太多的作用对你有感觉。

OpenCL设备没有与之关联的“数字”。如果您需要关联的数字,则必须自己定义该数字。写std :: vector< cl_device_id> gt;设备{device1,device2,device3};,在其中您知道设备[0]始终是指第一个设备,device [1]是指第二个设备, 等等。

如果您需要一种更“稳定”的方式参考设备,则使用其名称是一个好主意,因为设备名称可以在应用程序的运行时不更改(但是它们可以在运行之间进行更改如果驱动程序更新了!):

std::string getDeviceName(cl_device_id did) {
    size_t size;
    clGetDeviceInfo(did, CL_DEVICE_NAME, 0, nullptr, &size);//Gets the size of the name
    std::string name (size);
    clGetDeviceInfo(did, CL_DEVICE_NAME, size, name.data(), nullptr);
    return name;
}

//...

std::map<std::string, cl_device_id> ids {//or std::unordered_map
    {getDeviceName(device1), device1},
    {getDeviceName(device2), device2},
    {getDeviceName(device3), device3}
};

无论您选择什么,重要的部分是您必须自己选择它:除了API所提供的任何API所提供的内容之外,没有任何内在订购对OpenCL对象。

OpenCL objects are Opaque Types, which means you cannot view their internal structure at runtime. You can dereference the pointer and try to read its bytes, but you probably shouldn't, since if its contents are even allocated in host memory (no guarantee of that!), it won't make much sense to you.

OpenCL Devices don't have a "number" associated with them. If you need a number associated, you have to define that yourself. There's nothing wrong with, say, writing std::vector<cl_device_id> devices {device1, device2, device3};, where you know devices[0] always refers to the first device, devices[1] refers to the second, and so on.

If you need a more "stable" way of referring to devices, using its name is a good idea, since device names are guaranteed not to change during the runtime of an application (but they can change between runs of an application if the drivers are updated!):

std::string getDeviceName(cl_device_id did) {
    size_t size;
    clGetDeviceInfo(did, CL_DEVICE_NAME, 0, nullptr, &size);//Gets the size of the name
    std::string name (size);
    clGetDeviceInfo(did, CL_DEVICE_NAME, size, name.data(), nullptr);
    return name;
}

//...

std::map<std::string, cl_device_id> ids {//or std::unordered_map
    {getDeviceName(device1), device1},
    {getDeviceName(device2), device2},
    {getDeviceName(device3), device3}
};

Whatever you choose, the important part is that you have to choose this for yourself: there's no intrinsic ordering to OpenCL objects, beyond whatever the API happens to provide as the value of its pointer.

太阳哥哥 2025-01-26 07:25:05

没错,cl_device_id类型在/usr/include/cl/cl.h.h标题中定义为:

typedef struct _cl_device_id *      cl_device_id;

这是OpenCL 抽象数据类型之一 - 要查看它们的完整列表,请查看在这里

OpenCL设计师有意隐藏数据结构,以这些类型的值指向。所有这些值具有与指针的大小相同,但是您不允许您取消或递增/减少这些指针 - 您只能分配,复制它们,比较它们并用作参数或返回值OPENCL功能。

You're right, the cl_device_id type is defined in the /usr/include/CL/cl.h header as:

typedef struct _cl_device_id *      cl_device_id;

This is one of OpenCL Abstract Data Types - to see the complete list of them please look here.

The OpenCL designers intentionally hide data structures, pointed by values of these types. All these values have the same size as pointer, however you are not allowed to dereference or increment/decrement these pointers - you can only assign them, copy them, compare them and use as arguments or return values in OpenCL functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文