First time CUDA coding, no errors of running, but the results are weird.
0 0 0 5581936
1 1 1 -1717986920
2 2 2 -854341140
3 3 3 19132063
4 4 4 -858993459
5 5 5 8001888
6 6 6 19124671
7 7 7 -682560762
8 8 8 3349568
9 9 9 3351040
10 10 10 1448006405
11 11 11 2435322
12 12 12 0
13 13 13 2130567168
14 14 14 7088378
15 15 15 0
Press any key to continue . . .
#include<iostream>
#include<cuda.h>
using namespace std;
void value_ints(int *a, int N)
{
int i;
for (i = 0; i < N; i++)
a[i] = i;
}
__global__ void add(int *a, int *b, int *c)
{
c[blockIdx.x]=a[blockIdx.x]+b[blockIdx.x];
}
#define N 16
int main()
{
int *a, *b, *c;
int *d_a, *d_b, *d_c;
int size=N*sizeof(int);
cudaMalloc((void **)&d_a, size);
cudaMalloc((void **)&d_b, size);
cudaMalloc((void **)&d_c, size);
a=(int *)malloc(size); value_ints(a, N);
b=(int *)malloc(size); value_ints(b, N);
c=(int *)malloc(size);
cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
add<<<N,1>>>(d_a, d_b, d_c);
cudaMemcpy(c, d_c, size, cudaMemcpyDeviceToHost);
for(int i=0; i<N; i++)
{
cout<<i<<" "<<*(a+i)<<" "<<*(b+i)<<" "<<*(c+i)<<endl;
}
free(a); free(b); free(c);
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
system("pause");
return;
}