user1873073
user1873073

Reputation: 3670

How to use clCreateImage

I am trying to create a cl_mem using clCreateImage but the program keeps crashing. I am following my book as close as possible but it's been a pretty bump road so far.

#include "stdafx.h"
#include <iostream>
#include <CL\cl.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cl_int status;

    cl_platform_id platform;
    status = clGetPlatformIDs(1, &platform, NULL);
    cl_device_id device;
    clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL);
    cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, (cl_context_properties) (platform), 0 };
    cl_context context = clCreateContext(props, 1, &device, NULL, NULL, &status);

    cl_image_desc desc;
    desc.image_type = CL_MEM_OBJECT_IMAGE2D;
    desc.image_width = 100;
    desc.image_height = 100;
    desc.image_depth = 0;
    desc.image_array_size = 0;
    desc.image_row_pitch = 0;
    desc.image_slice_pitch = 0;
    desc.num_mip_levels = 0;
    desc.num_samples = 0;
    desc.buffer = NULL;

    cl_image_format format;
    format.image_channel_order = CL_R;
    format.image_channel_data_type = CL_FLOAT;

    // crashes on the next line with -- Unhandled exception at 0x72BCC9F1 in Convolution.exe: 0xC0000005: Access violation executing location 0x00000000.
    cl_mem d_inputImage = clCreateImage(context, CL_MEM_READ_ONLY, &format, &desc, NULL, &status);
    // never gets here

    cout << "--->"; int exit; cin >> exit;
    return 0;
}

Upvotes: 0

Views: 5187

Answers (2)

zac
zac

Reputation: 161

I think it's easier to code ignoring the usaless paremeters as follow:

cl_image_desc desc;
memset(&desc, '\0', sizeof(cl_image_desc));
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
desc.image_width = width;
desc.image_height = height;
desc.mem_object= NULL; // or someBuf;

Additionally, the "host_ptr" can be NULL. The common errors are usually the wrong image format which isn't supported by device, the wrong size and wrong version.

Upvotes: 0

DarkZeros
DarkZeros

Reputation: 8410

clCreateImage has the following parameters:

 cl_mem clCreateImage (     cl_context context,
                           cl_mem_flags flags,
                           const cl_image_format *image_format,
                           const cl_image_desc *image_desc,
                           void *host_ptr,
                           cl_int *errcode_ret)

In the doc page there is no mention that "host_ptr" may be NULL. Try with a valid pointer there. This is different from clCrateBuffer where a NULL pointer is allowed. However in CreateBuffer there is also no mention to that case, but I do know that it works. So it may be a driver/library bug.

Since it is fairly clear that the OpenCL library is trying to access a NULL pointer location as this error code states :Access violation executing location 0x00000000 I recomend to first try with that.

Upvotes: 1

Related Questions