OpenCL - is it possible to invoke another function from within a kernel?

前端 未结 2 1973
情歌与酒
情歌与酒 2021-02-02 00:40

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 comput

相关标签:
2条回答
  • 2021-02-02 01:01

    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.

    Example

    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 讨论(0)
  • 2021-02-02 01:04

    You can have auxiliary functions for use in the kernel, see OpenCL user defined inline functions . You can not pass function pointers into the kernel.

    0 讨论(0)
提交回复
热议问题