16

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

Alex
  • 12,578
  • 15
  • 99
  • 195
  • 3
    gcc 4.8.x is not an [officially supported compiler](http://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#linux-5-5) under linux. – Robert Crovella Jan 30 '14 at 20:53
  • @Robert Crovella GCC 4.7.2 has the same effect - can't compile C++11 in nvcc+gcc. Or nevertheless how can I do this, or can I use C++11 in nvcc+icc(Intel Compiler)? – Alex Jan 30 '14 at 22:17
  • Just from the top of my head: Last may I attended a lecture on GPU programming. Just 1.5 hour. From there I remember that all the code we saw on the slides was a special language for GPU, based on C99. Not sure if it was CUDA, I don’t remember it well. But if it was, it means that the language is not even C or C++, it is only C99-based. Maybe new features will be taken from new standards of C or C++, maybe not. You would have to track development of the specific language. But if it was something else, just ignore this comment. I’ll try to find more on this topic later. – Palec Feb 09 '14 at 14:33

2 Answers2

9

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.

Ax3l
  • 1,529
  • 12
  • 20
3

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.

BenC
  • 8,729
  • 3
  • 49
  • 68
  • 1
    Big thanks! Good news that: "The idea is that nvcc will support **whatever c++11 constructs** are supported by the host compiler. They will release documentation describing the caveats sometime in the future. The big limitation is that lambdas cannot be passed from the host to a __global__ function launch. In my experience, **all of the constructs you mentioned are supported with g++ as the host compiler.** " – Alex Aug 30 '14 at 15:30
  • Update: CUDA 7.0 adds *in kernel* c++11 support. I updated my answer to reflect that. – Ax3l Jan 15 '15 at 13:24