-1

I'm working through the examples of the "CUDA by Example" book. The following code doesn't give me an answer and work as it should. Where's the mistake?

Will appreciate your help and answers.

I get an output,which reads Calculation done on GPU yields the answer: &d Press enter to stop

#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <stdio.h>

using namespace std;

__global__ void add_integers_cuda(int a, int b, int *c)
{
    *c = a + b;
}

int main(void)
{
    int c;
    int *dev_ptr;

    cudaMalloc((void **)&dev_ptr, sizeof(int)); //allocate sizeof(int) bytes of contiguous memory in the gpu device and return the address of first byte to dev_ptr.

// call the kernel
    add_integers_cuda <<<1,1>>>(2,7,dev_ptr);

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

    printf("Calculation done on GPU yields the answer: &d\n",c );

    cudaFree(dev_ptr);

    printf("Press enter to stop.");
    cin.ignore(255, '\n');

    return 0;

}

"

talonmies
  • 70,661
  • 34
  • 192
  • 269

1 Answers1

2

&d is not a correct printf formatting character here:

printf("Calculation done on GPU yields the answer: &d\n",c );

You won't get the output you are expecting.

You should use %d instead:

printf("Calculation done on GPU yields the answer: %d\n",c );

This particular issue has nothing to do with CUDA of course.

You may also want to run CUDA codes with cuda-memcheck and/or use proper CUDA error checking if you are just learning and having trouble. Neither of those would have pointed out the above error, however.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • Thanks! That was a really silly mistake! Thanks for pointing it out. You're right, it has nothing to do with CUDA! : ) – user6883610 Sep 27 '16 at 14:17