Skip to content

Writing optimised BAVCL code

Marcel Pawelczyk edited this page Dec 3, 2021 · 6 revisions

Tips and tricks to get the most out of BAVCL and ILGPU.

Updating Vectors From The CPU

Let's suppose you have instantiated a new Vector with some values and you want to edit values by index on the CPU to then update the array stored in the GPU. This typically would occur in two settings:

  1. You have a Vector that IS cached on the GPU. In which case it is highly advised to update the vector using:
using BAVCL;

GPU gpu = new GPU();

/* 
   Let's suppose this vector comes from somewhere in your codebase, 
   in this example, I need to instantiate it, but the following is Cached
*/
Vector vector = new Vector(gpu, new float[5] {1f,2f,3f,4f,5f});

// -------------------------------------------------------------------------

// SITUATION A
// For single value changes

vector.SyncCPU();                        // REFER to *A1 below
vector.value[2] = 99f;                   // Perform value change
vector.UpdateCache();

// -------------------------------------------------------------------------

// SITUATION B
// For multiple value changes

vector.SyncCPU();                        // REFER to *A1 below
vector.value[2] = 99f;                   // Perform value changes
vector.value[4] = 99f;                   // Perform value changes
                                         // Perform value changes ... etc
vector.UpdateCache();

// -------------------------------------------------------------------------

// SITUATION C
// Updating the entire array

float[] updatedValues = new float[8] {1f,1f,2f,3f,5f,8f,13f,21f};

/* 
   UpdateCache will take care of syncing the CPU 
   and Updating the GPU
*/
vector.UpdateCache(updatedValues)

Notes: A1:

  1. You have a vector that IS NOT cached on the GPU.
Clone this wiki locally