error.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/utilities/export.hpp>
9 
10 #include <cuda.h>
11 #include <cuda_runtime_api.h>
12 
13 #include <stdexcept>
14 #include <string>
15 #include <type_traits>
16 
17 namespace CUDF_EXPORT cudf {
30 struct logic_error : std::logic_error {
36  explicit logic_error(char const* const message) : std::logic_error(message) {}
37 
43  explicit logic_error(std::string const& message) : std::logic_error(message) {}
44 
45  // TODO Add an error code member? This would be useful for translating an
46  // exception to an error code in a pure-C API
47 
48  ~logic_error() override
49  {
50  // Needed so that the first instance of the implicit destructor for any TU isn't 'constructed'
51  // from a host+device function marking the implicit version also as host+device
52  }
53 };
58 struct cuda_error : std::runtime_error {
65  explicit cuda_error(std::string const& message, cudaError_t const& error)
66  : std::runtime_error(message), _cudaError(error)
67  {
68  }
69 
75  [[nodiscard]] cudaError_t error_code() const { return _cudaError; }
76 
77  protected:
78  cudaError_t _cudaError;
79 };
80 
82  using cuda_error::cuda_error; // Inherit constructors
83 };
84 
92 struct data_type_error : std::invalid_argument {
98  explicit data_type_error(char const* const message) : std::invalid_argument(message) {}
99 
105  explicit data_type_error(std::string const& message) : std::invalid_argument(message) {}
106 };
109 } // namespace CUDF_EXPORT cudf
110 
111 #define STRINGIFY_DETAIL(x) #x
112 #define CUDF_STRINGIFY(x) STRINGIFY_DETAIL(x)
113 
143 #define CUDF_EXPECTS(...) \
144  GET_CUDF_EXPECTS_MACRO(__VA_ARGS__, CUDF_EXPECTS_3, CUDF_EXPECTS_2) \
145  (__VA_ARGS__)
146 
148 
149 #define GET_CUDF_EXPECTS_MACRO(_1, _2, _3, NAME, ...) NAME
150 
151 #define CUDF_EXPECTS_3(_condition, _reason, _exception_type) \
152  do { \
153  static_assert(std::is_base_of_v<std::exception, _exception_type>); \
154  (_condition) ? static_cast<void>(0) \
155  : throw _exception_type /*NOLINT(bugprone-macro-parentheses)*/ \
156  {"CUDF failure at: " __FILE__ ":" CUDF_STRINGIFY(__LINE__) ": " _reason}; \
157  } while (0)
158 
159 #define CUDF_EXPECTS_2(_condition, _reason) CUDF_EXPECTS_3(_condition, _reason, cudf::logic_error)
160 
162 
182 #define CUDF_FAIL(...) \
183  GET_CUDF_FAIL_MACRO(__VA_ARGS__, CUDF_FAIL_2, CUDF_FAIL_1) \
184  (__VA_ARGS__)
185 
187 
188 #define GET_CUDF_FAIL_MACRO(_1, _2, NAME, ...) NAME
189 
190 #define CUDF_FAIL_2(_what, _exception_type) \
191  /*NOLINTNEXTLINE(bugprone-macro-parentheses)*/ \
192  throw _exception_type { "CUDF failure at:" __FILE__ ":" CUDF_STRINGIFY(__LINE__) ": " _what }
193 
194 #define CUDF_FAIL_1(_what) CUDF_FAIL_2(_what, cudf::logic_error)
195 
197 
198 namespace CUDF_EXPORT cudf {
199 namespace detail {
200 // @cond
201 inline void throw_cuda_error(cudaError_t error, char const* file, unsigned int line)
202 {
203  // Calls cudaGetLastError to clear the error status. It is nearly certain that a fatal error
204  // occurred if it still returns the same error after a cleanup.
205  cudaGetLastError();
206  auto const last = cudaFree(nullptr);
207  auto const msg = std::string{"CUDA error encountered at: " + std::string{file} + ":" +
208  std::to_string(line) + ": " + std::to_string(error) + " " +
209  cudaGetErrorName(error) + " " + cudaGetErrorString(error)};
210  // Call cudaDeviceSynchronize to ensure `last` did not result from an asynchronous error.
211  // between two calls.
212  if (error == last && last == cudaDeviceSynchronize()) {
213  throw fatal_cuda_error{"Fatal " + msg, error};
214  } else {
215  throw cuda_error{msg, error};
216  }
217 }
218 // @endcond
219 } // namespace detail
220 } // namespace CUDF_EXPORT cudf
221 
229 #define CUDF_CUDA_TRY(call) \
230  do { \
231  cudaError_t const status = (call); \
232  if (cudaSuccess != status) { cudf::detail::throw_cuda_error(status, __FILE__, __LINE__); } \
233  } while (0);
234 
248 #ifndef NDEBUG
249 #define CUDF_CHECK_CUDA(stream) \
250  do { \
251  CUDF_CUDA_TRY(cudaStreamSynchronize(stream)); \
252  CUDF_CUDA_TRY(cudaPeekAtLastError()); \
253  } while (0);
254 #else
255 #define CUDF_CHECK_CUDA(stream) CUDF_CUDA_TRY(cudaPeekAtLastError());
256 #endif
cuDF interfaces
Definition: host_udf.hpp:26
Exception thrown when a CUDA error is encountered.
Definition: error.hpp:58
cuda_error(std::string const &message, cudaError_t const &error)
Construct a new cuda error object with error message and code.
Definition: error.hpp:65
cudaError_t _cudaError
CUDA error code.
Definition: error.hpp:78
cudaError_t error_code() const
Returns the CUDA error code associated with the exception.
Definition: error.hpp:75
Exception thrown when an operation is attempted on an unsupported dtype.
Definition: error.hpp:92
data_type_error(std::string const &message)
Construct a new data_type_error object with error message.
Definition: error.hpp:105
data_type_error(char const *const message)
Constructs a data_type_error with the error message.
Definition: error.hpp:98
Exception thrown when logical precondition is violated.
Definition: error.hpp:30
logic_error(char const *const message)
Constructs a logic_error with the error message.
Definition: error.hpp:36
logic_error(std::string const &message)
Construct a new logic error object with error message.
Definition: error.hpp:43