Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple c++ HelloWorld with cuda

Tags:

c++

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!

like image 732
Sebastian Caicedo Alfonso Avatar asked Dec 30 '15 10:12

Sebastian Caicedo Alfonso


2 Answers

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

like image 155
talonmies Avatar answered Sep 23 '22 21:09

talonmies


In guide also below lines are mentioned,

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

Please change g++ to nvcc in your Makefile.

like image 40
Pravar Jawalekar Avatar answered Sep 22 '22 21:09

Pravar Jawalekar