Creating a context on any platform/device

I currently am writing some unittests for OpenCL kernels and need to create a context. As I am not after performance it does not matter to me which device is running the kernel. So I want to create the context with as little restrictions as possible and thought of this code:

#define __CL_ENABLE_EXCEPTIONS
#include <CL/cl.hpp>
#include <iostream>

int main() {
  try {
    cl::Context context(CL_DEVICE_TYPE_ALL);
  }catch(const cl::Error &err) {
    std::cerr << "Caught cl::Error (" << err.what() << "): " << err.err() << "\n";
  }
}

Which returns

Caught cl::Error (clCreateContextFromType): -32

-32 is CL_INVALID_PLATFORM and the documentation of clCreateContextFromType says:

CL_INVALID_PLATFORM if properties is NULL and no platform could be selected or if platform value specified in properties is not a valid platform.

As I have not provided any properties they are of course NULL. Why can't any platform be selected?

I also have tried CL_DEVICE_TYPE_DEFAULT with the same result.

Here is a list of my platform and device that were detected:

NVIDIA CUDA
        (GPU) GeForce GTX 560

As a side node: Specifying the platform in the properties works as intented.

Upvotes: 1

Views: 1324

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

I tried your code, using CL_DEVICE_TYPE_ALL and it worked on my setup. I'm not sure why it's not working on yours...

As a workaround, maybe you could do something like this:

// Get all available platforms
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);

// Pick the first and get all available devices
std::vector<cl::Device> devices;
platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices);

// Create a context on the first device, whatever it is
cl::Context context(devices[0]);

Upvotes: 1

DarkZeros
DarkZeros

Reputation: 8410

cl::Context context(cl_device_type) is calling the complete constructor with default parameters:

cl::Context::Context(cl_device_type     type,
                     cl_context_properties *    properties = NULL,
                     void(CL_CALLBACK *notifyFptr)(const char *,const void *,::size_t,void *)    = NULL,
                     void *     data = NULL,
                     cl_int *   err = NULL   
)

Witch is only a wrapper of C++ to the underlying clCreateContextFromType().

This function allows that a NULL pointer is passed as a property, but then, the platform selection is implementation dependent. And it looks like in your case it does not default to nVIDIA platform.

You will have to pass some info to the constructor I'm afraid....

Upvotes: 0

Related Questions