opencl Kernel Skelleton


Lets walk through the most simple kernel there is and some variations of it

__kernel void myKernel() {

A kernel which can be started from your main code is identified by the __kernel keyword. A Kernel function can only have return type void.

__kernel void myKernel(float a, uint b, byte c) {


Of course you can create more functions which are not exposed as kernels. In this case you can just omit the __kernel modifier.

A function may expose variables like any other C/C++ function would. The only difference is when you want to reference memory. This applies to all pointers whether they are arguments or used in code.

float*  ptr;

is a pointer to a memory region only the executing thread has access to. In fact it is the same as

__private float* ptr;

There are four different memory region modifiers available. Inside the kernel you usually don't have to worry about it, but for arguments this is essential.

  • __global: This modifier refers to a pointer which is placed in the global memory
  • __constant: refers to a constant memory pointer
  • __local: refers to a shared memory pointer
  • __private: refers to a local memory pointer

In addition we can define how we want to access the memory

  • no modifier: read and write
  • __read_only
  • __write_only

Those flags have to match the way we allocated the memory buffer back on our host.