alpaka
Abstraction Library for Parallel Kernel Acceleration
Cuda.hpp
Go to the documentation of this file.
1 /* Copyright 2022 Axel Huebl, Benjamin Worpitz, Matthias Werner, RenĂ© Widera, Andrea Bocci, Bernhard Manfred Gruber
2  * SPDX-License-Identifier: MPL-2.0
3  */
4 
5 #pragma once
6 
8 
9 #include <iostream>
10 #include <stdexcept>
11 #include <string>
12 
13 #ifdef ALPAKA_ACC_GPU_CUDA_ENABLED
14 
16 {
17  //! CUDA driver API error checking with log and exception, ignoring specific error values
18  ALPAKA_FN_HOST inline auto cudaDrvCheck(CUresult const& error, char const* desc, char const* file, int const& line)
19  -> void
20  {
21  if(error == CUDA_SUCCESS)
22  return;
23 
24  char const* cu_err_name = nullptr;
25  char const* cu_err_string = nullptr;
26  CUresult cu_result_name = cuGetErrorName(error, &cu_err_name);
27  CUresult cu_result_string = cuGetErrorString(error, &cu_err_string);
28  std::string sError = std::string(file) + "(" + std::to_string(line) + ") " + std::string(desc) + " : '";
29  if(cu_result_name == CUDA_SUCCESS && cu_result_string == CUDA_SUCCESS)
30  {
31  sError += std::string(cu_err_name) + "': '" + std::string(cu_err_string) + "'!";
32  }
33  else
34  {
35  // cuGetError*() failed, so append corresponding error message
36  if(cu_result_name == CUDA_ERROR_INVALID_VALUE)
37  {
38  sError += " cuGetErrorName: 'Invalid Value'!";
39  }
40  if(cu_result_string == CUDA_ERROR_INVALID_VALUE)
41  {
42  sError += " cuGetErrorString: 'Invalid Value'!";
43  }
44  }
45 # if ALPAKA_DEBUG >= ALPAKA_DEBUG_MINIMAL
46  std::cerr << sError << std::endl;
47 # endif
49  throw std::runtime_error(sError);
50  }
51 } // namespace alpaka::cuda::detail
52 
53 //! CUDA driver error checking with log and exception.
54 # define ALPAKA_CUDA_DRV_CHECK(cmd) ::alpaka::cuda::detail::cudaDrvCheck(cmd, #cmd, __FILE__, __LINE__)
55 
57 
58 #endif
#define ALPAKA_DEBUG_BREAK
Definition: Debug.hpp:76
#define ALPAKA_FN_HOST
Definition: Common.hpp:40
ALPAKA_FN_HOST auto cudaDrvCheck(CUresult const &error, char const *desc, char const *file, int const &line) -> void
CUDA driver API error checking with log and exception, ignoring specific error values.
Definition: Cuda.hpp:18