@@ -153,8 +153,9 @@ class OpenCLDepthPacketProcessorImpl
153
153
cl::Buffer buf_filtered;
154
154
155
155
bool isInitialized;
156
+ const int deviceId;
156
157
157
- OpenCLDepthPacketProcessorImpl () : isInitialized(false )
158
+ OpenCLDepthPacketProcessorImpl (const int deviceId = - 1 ) : isInitialized(false ), deviceId(deviceId )
158
159
{
159
160
newIrFrame ();
160
161
newDepthFrame ();
@@ -221,43 +222,81 @@ class OpenCLDepthPacketProcessorImpl
221
222
options = oss.str ();
222
223
}
223
224
224
- bool selectDevice (const std::vector<cl::Platform> &platforms, const int type )
225
+ void getDevices (const std::vector<cl::Platform> &platforms, std::vector<cl::Device> &devices )
225
226
{
226
- bool selected = false ;
227
-
227
+ devices.clear ();
228
228
for (size_t i = 0 ; i < platforms.size (); ++i)
229
229
{
230
230
const cl::Platform &platform = platforms[i];
231
- std::string platformName, platformVendor;
232
- platform.getInfo (CL_PLATFORM_NAME, &platformName);
233
- platform.getInfo (CL_PLATFORM_VENDOR, &platformVendor);
234
231
235
- std::cout << OUT_NAME (" selectDevice" ) " found platform: " << platformName << " vendor: " << platformVendor << std::endl;
236
-
237
- std::vector<cl::Device> devices;
238
- if (platform.getDevices (type, &devices) != CL_SUCCESS)
232
+ std::vector<cl::Device> devs;
233
+ if (platform.getDevices (CL_DEVICE_TYPE_ALL, &devs) != CL_SUCCESS)
239
234
{
240
- std::cerr << OUT_NAME (" selectDevice" ) " error while getting opencl devices." << std::endl;
241
- return false ;
235
+ continue ;
242
236
}
243
237
244
- for (size_t j = 0 ; j < devices.size (); ++j)
238
+ devices.insert (devices.end (), devs.begin (), devs.end ());
239
+ }
240
+ }
241
+
242
+ void listDevice (std::vector<cl::Device> &devices)
243
+ {
244
+ std::cout << OUT_NAME (" listDevice" ) " devices:" << std::endl;
245
+ for (size_t i = 0 ; i < devices.size (); ++i)
246
+ {
247
+ cl::Device &dev = devices[i];
248
+ std::string devName, devVendor, devType;
249
+ size_t devTypeID;
250
+ dev.getInfo (CL_DEVICE_NAME, &devName);
251
+ dev.getInfo (CL_DEVICE_VENDOR, &devVendor);
252
+ dev.getInfo (CL_DEVICE_TYPE, &devTypeID);
253
+
254
+ switch (devTypeID)
245
255
{
246
- cl::Device &dev = devices[i];
247
- std::string devName, devVendor;
248
- dev.getInfo (CL_DEVICE_NAME, &devName);
249
- dev.getInfo (CL_DEVICE_VENDOR, &devVendor);
256
+ case CL_DEVICE_TYPE_CPU:
257
+ devType = " CPU" ;
258
+ break ;
259
+ case CL_DEVICE_TYPE_GPU:
260
+ devType = " GPU" ;
261
+ break ;
262
+ case CL_DEVICE_TYPE_ACCELERATOR:
263
+ devType = " ACCELERATOR" ;
264
+ break ;
265
+ case CL_DEVICE_TYPE_CUSTOM:
266
+ devType = " CUSTOM" ;
267
+ break ;
268
+ default :
269
+ devType = " UNKNOWN" ;
270
+ }
250
271
251
- std::cout << OUT_NAME (" selectDevice" ) " found device: " << devName << " vendor: " << devVendor << std::endl;
272
+ std::cout << " " << i << " : " << devName << " (" << devType << " )[" << devVendor << ' ]' << std::endl;
273
+ }
274
+ }
252
275
253
- if (!selected )
254
- {
255
- selected = true ;
256
- device = dev;
257
- }
258
- }
276
+ bool selectDevice (std::vector<cl::Device> &devices )
277
+ {
278
+ if (deviceId != - 1 && devices. size () > ( size_t )deviceId)
279
+ {
280
+ device = devices[deviceId];
281
+ return true ;
259
282
}
260
283
284
+ bool selected = false ;
285
+ size_t selectedType = 0 ;
286
+
287
+ for (size_t i = 0 ; i < devices.size (); ++i)
288
+ {
289
+ cl::Device &dev = devices[i];
290
+ size_t devTypeID;
291
+ dev.getInfo (CL_DEVICE_TYPE, &devTypeID);
292
+
293
+ if (!selected || (selectedType != CL_DEVICE_TYPE_GPU && devTypeID == CL_DEVICE_TYPE_GPU))
294
+ {
295
+ selectedType = devTypeID;
296
+ selected = true ;
297
+ device = dev;
298
+ }
299
+ }
261
300
return selected;
262
301
}
263
302
@@ -289,16 +328,42 @@ class OpenCLDepthPacketProcessorImpl
289
328
return false ;
290
329
}
291
330
292
- if (!selectDevice (platforms, CL_DEVICE_TYPE_GPU))
331
+ std::vector<cl::Device> devices;
332
+ getDevices (platforms, devices);
333
+ listDevice (devices);
334
+ if (selectDevice (devices))
293
335
{
294
- std::cout << OUT_NAME (" init" ) " could not find any GPU device. trying CPU devices" << std::endl;
336
+ std::string devName, devVendor, devType;
337
+ size_t devTypeID;
338
+ device.getInfo (CL_DEVICE_NAME, &devName);
339
+ device.getInfo (CL_DEVICE_VENDOR, &devVendor);
340
+ device.getInfo (CL_DEVICE_TYPE, &devTypeID);
295
341
296
- if (! selectDevice (platforms, CL_DEVICE_TYPE_CPU) )
342
+ switch (devTypeID )
297
343
{
298
- std::cerr << OUT_NAME (" init" ) " could not find any suitable device" << std::endl;
299
- return false ;
344
+ case CL_DEVICE_TYPE_CPU:
345
+ devType = " CPU" ;
346
+ break ;
347
+ case CL_DEVICE_TYPE_GPU:
348
+ devType = " GPU" ;
349
+ break ;
350
+ case CL_DEVICE_TYPE_ACCELERATOR:
351
+ devType = " ACCELERATOR" ;
352
+ break ;
353
+ case CL_DEVICE_TYPE_CUSTOM:
354
+ devType = " CUSTOM" ;
355
+ break ;
356
+ default :
357
+ devType = " UNKNOWN" ;
300
358
}
359
+ std::cout << OUT_NAME (" init" ) " selected device: " << devName << " (" << devType << " )[" << devVendor << ' ]' << std::endl;
360
+ }
361
+ else
362
+ {
363
+ std::cerr << OUT_NAME (" init" ) " could not find any suitable device" << std::endl;
364
+ return false ;
301
365
}
366
+
302
367
context = cl::Context (device);
303
368
304
369
std::string options;
@@ -505,8 +570,8 @@ class OpenCLDepthPacketProcessorImpl
505
570
}
506
571
};
507
572
508
- OpenCLDepthPacketProcessor::OpenCLDepthPacketProcessor () :
509
- impl_ (new OpenCLDepthPacketProcessorImpl())
573
+ OpenCLDepthPacketProcessor::OpenCLDepthPacketProcessor (const int deviceId ) :
574
+ impl_ (new OpenCLDepthPacketProcessorImpl(deviceId ))
510
575
{
511
576
}
512
577
0 commit comments