使用 C++ 在 Xcode 6.1.1 下编译简单的 OpenCL 示例代码时出现问题

Problems compiling simple OpenCL example code under Xcode 6.1.1 with C++

我有一个简单的 hello world OpenCL 程序。出于某种原因,当我尝试从代码中引用内核名称时,编译器出错。

在kernel.cl我有

kernel void square_kernel(global float *input, global float * output, const unsigned int count)
{
    size_t i = get_global_id(0);
    if(i<count)
        output[i] = input[i] * input[i];
}

在 main.cpp 我有:

...
#include "kernel.cl.h"
...
int main()
{
    ...
    dispatch_sync(queue, ^{
        size_t wgs;
        gcl_get_kernel_block_workgroup_info(square_kernel, CL_KERNEL_WORK_GROUP_SIZE, sizeof(wgs), &wgs, NULL);

        cl_ndrange range = {
            1, // number of dims
            {0, 0, 0}, // offset in each dim
            {DATA_SIZE, 0, 0}, // global range (total)
            {wgs, 0, 0} // local size of each work group #work_groups = DATA_SIZE  / wgs    
        };

        // call kernel
        square_kernel(&range, (cl_float *)mem_in, (cl_float *)mem_out, DATA_SIZE);

        // copy the output
        gcl_memcpy(results, mem_out, sizeof(cl_float) * DATA_SIZE);

    });
    ...
}

我在引用它的地方得到 "use of undeclared identifier square_kernel"。 cl 文件必须正在构建,因为它会生成字节代码文件。

现在我突然想到,这可能是因为我从 C++ 文件中引用它。但是,如果这是导致问题的原因,我不确定如何解决该问题。如果可以的话,我不想使用 C。有什么想法吗?

值得检查 auto-generated kernel.cl.h header 到 double-check 您在这种情况下尝试调用的函数的定义。我刚刚创建了一个新的 Xcode 项目并用它构建了您的 OpenCL 内核,生成的函数定义如下所示:

extern void (^square_kernel_kernel)(const cl_ndrange *ndrange, cl_float* input, cl_float* output, cl_uint count);

注意函数名称中的额外 _kernel