2

Can I use a PTX function contained in a PTX file as an external device function to link it to another .cu file which should call that function?

This is another question from CUDA - link kernels together where the function itself is not contained in a .cu file but I rather have a PTX function to be linked somehow.

tomix86
  • 1,336
  • 2
  • 18
  • 29
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 1
    You can load the file containing PTX code in your own code from the filesystem by `cuModuleLoad` and `cuModuleGetFunction`. – Vitality Dec 18 '13 at 18:43
  • 1
    If you want to use the driver API, I think it's pretty straightforward how to load PTX, as @JackOLantern indicates and there is a [relevant CUDA sample](http://docs.nvidia.com/cuda/cuda-samples/index.html#ptx-just-in-time-compilation). To make it work using strictly the runtime API, you may also find [this question](http://stackoverflow.com/questions/20012318/how-to-compile-ptx-code) or [this question](http://stackoverflow.com/questions/11116722/how-can-i-call-a-ptx-function-from-cuda-c) interesting. However I'm not yet able to completely connect the dots for you. – Robert Crovella Dec 21 '13 at 22:32
  • @JackOLantern make it an answer and I'll accept it. – Marco A. Jan 17 '14 at 17:31

1 Answers1

2

You can load the file containing PTX code in your own code from the filesystem by cuModuleLoad and cuModuleGetFunction as follows:

CUmodule module;
CUfunction function;

const char* module_file = "my_ptx_file.ptx";
const char* kernel_name = "my_kernel_name";

err = cuModuleLoad(&module, module_file);
err = cuModuleGetFunction(&function, module, kernel_name);

You can also pass the PTX code to the CUDA driver directly as a string, see Passing the PTX program to the CUDA driver directly.

Community
  • 1
  • 1
Vitality
  • 20,705
  • 4
  • 108
  • 146