need "hello world" example request a simple complete example

Hi. I am brand new to GPU computing, and somewhat new to C for that matter. I copied the example in the Programming Guide, Figure 6-1. Matrix Multiplication, into a C file and called it test2.c, but when I try to compile it with nvcc I get a bunch of errors (below). Can someone please provide a complete, simple example (in C), including the nvcc command that is needed to compile it? Thanks very much! Are any include statements needed in the file?
John

test2.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
test2.c: In function ‘Mul’:
test2.c:18: error: ‘cudaMemcpyHostToDevice’ undeclared (first use in this function)
test2.c:18: error: (Each undeclared identifier is reported only once
test2.c:18: error: for each function it appears in.)
test2.c:29: error: ‘dim3’ undeclared (first use in this function)
test2.c:29: error: expected ‘;’ before ‘dimBlock’
test2.c:30: error: expected ‘;’ before ‘dimGrid’
test2.c:32: error: ‘Muld’ undeclared (first use in this function)
test2.c:32: error: expected expression before ‘<’ token
test2.c:34: error: ‘cudaMemcpyDeviceToHost’ undeclared (first use in this function)
test2.c: At top level:
test2.c:44: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’

John,

You would probably save yourself a lot of trouble by simply selecting a program within the SDK which is closest to what you wish to do and altering it to fit your requirements. Messing around with the NVCC compiler, or any compiler for that matter, can be tricky and since you’re already new to C I would say cut out all the external issues that you don’t need to deal with and allow yourself to focus on just writing code and not debugging compiler errors. I took a class on CUDA and that was generally the accepted method. There is a template project within the CUDA SDK that you might wish to look at.

Hope this helps,
Mack

Thanks Mack!

There is a guide here which walks you through compiling and running some of the examples, hope it helps!

For quick and dirty simple examples, I’m often too lazy to copy the makefile junk over or setup CMake or what not. You can build from the command line without any trouble.

gpu_hello_world.cu (note: I didn’t compile this so there may be syntax errors…, it also shows bad programming practice since I don’t check fro error return values from CUDA)

#include <stdio.h>

__global__ void hello_world_kernel(int *d_data)

 Â  Â {

 Â  Â d_data[threadIdx.x + blockDim.x*blockIdx.x] = threadIdx.x;

 Â  Â }

int main()

 Â  Â {

 Â  Â int *d_data;

 Â  Â int h_data[32];

 Â  Â cudaMalloc(&d_data, 32*sizeof(int));

  Â kernel<<<1, 32>>>(d_data);

  Â cudaMemcpy(h_data, d_data, sizeof(int)*32, cudaMemcpyHostToDevice);

 Â  Â for (int i = 0; i < 32; i++)

 Â  Â  Â  Â printf("%d\n", h_data[i];

 Â  Â cudaFree(d_data)

 Â  Â }

Compile with:

nvcc -o hello_world_gpu hello_world_gpu.cu

not sure if it’s what you’re looking for, but i found that i could re-use the makefile from the toolkit examples in another directory by just changing one line (the include) and defining some variables (SRCDIR and ROOTDIR; perhaps BINDIR as well if you want your output local). then you can use “make” in you own directory. the only tricky part really is that those directories must end in a trailing slash (on linux).

same thing with more words at [url=“Andrew Cooke: C[omp]ute”]http://www.acooke.org/cute/ReusingCUD0.html[/url]

andrew

Hi!

I know that this thread is pretty old but I think this may help some of you with the above-mentioned error:

[codebox]test2.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’[/codebox]

The problem is the file extension .c! Just rename your file from test2.c to test2.cu and run nvcc -o test2 test2.cu

The NVIDIA compiler handles .c files as normal C files where global is unknown.

Kind regards,

user23

Thanx user23, usually its the little things that makes compiling hard ;-)

I’ve been searching the net and docs for the last two hours in trying to figure out why my code just wouldn’t compile, even after making 100% sure that I’ve typed everything correctly. And there it was, exactly because of the file extension.

Thank you very much for that little piece of advice that came to the rescue.

Kind Regards,
Polla