Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use C++11 in the .cu-files (CUDA5.5) in Windows7x64 (MSVC) and Linux64 (GCC4.8.2)?

When I compile the following code containing the design C++11, in Windows7x64 (MSVS2012 + Nsight 2.0 + CUDA5.5), then I do not get errors, and everything compiles and works well:

#include <thrust/device_vector.h>

int main() {
    thrust::device_vector<int> dv(10);
    auto iter = dv.begin();

    return 0;
}

But when I try to compile it under the Linux64 (Debian 7 Wheezey + Nsight Eclipse from CUDA5.5), I get errors:

../src/CudaCpp11.cu(5): error: explicit type is missing ("int" assumed)

../src/CudaCpp11.cu(5): error: no suitable conversion function from

"thrust::detail::normal_iterator>" to "int" exists

2 errors detected in the compilation of "/tmp/tmpxft_00001520_00000000-6_CudaCpp11.cpp1.ii". make: * [src/CudaCpp11.o] Error 2

When I added line:-stdc++11

in Properties-> Build-> Settings-> Tool Settings-> Build Stages-> Preprocessor options (-Xcompiler)

I get more errors:

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined

/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected a ";"

...

/usr/include/c++/4.8/bits/cpp_type_traits.h(314): error: namespace "std::__gnu_cxx" has no member

"__normal_iterator"

/usr/include/c++/4.8/bits/cpp_type_traits.h(314): error: expected a ">"

nvcc error : 'cudafe' died due to signal 11 (Invalid memory reference) make: * [src/CudaCpp11.o] Error 11

Only when I use thrust::device_vector<int>::iterator iter = dv.begin(); in Linux-GCC then I do not get an error. But in Windows MSVS2012 all c++11 features works fine!

Can I use C++11 in the .cu-files (CUDA5.5) in Windows7x64 (MSVC) and Linux64 (GCC4.8.2)?

like image 720
Alex Avatar asked Jan 30 '14 13:01

Alex


People also ask

What version of C++ does CUDA support?

c++ version supported by cuda 5.0 c++, c++11, cuda asked by soriak on 01:05PM - 22 Aug 12 UTC CUDA 5.0: no support for GCC 4.7, no support for C++11 features nvcc does not understand C++11 syntax used in the standard includes of GCC 4.6

Is there a C++11 header file for cu files?

Although the .cu files and their .h files themselves do only include header files of C++11 code. I have read that there are issues concerning the C++11 standard on stackoverflow, but the posts were quite old.

How to include a cu file in a CPP file?

You can either create a thin wrapper (host-side function callKernel that will simply pass all the input to GPU kernel) or use #include to include the cu file into each CPP file. Note that if you use #include you do not need to pass the cu file to compiler and linker.

What is the cu file extension?

CUDA Source Code File What is a CU file? Developer file written for CUDA, an NVIDIA architecture created for parallel processing on nVidia GPUs; contains source code written in the "C for CUDA" language, which is similar to the standard C language with a few CUDA-specific extensions.


2 Answers

You will probably have to split the main.cpp from your others.cu like this:

others.hpp:

void others();

others.cu:

#include "others.hpp"
#include <boost/typeof/std/utility.hpp>
#include <thrust/device_vector.h>

void others() {
    thrust::device_vector<int> dv(10);
    BOOST_AUTO(iter, dv.begin()); // regular C++
}

main.cpp:

#include "others.hpp"

int main() {
    others();

    return 0;
}

This particular answer shows that compiling with an officially supported gcc version (as Robert Crovella stated correctly) should work out at least for c++11 code in the main.cpp file:

g++ -std=c++0x -c main.cpp
nvcc -arch=sm_20 -c others.cu 
nvcc -lcudart -o test main.o others.o

(tested on Debian 8 with nvcc 5.5 and gcc 4.7.3).

To answer your underlying question: I am not aware that one can use C++11 in .cu files with CUDA 5.5 in Linux (and I was not aware the shown example with host-side C++11 gets properly de-cluttered under MSVC). I even filed a feature request for constexpr support which is still open.

The CUDA programming guide for CUDA 5.5 states:

For the host code, nvcc supports whatever part of the C++ ISO/IEC 14882:2003 specification the host c++ compiler supports.

For the device code, nvcc supports the features illustrated in Code Samples with some restrictions described in Restrictions; it does not support run time type information (RTTI), exception handling, and the C++ Standard Library.

Anyway, it is possible to use some of the C++11 features like auto in kernels, e.g. with boost::auto. As an outlook, other C++11 features like threads may be quite unlikely to end up in CUDA and I heard no official plans about them yet (as of supercomputing 2013).

Shameless plug: If you are interested in more of these tweeks, feel free to have a look in our library libPMacc which provides multi-GPU grid and particle abstractions for simulations. We implemented lambda, a STL-like access concept for 1-3D matrices and other useful stuff there.

All the best, Axel


Update: Since CUDA 7.0 C++11 support in kernels has been added officially. As BenC pointed our correctly, parts of this feature were already silently added in CUDA 6.5.

like image 99
Ax3l Avatar answered Oct 03 '22 18:10

Ax3l


According to Jared Hoberock (Thrust developer), it seems that C++11 support has been added to CUDA 6.5 (although it is still experimental and undocumented). This may make things easier when starting to use C++11 in very large C++/CUDA projects, since splitting everything can be quite cumbersome for large projects when you use CMake for instance.

like image 44
BenC Avatar answered Oct 03 '22 20:10

BenC