Building GPL C program with CUDA module

后端 未结 1 790
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 12:14

I am attempting to modify a GPL program written in C. My goal is to replace one method with a CUDA implementation, which means I need to compile with nvcc instead of gcc. I

相关标签:
1条回答
  • 2020-12-08 12:25

    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.

    0 讨论(0)
提交回复
热议问题