19 #include <system_error>
21 #include <kvikio/shim/cuda.hpp>
22 #include <kvikio/shim/cufile_h_wrapper.hpp>
27 using std::runtime_error::runtime_error;
30 #ifndef CUDA_DRIVER_TRY
31 #define CUDA_DRIVER_TRY(...) \
32 GET_CUDA_DRIVER_TRY_MACRO(__VA_ARGS__, CUDA_DRIVER_TRY_2, CUDA_DRIVER_TRY_1) \
34 #define GET_CUDA_DRIVER_TRY_MACRO(_1, _2, NAME, ...) NAME
35 #define CUDA_DRIVER_TRY_2(_call, _exception_type) \
37 CUresult const error = (_call); \
38 if (error == CUDA_ERROR_STUB_LIBRARY) { \
39 throw(_exception_type){std::string{"CUDA error at: "} + __FILE__ + ":" + \
40 KVIKIO_STRINGIFY(__LINE__) + \
41 ": CUDA_ERROR_STUB_LIBRARY(" \
42 "The CUDA driver loaded is a stub library)"}; \
44 if (error != CUDA_SUCCESS) { \
45 const char* err_name = nullptr; \
46 const char* err_str = nullptr; \
47 CUresult err_name_status = cudaAPI::instance().GetErrorName(error, &err_name); \
48 CUresult err_str_status = cudaAPI::instance().GetErrorString(error, &err_str); \
49 if (err_name_status == CUDA_ERROR_INVALID_VALUE) { err_name = "unknown"; } \
50 if (err_str_status == CUDA_ERROR_INVALID_VALUE) { err_str = "unknown"; } \
51 throw(_exception_type){std::string{"CUDA error at: "} + __FILE__ + ":" + \
52 KVIKIO_STRINGIFY(__LINE__) + ": " + std::string(err_name) + "(" + \
53 std::string(err_str) + ")"}; \
56 #define CUDA_DRIVER_TRY_1(_call) CUDA_DRIVER_TRY_2(_call, kvikio::CUfileException)
60 #define CUFILE_TRY(...) \
61 GET_CUFILE_TRY_MACRO(__VA_ARGS__, CUFILE_TRY_2, CUFILE_TRY_1) \
63 #define GET_CUFILE_TRY_MACRO(_1, _2, NAME, ...) NAME
64 #define CUFILE_TRY_2(_call, _exception_type) \
66 CUfileError_t const error = (_call); \
67 if (error.err != CU_FILE_SUCCESS) { \
68 if (error.err == CU_FILE_CUDA_DRIVER_ERROR) { \
69 CUresult const cuda_error = error.cu_err; \
70 CUDA_DRIVER_TRY(cuda_error); \
72 throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
73 KVIKIO_STRINGIFY(__LINE__) + ": " + \
74 cufileop_status_error(error.err)}; \
77 #define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
80 #ifndef CUFILE_CHECK_STREAM_IO
81 #define CUFILE_CHECK_STREAM_IO(...) \
82 GET_CUFILE_CHECK_STREAM_IO_MACRO( \
83 __VA_ARGS__, CUFILE_CHECK_STREAM_IO_2, CUFILE_CHECK_STREAM_IO_1) \
85 #define GET_CUFILE_CHECK_STREAM_IO_MACRO(_1, _2, NAME, ...) NAME
86 #define CUFILE_CHECK_STREAM_IO_2(_nbytes_done, _exception_type) \
88 auto const _nbytes = *(_nbytes_done); \
90 throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
91 KVIKIO_STRINGIFY(__LINE__) + ": " + std::to_string(_nbytes)}; \
94 #define CUFILE_CHECK_STREAM_IO_1(_call) CUFILE_CHECK_STREAM_IO_2(_call, kvikio::CUfileException)