2 + 7 = 57 Wrong calculation

Hi All,

I just started CUDA C programming on CentOS ( 64-bit), CUDA 2.3. I go with well known example of CUDA by examples book. The code is below. The problem is here. One I make 2 + 7, it shows 57. Output of this code is here.

[I@cuda_test]$ ./add_num

2 + 7 = 57

What could be the problem in my code/system/settings?

I suspect that there is a char-int data-type problem. It is because ascii for ‘9’ is 57.

Any help would be appreciated.

#include <stdio.h>

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

{

	*c = a + b;

}

int main( void )

{

	int c;

	int *dev_c;

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

	add<<<1,1>>>( 2, 7, dev_c );	

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

	printf( "2 + 7 = %d\n", c );

	cudaFree( dev_c );

	return 0;

}

Error check your calls please… Somethingz not going right there… And initialize c to -1 and see if 57 morphs to -1

Kernel is not correct.

global void add( int a, int b, int *c)

{

  c[0] = a + b;

}

See if it works :)

Kernel is not correct.

global void add( int a, int b, int *c)

{

  c[0] = a + b;

}

See if it works :)

How is that any different from the original poster’s kernel?

How is that any different from the original poster’s kernel?

it’s not

it’s not

I realized that I was working on an emulator, not a real device.

Also, I change the code to

cudaError err = cudaMalloc( (void**)&dev_c, sizeof(int) ) ;



if (err != cudaSuccess) 

{

    printf( "Error: %s \n", cudaGetErrorString( err ) );

    exit( EXIT_FAILURE );

    }

In this case, it say “Error: no CUDA-capable device is available”

I will setup it to use the device.

Thanks all…

I realized that I was working on an emulator, not a real device.

Also, I change the code to

cudaError err = cudaMalloc( (void**)&dev_c, sizeof(int) ) ;



if (err != cudaSuccess) 

{

    printf( "Error: %s \n", cudaGetErrorString( err ) );

    exit( EXIT_FAILURE );

    }

In this case, it say “Error: no CUDA-capable device is available”

I will setup it to use the device.

Thanks all…