The CUDA driver API provides loading the file containing PTX code from the filesystem. One usually does the following:
CUmodule module;
CUfunction function;
const char* module_file = "my_prg.ptx";
const char* kernel_name = "vector_add";
err = cuModuleLoad(&module, module_file);
err = cuModuleGetFunction(&function, module, kernel_name);
In case one generates the PTX files during runtime (on the fly) going through file IO seems to be a waste (since the driver has to load it back in again).
Is there a way to pass the PTX program to the CUDA driver directly (e.g. as a C string) ?
Use the cuModuleLoadDataEx function to load PTX source from a NULL-terminated string.
Taken from the
ptxjit
CUDA example:Define the PTX program as a C string as
then
and finally