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?