2016-08-18 8 views
1

Ich lerne CUDA und brauche etwas Hilfe. Hier ist mein Programm von NVIDIAs Präsentation:Hinzufügen von zwei Zahlen CUDA

__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; 

     // © NVIDIA Corporation 2011 
     // Addition on the Device: main() 
     // Copy inputs to device 
     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); 

     // Cleanup 
     cudaFree(d_a); cudaFree(d_b); cudaFree(d_c); 

     printf("%d",&c); 
     return 0; 
    } 

Jedes Mal, wenn ich diesem Code den Wert von c laufen ist nicht 9. Wie realen Wert der c zu bekommen?

Antwort

4

Versuchen

printf("%d", c); 

statt

printf("%d", &c); 

wie zur Zeit drucken Sie die Adresse des lokalen var c statt dessen Wert

+0

Ihnen danken. Jetzt funktioniert es richtig! –