0

I have this naive question about this error-handler I found on Stack Overflow. Here it is:

#define CUDA_HANDLE_ERROR(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess)
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

The only thing which I didn't get is bool abort = true statement in the parameter section of gpuAssert(..).

What's the purpose of this abort flag ?

talonmies
  • 70,661
  • 34
  • 192
  • 269
Nouman Tajik
  • 433
  • 1
  • 5
  • 18
  • `if (abort) exit(code);` is pretty obvious, isn't it? – talonmies Jul 25 '19 at 10:49
  • This seems C++ (C doesn't have default arguments) – David Ranieri Jul 25 '19 at 10:49
  • @DavidRanieri: Indeed, CUDA is compiled with a C++ compiler – talonmies Jul 25 '19 at 10:50
  • @talonmies: I have my own handler which doesn't use this flag. I'm confused as why to set the abort variable to true always, and if it is already set to true, why to check this in the if statement anyways ? – Nouman Tajik Jul 25 '19 at 10:53
  • it is set to true *by default*. If you don't want to exit on an error, set it to false when the assert is called – talonmies Jul 25 '19 at 10:54
  • 2
    Were you to use gpuAssert instead of CUDA_HANDLE_ERROR macro, you would have control over whether your program crashes with error or keeps working if some operation does not complete successfuly. – IcedLance Jul 25 '19 at 10:54

2 Answers2

3

Well, when we call that function, it is not a must to put the abort argument or not.

But if you call like gpuAssert(code, file, line, false) then the abort flag will be false, Then the program will not exit.

Because the abort flag is the default to true, but we can set it to false.

1

This is called a "default argument", and you can read more about them at: https://en.cppreference.com/w/cpp/language/default_arguments

The basic gist is that if you have a function declared like this:

void point(int x = 3, int y = 4);

Then you can use it like this, with these results:

point(1,2); // calls point(1,2)
point(1);   // calls point(1,4) - second argument omitted, defaults to 4
point();    // calls point(3,4) - both arguments omitted, defaults to 3, 4

Note that the default arguments must be at the end of the list (well mostly, there are some exceptions - the link up top has all the rules)

Frodyne
  • 3,547
  • 6
  • 16