20 #include <system_error>
22 #include <kvikio/shim/cuda.hpp>
23 #include <kvikio/shim/cufile_h_wrapper.hpp>
28 using std::runtime_error::runtime_error;
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) \
35 #define GET_CUDA_DRIVER_TRY_MACRO(_1, _2, NAME, ...) NAME
36 #define CUDA_DRIVER_TRY_2(_call, _exception_type) \
38 kvikio::detail::cuda_driver_try_2<_exception_type>(_call, __LINE__, __FILE__); \
40 #define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, kvikio::CUfileException)
44 #define CUFILE_TRY(...) \
45 GET_CUFILE_TRY_MACRO(__VA_ARGS__, CUFILE_TRY_2, CUFILE_TRY_1) \
47 #define GET_CUFILE_TRY_MACRO(_1, _2, NAME, ...) NAME
48 #define CUFILE_TRY_2(_call, _exception_type) \
50 kvikio::detail::cufile_try_2<_exception_type>(_call, __LINE__, __FILE__); \
52 #define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
55 #ifndef CUFILE_CHECK_BYTES_DONE
56 #define CUFILE_CHECK_BYTES_DONE(...) \
57 GET_CUFILE_CHECK_BYTES_DONE_MACRO( \
58 __VA_ARGS__, CUFILE_CHECK_BYTES_DONE_2, CUFILE_CHECK_BYTES_DONE_1) \
60 #define GET_CUFILE_CHECK_BYTES_DONE_MACRO(_1, _2, NAME, ...) NAME
61 #define CUFILE_CHECK_BYTES_DONE_2(_nbytes_done, _exception_type) \
63 kvikio::detail::cufile_check_bytes_done_2<_exception_type>(_nbytes_done, __LINE__, __FILE__); \
65 #define CUFILE_CHECK_BYTES_DONE_1(_call) CUFILE_CHECK_BYTES_DONE_2(_call, kvikio::CUfileException)
69 template <
typename Exception>
70 void cuda_driver_try_2(CUresult error,
int line_number,
const char* filename)
72 if (error == CUDA_ERROR_STUB_LIBRARY) {
73 throw Exception{std::string{
"CUDA error at: "} + std::string(filename) +
":" +
74 KVIKIO_STRINGIFY(line_number) +
75 ": CUDA_ERROR_STUB_LIBRARY("
76 "The CUDA driver loaded is a stub library)"};
78 if (error != CUDA_SUCCESS) {
79 const char* err_name =
nullptr;
80 const char* err_str =
nullptr;
81 CUresult err_name_status = cudaAPI::instance().GetErrorName(error, &err_name);
82 CUresult err_str_status = cudaAPI::instance().GetErrorString(error, &err_str);
83 if (err_name_status == CUDA_ERROR_INVALID_VALUE) { err_name =
"unknown"; }
84 if (err_str_status == CUDA_ERROR_INVALID_VALUE) { err_str =
"unknown"; }
85 throw Exception{std::string{
"CUDA error at: "} + filename +
":" +
86 KVIKIO_STRINGIFY(line_number) +
": " + std::string(err_name) +
"(" +
87 std::string(err_str) +
")"};
91 template <
typename Exception>
92 void cufile_try_2(
CUfileError_t error,
int line_number,
const char* filename)
94 if (error.err != CU_FILE_SUCCESS) {
95 if (error.err == CU_FILE_CUDA_DRIVER_ERROR) {
96 CUresult
const cuda_error = error.cu_err;
97 CUDA_DRIVER_TRY(cuda_error);
99 throw Exception{std::string{
"cuFile error at: "} + filename +
":" +
100 KVIKIO_STRINGIFY(line_number) +
": " +
101 cufileop_status_error((CUfileOpError)std::abs(error.err))};
105 template <
typename Exception>
106 void cufile_check_bytes_done_2(ssize_t nbytes_done,
int line_number,
const char* filename)
108 if (nbytes_done < 0) {
109 auto const err = std::abs(nbytes_done);
110 auto const msg = (err > CUFILEOP_BASE_ERR)
111 ? std::string(cufileop_status_error((CUfileOpError)err))
112 : std::string(std::strerror(err));
113 throw Exception{std::string{
"cuFile error at: "} + filename +
":" +
114 KVIKIO_STRINGIFY(line_number) +
": " + msg};