【发布时间】:2014-03-08 23:47:10
【问题描述】:
我正在尝试在运行时加载 OpenCL 库,以便相同的 exe 可以在没有 OpenCL 驱动程序的平台上运行而不会找到未解析的符号。我正在使用 Qt 来执行此操作,但我认为我不会因为 Qt 而面临问题。这是我检查是否安装了 OpenCL 1.1 的函数:
QLibrary *MyOpenCL::openCLLibrary = NULL;
bool MyOpenCL::loadOpenCL()
{
if(openCLLibrary)
return true;
QLibrary *lib = new QLibrary("OpenCL");
if(!lib->load())
return false;
bool result = false;
typedef cl_int (*MyPlatorms)(cl_uint, cl_platform_id *, cl_uint *);
MyPlatorms pobj = (MyPlatorms) lib->resolve("clGetPlatformIDs");
if(pobj)
{
cl_uint nplatforms = 0;
cl_uint myerr = pobj(0, NULL, &nplatforms);
if((myerr == CL_SUCCESS) && (nplatforms > 0))
{
cl_platform_id *mplatforms = new cl_platform_id[nplatforms];
myerr = pobj(nplatforms, mplatforms, NULL);
typedef cl_int (*MyPlatformInfo)(cl_platform_id, cl_platform_info, size_t, void *, size_t *);
MyPlatformInfo pinfoobj = (MyPlatformInfo) lib->resolve("clGetPlatformInfo");
if(pinfoobj)
{
size_t size;
for(unsigned int i = 0; i < nplatforms; i++)
{
size = 0;
myerr = pinfoobj(mplatforms[i], CL_PLATFORM_VERSION, 0, NULL, &size);//size = 27
if(size < 1)
continue;
char *ver = new char[size];
myerr = pinfoobj(mplatforms[i], CL_PLATFORM_VERSION, size, ver, NULL);
qDebug() << endl << ver;//segmentation fault at this line
...
}
可以看出Qt成功解析了clGetPlatformIDs()。它甚至显示有 1 个平台可用。但是当我传递数组来存储 cl_platform_id 时,它会崩溃。 为什么会这样?
编辑: 我正在使用带有 OpenCL APP SDK 2.9 的 MinGW 编译器的 Qt 4.8.1。 我正在使用来自 Khronos 网站的 OpenCL 1.1 标头。 我的笔记本电脑有 Windows7 64 位也有 ATI Radeon 7670m GPU,它有 OpenCL 1.1 驱动程序。
【问题讨论】:
-
不相关,但如果您使用 C++ 编程,为什么要使用
calloc?为什么不cl_platform_id *mplatforms = new cl_platform_id[nplatforms];? -
QLibrary *MyOpenCL::openCLLibrary = NULL;-> 这是一个非常非常糟糕的主意。请在源文件中使用Q_GLOBAL_STATIC(QLibrary, myOpenCLLibrary)代替(注意,不是标题)。更不用说,您应该使用 Q_NULLPTR 而不是“NULL”。 -
@LaszloPapp 为什么?它看起来像一个普通的
static成员变量定义。 -
@JoachimPileborg OpenCL 就像 C 语言的一个子集。到目前为止,我看到的所有教程都在使用 OpenCL 时使用 C 风格的编程。那么如果我在 C++ 中使用 C 风格的编程,将来会有什么问题吗?
-
@Cool_Coder 那么对于使用 C 函数的 C++ 来说,在指针之类的东西方面确实没有区别。指针是指针是指针。 如何分配并不重要。