Reputation: 128
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.
Upvotes: 1
Views: 466
Reputation: 20386
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.
Upvotes: 1
Reputation: 4191
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.
Upvotes: 0