A uber-shader(unified shader template) controlled by Macro from C++, include all shaders, then to different shader by Macro when compile

a uber-shader(a unified shader template) controlled by Macro from C++, which include all the possibility of all shaders, then can be changed to different shader by Macro when compile.
is there demo showing this process? define various Macro at various conditions in C++, then different path which controlled by different marco will be implemented and run.
Then there will be only one BIG shader .cu file.

A shader is not a CUDA concept. It pertains to compute within a graphics API like OpenGL. If you are asking about shaders, this is not the right forum.

Believe me, you do not want to implement an “uber-kernel” via macros, especially if the number of variants supported is more than two or three.

In the earliest days of CUDA, before CUDA became C++ based, that is what I needed to do for various kernels in CUBLAS. It made the code difficult to read and follow, difficult to maintain, and easy for bugs to creep in. The moment template support was added to CUDA, those kernels were transitioned to make use of proper C++ templates.

sorry, i mean, .cu

I don’t know of any demo showing that process. To a first order approximation, CUDA claims compatibility with C++.

So if you know how to do what you want in C++, that is usually a good starting point for a CUDA realization.

If you don’t know how to do what you want in C++, this isn’t the forum I would suggest. Start by understanding how to do it in C++. If needed, use forums for that to determine how. Then you can have a more focused discussion here by presenting what you want to accomplish.

That’s my general suggestion. Do as you wish, of course.

but how about situations like this:
some variables including local and constant ones, as well as some code, actually are not need in some conditions which set in C++?
eg.

#define DO_A

in .cu :

#ifdef DO_A
__constant__ int a;
...
#endif

global void main()
{
...
#ifdef DO_A
a = 1;
...
#endif
...
}

So it’s able to ignore these code and var at compile time, instead of adding a if(Is_Do_A){…} which add more var and dynamic branch in .cu

thanks a lot!
new to CUDA, and curious about how to manage CUDA with many different conditions.
in OpenGL/D3D shaders, uber-shader is an option, which like said above, use macro to control different conditions in just 1 shader, then it will be compiled to many shaders by different macro under different conditions.

The #ifdef you have shown will work to bring sections of code in our out of the compiled object. Just like in C or C++.

eg. GLSL uber-shader including sectionA & sectionB, then it will be compiled to 3 binary shaders at most if needed: A, B, AB.

how about CUDA? also able to be compiled to 3 binary ones at most?

thanks!

certainly that is possible. just like it is possible in C or C++