OpenCL-是否可以从内核中调用另一个函数?
我正在关注这里的教程: http://opencl.codeplex.com/wikipage?title=OpenCL%20Tutorials%20-%201
I am following along with a tutorial located here: http://opencl.codeplex.com/wikipage?title=OpenCL%20Tutorials%20-%201
他们列出的内核是这个,它计算两个数字的总和并将其存储在输出变量中:
The kernel they have listed is this, which computes the sum of two numbers and stores it in the output variable:
__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()可以调用和使用它?你能给出示例代码吗?
1) Say for example that the operation performed was much more complex than a summation - something that warrants its own function. Let's call it ComplexOp(in1, in2, out). How would I go about implementing this function such that vector_add_gpu() can call and use it? Can you give example code?
2)现在让我们举一个例子,我现在想调用一个对两个数字进行运算的泛型函数.我将如何设置它,以便可以将指向该函数的指针传递给内核并在必要时调用它?
2) Now let's take the example to the extreme, and I now want to call a generic function that operates on the two numbers. How would I set it up so that the kernel can be passed a pointer to this function and call it as necessary?
是可以的.您只需要记住,OpenCL基于C99,但有一些警告.您可以在同一内核文件中或在单独的文件中创建其他函数,并将其仅包含在开头.辅助功能不需要声明为内联,但是请记住,OpenCL将在调用时内联这些功能.调用辅助功能时,指针也不可用.
Yes it is possible. You just have to remember that OpenCL is based on C99 with some caveats. You can create other functions either inside of the same kernel file or in a seperate file and just include it in the beginning. Auxiliary functions do not need to be declared as inline however, keep in mind that OpenCL will inline the functions when called. Pointers are also not available to use when calling auxiliary functions.
示例
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]);
}