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

c++11, cuda, gcc, nvcc, nvidia

Solution

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.

Problem

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)?

Original source

Related problems