proper device number query

Discussion created by Meteorhead on Jul 6, 2010
Latest reply on Jul 15, 2010 by nou


My question would be, what is the proper way of querying the number of devices in an OpenCL program. I found that the sample CLInfo cpp application returns the correct number, however a simple c program returns a lot more devices and dies with seg fault even if queryied for attributes, not to mention using.

The following two code samples show the cpp and an old c sample way of querying device number. I would like to stick with the c way, since I'd like to keep my code portable and NVIDIA don't have cl.hpp (as far as I know), therefore I'd like to stick with cl.h.

For some lucky reason the working devices are at the beginning of the devices array in the c code, so if I know how many GPUs are in the machine, I can use all of them, but I'd rather want to write a code that can find out for itself. All ideas are appreciated.


cl::vector<cl::Device> devices; (*p).getDevices(CL_DEVICE_TYPE_ALL, &devices); std::cout << "Number of devices:\t\t\t\t " << devices.size() << std::endl; for (cl::vector<cl::Device>::iterator i = devices.begin(); i != devices.end(); ++i) { ... } //------------------------------------------------------------------------------------------- cl_device_id* devices; size_t devicenumber; clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL, &devicenumber); devices = (cl_device_id*)malloc(devicenumber); clGetContextInfo(context, CL_CONTEXT_DEVICES, devicenumber, devices, NULL); checkErr( devicenumber != 0 ? CL_SUCCESS : -1, "devicenumber <= 0");