如何使用clCreateImage

时间:2013-08-07 18:09:22

标签: opencl

我正在尝试使用clCreateImage创建一个cl_mem,但程序一直在崩溃。我尽可能地关注我的书,但到目前为止,这是一条相当不错的道路。

#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;
}

2 个答案:

答案 0 :(得分:1)

clCreateImage具有以下参数:

 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)

在doc页面中没有提到“host_ptr”可能为NULL。尝试使用有效的指针。这与允许使用NULL指针的clCrateBuffer不同。但是在CreateBuffer中也没有提到这种情况,但我确实知道它有效。所以它可能是驱动程序/库错误。

由于很明显OpenCL库试图访问NULL指针位置,因为此错误代码指出:Access violation executing location 0x00000000我建议首先尝试使用它。

答案 1 :(得分:0)

我认为忽略usaless paremeter的代码更容易,如下所示:

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;

此外,&#34; host_ptr&#34;可以是NULL。 常见错误通常是错误的图像格式,设备不支持,错误的大小和错误的版本。