tags:

views:

164

answers:

3

Is it possible to define my own functions in OpenCL code, in order that the kernels could call them? It yes, where can I see some simple example?

Thanks

+1  A: 

Based on the code samples here you can just write functions like:

inline int add(int a,int b)
{
   return a+b;
}

(Eg. look at the .cl file in the DXTC or bitonic sort examples.)

I don't know if that's an nvidia only extension but the OpenCL documentation talks about "auxiliary functions" as well as kernels.

Strange, the OpenCL spec doesn't say anything about __device
dmessf
Yes. I'm wondering if I misremembered. Or if Apple added it as an extension. So if you modded me up, mod me down again until it's tested :-)
Or...if I'm confusing with CUDA code. I've been playing with both.
A: 

user207442 is right: the documentation specifies that OpenCL supports auxiliary functions. See the last 2 pages of this link. It seems the auxiliary functions can be defined à la standard C99.

Yktula
A: 

Function used to create program is ...

cl_program clCreateProgramWithSource  (     
    cl_context context,
    cl_uint count,
    const char **strings,
    const size_t *lengths,
    cl_int *errcode_ret)

You can place functions inside the strings parameter like this,

float AddVector(float a, float b)
{
    return a + b;
}

kernel void VectorAdd(
    global read_only float* a,
    global read_only float* b,
    global write_only float* c )
{
    int index = get_global_id(0);
    //c[index] = a[index] + b[index];
    c[index] = AddVector(a[index], b[index]);
}

Now you have one user defined function "AddVector" and a kernel function "VectorAdd"

Kayhano