13

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?

Dan
  • 12,857
  • 7
  • 40
  • 57
  • does main.cu include any other files which includes boost code? – ronag Sep 13 '11 at 09:51
  • Yes actually thinking about it it includes `wrapper.h` which includes `boost\program_options.hpp`. Is there a good way to separate the two? – Dan Sep 13 '11 at 09:53

4 Answers4

8

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++.

Aurelius
  • 1,146
  • 2
  • 13
  • 25
5

You have to split the code in two parts:

  1. the kernel have to be compiled by nvcc
  2. the program that invokes the kernel has to be compiled by g++.

Then link the two objects together and everything should be working. nvcc is required only to compile the CUDA kernel code.

fabrizioM
  • 46,639
  • 15
  • 102
  • 119
  • Indeed, that much was already said in my question. The solution would really be how to achieve that or to get nvcc to handle it properly. – Dan Sep 13 '11 at 18:19
  • I'm using VS2010 to compile this don't know if that could be causing issues? – Dan Sep 16 '11 at 10:54
0

Another option is to wrap cpp only code in

#ifndef __CUDACC__
randyrand
  • 433
  • 1
  • 4
  • 9
0

Thanks to @ronag's comment I realised I was still (indirectly) including boost/program_options.hpp indirectly in my header since I had some member variables in my wrapper class definition which needed it.

To get around this I moved these variables outside the class and thus could move them outside the class defintion and into the .cpp file. They are no longer member variables and now global inside wrapper.cpp

This seems to work but it is ugly and I have the feeling nvcc should handle this gracefully; if anybody else has a proper solution please still post it :)

Dan
  • 12,857
  • 7
  • 40
  • 57