我正在尝试使用 C++ 和 OpenCL 执行并行计算,但我遇到了一个错误,试图将监听器函数传递给 clCreateContext。我的程序(未显示)正在无错误地崩溃,因此我需要添加一个函数以将 OpenCL 错误转发到 stdout/stderr。 clCreateContext 函数有一个函数指针参数,可以将其设置为将错误消息转发到 stdout 或 stderr。但是使用 Codeblocks/MinGW 时出现编译时错误:
invalid conversion from 'void (*)(const char*, const void*, size_t, void*)' to
'void (*)(const char*, const void*, size_t, void*)'
我已经在下面的代码中复制了这个问题:
#include <stdlib.h>
#include <stdio.h>
#include <CL\cl.h>
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);
}
int main()
{
/*Get platform and device info*/
cl_platform_id platform_id = NULL;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
/*Create openCL context*/
cl_context context = clCreateContext(NULL, 1, &device_id, &pfn_notify, NULL, &ret);
/*Some line after this throws an error*/
}
我已经看过使用这个确切方法和 pfn_notify 的代码示例,但我不确定为什么我的程序甚至无法编译。提前致谢,如果还有其他我可以发布的内容可以提供帮助,请告诉我。
最佳答案
你需要声明你的函数为
void CL_CALLBACK pfn_notify(.....
正如评论者所指出的;如果此代码在 C++ 文件中,则还需要:
extern "C" void CL_CALLBACK pfn_notify(.....
关于c++ - 在 OpenCL clCreateContext 中从 void (*) (...) 到 void (*) (...) 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23553062/