Tricks in C : Pointers
Posted: Wed Jan 31, 2024 6:12 pm
Little tricks to remember that are useful in writing algo's in C/C++
How to create a pointer to an initialised class:
Create a list of pointers to class objects
How to initialise a class within a class using a pointer
How to point to another array:
(This works for pointing to an array on your GPU too)
Create a pointer Array that points to any Array
(This doesnt work for the GPU because it uses a 2d array - not compatable with GPU see next example for GPU.)
If you need a way to get lots of pointers onto the GPU kernel use a struct for example try this:
Use a struct of pointers to carry the model parameters onto the kernel rather than list out all the parameters just point to them. Just found this out, although I think I knew from OpenCL. Using structs and pointers for GPU programming is the way forward. Just point to everything you need.
Kickin' as I'm tryin' to sleep
I got the mud beneath my shoes
Rubber band, rubber band
Gun in hand, gun in hand I wanna use
Roamin', roamin', roam
(Get away, gotta get away)
And I think I think too much
(I don't care, yeah but I don't care)
Roamin', roamin', roam
(Get away, gotta get away)
And I think I think too much
How to create a pointer to an initialised class:
- Code: Select all
myclass instance(var1,var2);
myclass* pointer;
pointer = &instance;
Create a list of pointers to class objects
- Code: Select all
myclass **pointer;
for(i=0;i<numobj;i++){
pointer[i] = new myclass;
*pointer[i] = myclass(var1,var2);
}
How to initialise a class within a class using a pointer
- Code: Select all
class1{
class2* thisclass
class1(){
thisclass = new class2(param1,param2);
}
How to point to another array:
(This works for pointing to an array on your GPU too)
- Code: Select all
int *pointer_array,*array;
pointer_array = &array[0]
Create a pointer Array that points to any Array
(This doesnt work for the GPU because it uses a 2d array - not compatable with GPU see next example for GPU.)
- Code: Select all
int **pointer_array,*anarray;
anarray = new int[sizeofmyarray];
pointer_array = new int*[howmany_arrays];
pointer_array[0] = &anarray[0];
int myfirst_element = *(pointer_array[0]+1);
If you need a way to get lots of pointers onto the GPU kernel use a struct for example try this:
- Code: Select all
typedef struct{
float* pointer1;
float* pointer2;
}myparam;
__global__ void my kernel(myparam mymodel,int* indexforarray){
GPU_1D_KERN_LOOP(index,size){
const int iter = (index /1) % size;
mymodel.pointer1[iter] = something;
mymodel.pointer2[indexforarray+iter] = somethingelse'
}
}
Use a struct of pointers to carry the model parameters onto the kernel rather than list out all the parameters just point to them. Just found this out, although I think I knew from OpenCL. Using structs and pointers for GPU programming is the way forward. Just point to everything you need.
Kickin' as I'm tryin' to sleep
I got the mud beneath my shoes
Rubber band, rubber band
Gun in hand, gun in hand I wanna use
Roamin', roamin', roam
(Get away, gotta get away)
And I think I think too much
(I don't care, yeah but I don't care)
Roamin', roamin', roam
(Get away, gotta get away)
And I think I think too much