Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while using CUDA and C++11

Tags:

c++

gcc

c++11

cuda

I am using CUDA 4.1 and GCC 4.5 ... (FINALLY! CUDA supports GCC 4.5, but still waiting for GCC 4.6). Anyways, is it possible to use C++11 with the CUDA 4.1?

I tried passing:

--compiler-options "-std=c++0x"

to nvcc and it throws a bunch of errors at me:

/usr/include/c++/4.5/exception_ptr.h(100): error: copy constructor for class "std::__exception_ptr::exception_ptr" may not have a parameter of type "std::__exception_ptr::exception_ptr"

/usr/include/c++/4.5/exception_ptr.h(100): error: expected a ")"

/usr/include/c++/4.5/exception_ptr.h(110): error: expected a ")"

/usr/include/c++/4.5/exception_ptr.h(132): error: identifier "type_info" is undefined

/usr/include/c++/4.5/exception_ptr.h(101): error: identifier "__o" is undefined

/usr/include/c++/4.5/exception_ptr.h(112): error: expected a ">"

/usr/include/c++/4.5/exception_ptr.h(112): error: identifier "__o" is undefined

/usr/include/c++/4.5/nested_exception.h(62): error: expected a ";"

/usr/include/c++/4.5/nested_exception.h(64): error: expected a ";"

/usr/include/c++/4.5/nested_exception.h(77): error: member function "std::nested_exception::~nested_exception" may not be redeclared outside its class

/usr/include/c++/4.5/nested_exception.h(77): error: function "std::<error>" may not be initialized

/usr/include/c++/4.5/nested_exception.h(77): error: expected an expression

/usr/include/c++/4.5/nested_exception.h(82): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(110): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(115): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(122): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(127): error: expected a ")"

/usr/include/c++/4.5/nested_exception.h(127): error: function template "std::__throw_with_nested" has already been defined

/usr/include/c++/4.5/bits/cpp_type_traits.h(180): error: identifier "char16_t" is undefined

/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: identifier "char32_t" is undefined

/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: class "std::__is_integer<<error-type>>" has already been defined

21 errors detected in the compilation of "/tmp/tmpxft_00000ef2_00000000-4_test_cuda.cpp1.ii".

Example test.cu

#include <cuda.h>

__host__ void test() {
  // nothing in method
}

Compiles fine with:

nvcc -c -o test.o test.cu

But not with C++0x

nvcc -c -o test.o test.cu --compiler-options "-std=c++0x"
like image 662
user988098 Avatar asked Jan 29 '12 21:01

user988098


2 Answers

No, as of this answer, nvcc does not support c++11 constructs, even if they are supported by the host compiler.

The reason it isn't as simple as passing -std=c++0x to the host compiler is that nvcc has to parse the entirety of the code in order to split it into its __host__and __device__ halves. This preprocess happens before the host compiler is presented with any code at all, so nvcc's parser needs to be able to grok c++11 for it to work.

like image 140
Jared Hoberock Avatar answered Oct 31 '22 00:10

Jared Hoberock


Update on @Jared Hoberock's answer based on another message he posted on Thrust's Google Group: it seems that C++11 support has been added to CUDA 6.5 (although it is still experimental and undocumented).

Dummy example: test.cu

#include <cuda.h>
#include <iostream>

__host__ void test() {
  float a = 12.;
  double b = 3.;
  auto c = a * b;
  std::cout << c << std::endl;
}

int main()
{
  test();
  return 0;
}

Compilation & execution

$ nvcc -std=c++11 test.cu -o test
$ ./test
36

Without -std=c++11, I get the following (expected) error:

test.cu(7): error: explicit type is missing ("int" assumed)

Note: this example may fail to compile with GCC 5.1.

Update

CUDA 7.0 officially introduced C++11 support:

CUDA 7 adds C++11 feature support to nvcc, the CUDA C++ compiler. This means that you can use C++11 features not only in your host code compiled with nvcc, but also in device code. New C++ language features include auto, lambda functions, variadic templates, static_assert, rvalue references, range-based for loops, and more. To enable C++11 support, pass the flag --std=c++11 to nvcc (this option is not required for Microsoft Visual Studio).

like image 37
BenC Avatar answered Oct 31 '22 00:10

BenC