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 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)"}; \
45 if (error != CUDA_SUCCESS) { \
46 const char* err_name = nullptr; \
47 const char* err_str = nullptr; \
48 CUresult err_name_status = kvikio::cudaAPI::instance().GetErrorName(error, &err_name); \
49 CUresult err_str_status = kvikio::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) + ")"}; \
57 #define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, kvikio::CUfileException)
61 #define CUFILE_TRY(...) \
62 GET_CUFILE_TRY_MACRO(__VA_ARGS__, CUFILE_TRY_2, CUFILE_TRY_1) \
64 #define GET_CUFILE_TRY_MACRO(_1, _2, NAME, ...) NAME
65 #define CUFILE_TRY_2(_call, _exception_type) \
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); \
73 throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
74 KVIKIO_STRINGIFY(__LINE__) + ": " + \
75 cufileop_status_error((CUfileOpError)std::abs(error.err))}; \
78 #define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
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) \
86 #define GET_CUFILE_CHECK_BYTES_DONE_MACRO(_1, _2, NAME, ...) NAME
87 #define CUFILE_CHECK_BYTES_DONE_2(_nbytes_done, _exception_type) \
89 auto const _nbytes = (_nbytes_done); \
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}; \
99 #define CUFILE_CHECK_BYTES_DONE_1(_call) CUFILE_CHECK_BYTES_DONE_2(_call, kvikio::CUfileException)