0

我正在尝试在包含我的显卡的平台上创建一个 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数组包含的平台也是有效的。

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

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

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

  • 英特尔酷睿 i5 2500 处理器
  • 英伟达 Geforce 210 GPU
  • 操作系统:Windows 7
  • AMD APP SDK 3.0 测试版
  • IDE:带有 gdb 的 Eclipse

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

4

1 回答 1

0

现在问题似乎解决了。

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

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

我感谢您的回答,并为我长时间的无线电沉默表示歉意。

于 2015-10-05T13:30:06.657 回答