forked from NVIDIA/cuCollections
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.hpp
More file actions
103 lines (95 loc) · 3.77 KB
/
error.hpp
File metadata and controls
103 lines (95 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cuda_runtime_api.h>
#include <stdexcept>
#include <string>
namespace cuco {
/**
* @brief Exception thrown when a CUDA error is encountered.
*
*/
struct cuda_error : public std::runtime_error {
cuda_error(const char* message) : std::runtime_error(message) {}
cuda_error(std::string const& message) : cuda_error{message.c_str()} {}
};
} // namespace cuco
#define STRINGIFY_DETAIL(x) #x
#define CUCO_STRINGIFY(x) STRINGIFY_DETAIL(x)
/**
* @brief Error checking macro for CUDA runtime API functions.
*
* Invokes a CUDA runtime API function call. If the call does not return
* `cudaSuccess`, invokes cudaGetLastError() to clear the error and throws an
* exception detailing the CUDA error that occurred
*
* Defaults to throwing `cuco::cuda_error`, but a custom exception may also be
* specified.
*
* Example:
* ```c++
*
* // Throws `rmm::cuda_error` if `cudaMalloc` fails
* CUCO_CUDA_TRY(cudaMalloc(&p, 100));
*
* // Throws `std::runtime_error` if `cudaMalloc` fails
* CUCO_CUDA_TRY(cudaMalloc(&p, 100), std::runtime_error);
* ```
*
*/
#define CUCO_CUDA_TRY(...) \
GET_CUCO_CUDA_TRY_MACRO(__VA_ARGS__, CUCO_CUDA_TRY_2, CUCO_CUDA_TRY_1) \
(__VA_ARGS__)
#define GET_CUCO_CUDA_TRY_MACRO(_1, _2, NAME, ...) NAME
#define CUCO_CUDA_TRY_2(_call, _exception_type) \
do { \
cudaError_t const error = (_call); \
if (cudaSuccess != error) { \
cudaGetLastError(); \
throw _exception_type{std::string{"CUDA error at: "} + __FILE__ + CUCO_STRINGIFY(__LINE__) + \
": " + cudaGetErrorName(error) + " " + cudaGetErrorString(error)}; \
} \
} while (0);
#define CUCO_CUDA_TRY_1(_call) CUCO_CUDA_TRY_2(_call, cuco::cuda_error)
/**
* @brief Error checking macro for CUDA runtime API that asserts the result is
* equal to `cudaSuccess`.
*
*/
#define CUCO_ASSERT_CUDA_SUCCESS(expr) \
do { \
cudaError_t const status = (expr); \
assert(cudaSuccess == status); \
} while (0)
/**
* @brief Macro for checking runtime conditions that throws an exception when
* a condition is violated.
*
* Example usage:
*
* @code
* CUCO_RUNTIME_EXPECTS(key == value, "Key value mismatch");
* @endcode
*
* @param[in] cond Expression that evaluates to true or false
* @param[in] reason String literal description of the reason that cond is
* expected to be true
* @throw std::runtime_error if the condition evaluates to false.
*/
#define CUCO_RUNTIME_EXPECTS(cond, reason) \
(!!(cond)) ? static_cast<void>(0) \
: throw std::runtime_error("cuco failure at: " __FILE__ \
":" CUCO_STRINGIFY(__LINE__) ": " reason)