#ifndef max #define max( a, b ) ( ((a) > (b)) ? (a) : (b) ) #endif
#ifndef min #define min( a, b ) ( ((a) < (b)) ? (a) : (b) ) #endif
In C, I have defined these two small functions and use quite often.
now when it comes to CUDA, I just notice that I need to do convert the function in CUDA form.
Is there any simple way to do it? or callable function ?
Just let me know where to look or terms that I have to search…
CUDA already supports min() and max() functions. These are overloaded functions with support for a large number of types, so there should be no need to define macros for this purpose. Note that functions are safer than macros for this purpose since the arguments are not evaluated twice. In addition to overloaded min() and max() there are also the C99-compatible math functions fmin(), fmax(), fminf(), fmaxf() for floating-point data.
The CUDA versions do the same, except that they are superior because cover all the intrinsic types CUDA supports and compile to a single PTX instruction without any conditional execution.
Yes it does. That may or may not be important. Most uses of the ? operator are trivial, so don’t worry about it… only larger functions of meaty branches really matter for thread divergence.
So you could create a bad example like
y= (x>10) ? Bigfunction1(x) : OtherFunction2(x);
But that’s a pretty rare use of the ? operator. Just think of the equivalent if statement, the generated code is almost always identical or nearly so.