Building GPL C program with CUDA module

て烟熏妆下的殇ゞ 提交于 2019-11-28 06:02:44

You don't need to compile everything with nvcc. Your guess that you can just compile your CUDA code with NVCC and leave everything else (except linking) is correct. Here's the approach I would use to start.

  1. Add a 1 new header (e.g. myCudaImplementation.h) and 1 new source file (with .cu extension, e.g. myCudaImplementation.cu). The source file contains your kernel implementation as well as a (host) C wrapper function that invokes the kernel with the appropriate execution configuration (aka <<<>>>) and arguments. The header file contains the prototype for the C wrapper function. Let's call that wrapper function runCudaImplementation()

  2. I would also provide another host C function in the source file (with prototype in the header) that queries and configures the GPU devices present and returns true if it is successful, false if not. Let's call this function configureCudaDevice().

  3. Now in your original C code, where you would normally call your CPU implementation you can do this.

    // must include your new header
    #include "myCudaImplementation.h"
    
    // at app initialization
    // store this variable somewhere you can access it later
    bool deviceConfigured = configureCudaDevice;          
    ...                             
    // then later, at run time
    if (deviceConfigured) 
        runCudaImplementation();
    else
        runCpuImplementation(); // run the original code
    
  4. Now, since you put all your CUDA code in a new .cu file, you only have to compile that file with nvcc. Everything else stays the same, except that you have to link in the object file that nvcc outputs. e.g.

    nvcc -c -o myCudaImplementation.o myCudaImplementation.cu <other necessary arguments>
    

Then add myCudaImplementation.o to your link line (something like:) g++ -o myApp myCudaImplementation.o

Now, if you have a complex app to work with that uses configure and has a complex makefile already, it may be more involved than the above, but this is the general approach. Bottom line is you don't want to compile all of your source files with nvcc, just the .cu ones. Use your host compiler for everything else.

I'm not expert with configure so can't really help there. You may be able to run configure to generate a makefile, and then edit that makefile -- it won't be a general solution, but it will get you started.

Note that in some cases you may also need to separate compilation of your .cu files from linking them. In this case you need to use NVCC's separate compilation and linking functionality, for which this blog post might be helpful.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!