Quxflux
Quxflux

Reputation: 3303

OpenCL kernel with generic data type

When writing code for kernels, is it possible to specify a generic data type so that copying the kernel for each used data type is not necessary? Currently I'm using preprocessor macros to define the whole function with various data types:

#define REDUCTION(type) __kernel void reduce_##type##_f(__global __read_only type* a) \
{ \
// do something
} \

REDUCTION(float)
REDUCTION(float2)

This however is not very comfortable. Is there some type specifier like gentype available?

Upvotes: 2

Views: 1038

Answers (1)

simpel01
simpel01

Reputation: 1782

You should be able to do that starting with OpenCL 2.1 which let you use C++ and templates in kernel code Knronos's OpenCL page.

With that, you can simply write:

template <class T>
void reduce_f(__global __read_only T* a) { 
   // do something
}

However I am not 100% sure templates would be available in the definition of __kernel functions. If that is not the case, you would still need to wrap the kernel declaration within preprocessing macros as so:

#define REDUCTION(type) __kernel void reduce_##type##_f(__global __read_only type* a) \
{ \
  return reduce_t(a); \
}

REDUCTION(float)

Upvotes: 3

Related Questions