clCreateContext fails with error code -6

Hi all,

I have a fairly basic function that initializes OpenCL resources. I do not why but it fails in creating clCreateContext with error code -6 which corresponds to CL_HOST_OUT_OF_MEMORY. I open the task manager and it shows 2GB of memory in use and I still have 6GB left so why am i getting this error?

Here is the code:

#include "cl_resources.h"

#ifdef _CHAR16T

#define CHAR16_T

#endif

#include "mex.h"

cl_context          context;

cl_device_id        *devices;

cl_command_queue    commandQueue;

cl_program program;

cl_kernel  kernel;

/*

 * Converts the contents of a file into a string

*/

int convertToString(const char *filename, std::string& s)

{

    size_t size;

    char*  str;

std::fstream f(filename, (std::fstream::in | std::fstream::binary));

if(f.is_open())

    {

        size_t fileSize;

        f.seekg(0, std::fstream::end);

        size = fileSize = (size_t)f.tellg();

        f.seekg(0, std::fstream::beg);

str = new char;

        if(!str)

        {

            f.close();

            return NULL;

        }

f.read(str, fileSize);

        f.close();

        str = '\0';

s = str;

        delete[] str;

        return 0;

    }

    mexPrintf("Error: Failed to open file %s\n", filename);

    return 1;

}

/*

 * \brief OpenCL related initialization 

 *        Create Context, Device list, Command Queue

 *        Create OpenCL memory buffer objects

 *        Load CL file, compile, link CL source 

 *		  Build program and kernel objects

 */

int initializeCL(void)

{

    cl_int status = 0;

size_t deviceListSize;

/*

     * Have a look at the available platforms and pick either

     * the AMD one if available or a reasonable default.

     */

cl_uint numPlatforms;

    cl_platform_id platform = NULL;

    status = clGetPlatformIDs(0, NULL, &numPlatforms);

    if(status != CL_SUCCESS)

    {

        mexPrintf("Error: Getting Platforms. (clGetPlatformsIDs)\n");

        return 1;

    }

if(numPlatforms > 0)

    {

        cl_platform_id* platforms = (cl_platform_id *)malloc(numPlatforms*sizeof(cl_platform_id));

        status = clGetPlatformIDs(numPlatforms, platforms, NULL);

        if(status != CL_SUCCESS)

        {

            mexPrintf("Error: Getting Platform Ids. (clGetPlatformsIDs)\n");

            return 1;

        }

        for(unsigned int i=0; i < numPlatforms; ++i)

        {

            char pbuff[100];

            status = clGetPlatformInfo(

                        platforms[i],

                        CL_PLATFORM_VENDOR,

                        sizeof(pbuff),

                        pbuff,

                        NULL);

            if(status != CL_SUCCESS)

            {

                mexPrintf("Error: Getting Platform Info. (clGetPlatformInfo)\n");

                return 1;

            }

            platform = platforms[i];

            if(!strcmp(pbuff, "Advanced Micro Devices, Inc."))

            {

                break;

            }

        }

        delete platforms;

    }

if(NULL == platform)

    {

        std::cout << "NULL platform found so Exiting Application." << std::endl;

        return 1;

    }

/*

     * If we could find our platform, use it. Otherwise use just available platform.

     */

cl_context_properties cps[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0 };

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

    // Create an OpenCL context

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

	cl_uint uiNumDevices;

	status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &uiNumDevices);

	if(status != CL_SUCCESS) 

    {  

        mexPrintf("Error: Creating Context. (clCreateContextFromType) error in NumDevices status is %d\n", status);

        return 1; 

    }

	devices = (cl_device_id *)malloc(uiNumDevices * sizeof(cl_device_id) );

    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, uiNumDevices, devices, NULL);

	if(status != CL_SUCCESS) 

    {  

        mexPrintf("Error: Creating Context. (clCreateContextFromType) clGetDeviceIds status is %d\n", status);

        return 1; 

    }

	context = clCreateContext(0, uiNumDevices, devices, NULL, NULL, &status);

  //context = clCreateContextFromType(cps, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);

	

    if(status != CL_SUCCESS) 

    {  

        mexPrintf("Error: Creating Context. (clCreateContext) status is %d\n", status);

		mexPrintf("uiNumDevices %d\n", uiNumDevices);

        return 1; 

    }

/* First, get the size of device list data */

    status = clGetContextInfo(context, 

                              CL_CONTEXT_DEVICES, 

                              0, 

                              NULL, 

                              &deviceListSize);

    if(status != CL_SUCCESS) 

    {  

        mexPrintf(

            "Error: Getting Context Info \

            (device list size, clGetContextInfo)\n");

        return 1;

    }

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

    // Detect OpenCL devices

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

   // devices = (cl_device_id *)malloc(deviceListSize);

    if(devices == 0)

    {

        mexPrintf("Error: No devices found.\n");

        return 1;

    }

/* Now, get the device list data */

    status = clGetContextInfo(

                 context, 

                 CL_CONTEXT_DEVICES, 

                 deviceListSize, 

                 devices, 

                 NULL);

    if(status != CL_SUCCESS) 

    { 

        mexPrintf("Error: Getting Context Info \

            (device list, clGetContextInfo)\n");

        return 1;

    }

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

    // Create an OpenCL command queue

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

    commandQueue = clCreateCommandQueue(

                       context, 

                       devices[0], 

                       0, 

                       &status);

    if(status != CL_SUCCESS) 

    { 

        mexPrintf("Creating Command Queue. (clCreateCommandQueue)\n");

        return 1;

    }

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

    // Load CL file, build CL program object, create CL kernel object

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

    const char * filename  = "sts_kernel.cl";

    std::string  sourceStr;

    status = convertToString(filename, sourceStr);

    if(status != CL_SUCCESS)

        return 1;

const char * source    = sourceStr.c_str();

    size_t sourceSize[]    = { strlen(source) };

program = clCreateProgramWithSource(

                  context, 

                  1, 

                  &source,

                  sourceSize,

                  &status);

    if(status != CL_SUCCESS) 

    { 

        mexPrintf("Error: Loading Binary into cl_program \

               (clCreateProgramWithBinary)\n");

      return 1;

    }

/* create a cl program executable for all the devices specified */

	char* build_log;

	size_t log_size;

    status = clBuildProgram(program, 1, devices, NULL, NULL, NULL);

    if(status != CL_SUCCESS) 

    { 

        mexPrintf("Error: Building Program (clBuildProgram)\n");

		// First call to know the proper size

		clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);

		build_log = new char[log_size+1];

		// Second call to get the log

		clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);

		build_log[log_size] = '\0';

		mexPrintf("%s\n", build_log);

		delete[] build_log;

        return 1; 

    }

/* get a kernel object handle for a kernel with the given name */

    kernel = clCreateKernel(program, "sts_kernel", &status);

    if(status != CL_SUCCESS) 

    {  

        mexPrintf("Error: Creating Kernel from program. (clCreateKernel)\n");

        return 1;

    }

return 0;

}

I really need to get this going…any help would be highly appreciated. This code work perfectly fine on ATI Radeon but does not run on GTX 260.

regards,

Richeek