Thursday, February 26, 2009

Variadic Macros - Quite Cool!!

For the past few days, I have been doing C/CUDA based development. A good break from the the regular Java programming.

CUDA does not support printf function in non-device emulation mode. During development we used to switch between emulation and non-emulation mode frequently. The switch was done using a compile time flag.

We needed a way to remove the all the printf calls in non-emulation mode and have them execute in the emulation mode. The solution was to use Variadic Macro (Thanks to my friend who found the existence of such a macro).

Finally our solution was:


#ifdef EMU
int log(char*msg, ...) {

va_list args;
va_start(args, msg);
return vprintf(msg, args);

}
#else
#define log(msg, ...)
#endif
We used the log variadic macro in our code. Nice learning!!