8 Replies Latest reply on Apr 30, 2013 5:21 AM by himanshu.gautam

    OpenCL C++ wrapper

    sajis997

      Hi forum

       

      I am using the C++ wrapper for the opencl and opengl interoperability. In that case i have to define the properties as follows:

       

       

      try

      {

            std::vector<cl::Platform> platformList;

            std::vector<cl::Kernel> allKernels;

           

            // Pick platform

            cl::Platform::get(&platformList);

       

       

           

            // Pick first platform

            cl_context_properties cprops[] =

                 {

                    CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),

                    CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),             

                    CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(),

                    0

                 };

           

            cl::Context context(CL_DEVICE_TYPE_GPU, cprops);

      .........................

      ..........................

       

      }

      catch(...)

      {

       

      }

       

       

      It compiles fine, but while runnning it i get the following error:

       

      clCreateContextFromType: Error code -2

       

       

      Does it mean that it failed to create context by type?

       

       

      How to get around this problem?

       

       

       

      Regards

      Sajjad

        • Re: OpenCL C++ wrapper
          nou

          yes. -2 means CL_DEVICE_NOT_AVAILABLE. can you run SimpleGL example from SDK?

            • Re: OpenCL C++ wrapper
              sajis997

              Hi

               

              Yes, i can run all the nvidia sdk examples .

               

               

              Where should i look into to track the problem?

               

               

               

              Regards

              Sajjad

                • Re: OpenCL C++ wrapper
                  himanshu.gautam

                   

                  Yes, i can run all the nvidia sdk examples .

                  Are you running on NVIDIA GPUs? Shouldn't you be asking this on NVIDIA forums then?

                  Please give your system details, and check AMD APP SDK's SimpleGL Sample.

                    • Re: OpenCL C++ wrapper
                      sajis997

                      Hi

                       

                      If i were getting any response from the CUDA forums about the queries regarding OpenCL, I should not have showed up here. It seems that they have decided to discard OpenCL from their standard implicitly.

                       

                      I have NViDIA GTX 560 M with recent driver and CUDA SDK 4.2 . OS is Ubuntu linux 12.04

                       

                       

                      Any thing else i can provide with?

                       

                       

                      Thanks

                      Sajjad

                        • Re: OpenCL C++ wrapper
                          dmeiser

                          Hey Sajjad,

                           

                          I you post a working example I could try to reproduce the error. I have access to cuda SDK 5 and 4.2 on a variety of hardware. I could try and see if it's fixed in newer versions of the SDK or if it's specific to a particular GPU.

                           

                          Cheers,

                          Dominic

                            • Re: OpenCL C++ wrapper
                              sajis997

                              Hi folks,

                               

                              Sorry for being detached for quite a while over this issue. I am inserting the following code snippet . It compiling fine on my system ,but getting error. Please try it out for me!

                               

                              ////////////////////////////////////////////////////////////////

                               

                              // vecadd.cpp

                              //

                              //    This is a simple example that demonstrates use OpenCL C++ Wrapper API.

                               

                               

                              // Enable OpenCL C++ exceptions

                              #define __CL_ENABLE_EXCEPTIONS

                               

                               

                              #if defined(__APPLE__) || defined(__MACOSX)

                              #include <OpenGL/opengl.h>

                              #else

                              #include <GL/glew.h>

                              #include <GL/gl.h>

                              #include <GL/glut.h>

                              #include <GL/glu.h>

                              #include <GL/glx.h>

                              #endif

                               

                               

                              #ifdef __APPLE__

                              #include <OpenCL/opencl.h>

                              #else

                              #include <CL/cl.hpp>

                              #include <CL/cl_gl.h>

                              #endif

                               

                               

                               

                               

                              #include <cstdio>

                              #include <cstdlib>

                              #include <iostream>

                               

                               

                              #define BUFFER_SIZE 20

                               

                               

                              int A[BUFFER_SIZE];

                              int B[BUFFER_SIZE];

                              int C[BUFFER_SIZE];

                               

                               

                              static char

                              kernelSourceCode[] =

                              "__kernel void                                                               \n"

                              "vadd(__global int * a, __global int * b, __global int * c)                                                                     \n"

                              "{                                                                           \n"

                              "    size_t i =  get_global_id(0);                                           \n"

                              "                                                                            \n"

                              "    c[i] = a[i] + b[i];                                                     \n"

                              "}                                                                           \n"

                              ;

                               

                               

                              int main(void)

                              {

                                  cl_int err;

                               

                               

                                  // Initialize A, B, C

                                  for (int i = 0; i < BUFFER_SIZE; i++) {

                                      A[i] = i;

                                      B[i] = i * 2;

                                      C[i] = 0;

                                  }

                               

                               

                                  try {

                                      std::vector<cl::Platform> platformList;

                               

                               

                                      // Pick platform

                                      cl::Platform::get(&platformList);

                               

                               

                                      // Pick first platform

                                        /*

                                      cl_context_properties cprops[] = {

                                           CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(), 0};

                                        */

                               

                               

                               

                                        cl_context_properties cprops[] =

                                           {

                                              CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),

                                              CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(),             

                                              CL_CONTEXT_PLATFORM, (cl_context_properties)(platformList[0])(),

                                              0

                                           };

                               

                                      cl::Context context(CL_DEVICE_TYPE_GPU, cprops);

                               

                               

                                      // Query the set of devices attched to the context

                                      std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();

                               

                               

                                      // Create and program from source

                                      cl::Program::Sources sources(1, std::make_pair(kernelSourceCode, 0));

                                      cl::Program program(context, sources);

                               

                               

                                      // Build program

                                      program.build(devices);

                               

                               

                                      // Create buffer for A and copy host contents

                                      cl::Buffer aBuffer = cl::Buffer(

                                          context,

                                          CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,

                                          BUFFER_SIZE * sizeof(int),

                                          (void *) &A[0]);

                               

                               

                                      // Create buffer for B and copy host contents

                                      cl::Buffer bBuffer = cl::Buffer(

                                          context,

                                          CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,

                                          BUFFER_SIZE * sizeof(int),

                                          (void *) &B[0]);

                               

                               

                                      // Create buffer for that uses the host ptr C

                                      cl::Buffer cBuffer = cl::Buffer(

                                          context,

                                          CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR,

                                          BUFFER_SIZE * sizeof(int),

                                          (void *) &C[0]);

                               

                               

                                      // Create kernel object

                                      cl::Kernel kernel(program, "vadd");

                               

                               

                                      // Set kernel args

                                      kernel.setArg(0, aBuffer);

                                      kernel.setArg(1, bBuffer);

                                      kernel.setArg(2, cBuffer);

                               

                               

                                      // Create command queue

                                      cl::CommandQueue queue(context, devices[0], 0);

                               

                                      // Do the work

                                      queue.enqueueNDRangeKernel(

                                          kernel,

                                          cl::NullRange,

                                          cl::NDRange(BUFFER_SIZE),

                                          cl::NullRange);

                               

                               

                               

                                      // Map cBuffer to host pointer. This enforces a sync with

                                      // the host backing space, remember we choose GPU device.

                                      int * output = (int *) queue.enqueueMapBuffer(

                                          cBuffer,

                                          CL_TRUE, // block

                                          CL_MAP_READ,

                                          0,

                                          BUFFER_SIZE * sizeof(int));

                               

                               

                                      for (int i = 0; i < BUFFER_SIZE; i++) {

                                          std::cout << C[i] << " ";

                                      }

                                      std::cout << std::endl;

                               

                               

                                      // Finally release our hold on accessing the memory

                                      err = queue.enqueueUnmapMemObject(

                                          cBuffer,

                                          (void *) output);

                               

                                      // There is no need to perform a finish on the final unmap

                                      // or release any objects as this all happens implicitly with

                                      // the C++ Wrapper API.

                                  }

                                  catch (cl::Error err) {

                                       std::cerr

                                           << "ERROR: "

                                           << err.what()

                                           << "("

                                           << err.err()

                                           << ")"

                                           << std::endl;

                               

                               

                                       return EXIT_FAILURE;

                                  }

                               

                               

                                  return EXIT_SUCCESS;

                              }

                               

                               

                               

                              ///////////////////////////////////////////////////////////////

                               

                               

                              I have already provided with my system specification in the chain of thread discussions.

                               

                              Will be waiting for your feed-back.

                               

                               

                              Regards

                              Sajjad