A simple c++ HelloWorld with cuda

I'm trying to make my first program with cuda. So I make this simple HelloWorld with guide of various pages.

#include <cstdlib>
#include <cstdio>
#include <cuda.h>

using namespace std;

__global__ void mykernel(void) {
}

int main(void) {
mykernel<<<1,1>>>();
printf("CPU Hello World!\n");
return 0;
} 

But I am getting this output:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/hellowordcuda
make[2]: Entering directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/hellowordcuda.o.d"
g++    -c -g -I/usr/local/cuda-7.5/include -MMD -MP -MF "build/Debug/GNU-Linux-x86/hellowordcuda.o.d" -o build/Debug/GNU-Linux-x86/hellowordcuda.o hellowordcuda.cpp
hellowordcuda.cpp:19:1: error: ‘__global__’ does not name a type
 __global__ void mykernel(void) {
 ^
hellowordcuda.cpp: In function ‘int main()’:
hellowordcuda.cpp:24:1: error: ‘mykernel’ was not declared in this scope
 mykernel<<<1,1>>>();
 ^
hellowordcuda.cpp:24:11: error: expected primary-expression before ‘<’ token
 mykernel<<<1,1>>>();
           ^
hellowordcuda.cpp:24:17: error: expected primary-expression before ‘>’ token
 mykernel<<<1,1>>>();
                 ^
hellowordcuda.cpp:24:19: error: expected primary-expression before ‘)’ token
 mykernel<<<1,1>>>();
                   ^
make[2]: *** [build/Debug/GNU-Linux-x86/hellowordcuda.o] Error 1
make[2]: Leaving directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
 make: *** [.build-impl] Error 2

I'm sorry for making a question so simple but I just don't find any answer for this.

Thanks a lot, any help would be greatly appreciated!

Upvotes: 5

Views: 5376

Answers (3)

It appears you're building directly with g++. You need to use NVidia's compiler (nvcc) to use CUDA, and make sure it knows to process the file as CUDA C. This can be achieved by changing the extension to .cu, or by playing around with compilation options which specify the file & processing type. I recommend the former.

Upvotes: 4

talonmies
talonmies

Reputation: 72349

There are two things you need to do to make this work:

  1. use the CUDA compiler driver nvcc to steer compilation of the code
  2. rename hellowordcuda.cpp to hellowordcuda.cu when passing the code to nvcc

The second point is necessary because nvcc uses the file extension to steer compilation, and if you code has a .cc or .cpp file extension, it will just pass the code to the host compiler and the same compilation errors will result

Upvotes: 14

Pravar Jawalekar
Pravar Jawalekar

Reputation: 605

In guide also below lines are mentioned,

$ nvcc hello.cu
$ a.out
Hello World!

Please change g++ to nvcc in your Makefile.

Upvotes: 4

Related Questions