constant declaration static keyword

device functions can’t have static variables in them, it makes sense since it’s effectively a kind of global variable that doesn’t match the GPU hardware design.

But in a lot of functions, you may have used good coding practice by defining compile-time constants with “[font=“Courier”]static const[/font]” instead of old-style [font=“Courier”]#define[/font]s in code like:

int myfunc(int val)

{

   static const int iterations=10;

   int i;

   for (i=0; i<iterations; ++i) {

       /* do stuff to val here */

   }

}  

This is a modern and very common way to do type checking on constants and also avoids preprocessor pollution and bugs.

CUDA does not allow static variables… well understood, but unfortunately this ban extends even to cases like the above example where there really ISN’T a static value, it’s just a constant definition.

My question: is this an accidental design bug in CUDA? Certainly the [font=“Courier”]const [/font]keyword shows it’s just a safe constant definition, but I think the compiler’s just been changed to immediately throw anything out once it sees any [font=“Courier”]static [/font]keyword.

So, we ask, what if you just remove the [font=“Courier”]static [/font]keyword? The code still works but now it could be in theory a real variable, and you have to HOPE that the compiler doesn’t allocate registers (or worse, local memory) for it.

This gets especially important with C++ classes, since the [font=“Courier”]static [/font]keyword shows that the constant isn’t another variable in each instance of the class’s data, it’s a class constant… a compiletime convenience. CUDA doesn’t card, it’s banned anyway.

What do people do? Have programmers moved back to old-school [font=“Courier”]#define PI 3.14159[/font] Or just leave out the [font=“Courier”]static [/font]and pray the compiler optimizes it correctly?

I just use “const int var=10” and I’ve never noticed an increase in register usage. nvcc uses SSA which makes every single new assignment generate a new variable (register) anyways. Then ptxas goes through and assigns real registers to these reusing registers as much as possible, and it has always done a very good job as far as I’m concerned.