error.hpp
1 /*
2  * Copyright (c) 2021-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <cstring>
19 #include <exception>
20 #include <system_error>
21 
22 #include <kvikio/shim/cuda.hpp>
23 #include <kvikio/shim/cufile_h_wrapper.hpp>
24 
25 namespace kvikio {
26 
27 struct CUfileException : public std::runtime_error {
28  using std::runtime_error::runtime_error;
29 };
30 
31 #ifndef CUDA_DRIVER_TRY
32 #define CUDA_DRIVER_TRY(...) \
33  GET_CUDA_DRIVER_TRY_MACRO(__VA_ARGS__, CUDA_DRIVER_TRY_2, CUDA_DRIVER_TRY_1) \
34  (__VA_ARGS__)
35 #define GET_CUDA_DRIVER_TRY_MACRO(_1, _2, NAME, ...) NAME
36 #define CUDA_DRIVER_TRY_2(_call, _exception_type) \
37  do { \
38  CUresult const error = (_call); \
39  if (error == CUDA_ERROR_STUB_LIBRARY) { \
40  throw(_exception_type){std::string{"CUDA error at: "} + __FILE__ + ":" + \
41  KVIKIO_STRINGIFY(__LINE__) + \
42  ": CUDA_ERROR_STUB_LIBRARY(" \
43  "The CUDA driver loaded is a stub library)"}; \
44  } \
45  if (error != CUDA_SUCCESS) { \
46  const char* err_name = nullptr; \
47  const char* err_str = nullptr; \
48  CUresult err_name_status = cudaAPI::instance().GetErrorName(error, &err_name); \
49  CUresult err_str_status = cudaAPI::instance().GetErrorString(error, &err_str); \
50  if (err_name_status == CUDA_ERROR_INVALID_VALUE) { err_name = "unknown"; } \
51  if (err_str_status == CUDA_ERROR_INVALID_VALUE) { err_str = "unknown"; } \
52  throw(_exception_type){std::string{"CUDA error at: "} + __FILE__ + ":" + \
53  KVIKIO_STRINGIFY(__LINE__) + ": " + std::string(err_name) + "(" + \
54  std::string(err_str) + ")"}; \
55  } \
56  } while (0)
57 #define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, kvikio::CUfileException)
58 #endif
59 
60 #ifndef CUFILE_TRY
61 #define CUFILE_TRY(...) \
62  GET_CUFILE_TRY_MACRO(__VA_ARGS__, CUFILE_TRY_2, CUFILE_TRY_1) \
63  (__VA_ARGS__)
64 #define GET_CUFILE_TRY_MACRO(_1, _2, NAME, ...) NAME
65 #define CUFILE_TRY_2(_call, _exception_type) \
66  do { \
67  CUfileError_t const error = (_call); \
68  if (error.err != CU_FILE_SUCCESS) { \
69  if (error.err == CU_FILE_CUDA_DRIVER_ERROR) { \
70  CUresult const cuda_error = error.cu_err; \
71  CUDA_DRIVER_TRY(cuda_error); \
72  } \
73  throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
74  KVIKIO_STRINGIFY(__LINE__) + ": " + \
75  cufileop_status_error((CUfileOpError)std::abs(error.err))}; \
76  } \
77  } while (0)
78 #define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
79 #endif
80 
81 #ifndef CUFILE_CHECK_BYTES_DONE
82 #define CUFILE_CHECK_BYTES_DONE(...) \
83  GET_CUFILE_CHECK_BYTES_DONE_MACRO( \
84  __VA_ARGS__, CUFILE_CHECK_BYTES_DONE_2, CUFILE_CHECK_BYTES_DONE_1) \
85  (__VA_ARGS__)
86 #define GET_CUFILE_CHECK_BYTES_DONE_MACRO(_1, _2, NAME, ...) NAME
87 #define CUFILE_CHECK_BYTES_DONE_2(_nbytes_done, _exception_type) \
88  do { \
89  auto const _nbytes = (_nbytes_done); \
90  if (_nbytes < 0) { \
91  auto const err = std::abs(_nbytes); \
92  auto const msg = (err > CUFILEOP_BASE_ERR) \
93  ? std::string(cufileop_status_error((CUfileOpError)err)) \
94  : std::string(std::strerror(err)); \
95  throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
96  KVIKIO_STRINGIFY(__LINE__) + ": " + msg}; \
97  } \
98  } while (0)
99 #define CUFILE_CHECK_BYTES_DONE_1(_call) CUFILE_CHECK_BYTES_DONE_2(_call, kvikio::CUfileException)
100 #endif
101 
102 } // namespace kvikio