CUDA compile multiple .cu files to one file

741 Views Asked by At

I am porting some calculation from C# to CUDA. There many classes in C# which I want to port, for each c# class I create .cu and .cuh file in my CUDA project. All classes related, and all they used in calculations. I need to save structure of my C# code, because it will be very easy to made error in other case.

P.S. In case I put all code in one file - everything works as expected but read or fix some issues becomes real pain.

I want to compile CUDA project and use it in my C# via ManagedCuda library. I can compile test CUDA project with one .cu file to .ptx file, load it in C# via ManagedCuda and call function from it.

But when I want to compile my real projects with multiple cu files, in result I got multiple .ptx files for each .cu file in project, even more I am not able to load this .ptx file via ManagedCuda, I got next error:

ErrorInvalidPtx: This indicates that a PTX JIT compilation failed.

But this error expected, because there cross reference in ptx files, and they have sense only if the loaded together.

My goal is to compile my CUDA project to one file, but in same time I do not want to be limited to only specific video card which I have. For this I need to use PTX(or cubin with ptx included) this PTX file will be compiled for specific device in moment you load it.

I tried to set Generate Relocatable Device Code to Yes (-rdc=true) and compile to PTX and Cubin - result same I get few independent files for each .cu file.

1

There are 1 best solutions below

0
On

The very short answer is no, you can't do that. The toolchain cannot merged PTX code at the compilation phase.

If you produce multiple PTX files, you will need to use the JIT linker facilities of the CUDA runtime to produce a module which can be loaded into your context. I have no idea whether Managed CUDA supports that or not.

Edit to add that is appears that Managed CUDA does support runtime linking (see here).