2

As I know, CUDA supports C and C++. But I can't' use C++ in my kernel.

I try a simple example like this

__global__ void simple(){
     cout<<"abc";
}

That is error. But if I change to printf("abc"); it is right.

Can you explain for me? Thank you very much!

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Robotic Vn
  • 437
  • 2
  • 7
  • 20

2 Answers2

5

From CUDA 7.5 nvidia slides:

C++11 Supported features:

  • auto
  • lambdas
  • std::initializer_list
  • variadic templates
  • static_asserts
  • constexpr
  • rvalue references
  • range based for loops

C++ Not supported features

  • thread_local
  • Standard libraries: std::*

std::cout is defined in the C++ standard library which is not supported by CUDA. Use C printf

From CUDA 6.5, the ‘compute_11′, ‘compute_12′, ‘compute_13′, ‘sm_11′, ‘sm_12′, and ‘sm_13′ architectures are deprecated. So nvcc will compile by default to CC 2.0 enabling printf support.

More info here and here

Hopobcn
  • 885
  • 10
  • 20
0

CUDA doesn't link the libraries & header files that are required to use the cout function. However, you can enable the use of printf()

This answer explains the process which enables this feature: printing from cuda kernels quoted here for easier access:

To enable use of plain printf() on devices of Compute Capability >= 2.0, it's important to compile for CC of at least CC 2.0 and disable the default, which includes a build for CC 1.0.

Right-click the .cu file in your project, select Properties, select Configuration Properties | CUDA C/C++ | Device. Click on the Code Generation line, click the triangle, select Edit. In the Code Generation dialog box, uncheck Inherit from parent or project defaults, type compute_20,sm_20 in the top window, click OK.

Community
  • 1
  • 1
Ajay
  • 313
  • 1
  • 3
  • 11