# My first program it doesn't behave as expected

**URL:** <https://forums.developer.nvidia.com/t/my-first-program-it-doesnt-behave-as-expected/10980>\
**Category:** CUDA Programming and Performance\
**Created:** [July 18, 2009, 10:47pm UTC](https://forums.developer.nvidia.com/t/my-first-program-it-doesnt-behave-as-expected/10980 "2009-07-18T22:47:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eafkuor](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@eafkuor](https://forums.developer.nvidia.com/u/eafkuor)\
**Post date:** [July 18, 2009, 10:47pm UTC](https://forums.developer.nvidia.com/t/my-first-program-it-doesnt-behave-as-expected/10980/1 "2009-07-18T22:47:23Z")

</div>

Hi everyone, I’m new here, and I’m sure this is gonna be the first of many days for me as a CUDA “programmer” :D

I’ve got Vista 64 and VisualStudio2008. I somehow managed to compile my first program, but it doesn’t do what I expected [External Image](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/thumbsdown.gif "Image hosted on another site. Click to open in a new tab.")

Here is the code:

```auto
#include <stdio.h>

__global__ void VecAdd(int* A, int* B, int* C){

	int i = threadIdx.x;

	C[i] = A[i] + B[i];

}

int main(){

	const int N=5;

	int A[N], B[N], C[N];

	

	for(int i=0;i<N;i++){

		A[i]=1;

		B[i]=2;

	}

	// Kernel invocation

	VecAdd<<<1, N>>>(A, B, C);

	

	

	for(int i=0;i<N;i++){

		printf("%d ", C[i]);

	}

	scanf("%d", A[0]);

	

}

```

It is supposed to sum the vectors A and B, and print all the elements of C. Shouldn’t it print “3 3 3 3 3”?

This is the weird output that I got instead:

```auto
-1 -2 -2147362916 1 0

```

How is it possible?

Thanks!

Simone

---

<div class="post-metadata">

**Author:** ![seibert](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seibert/32/520688_2.png) [@seibert](https://forums.developer.nvidia.com/u/seibert)\
**Post date:** [July 19, 2009, 12:25am UTC](https://forums.developer.nvidia.com/t/my-first-program-it-doesnt-behave-as-expected/10980/2 "2009-07-19T00:25:33Z")

</div>

The GPU can only operate on data that has been copied to the device memory. You need to allocate a device version of your A, B, and C arrays and copy data to/from them as needed. (cudaMalloc and cudaMemcpy are the important functions here)

Your kernel call actually failed when the GPU tried to access host pointers, but since you never check error messages in this code, that would not have been apparent.

To see a simple example which includes cudaMalloc and cudaMemcpy, see this article from Dr. Dobbs:

[url=“[CUDA, Supercomputing for the Masses: Part 2 | Dr Dobb's](http://www.ddj.com/cpp/207402986)”][http://www.ddj.com/cpp/207402986[/url]](http://www.ddj.com/cpp/207402986%5B/url%5D)

(The entire article series is very good.)

---

<div class="post-metadata">

**Author:** ![eafkuor](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@eafkuor](https://forums.developer.nvidia.com/u/eafkuor)\
**Post date:** [July 19, 2009, 10:22am UTC](https://forums.developer.nvidia.com/t/my-first-program-it-doesnt-behave-as-expected/10980/3 "2009-07-19T10:22:43Z")

</div>

> [@](#):
>
> The GPU can only operate on data that has been copied to the device memory. You need to allocate a device version of your A, B, and C arrays and copy data to/from them as needed. (cudaMalloc and cudaMemcpy are the important functions here)
> 
> Your kernel call actually failed when the GPU tried to access host pointers, but since you never check error messages in this code, that would not have been apparent.
> 
> To see a simple example which includes cudaMalloc and cudaMemcpy, see this article from Dr. Dobbs:
> 
> http://www.ddj.com/cpp/207402986
> 
> (The entire article series is very good.)

Thank you very much seibert, actually a friend of mine who is pretty good at cuda told me about that a couple of hours after I made the post; eventually we decided that the best thing for me was to continue reading the programming manual that comes with cuda [External Media](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/thumbup.gif "Media hosted on another site. Click to open in a new tab.") (and in fact, it clarified those aspects as I went on reading)

The problem is that I stopped at the first example in the manual:

```auto
// Kernel definition

__global__ void VecAdd(float* A, float* B, float* C)

{

int i = threadIdx.x;

C[i] = A[i] + B[i];

}

int main()

{

// Kernel invocation

VecAdd<<<1, N>>>(A, B, C);

}

```

and tried to modify it and run it, without knowing much about cuda. My fault :"\>

Anyway thanks for the link!
