Pass in C++ template arguments when compiling PTX (or into compiled PTX)

I have a CUDA kernel that takes in (C++) template argument for important information such the size of an array I want to initialize in the kernel stack. It used to work fine when my host program is written in C/C++ that I can easily pass it as a template argument. For example:

template global void foo()
{
double W[numberOfTaps];
}

Now I moved to MATLAB’s Parallel Computation Toolbox, where I can compile my CUDA kernel into PTX and call it from MATLAB (where my host code is):
kernel=parallel.gpu.CUDAKernel(‘foo.ptx’, ‘foo.cu’);
feval(kernel);

The problem is that there’s no way to enter the template arguments from MATLAB. Any ideas I can use templates to pass template arguments into the kernel from non C/C++ host environment?

Thanks.

I have a CUDA kernel that takes in (C++) template argument for important information such the size of an array I want to initialize in the kernel stack. It used to work fine when my host program is written in C/C++ that I can easily pass it as a template argument. For example:

template global void foo()
{
double W[numberOfTaps];
}

Now I moved to MATLAB’s Parallel Computation Toolbox, where I can compile my CUDA kernel into PTX and call it from MATLAB (where my host code is):
kernel=parallel.gpu.CUDAKernel(‘foo.ptx’, ‘foo.cu’);
feval(kernel);

The problem is that there’s no way to enter the template arguments from MATLAB. Any ideas I can use templates to pass template arguments into the kernel from non C/C++ host environment?

Thanks.

Not easily.

Templates are instantiated by nvcc as part of the top level C++ compilation. This happens well before PTX is ever generated. They’re not runtime arguments, they’re compile-only code definitions that are visible only at compile time.

An ugly and unportable workaround might be to write a little Matlab wrapper, having Matlab use a system call to nvcc itself, passing your template arguments as -D define switches in the nvcc command line, then using the generated PTX in Matlab. Ugly but maybe it’d work.

Not easily.

Templates are instantiated by nvcc as part of the top level C++ compilation. This happens well before PTX is ever generated. They’re not runtime arguments, they’re compile-only code definitions that are visible only at compile time.

An ugly and unportable workaround might be to write a little Matlab wrapper, having Matlab use a system call to nvcc itself, passing your template arguments as -D define switches in the nvcc command line, then using the generated PTX in Matlab. Ugly but maybe it’d work.

I thought about this too, but I have trouble figuring out how to define values from the -D switch. I’ve defined macro variables before (“-DMACROVAR” is translates to “define MACROVAR”), but how can I make it assume a value that’s equivalent to “define MACROVAR 17”?

Thanks

I thought about this too, but I have trouble figuring out how to define values from the -D switch. I’ve defined macro variables before (“-DMACROVAR” is translates to “define MACROVAR”), but how can I make it assume a value that’s equivalent to “define MACROVAR 17”?

Thanks

It’s probably like GCC, where you use something like “nvcc -D MACROVAR=17”

It may be possible (or necessary) to concatenate the definition with the D symbol, like “nvcc -DMACROVAR=17”
A quick test should tell you if so, I’m not on my dev machine so I can’t try it.

You may need to use single or double quotes (depending on your shell and OS) if the macro definition has spaces or escape characters.

It’s probably like GCC, where you use something like “nvcc -D MACROVAR=17”

It may be possible (or necessary) to concatenate the definition with the D symbol, like “nvcc -DMACROVAR=17”
A quick test should tell you if so, I’m not on my dev machine so I can’t try it.

You may need to use single or double quotes (depending on your shell and OS) if the macro definition has spaces or escape characters.

Thanks. Since I don’t use gcc, I thought -D follows the syntax in define, which doesn’t use a equal sign for assignment. It’s working now.

Thanks. Since I don’t use gcc, I thought -D follows the syntax in define, which doesn’t use a equal sign for assignment. It’s working now.