 # [Beginner] Math operations giving incorrect answers

Hey guys,

New to CUDA C, running into a few problems. Whenever I executed any expressions on the device using numbers, the answers I get back are always incorrect.

For example this code (which is just directly copied from Nvidia) adds two vectors together to a third vector and prints out the results.

``````#include<iostream>

const int N = 10;

__global__ void add(int *a, int *b, int *c) {

int index = blockIdx.x;

if(index < N) {

c[index] = a[index] + b[index];

}

}

int main() {

int a[N], b[N], c[N];

int *dev_a, *dev_b, *dev_c;

cudaMalloc((void**)&dev_a, N * sizeof(int));

cudaMalloc((void**)&dev_b, N * sizeof(int));

cudaMalloc((void**)&dev_c, N * sizeof(int));

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

a[i] = i;

b[i] = i*i;

}

cudaMemcpy(dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice);

cudaMemcpy(dev_b, b, N * sizeof(int), cudaMemcpyHostToDevice);

cudaMemcpy(&c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost);

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

std::cout << a[i] << " + " << b[i] << " = " << c[i] << "\n";

}

cudaFree(dev_a);

cudaFree(dev_b);

cudaFree(dev_c);

return 0;

}
``````

But the results I get are:

``````0 + 0 = 4207985

1 + 1 = 0

2 + 4 = 4203010

3 + 9 = 0

4 + 16 = 928020192

5 + 25 = 32767

6 + 36 = 6307276

7 + 49 = 0

8 + 64 = 1

9 + 81 = 0
``````

So I figured, let’s try an even simpler one, this code adds two integers and displays the result;

``````#include<iostream>

using namespace std;

__global__ void integerSum(int, int, int*);

int main() {

int a, b, c = 0;

int *dev_c;

cudaMalloc((void**)&dev_c, sizeof(int));

cout << "Enter 1st int: ";

cin >> a;

cout << "Enter second int: ";

cin >> b;

integerSum<<<1,1>>>(a, b, dev_c);

cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);

cout << a << " + " << b << " = " << c << endl;

cudaFree(dev_c);

return 0;

}

__global__ void integerSum(int a, int b, int *dev_c) {

*dev_c = a + b;

}
``````

to which I get answers such as:

``````Enter 1st int: 5

Enter second int: 6

5 + 6 = 0
``````

I get no errors when compiling.

If it makes any difference, I am running Linux Ubuntu 10.10 with CUDA 3.2 RC.

Hey guys,

New to CUDA C, running into a few problems. Whenever I executed any expressions on the device using numbers, the answers I get back are always incorrect.

For example this code (which is just directly copied from Nvidia) adds two vectors together to a third vector and prints out the results.

``````#include<iostream>

const int N = 10;

__global__ void add(int *a, int *b, int *c) {

int index = blockIdx.x;

if(index < N) {

c[index] = a[index] + b[index];

}

}

int main() {

int a[N], b[N], c[N];

int *dev_a, *dev_b, *dev_c;

cudaMalloc((void**)&dev_a, N * sizeof(int));

cudaMalloc((void**)&dev_b, N * sizeof(int));

cudaMalloc((void**)&dev_c, N * sizeof(int));

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

a[i] = i;

b[i] = i*i;

}

cudaMemcpy(dev_a, a, N * sizeof(int), cudaMemcpyHostToDevice);

cudaMemcpy(dev_b, b, N * sizeof(int), cudaMemcpyHostToDevice);

cudaMemcpy(&c, dev_c, N * sizeof(int), cudaMemcpyDeviceToHost);

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

std::cout << a[i] << " + " << b[i] << " = " << c[i] << "\n";

}

cudaFree(dev_a);

cudaFree(dev_b);

cudaFree(dev_c);

return 0;

}
``````

But the results I get are:

``````0 + 0 = 4207985

1 + 1 = 0

2 + 4 = 4203010

3 + 9 = 0

4 + 16 = 928020192

5 + 25 = 32767

6 + 36 = 6307276

7 + 49 = 0

8 + 64 = 1

9 + 81 = 0
``````

So I figured, let’s try an even simpler one, this code adds two integers and displays the result;

``````#include<iostream>

using namespace std;

__global__ void integerSum(int, int, int*);

int main() {

int a, b, c = 0;

int *dev_c;

cudaMalloc((void**)&dev_c, sizeof(int));

cout << "Enter 1st int: ";

cin >> a;

cout << "Enter second int: ";

cin >> b;

integerSum<<<1,1>>>(a, b, dev_c);

cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);

cout << a << " + " << b << " = " << c << endl;

cudaFree(dev_c);

return 0;

}

__global__ void integerSum(int a, int b, int *dev_c) {

*dev_c = a + b;

}
``````

to which I get answers such as:

``````Enter 1st int: 5

Enter second int: 6

5 + 6 = 0
``````

I get no errors when compiling.

If it makes any difference, I am running Linux Ubuntu 10.10 with CUDA 3.2 RC.

Can please post the return code you get, when you start the kernel and from all copy operations. I suppose the kernel does is not start for some reason or memory transactions fail and you are outputting random data from memory.

BTW: I would not recommend to use the release candidate. Have you tried compiling it with V3.1 or 3.0? These versions are stable.

Regards,

Kwyjibo

Can please post the return code you get, when you start the kernel and from all copy operations. I suppose the kernel does is not start for some reason or memory transactions fail and you are outputting random data from memory.

BTW: I would not recommend to use the release candidate. Have you tried compiling it with V3.1 or 3.0? These versions are stable.

Regards,

Kwyjibo

I execute your code on my linux box with GTX480, cuda 3.2

it works.

I execute your code on my linux box with GTX480, cuda 3.2

it works.

I have exactly the same problem. I’ve compiled some simple test programs then converted them into MEX files in Matlab. The programs are only squaring the number, vector or matrix that they are passed, however the return value is either the number passed in, or something random. I’m using the Visual Studio 2008 compiler with CUDA 3.2 and Matlab 2010b with a 8600GT. The only thing I could think of was that the graphics drivers are very old, so could this be messing up the CUDA executed code?

I have exactly the same problem. I’ve compiled some simple test programs then converted them into MEX files in Matlab. The programs are only squaring the number, vector or matrix that they are passed, however the return value is either the number passed in, or something random. I’m using the Visual Studio 2008 compiler with CUDA 3.2 and Matlab 2010b with a 8600GT. The only thing I could think of was that the graphics drivers are very old, so could this be messing up the CUDA executed code?

I use cuda3.2 and driver 260.24 on linux box.

I think you should update your driver.

I use cuda3.2 and driver 260.24 on linux box.

I think you should update your driver.