clCreateContextFromType在执行时以SEGFAULT结尾

时间:2015-04-09 11:59:16

标签: c++ opencl gpgpu opencl.net

我正在尝试在包含我的图形卡的平台上创建一个OpenCL上下文。但是当我打电话给clCreateContextFromType()时,会抛出一个SEGFAULT。

int main(int argc, char** argv)
{
    /*
    ...
    */
    cl_platform_id* someValidPlatformId;
    //creating heap space using malloc to store all platform ids
    getCLPlatforms(someValidPlatformId);
    //error handling for getCLPLatforms()

    //OCLPlatform(cl_platform_id platform)
    OCLPLatform platform = OCLPlatform(someValidPlatformId[0]);
    //OCLContext::OCL_GPU_DEVICE == CL_DEVICE_TYPE_GPU
    OCLContext context = OCLContext(platform,OCLContext::OCL_GPU_DEVICE);

    /*
    ...
    */
}

cl_platform_id* getCLPlatforms(cl_platform_id* platforms)
{
    cl_int errNum;
    cl_uint numPlatforms;

    numPlatforms = (cl_uint) getCLPlatformsCount(); //returns the platform count 
                                                    //using clGetPlatformIDs() 
                                                    //as described in the Khronos API
    if(numPlatforms == 0)
        return NULL;

    errNum = clGetPlatformIDs(numPlatforms,platforms,NULL);
    if(errNum != CL_SUCCESS)
        return NULL;

    return platforms;
}

OCLContext::OCLContext(OCLPlatform platform,unsigned int type)
{
    this->initialize(platform,type);
}

void OCLContext::initialize(OCLPlatform platform,unsigned int type)
{
    cl_int errNum;

    cl_context_properties contextProperties[] =
    {
        CL_CONTEXT_PLATFORM,
        (cl_context_properties)platform.getPlatformId(),
        0
    };

    cout << "a" << endl;std::flush(cout);

    this->context = clCreateContextFromType(contextProperties,
                                           (cl_device_type)type,
                                           &pfn_notify,
                                           NULL,&errNum);
    if(errNum != CL_SUCCESS)
        throw OCLContextException();

    cout << "b" << endl;std::flush(cout);
    /*
    ...
    */
}

给定的type是CL_DEVICE_TYPE_GPU,cl_context_properties数组包含的平台也是有效的。

为了调试错误,我实现了Khronos API描述的以下pfn_notify()函数:

static void pfn_notify(const char* errinfo, 
                       const void* private_info,
                       size_t cb, void* user_data)
{
    fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
    flush(cout);
}

以下是shell的输出:

$ ./OpenCLFramework.exe
a
Segmentation fault

我正在使用的机器具有以下属性:

  • Intel Core i5 2500 CPU
  • NVIDIA Geforce 210 GPU
  • 操作系统:Windows 7
  • AMD APP SDK 3.0 Beta
  • IDE:带有gdb的Eclipse

如果有人知道这个问题的答案,那就太好了。

1 个答案:

答案 0 :(得分:0)

现在问题似乎已经解决了。

注入一个有效的cl_platform_id整个gdb解决了SEGFAULT。所以我更深入地挖掘了错误的问题是我将值保存为标准基元。当我调用一个函数时,这个值被转换为cl_platform_id,某些函数处理失败了。因此看起来它是混合导致这种失败的类型。

现在我将值保存为cl_platform_id并在需要时将其转换为基元,反之亦然。

我感谢你的回答,并为我长期的无线电沉默道歉。