Pointers in compute kernel / design alternative. building a generic RK4 capability

OK, I know this does not work:

[codebox]struct deriv


float * x;

float * dx;

float dxi[4];


struct p


float rho, drho;



I can’t make x, dx in deriv point to rho, drho in p and expect the pointers to work in a compute kernel.

This is the way I typically build a Runge-Kutta 4 integrator. I iterate over an array of deriv objects instead of writing a custom integrator for struct p, which may have any number of differential equations, and there may be different variants of “p” being integrated in parallel.

Can someone propose an alternative method to accomplish a generic RK4 code without having to hand-code the RK4 integration routine for each differential equation? Right now I am at a loss for a good, clean way to do this, and I’m sure someone here has gone through this before…



If rho and drho are in global memory, then yes, you should be able to have pointers to them in your deriv structure.

Using pointers isn’t always the fastest way to do things on the GPU, however, and sometimes it is worth sacrificing some generality for performance.

Why not? I use linked data structures in several scenarios. You need to make sure the actual memory (i.e. ptr value) was returned by a CUDA malloc call. Try passing a (long *debug) to your kernel and set (debug = (long)ptr) to make sure you’re setting the pointers correctly.

If there’s any locality though, consider copying an array of (rho, drho) to shared mem and instead using a relative offset instead of a pointer.