OpenCL C++ Wrapper API 1.1
OpenCL C++ Wrapper API 1.1 Documentation

Introduction

For many large applications C++ is the language of choice and so it seems reasonable to define C++ bindings for OpenCL.

The interface is contained with a single C++ header file cl.hpp and all definitions are contained within the namespace cl. There is no additional requirement to include cl.h and to use either the C++ or original C bindings it is enough to simply include cl.hpp.

The bindings themselves are lightweight and correspond closely to the underlying C API. Using the C++ bindings introduces no additional execution overhead.

For detail documentation on the bindings see:

The OpenCL C++ Wrapper API 1.1 (revision 04) http://www.khronos.org/registry/cl/specs/opencl-cplusplus-1.1.pdf

Example

The following example shows a general use case for the C++ bindings, including support for the optional exception feature and also the supplied vector and string classes, see following sections for decriptions of these features.

 #define __CL_ENABLE_EXCEPTIONS
 
 #if defined(__APPLE__) || defined(__MACOSX)
 #include <OpenCL/cl.hpp>
 #else
 #include <CL/cl.hpp>
 #endif
 #include <cstdio>
 #include <cstdlib>
 #include <iostream>
 
  const char * helloStr  = "__kernel void "
                           "hello(void) "
                           "{ "
                           "  "
                           "} ";
 
  int
  main(void)
  {
     cl_int err = CL_SUCCESS;
     try {

       std::vector<cl::Platform> platforms;
       cl::Platform::get(&platforms);
       if (platforms.size() == 0) {
           std::cout << "Platform size 0\n";
           return -1;
       }

       cl_context_properties properties[] = 
          { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
       cl::Context context(CL_DEVICE_TYPE_CPU, properties); 
 
       std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
 
       cl::Program::Sources source(1,
           std::make_pair(helloStr,strlen(helloStr)));
       cl::Program program_ = cl::Program(context, source);
       program_.build(devices);
 
       cl::Kernel kernel(program_, "hello", &err);
 
       cl::Event event;
       cl::CommandQueue queue(context, devices[0], 0, &err);
       queue.enqueueNDRangeKernel(
           kernel, 
           cl::NullRange, 
           cl::NDRange(4,4),
           cl::NullRange,
           NULL,
           &event); 
 
       event.wait();
     }
     catch (cl::Error err) {
        std::cerr 
           << "ERROR: "
           << err.what()
           << "("
           << err.err()
           << ")"
           << std::endl;
     }
 
    return EXIT_SUCCESS;
  }
 All Classes Namespaces Files Functions Variables Typedefs Defines