OpenCL - 是否可以在内核中调用另一个函数?

问题描述:

我与设在这里的教程沿着以下:http://opencl.codeplex.com/wikipage?title=OpenCL%20Tutorials%20-%201OpenCL - 是否可以在内核中调用另一个函数?

他们列出的内核是这样的,它计算在输出变量两个数字,并将其存储的总和:

__kernel void vector_add_gpu (__global const float* src_a, 
        __global const float* src_b, 
        __global float* res, 
      const int num) 
{ 
    /* get_global_id(0) returns the ID of the thread in execution. 
    As many threads are launched at the same time, executing the same kernel, 
    each one will receive a different ID, and consequently perform a different computation.*/ 
    const int idx = get_global_id(0); 

    /* Now each work-item asks itself: "is my ID inside the vector's range?" 
    If the answer is YES, the work-item performs the corresponding computation*/ 
    if (idx < num) 
     res[idx] = src_a[idx] + src_b[idx]; 
} 

1)说例如,所执行的操作比总结要复杂得多 - 这是保证其功能的事情。我们称之为ComplexOp(in1,in2,out)。我将如何去实现这个函数,使vector_add_gpu()可以调用和使用它?你能举出例子代码吗?

2)现在让我们以极端的例子为例,现在我想调用一个通用函数来处理这两个数字。我如何设置它以便内核可以传递一个指向这个函数的指针并根据需要调用它?

+1

只是一个评论。这是OpenCL而不是CUDA。您不必强制使用多个工作组大小。我经常看到thouse丑陋的'如果(idx DarkZeros

您可以在内核中使用辅助函数,请参阅OpenCL user defined inline functions。你不能将函数指针传入内核。

+4

我喜欢你的名字也是亚当S –

是的,这是可能的。你只需要记住OpenCL是基于C99的一些注意事项。您可以在同一个内核文件内或单独的文件中创建其他函数,只需在开头添加它。辅助函数不需要声明为内联,但请记住,OpenCL将在调用时内联函数。调用辅助功能时指针也不可用。

float4 hit(float4 ray_p0, float4 ray_p1, float4 tri_v1, float4 tri_v2, float4 tri_v3) 
{ 
//logic to detect if the ray intersects a triangle 
} 

__kernel void detection(__global float4* trilist, float4 ray_p0, float4 ray_p1) 
{ 
int gid = get_global_id(0); 
float4 hitlocation = hit(ray_p0, ray_p1, trilist[3*gid], trilist[3*gid+1], trilist[3*gid+2]); 
} 
+0

你是什么意思?指示辅助功能时指针也无法使用? – Nigel

+0

在opencl内核中,指针不能在内核中的任何地方或从内核调用函数时使用。指针是这些警告之一。你不需要指针传递的一个原因是函数总是内联的,而不是交给一个单独的内存地址。 –