Skip to content

Buffer Handling: Primitive Types

Hüseyin Tuğrul BÜYÜKIŞIK edited this page Jun 13, 2019 · 2 revisions

Cekirdekler API lets one use different type of arrays from different contexts:

  • C# arrays as float[], int[], uint[], char[], byte[], double[], long[]
  • C++ array wrappers ClFloatArray,ClIntArray,ClUIntArray,ClCharArray,ClByteArray,ClDoubleArray,ClLongArray that are faster for streaming data.
  • ClArray<T> that can switch between C# arrays or C++ array wrappers(ClFastArr<T> for CLFloatArray ...) whenever needed, resize and keep some of old data and dispose automatically.

If developer needs to work on an already-written project (e.g. to make it faster) without any extra buffer creations, he/she can use ClArray<T> with its implicit conversion method:

    float [] x = new float[N];
    ... x is populated with data by project ...

    ClArray<float> xCL = x;

this usage creates a new ClArray instance but add the ability to clearly adjust necessary memory copy flags for it and pass it into a compute method easily.

If same C# array needs to be swapped with a faster C++ array version, simply setting this flag:

 xCL.fastArr = true;

creates a new C++ wrapper and copies data from x to new array inside so a faster communication with GPUs will happen for many consecutive kernel executions as in this example:

     xCL.compute(cr, 1, "vectorAdd vectorMul vectorDiv vectorSub", 1000, 100);

here, same "fast" version of x is used for consecutively executed 4 kernels.

Now to get the result back into x array(because ClArray<T> doesn't have a track of last swapped array(yet)), one needs to do an explicit copy with:

     xCL.CopyTo(x,0);

this copies all elements from C++ array to x array, starting from zero index. For the opposite direction copy,

    xCL.CopyFrom(x,0);

has to be used.


     ClArray<float> xCL = new float[N];  // uses this C# array, changes it with compute()
     ClArray<float> xCL = new ClFloatArray(N);  // uses this C++ array, changes it with compute()
     ClArray<float> xCL = new ClArray<float>(N);  // uses this C++ array by default, changes it with compute()
     xCL.fastArr = true; // releases any C# array inside and allocates a C++ array to use, copying values after
     xCL.fastArr = false; // releases any C++ array inside and creates a C# array to use, copying values after
     xCl.CopyFrom(any type,0); // gets same-length array values from parameter
     xCl.CopyTo(any type,0); // puts values to same-length array (parameter)