Why am I getting a wrong result for adding two numbers in cuda? I am getting 1 as an answer instead of 9. Can anybody tell me why? Does this have something to do with the pointers? I have used the following code:
#include <iostream>
#include <cuda_runtime.h>
#include <cuda.h>
using namespace std;
__global__ void add(int *a, int *b, int *c)
{
*c = *a + *b;
}
int main(void) {
int a, b, c; // host copies of a, b, c
int *d_a, *d_b, *d_c; // device copies of a, b, c
int size = sizeof(int);
// Allocate space for device copies of a, b, c
cudaMalloc((void **)&d_a, size);
cudaMalloc((void **)&d_b, size);
cudaMalloc((void **)&d_c, size);
// Setup input values
a = 2;
b = 7;
cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);
// Launch add() kernel on GPU
add<<<1,1>>>(d_a, d_b, d_c);
// Copy result back to host
cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);
cout << "answer is " << c <<endl;
// Cleanup
cudaFree(d_a); cudaFree(d_b); cudaFree(d_c);
//return 0;
}
Aucun commentaire:
Enregistrer un commentaire