@@ -158,8 +158,9 @@ class OpenCLDepthPacketProcessorImpl
158158 cl::Buffer buf_filtered;
159159
160160 bool isInitialized;
161+ const int deviceId;
161162
162- OpenCLDepthPacketProcessorImpl () : isInitialized(false )
163+ OpenCLDepthPacketProcessorImpl (const int deviceId = - 1 ) : isInitialized(false ), deviceId(deviceId )
163164 {
164165 newIrFrame ();
165166 newDepthFrame ();
@@ -226,43 +227,81 @@ class OpenCLDepthPacketProcessorImpl
226227 options = oss.str ();
227228 }
228229
229- bool selectDevice (const std::vector<cl::Platform> &platforms, const int type )
230+ void getDevices (const std::vector<cl::Platform> &platforms, std::vector<cl::Device> &devices )
230231 {
231- bool selected = false ;
232-
232+ devices.clear ();
233233 for (size_t i = 0 ; i < platforms.size (); ++i)
234234 {
235235 const cl::Platform &platform = platforms[i];
236- std::string platformName, platformVendor;
237- platform.getInfo (CL_PLATFORM_NAME, &platformName);
238- platform.getInfo (CL_PLATFORM_VENDOR, &platformVendor);
239236
240- std::cout << OUT_NAME (" selectDevice" ) " found platform: " << platformName << " vendor: " << platformVendor << std::endl;
241-
242- std::vector<cl::Device> devices;
243- if (platform.getDevices (type, &devices) != CL_SUCCESS)
237+ std::vector<cl::Device> devs;
238+ if (platform.getDevices (CL_DEVICE_TYPE_ALL, &devs) != CL_SUCCESS)
244239 {
245- std::cerr << OUT_NAME (" selectDevice" ) " error while getting opencl devices." << std::endl;
246- return false ;
240+ continue ;
247241 }
248242
249- for (size_t j = 0 ; j < devices.size (); ++j)
243+ devices.insert (devices.end (), devs.begin (), devs.end ());
244+ }
245+ }
246+
247+ void listDevice (std::vector<cl::Device> &devices)
248+ {
249+ std::cout << OUT_NAME (" listDevice" ) " devices:" << std::endl;
250+ for (size_t i = 0 ; i < devices.size (); ++i)
251+ {
252+ cl::Device &dev = devices[i];
253+ std::string devName, devVendor, devType;
254+ size_t devTypeID;
255+ dev.getInfo (CL_DEVICE_NAME, &devName);
256+ dev.getInfo (CL_DEVICE_VENDOR, &devVendor);
257+ dev.getInfo (CL_DEVICE_TYPE, &devTypeID);
258+
259+ switch (devTypeID)
250260 {
251- cl::Device &dev = devices[i];
252- std::string devName, devVendor;
253- dev.getInfo (CL_DEVICE_NAME, &devName);
254- dev.getInfo (CL_DEVICE_VENDOR, &devVendor);
261+ case CL_DEVICE_TYPE_CPU:
262+ devType = " CPU" ;
263+ break ;
264+ case CL_DEVICE_TYPE_GPU:
265+ devType = " GPU" ;
266+ break ;
267+ case CL_DEVICE_TYPE_ACCELERATOR:
268+ devType = " ACCELERATOR" ;
269+ break ;
270+ case CL_DEVICE_TYPE_CUSTOM:
271+ devType = " CUSTOM" ;
272+ break ;
273+ default :
274+ devType = " UNKNOWN" ;
275+ }
255276
256- std::cout << OUT_NAME (" selectDevice" ) " found device: " << devName << " vendor: " << devVendor << std::endl;
277+ std::cout << " " << i << " : " << devName << " (" << devType << " )[" << devVendor << ' ]' << std::endl;
278+ }
279+ }
257280
258- if (!selected )
259- {
260- selected = true ;
261- device = dev;
262- }
263- }
281+ bool selectDevice (std::vector<cl::Device> &devices )
282+ {
283+ if (deviceId != - 1 && devices. size () > ( size_t )deviceId)
284+ {
285+ device = devices[deviceId];
286+ return true ;
264287 }
265288
289+ bool selected = false ;
290+ size_t selectedType = 0 ;
291+
292+ for (size_t i = 0 ; i < devices.size (); ++i)
293+ {
294+ cl::Device &dev = devices[i];
295+ size_t devTypeID;
296+ dev.getInfo (CL_DEVICE_TYPE, &devTypeID);
297+
298+ if (!selected || (selectedType != CL_DEVICE_TYPE_GPU && devTypeID == CL_DEVICE_TYPE_GPU))
299+ {
300+ selectedType = devTypeID;
301+ selected = true ;
302+ device = dev;
303+ }
304+ }
266305 return selected;
267306 }
268307
@@ -294,16 +333,42 @@ class OpenCLDepthPacketProcessorImpl
294333 return false ;
295334 }
296335
297- if (!selectDevice (platforms, CL_DEVICE_TYPE_GPU))
336+ std::vector<cl::Device> devices;
337+ getDevices (platforms, devices);
338+ listDevice (devices);
339+ if (selectDevice (devices))
298340 {
299- std::cout << OUT_NAME (" init" ) " could not find any GPU device. trying CPU devices" << std::endl;
341+ std::string devName, devVendor, devType;
342+ size_t devTypeID;
343+ device.getInfo (CL_DEVICE_NAME, &devName);
344+ device.getInfo (CL_DEVICE_VENDOR, &devVendor);
345+ device.getInfo (CL_DEVICE_TYPE, &devTypeID);
300346
301- if (! selectDevice (platforms, CL_DEVICE_TYPE_CPU) )
347+ switch (devTypeID )
302348 {
303- std::cerr << OUT_NAME (" init" ) " could not find any suitable device" << std::endl;
304- return false ;
349+ case CL_DEVICE_TYPE_CPU:
350+ devType = " CPU" ;
351+ break ;
352+ case CL_DEVICE_TYPE_GPU:
353+ devType = " GPU" ;
354+ break ;
355+ case CL_DEVICE_TYPE_ACCELERATOR:
356+ devType = " ACCELERATOR" ;
357+ break ;
358+ case CL_DEVICE_TYPE_CUSTOM:
359+ devType = " CUSTOM" ;
360+ break ;
361+ default :
362+ devType = " UNKNOWN" ;
305363 }
364+ std::cout << OUT_NAME (" init" ) " selected device: " << devName << " (" << devType << " )[" << devVendor << ' ]' << std::endl;
365+ }
366+ else
367+ {
368+ std::cerr << OUT_NAME (" init" ) " could not find any suitable device" << std::endl;
369+ return false ;
306370 }
371+
307372 context = cl::Context (device);
308373
309374 std::string options;
@@ -510,8 +575,8 @@ class OpenCLDepthPacketProcessorImpl
510575 }
511576};
512577
513- OpenCLDepthPacketProcessor::OpenCLDepthPacketProcessor () :
514- impl_ (new OpenCLDepthPacketProcessorImpl())
578+ OpenCLDepthPacketProcessor::OpenCLDepthPacketProcessor (const int deviceId ) :
579+ impl_ (new OpenCLDepthPacketProcessorImpl(deviceId ))
515580{
516581}
517582
0 commit comments