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)
59 #ifdef KVIKIO_CUFILE_FOUND
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(error.err)}; \
78 #define CUFILE_TRY_1(_call) CUFILE_TRY_2(_call, kvikio::CUfileException)
82 #ifndef CUFILE_CHECK_STREAM_IO
83 #define CUFILE_CHECK_STREAM_IO(...) \
84 GET_CUFILE_CHECK_STREAM_IO_MACRO( \
85 __VA_ARGS__, CUFILE_CHECK_STREAM_IO_2, CUFILE_CHECK_STREAM_IO_1) \
87 #define GET_CUFILE_CHECK_STREAM_IO_MACRO(_1, _2, NAME, ...) NAME
88 #ifdef KVIKIO_CUFILE_FOUND
89 #define CUFILE_CHECK_STREAM_IO_2(_nbytes_done, _exception_type) \
91 auto const _nbytes = *(_nbytes_done); \
93 throw(_exception_type){std::string{"cuFile error at: "} + __FILE__ + ":" + \
94 KVIKIO_STRINGIFY(__LINE__) + ": " + std::to_string(_nbytes)}; \
99 #define CUFILE_CHECK_STREAM_IO_2(_nbytes_done, _exception_type) \
103 #define CUFILE_CHECK_STREAM_IO_1(_call) CUFILE_CHECK_STREAM_IO_2(_call, kvikio::CUfileException)