Hello all, I have a code that should work right but all of my codes that has device codes somehow do some weird
things. Does anyone have a clue of what is happening. Can this be a driver problem? If so, can somebody
teach me how to solve it??? Thank you.
Code:
#include “…/common/book.h”
#define N 50
global void add(int *a, int *b, int *c) {
int tid = blockIdx.x; //handle the data at this index
if (tid <N)
c[tid] = a[tid] + b[tid];
}
int main( void ) {
int a[N], b[N], c[N];
int *dev_a, *dev_b, *dev_c; //for GPU memory allocation
//memory allocation on GPU
cudaMalloc( (void**)&dev_a, N*sizeof(int));
cudaMalloc( (void**)&dev_b, N*sizeof(int));
cudaMalloc( (void**)&dev_c, N*sizeof(int));
//fill in a, b
for (int i=0; i<N; i++) {
a[i] = -i;
b[i] = i*i;
}
//copy of arrays 'a', 'b' on GPU
cudaMemcpy(dev_a, a, N*sizeof(int), cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, N*sizeof(int), cudaMemcpyHostToDevice);
add<<<1, 1>>>(dev_a, dev_b, dev_c);
cudaThreadSynchronize();
//copy the array 'c' back from the GPU to CPU
cudaMemcpy(c, dev_c, N*sizeof(int), cudaMemcpyDeviceToHost);
//display results
for (int i=0; i<N; i++) {
printf( "%d + %d = %d\n", a[i], b[i], c[i] );
}
//free the memory allocated on the GPU
cudaFree(dev_a);
cudaFree(dev_b);
cudaFree(dev_c);
return 0;
}
Errors: This is the result from following file
My computer setting is
Win 7
Intel Core2
Geforce 9800GT