clCreateContextFromType 在执行时以 SEGFAULT 结束
clCreateContextFromType ends up in a SEGFAULT while execution
我正在尝试在包含我的显卡的平台上创建一个 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
我正在使用的机器具有以下属性:
- 英特尔酷睿 i5 2500 CPU
- NVIDIA Geforce 210 GPU
- OS: Windows 7
- AMD APP SDK 3.0 测试版
- IDE:Eclipse 与 gdb
如果有人知道这个问题的答案就太好了。
问题好像解决了
通过 gdb 注入有效的 cl_platform_id
解决了 SEGFAULT。所以我挖得更深一点,错误的问题是我将该值保存为标准原语。当我调用一个将此值转换为 cl_platform_id
的函数时,一些函数无法处理它。所以它看起来像是导致这种失败的类型混合。
现在我将值保存为 cl_platform_id
并在需要时将其转换为原语,而不是相反。
感谢您的回答,并为我长时间的无线电沉默表示歉意。
我正在尝试在包含我的显卡的平台上创建一个 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
我正在使用的机器具有以下属性:
- 英特尔酷睿 i5 2500 CPU
- NVIDIA Geforce 210 GPU
- OS: Windows 7
- AMD APP SDK 3.0 测试版
- IDE:Eclipse 与 gdb
如果有人知道这个问题的答案就太好了。
问题好像解决了
通过 gdb 注入有效的 cl_platform_id
解决了 SEGFAULT。所以我挖得更深一点,错误的问题是我将该值保存为标准原语。当我调用一个将此值转换为 cl_platform_id
的函数时,一些函数无法处理它。所以它看起来像是导致这种失败的类型混合。
现在我将值保存为 cl_platform_id
并在需要时将其转换为原语,而不是相反。
感谢您的回答,并为我长时间的无线电沉默表示歉意。