1
votes

As known, OpenCV 3.0 supports new class cv::Umat which provides Transparent API (TAPI) to use OpenCL automaticaly if it can: http://code.opencv.org/projects/opencv/wiki/Opencv3#tapi

There are two introductions to the cv::Umat and TAPI:

But if I have:

  1. Intel CPU Core i5 (Haswell) 4xCores (OpenCL Intel CPUs with SSE 4.1, SSE 4.2 or AVX support)
  2. Intel Integrated HD Graphics which supports OpenCL 1.2
  3. 1st nVidia GPU GeForce GTX 970 (Maxwell) which supports OpenCL 1.2 and CUDA
  4. 2nd nVidia GPU GeForce GTX 970 ...

If I turn on OpenCL in OpenCV, then how can I change the device on which OpenCL-code will be executed: on 8 Cores of CPU, on Integrated HD Graphics, on 1st nVidia GPU or 2nd nVidia GPU?

How can I select one of each of these 4 devices to use OpenCL for parallel execution algorithms with cv::Umat?

For example, how can I use OpenCL acceleration on 4xCores of CPU Core-i5 with cv::Umat?

2
docs.opencv.org/modules/ocl/doc/introduction.html "Also the user can use cv::ocl::setDevice function (with cv::ocl::getOpenCLPlatforms and cv::ocl::getOpenCLDevices). This function initializes OpenCL runtime and setup the passed device as computing device." - DarkZeros
@DarkZeros Thank you! Yes, it might be work on OpenCV 2.4.12, but cv::ocl::setDevice is not in OpenCV 3.0 where appeared cv:Umat. There are namespace ocl docs.opencv.org/master/d5/d96/namespacecv_1_1ocl.html#gsc.tab=0 also class Device with enum-s TYPE_ and VENDOR_ docs.opencv.org/master/d7/d9f/… and functions attachContext()/initializeContextFromHandle(). But there are not function to set global variable to use required Device. How can I set Device for whole program, or this thread, or this cv::Umat in OpenCV3.0? - Alex

2 Answers

6
votes

I use something like this to check versions and hardware being used for OpenCL support.

ocl::setUseOpenCL(true);
if (!ocl::haveOpenCL())
{
    cout << "OpenCL is not available..." << endl;
    //return;
}

cv::ocl::Context context;
if (!context.create(cv::ocl::Device::TYPE_GPU))
{
    cout << "Failed creating the context..." << endl;
    //return;
}

cout << context.ndevices() << " GPU devices are detected." << endl; //This bit provides an overview of the OpenCL devices you have in your computer
for (int i = 0; i < context.ndevices(); i++)
{
    cv::ocl::Device device = context.device(i);
    cout << "name:              " << device.name() << endl;
    cout << "available:         " << device.available() << endl;
    cout << "imageSupport:      " << device.imageSupport() << endl;
    cout << "OpenCL_C_Version:  " << device.OpenCL_C_Version() << endl;
    cout << endl;
}

Then you can set your preferred hardware to use, using this

cv::ocl::Device(context.device(1));

Hope this helps you.

3
votes

You can also set a desired OpenCL device from within your code using environment variable method as follows (example is first GPU device):

if (putenv("OPENCV_OPENCL_DEVICE=:GPU:0") != 0 || !cv::ocl::useOpenCL())
{
    std::cerr << "Failed to set a desired OpenCL device" << std::endl;
    std::cerr << "Press any key to exit..." << std::endl;
    getchar();
    return 1;
}

Call to cv::ocl::useOpenCL() will force OpenCV to setup a default OpenCL device to the one specified in the environment variable OPENCV_OPENCL_DEVICE which is setup prior to that call.

I checked that this actually happens by setting a break-point at opencv_core310d.dll!cv::ocl::selectOpenCLDevice() Line 2256 (opencv\source\modules\core\src\ocl.cpp):

static cl_device_id selectOpenCLDevice()
{
    std::string platform, deviceName;
    std::vector<std::string> deviceTypes;

    const char* configuration = getenv("OPENCV_OPENCL_DEVICE");
    if (configuration &&
            (strcmp(configuration, "disabled") == 0 ||
             !parseOpenCLDeviceConfiguration(std::string(configuration), platform, deviceTypes, deviceName)
            ))
        return NULL;