17 #ifndef CUDA_API_WRAPPERS_ERROR_HPP_ 18 #define CUDA_API_WRAPPERS_ERROR_HPP_ 21 #include <cuda_runtime_api.h> 23 #include <type_traits> 36 enum named_t : ::std::underlying_type<status_t>::type {
37 success = CUDA_SUCCESS,
39 not_yet_initialized = CUDA_ERROR_NOT_INITIALIZED,
40 already_deinitialized = CUDA_ERROR_DEINITIALIZED,
41 profiler_disabled = CUDA_ERROR_PROFILER_DISABLED,
42 #if CUDA_VERSION >= 10100 43 profiler_not_initialized = CUDA_ERROR_PROFILER_NOT_INITIALIZED,
45 profiler_already_started = CUDA_ERROR_PROFILER_ALREADY_STARTED,
46 profiler_already_stopped = CUDA_ERROR_PROFILER_ALREADY_STOPPED,
47 #if CUDA_VERSION >= 11100 48 stub_library = CUDA_ERROR_STUB_LIBRARY,
49 device_not_licensed = CUDA_ERROR_DEVICE_NOT_LICENSED,
51 prior_launch_failure = cudaErrorPriorLaunchFailure,
52 launch_timeout = CUDA_ERROR_LAUNCH_TIMEOUT,
53 launch_out_of_resources = CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES,
54 kernel_launch_incompatible_texturing_mode = CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING,
55 invalid_kernel_function = cudaErrorInvalidDeviceFunction,
56 invalid_configuration = cudaErrorInvalidConfiguration,
57 invalid_device = CUDA_ERROR_INVALID_DEVICE,
58 invalid_value = CUDA_ERROR_INVALID_VALUE,
59 invalid_pitch_value = cudaErrorInvalidPitchValue,
60 invalid_symbol = cudaErrorInvalidSymbol,
61 map_buffer_object_failed = CUDA_ERROR_MAP_FAILED,
62 unmap_buffer_object_failed = CUDA_ERROR_UNMAP_FAILED,
63 array_still_mapped = CUDA_ERROR_ARRAY_IS_MAPPED,
64 resource_already_mapped = CUDA_ERROR_ALREADY_MAPPED,
65 resource_already_acquired = CUDA_ERROR_ALREADY_ACQUIRED,
66 resource_not_mapped = CUDA_ERROR_NOT_MAPPED,
67 not_mapped_as_pointer = CUDA_ERROR_NOT_MAPPED_AS_POINTER,
68 not_mapped_as_array = CUDA_ERROR_NOT_MAPPED_AS_ARRAY,
69 invalid_host_pointer = cudaErrorInvalidHostPointer,
70 invalid_device_pointer = cudaErrorInvalidDevicePointer,
71 invalid_texture = cudaErrorInvalidTexture,
72 invalid_texture_binding = cudaErrorInvalidTextureBinding,
73 invalid_channel_descriptor = cudaErrorInvalidChannelDescriptor,
74 invalid_memcpy_direction = cudaErrorInvalidMemcpyDirection,
75 address_of_constant = cudaErrorAddressOfConstant,
76 texture_fetch_failed = cudaErrorTextureFetchFailed,
77 texture_not_bound = cudaErrorTextureNotBound,
78 synchronization_error = cudaErrorSynchronizationError,
79 invalid_filter_setting = cudaErrorInvalidFilterSetting,
80 invalid_norm_setting = cudaErrorInvalidNormSetting,
81 mixed_device_execution = cudaErrorMixedDeviceExecution,
82 unknown = CUDA_ERROR_UNKNOWN,
83 not_yet_implemented = cudaErrorNotYetImplemented,
84 memory_value_too_large = cudaErrorMemoryValueTooLarge,
85 invalid_resource_handle = CUDA_ERROR_INVALID_HANDLE,
86 #if CUDA_VERSION >= 10000 87 resource_not_in_valid_state = CUDA_ERROR_ILLEGAL_STATE,
89 async_operations_not_yet_completed = CUDA_ERROR_NOT_READY,
90 insufficient_driver = cudaErrorInsufficientDriver,
91 set_on_active_process = cudaErrorSetOnActiveProcess,
92 invalid_surface = cudaErrorInvalidSurface,
93 symbol_not_found = CUDA_ERROR_NOT_FOUND,
94 no_device = CUDA_ERROR_NO_DEVICE,
95 ecc_uncorrectable = CUDA_ERROR_ECC_UNCORRECTABLE,
96 shared_object_symbol_not_found = CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND,
97 invalid_source = CUDA_ERROR_INVALID_SOURCE,
98 file_not_found = CUDA_ERROR_FILE_NOT_FOUND,
99 shared_object_init_failed = CUDA_ERROR_SHARED_OBJECT_INIT_FAILED,
100 jit_compiler_not_found = CUDA_ERROR_JIT_COMPILER_NOT_FOUND,
101 #if CUDA_VERSION >= 11100 102 unsupported_ptx_version = CUDA_ERROR_UNSUPPORTED_PTX_VERSION,
104 #if CUDA_VERSION >= 11200 105 jit_compilation_disabled = CUDA_ERROR_JIT_COMPILATION_DISABLED,
107 #if CUDA_VERSION >= 11400 108 unsupported_exec_affinity = CUDA_ERROR_UNSUPPORTED_EXEC_AFFINITY,
110 unsupported_limit = CUDA_ERROR_UNSUPPORTED_LIMIT,
111 duplicate_variable_name = cudaErrorDuplicateVariableName,
112 duplicate_texture_name = cudaErrorDuplicateTextureName,
113 duplicate_surface_name = cudaErrorDuplicateSurfaceName,
114 devices_unavailable = cudaErrorDevicesUnavailable,
115 invalid_kernel_image = CUDA_ERROR_INVALID_IMAGE,
116 no_kernel_image_for_device = CUDA_ERROR_NO_BINARY_FOR_GPU,
117 incompatible_driver_context = cudaErrorIncompatibleDriverContext,
118 missing_configuration = cudaErrorMissingConfiguration,
119 invalid_context = CUDA_ERROR_INVALID_CONTEXT,
120 context_already_current = CUDA_ERROR_CONTEXT_ALREADY_CURRENT,
121 context_already_in_use = CUDA_ERROR_CONTEXT_ALREADY_IN_USE,
122 peer_access_already_enabled = CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED,
123 peer_access_not_enabled = CUDA_ERROR_PEER_ACCESS_NOT_ENABLED,
124 device_already_in_use = cudaErrorDeviceAlreadyInUse,
125 primary_context_already_active = CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE,
126 context_is_destroyed = CUDA_ERROR_CONTEXT_IS_DESTROYED,
127 primary_context_is_uninitialized = CUDA_ERROR_CONTEXT_IS_DESTROYED,
128 #if CUDA_VERSION >= 10200 129 device_uninitialized = cudaErrorDeviceUninitialized,
131 assert = CUDA_ERROR_ASSERT,
132 too_many_peers = CUDA_ERROR_TOO_MANY_PEERS,
133 host_memory_already_registered = CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED,
134 host_memory_not_registered = CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED,
135 operating_system = CUDA_ERROR_OPERATING_SYSTEM,
136 peer_access_unsupported = CUDA_ERROR_PEER_ACCESS_UNSUPPORTED,
137 launch_max_depth_exceeded = cudaErrorLaunchMaxDepthExceeded,
138 launch_file_scoped_tex = cudaErrorLaunchFileScopedTex,
139 launch_file_scoped_surf = cudaErrorLaunchFileScopedSurf,
140 sync_depth_exceeded = cudaErrorSyncDepthExceeded,
141 launch_pending_count_exceeded = cudaErrorLaunchPendingCountExceeded,
142 invalid_device_function = cudaErrorInvalidDeviceFunction,
143 not_permitted = CUDA_ERROR_NOT_PERMITTED,
144 not_supported = CUDA_ERROR_NOT_SUPPORTED,
145 hardware_stack_error = CUDA_ERROR_HARDWARE_STACK_ERROR,
146 illegal_instruction = CUDA_ERROR_ILLEGAL_INSTRUCTION,
147 misaligned_address = CUDA_ERROR_MISALIGNED_ADDRESS,
148 exception_during_kernel_execution = CUDA_ERROR_LAUNCH_FAILED,
149 cooperative_launch_too_large = CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE,
150 invalid_address_space = CUDA_ERROR_INVALID_ADDRESS_SPACE,
151 invalid_pc = CUDA_ERROR_INVALID_PC,
152 illegal_address = CUDA_ERROR_ILLEGAL_ADDRESS,
153 invalid_ptx = CUDA_ERROR_INVALID_PTX,
154 invalid_graphics_context = CUDA_ERROR_INVALID_GRAPHICS_CONTEXT,
155 nvlink_uncorrectable = CUDA_ERROR_NVLINK_UNCORRECTABLE,
156 startup_failure = cudaErrorStartupFailure,
157 api_failure_base = cudaErrorApiFailureBase,
158 #if CUDA_VERSION >= 10000 159 system_not_ready = CUDA_ERROR_SYSTEM_NOT_READY,
161 #if CUDA_VERSION >= 10100 162 system_driver_mismatch = CUDA_ERROR_SYSTEM_DRIVER_MISMATCH,
163 not_supported_on_device = CUDA_ERROR_COMPAT_NOT_SUPPORTED_ON_DEVICE,
165 #if CUDA_VERSION >= 10000 166 stream_capture_unsupported = CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED,
167 stream_capture_invalidated = CUDA_ERROR_STREAM_CAPTURE_INVALIDATED,
168 stream_capture_merge = CUDA_ERROR_STREAM_CAPTURE_MERGE,
169 stream_capture_unmatched = CUDA_ERROR_STREAM_CAPTURE_UNMATCHED,
170 stream_capture_unjoined = CUDA_ERROR_STREAM_CAPTURE_UNJOINED,
171 stream_capture_isolation = CUDA_ERROR_STREAM_CAPTURE_ISOLATION,
172 stream_capture_disallowed_implicit_dependency = CUDA_ERROR_STREAM_CAPTURE_IMPLICIT,
173 not_permitted_on_captured_event = CUDA_ERROR_CAPTURED_EVENT,
175 #if CUDA_VERSION >= 10100 176 stream_capture_wrong_thread = CUDA_ERROR_STREAM_CAPTURE_WRONG_THREAD,
178 #if CUDA_VERSION >= 10200 179 timeout_lapsed = CUDA_ERROR_TIMEOUT,
180 graph_update_would_violate_constraints = CUDA_ERROR_GRAPH_EXEC_UPDATE_FAILURE,
182 #if CUDA_VERSION >= 11400 183 mps_connection_failed = CUDA_ERROR_MPS_CONNECTION_FAILED,
184 mps_rpc_failure = CUDA_ERROR_MPS_RPC_FAILURE,
185 mps_server_not_ready = CUDA_ERROR_MPS_SERVER_NOT_READY,
186 mps_max_clients_reached = CUDA_ERROR_MPS_MAX_CLIENTS_REACHED,
187 mps_max_connections_reached = CUDA_ERROR_MPS_MAX_CONNECTIONS_REACHED,
188 async_error_in_external_device = CUDA_ERROR_EXTERNAL_DEVICE,
194 constexpr
inline bool operator!=(
const status_t& lhs,
const named_t& rhs) noexcept {
return lhs !=
static_cast<status_t>(rhs); }
196 constexpr
inline bool operator!=(
const named_t& lhs,
const status_t& rhs) noexcept {
return static_cast<status_t>(lhs) != rhs; }
204 constexpr
bool is_success(cudaError_t status) {
return static_cast<status_t>(status) == static_cast<status_t>(status::success); }
210 constexpr
bool is_failure(cudaError_t status) {
return is_failure(static_cast<status_t>(status)); }
221 const char* description;
222 auto description_lookup_status = cuGetErrorString(status, &description);
223 return (description_lookup_status == CUDA_SUCCESS) ?
224 description : cudaGetErrorString(static_cast<cudaError_t>(status));
226 inline ::std::string
describe(cudaError_t status) {
return cudaGetErrorString(status); }
231 template <
typename I,
bool UpperCase = false>
232 ::std::string as_hex(I x)
234 static_assert(::std::is_unsigned<I>::value,
"only signed representations are supported");
235 unsigned num_hex_digits = 2*
sizeof(I);
236 if (x == 0)
return "0x0";
238 enum { bits_per_hex_digit = 4 };
239 static const char* digit_characters =
240 UpperCase ?
"0123456789ABCDEF" :
"0123456789abcdef" ;
242 ::std::string result(num_hex_digits,
'0');
243 for (
unsigned digit_index = 0; digit_index < num_hex_digits ; digit_index++)
245 size_t bit_offset = (num_hex_digits - 1 - digit_index) * bits_per_hex_digit;
246 auto hexadecimal_digit = (x >> bit_offset) & 0xF;
247 result[digit_index] = digit_characters[hexadecimal_digit];
249 return "0x0" + result.substr(result.find_first_not_of(
'0'), ::std::string::npos);
254 template <
typename I,
bool UpperCase = false>
255 inline ::std::string ptr_as_hex(
const I* ptr)
257 return as_hex(reinterpret_cast<uintptr_t>(ptr));
275 ::std::runtime_error(
describe(error_code)), code_(error_code)
279 ::std::runtime_error(what_arg +
": " +
describe(error_code)),
290 runtime_error(static_cast<status_t>(error_code), what_arg) { }
292 runtime_error(static_cast<status_t>(error_code), what_arg) { }
296 ::std::runtime_error(::std::move(err)), code_(error_code)
304 return runtime_error(error_code, ::std::runtime_error(::std::move(complete_what_arg)));
316 #define throw_if_error_lazy(status__, ... ) \ 318 const ::cuda::status_t tie_status__ = static_cast<::cuda::status_t>(status__); \ 319 if (::cuda::is_failure(tie_status__)) { \ 320 throw ::cuda::runtime_error(tie_status__, (__VA_ARGS__)); \ 337 if (
is_failure(status)) {
throw runtime_error(status, message); }
340 inline void throw_if_error(cudaError_t status, const ::std::string& message) noexcept(
false)
347 if (
is_failure(status)) {
throw runtime_error(status, message); }
350 inline void throw_if_error(cudaError_t status, ::std::string&& message) noexcept(
false)
376 dont_clear_errors =
false,
377 do_clear_errors =
true 382 namespace outstanding_runtime_error {
391 return static_cast<status_t>(cudaGetLastError());
401 return static_cast<status_t>(cudaPeekAtLastError());
414 namespace outstanding_error {
420 inline status_t get(
bool try_clearing =
false) noexcept(
true)
422 static constexpr
const unsigned dummy_flags{0};
423 auto status = cuInit(dummy_flags);
424 if (not
is_success(status)) {
return status; }
425 return static_cast<status_t>(try_clearing ? cudaGetLastError() : cudaPeekAtLastError());
438 inline void ensure_none(const ::std::string &message) noexcept(
false)
479 return ::std::string(
"device ") + ::std::to_string(device_id);
487 inline ::std::string identify(
handle_t handle)
489 return "context " + cuda::detail_::ptr_as_hex(handle);
494 return identify(handle) +
" on " + device::detail_::identify(device_id);
503 return "current context: " + context::detail_::identify(handle);
507 return "current context: " + context::detail_::identify(handle, device_id);
515 namespace primary_context {
520 return "context " + context::detail_::identify(handle, device_id);
522 inline ::std::string identify(
handle_t handle)
524 return "context " + context::detail_::identify(handle);
532 inline ::std::string identify(
handle_t handle)
534 return "stream " + cuda::detail_::ptr_as_hex(handle);
538 return identify(handle) +
" on " + device::detail_::identify(device_id);
542 return identify(handle) +
" in " + context::detail_::identify(context_handle);
546 return identify(handle) +
" in " + context::detail_::identify(context_handle, device_id);
553 inline ::std::string identify(
handle_t handle)
555 return "event " + cuda::detail_::ptr_as_hex(handle);
559 return identify(handle) +
" on " + device::detail_::identify(device_id);
563 return identify(handle) +
" on " + context::detail_::identify(context_handle);
567 return identify(handle) +
" on " + context::detail_::identify(context_handle, device_id);
575 inline ::std::string identify(
const void* ptr)
577 return "kernel " + cuda::detail_::ptr_as_hex(ptr);
579 inline ::std::string identify(
const void* ptr,
device::id_t device_id)
581 return identify(ptr) +
" on " + device::detail_::identify(device_id);
585 return identify(ptr) +
" in " + context::detail_::identify(context_handle);
589 return identify(ptr) +
" in " + context::detail_::identify(context_handle, device_id);
591 inline ::std::string identify(
handle_t handle)
593 return "kernel at " + cuda::detail_::ptr_as_hex(handle);
597 return identify(handle) +
" in " + context::detail_::identify(context_handle);
601 return identify(handle) +
" on " + device::detail_::identify(device_id);
605 return identify(handle) +
" in " + context::detail_::identify(context_handle, device_id);
614 inline ::std::string identify(
region_t region)
616 return ::std::string(
"memory region at ") + cuda::detail_::ptr_as_hex(region.data())
617 +
" of size " + ::std::to_string(region.size());
626 #endif // CUDA_API_WRAPPERS_ERROR_HPP_ static runtime_error with_message_override(status_t error_code, ::std::string complete_what_arg)
Construct a runtime error which will not produce the default description for the error code...
Definition: error.hpp:302
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
detail_::region_helper< memory::region_t > region_t
A child class of the generic region_t with some managed-memory-specific functionality.
Definition: memory.hpp:1960
void ensure_none() noexcept(false)
Does nothing (except possibly throwing an exception)
Definition: error.hpp:464
CUcontext handle_t
Raw CUDA driver handle for a context; see {context_t}.
Definition: types.hpp:878
Operation was successful; no errors.
Definition: error.hpp:38
constexpr bool is_failure(status_t status)
Determine whether the API call returning the specified status had failed.
Definition: error.hpp:209
CUdevice id_t
Numeric ID of a CUDA device used by the CUDA Runtime API.
Definition: types.hpp:850
void throw_if_error(status_t status, const ::std::string &message) noexcept(false)
Do nothing...
Definition: error.hpp:335
named_t
Aliases for CUDA status codes.
Definition: error.hpp:36
A (base?) class for exceptions raised by CUDA code; these errors are thrown by essentially all CUDA R...
Definition: error.hpp:271
status_t code() const
Obtain the CUDA status code which resulted in this error being thrown.
Definition: error.hpp:308
inline ::std::string describe(status_t status)
Obtain a brief textual explanation for a specified kind of CUDA Runtime API status or error code...
Definition: error.hpp:215
bool operator==(const context_t &lhs, const context_t &rhs) noexcept
Definition: context.hpp:762
CUarray handle_t
Raw CUDA driver handle for arrays (of any dimension)
Definition: array.hpp:34
Fundamental CUDA-related type definitions.
constexpr bool is_success(status_t status)
Determine whether the API call returning the specified status had succeeded.
Definition: error.hpp:203
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:77