Search code examples
c++boostcudanvcc

Cuda with Boost


I am currently writing a CUDA application and want to use the boost::program_options library to get the required parameters and user input.

The trouble I am having is that NVCC cannot handle compiling the boost file any.hpp giving errors such as

1>C:\boost_1_47_0\boost/any.hpp(68): error C3857: 'boost::any': multiple template parameter lists are not allowed

I searched online and found it is because NVCC cannot handle the certain constructs used in the boost code but that NVCC should delegate compilation of host code to the C++ compiler. In my case I am using Visual Studio 2010 so host code should be passed to cl.

Since NVCC seemed to be getting confused I even wrote a simple wrapper around the boost stuff and stuck it in a separate .cpp (instead of .cu) file but I am still getting build errors. Weirdly the error is thrown upon compiling my main.cu instead of the wrapper.cpp but still is caused by boost even though main.cu doesn't include any boost code.

Does anybody know of a solution or even workaround for this problem?


Solution

  • Dan, I have written a CUDA code using boost::program_options in the past, and looked back to it to see how I dealt with your problem. There are certainly some quirks in the nvcc compile chain. I believe you can generally deal with this if you've decomposed your classes appropriately, and realize that often NVCC can't handle C++ code/headers, but your C++ compiler can handle the CUDA-related headers just fine.

    I essentially have main.cpp which includes my program_options header, and the parsing stuff dictating what to do with the options. The program_options header then includes the CUDA-related headers/class prototypes. The important part (as I think you've seen) is to just not have the CUDA code and accompanying headers include that options header. Pass your objects to an options function and have that fill in relevant info. Something like an ugly version of a Strategy Pattern. Concatenated:

    main.cpp:
    #include "myprogramoptionsparser.hpp"
    (...)
    CudaObject* MyCudaObj = new CudaObject;
    GetCommandLineOptions(argc,argv,MyCudaObj);
    
    myprogramoptionsparser.hpp:
    #include <boost/program_options.hpp>
    #include "CudaObject.hpp"
    
    void GetCommandLineOptions(int argc,char **argv,CudaObject* obj){
    (do stuff to cuda object) }
    
    CudaObject.hpp:
    (do not include myprogramoptionsparser.hpp)
    
    CudaObject.cu:
    #include "CudaObject.hpp"
    

    It can be a bit annoying, but the nvcc compiler seems to be getting better at handling more C++ code. This has worked fine for me in VC2008/2010, and linux/g++.