In my program i have a lot of parameters that are changed rarely, so i don’t mind recompiling when they change. The compiler should detect those as constants to save registers/variables, unroll loops and so on. But i really don’t like #defines. I tried to put my constants in a header file like this
const struct
{
int a;
float b;
int c;
} CRAND = {
1,
2.f,
3
};
and i can use it in my C+±code. But when i try to use it in a kernel, i get
ranlux.cu(69): error: identifier “CRAND” is undefined
Any advise?
Thanks.
enums only work for ints.
My workaround now is that i put the constant struct into a header file and i include it once for all the host code and once in every kernel. That works, it’s ok for me, but there really should be a more elegant solution, right? I thought that i’m just missing something obvious. Thanks for the answers so far.
I use static const instead of #define.
static const int a = 1;
static const float b = 2.0f;
static const int c = 3;
It’s the “preferred” way in C++ because static const carries type information. It’s just as good as #define when it comes to compiler optimizations based on constants.