Run a host function from a nvcc generated .obj from another application?


I’ve used the nvcc compiler to generate file1.obj with a host Launch function that calls the global Kernel function.

I’ve added this file1.obj to a Visual Studio project file2. It compiles okay but complains with error LNK2005: main already defined in file2.obj

What I want is for my program file2.exe to call the host Launch function in the separately nvcc compiled file1.obj, which then calls the GPU code Kernel function.

Can I compile CUDA code without an entry point so that the main in my file2.exe is used instead of the one in file1.obj which it currently seems to run? My file2 project adds the cudart.lib but otherwise is not specially set-up for CUDA.

What is the recommended way of linking and then calling CUDA code from another Visual Studio application?

Thanks for any help,

C/C++ require exactly one function called main() to be present at link time. You’ll get an error if there is none, and an error if there is more than one. CUDA is basically a subset of C++. So you would want to get rid of the second instance of main().

Thanks for the help!

Okay, I’ve renamed the main in the file2 project to myProc so no more conflict. In the main in file1 I’ve used LoadLibrary to call myProc function in file2.dll. Now in myProc I want to call Launch function in file1 which runs the CUDA kernel.

So in file1

extern “C”
__declspec(dllexport) void Launch(…

In file2, I have this line near the top

extern “C” __declspec(dllexport) void Launch(…

and then in myProc I call Launch (the host function in file1 that calls the CUDA kernel) but it crashes with

Unhandled exception at 0x00007FEDF89A2B7 (nvcuda.dll) in File1.exe: 0xC0000005: Access violation reading location 0x000000006065000.

I want to do this because File1.exe is dynamic code that is compiled by nvcc whilst my application is running. All my code that does not change is in the dll.

So in summary I want a function in File2.dll to run the CUDA code in File1.exe, not able to get this working. How do I do this?

Thanks for any help,

That looks like it would be difficult/probably require some kind of system call.
I don’t think you want to do it that way.

Having a function in f2.dll call a function in f3.dll should be possible.

I would suggest that none of this stuff really has anything to do with CUDA. I think you’re stumbling around with dll mechanics. You might want to prototype something that doesn’t use CUDA just to get the mechanics sorted. Then when you want to add CUDA, there are plenty of writeups all over the place about how to build a CUDA-enabled dll.

That was it in a nutshell. I just compile it as a CUDA project dll. Then I still use LoadLibrary to load the CUDA enabled dll from my exe, then call the function that has been the host function decorated with extern “C” __declspec(dllexport) that calls the CUDA kernel.



So I guess you’re just asking the question “How do I create a CUDA dll?”

There are various online examples of that.

It’s all okay now. I’ve created the CUDA dll and everything works as expected. Thanks.