Robotic Vn
Robotic Vn

Reputation: 577

C++ code in kernel CUDA?

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!

Upvotes: 2

Views: 1588

Answers (2)

Hopobcn
Hopobcn

Reputation: 905

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

Upvotes: 5

Ajay
Ajay

Reputation: 313

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.

Upvotes: 0

Related Questions