考虑以下示例:
std::string kernelCode =
"void kernel copy(global int* image, global int* result)"
"{"
"result[get_global_id(0)] = foo(get_global_id(0));"
"}";
sources.push_back({kernelCode.c_str(), kernelCode.length()});
cl::Program program(context, sources);
if (program.build({defaultDevice}) != CL_SUCCESS)
{
std::cerr << "Error while building kernel: " <<
program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(defaultDevice) << std::endl;
exit(1);
}
else
{
qDebug() << "Kernel compiled succesfully\n";
}
如何使用或定义如下函数:
int foo(int id)
{
//...
}
在内核中使用?
答案 0 :(得分:1)
Source可以包含多个函数,因此只需添加前缀:
std::string kernelCode =
"int foo(int id)"
"{"
" return id + 1;"
"}"
"void kernel copy(global int* image, global int* result)"
"{"
" result[get_global_id(0)] = foo(get_global_id(0));"
"}";