3

My source code of simple C++ cuda code

#include <iostream>
#include <cuda.h>

using namespace std;

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

int main()
{
    int a, b, c;
    int *d_a, *d_b, *d_c;
    int size = sizeof(int);

    cudaMalloc((void **)&d_a, size);
    cudaMalloc((void **)&d_b, size);
    cudaMalloc((void **)&d_c, size);


    a = 10;
    b = 35;
    c = 0;

    cudaMemcpy(d_a, &a, size, cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, &b, size, cudaMemcpyHostToDevice);

    AddIntsCUDA<<<1, 1>>>(d_a, d_b, d_c);

    cudaMemcpy(&c, d_c, size, cudaMemcpyDeviceToHost);

    cout << "The Answer is "<< c << endl;

    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);

    system("pause");

    return 0;
}

Console Output output shows c = 0 but i expect sum of a and b output (should like this 45 because a = 10, b = 35) explain me what the hell is happening in this code

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
r00tk1ll3r
  • 63
  • 1
  • 10
  • 1
    You are ignoring values returned from Cuda functions. Don't they return errors? – Daniel Langr Nov 12 '19 at 06:57
  • no its not returning any errors – r00tk1ll3r Nov 12 '19 at 06:59
  • Try adding a cudaError_t err = cudaDeviceSynchronize(); after the kernel launch and before the copy. And print the value of err. – iliar Nov 12 '19 at 07:02
  • 1
    showing err = 35 its basically runtime error code – r00tk1ll3r Nov 12 '19 at 07:05
  • @iliar why variable a and b not sum to c – r00tk1ll3r Nov 12 '19 at 07:06
  • use const char* cudaGetErrorString ( cudaError_t error ) to get the error string at runtime, or look here: https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html#group__CUDART__TYPES_1gf599e5b8b829ce7db0f5216928f6ecb6 It seems that you need to update your driver. – iliar Nov 12 '19 at 07:10
  • 4
    Cuda error 35 is "cudaErrorInsufficientDriver". From documentation: _This indicates that the installed NVIDIA CUDA driver is older than the CUDA runtime library. This is not a supported configuration. Users should install an updated NVIDIA display driver to allow the application to run_. Check the compatibility here: https://docs.nvidia.com/deploy/cuda-compatibility/index.html. – Daniel Langr Nov 12 '19 at 07:11
  • yeah its correct thanks my graphic card driver is old – r00tk1ll3r Nov 12 '19 at 07:19
  • @DanielsaysreinstateMonica may god bless your time – r00tk1ll3r Nov 12 '19 at 07:24
  • @DanielsaysreinstateMonica If you turn that into an answer, then OP can do more than ask god to bless your time. They could then also do it the SO-appreciated way of honoring the most helpful answer with an accept. Which incidentally also would support the idea of StackOverflow to make good Q/A pairs. To make this a better one, edit the title to "Meaning of CUDA return value error 35?". That would make the question helpfully findable. Ideally one of you also edits the question to show the code which actually takes and outputs that value. I think there is an upvote for both of you in this then. – Yunnosch Nov 12 '19 at 07:36
  • also thanks to @iliar for your time – r00tk1ll3r Nov 12 '19 at 07:39
  • @Yunnosch why i couldnt change the title of my own question – r00tk1ll3r Nov 12 '19 at 07:41
  • 1
    @iliar In my opinion (but admittedly possibly not in others... I cannot guarantee), your contribution can also be turned into an answer. To contrast to Daniel, stress your recommendations on debugging. They make a separate and in my opinion also helpful answer about a different way of solving the problem. Or since you had the earlier comment referring to docu and the general right answer, you can explain that and refer to Daniels more immediatly applicable link. – Yunnosch Nov 12 '19 at 07:44
  • zaid, you cannot? Did you try? – Yunnosch Nov 12 '19 at 07:44
  • @Yunnosch, thanks for your advice, I made an answer. However, I think if DanielsaysreinstateMonica creates an answer too, his should be the one accepted. – iliar Nov 12 '19 at 07:48
  • no, I can't change whol question – r00tk1ll3r Nov 12 '19 at 07:49
  • @iliar I do not contradict you, but the decision on the most helpful answer is solely OPs. If there is no other answer than yours for a longer time.... Otherwise I applaud your fairness and grasp of SO ideas. – Yunnosch Nov 12 '19 at 07:50
  • zaid In which way does an attempt to [edit] your question fail? (I ask because I am convinced that it must be possible.) I will apply my proposed edit to the title, with your permission. The change to the code I do not dare. Please spend some more time trying to edit yourself. – Yunnosch Nov 12 '19 at 07:52
  • @Yunnosch but anyway title changed!!! – r00tk1ll3r Nov 12 '19 at 07:57
  • 1
    zaid Please do the appropriate code change now. Note that normally changing a question which already has an answer must be done with extreme care. SO users do not like "moving target" questions, which are the danger here. But the edit I propose will still sufficiently match the now existing answer, if the history is taken into account. I added an explaining and hopefully protecting comment to iliars nice answer. – Yunnosch Nov 12 '19 at 08:04

1 Answers1

2

Try adding a cudaError_t err = cudaDeviceSynchronize(); after the kernel launch and before the copy. And print the value of err.

Use const char* cudaGetErrorString ( cudaError_t error ) to get the error string at runtime, or look here:
https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html#group__CUDART__TYPES_1gf599e5b8b829ce7db0f5216928f6ecb6

Following your comment that it's error number 35, it seems that you need to update your driver.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
iliar
  • 932
  • 6
  • 11
  • You don't store it. Merely std::cout << cudaGetErrorString (err); – iliar Nov 12 '19 at 07:54
  • 1
    Hello voting fellow SO users. If the question at some point already shows the proposed debugging instrumentation, please do not begrudge neither this answer for proposing it (it is a very helpful answer to the problem as it originally occurs to people witht the same problem), nor the question for adding it after this answer (it is a more helpful question with the title and this debugging code inside). Please see the history and the common goal. – Yunnosch Nov 12 '19 at 08:02