As with compiling any C or C++ code, external manipulation of preprocessor symbols (by which I mean outside of the code itself) requires passing additional arguments to the compiler. If you had this code:
__global__ void func(...) {
...
#ifdef X
do something
#endif
}
then the way to set X
during compilation would be to pass -DX
to the compiler, which defines the symbol X
during the current preprocessor pass.
You can do exactly the same thing in PyCUDA by passing options to the build using the options
keyword in the SourceModule
constructor. Compiler options are passed as a list, so your PyCUDA example could be implemented something like this:
my_global_var=True
....
build_options = []
if my_global_var:
build_options.append('-Dmy_global_var')
mod=SourceModule(""" __global__ void func() {
...
#ifdef my_global_var
do something
#endif }
""", options=build_options)
[Disclaimer: Totally untested, use at own risk - I don't have a working PyCUDA installation ATM]
Here, we simply build a list containing as many entries as there are options you need to pass to the compiler, and then instantiate a SourceModule
instance with those options. After that, it should just work...