Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, NVIDIA CORPORATION.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cuproj/assert.cuh>
20
21#include <cuda_runtime_api.h>
22
23#include <stdexcept>
24#include <string>
25
26namespace cuproj {
27
40struct logic_error : public std::logic_error {
41 logic_error(char const* const message) : std::logic_error(message) {}
42 logic_error(std::string const& message) : std::logic_error(message) {}
43};
44
48struct cuda_error : public std::runtime_error {
49 cuda_error(std::string const& message) : std::runtime_error(message) {}
50};
51
56} // namespace cuproj
57
58#define CUPROJ_STRINGIFY_DETAIL(x) #x
59#define CUPROJ_STRINGIFY(x) CUPROJ_STRINGIFY_DETAIL(x)
60
76#define CUPROJ_EXPECTS(cond, reason) \
77 (!!(cond)) ? static_cast<void>(0) \
78 : throw cuproj::logic_error("cuProj failure at: " __FILE__ \
79 ":" CUPROJ_STRINGIFY(__LINE__) ": " reason)
80
101#ifndef __CUDA_ARCH__
102#define CUPROJ_HOST_DEVICE_EXPECTS(cond, reason) CUPROJ_EXPECTS(cond, reason)
103#else
104#define CUPROJ_HOST_DEVICE_EXPECTS(cond, reason) cuproj_assert(cond&& reason)
105#endif
106
120#define CUPROJ_FAIL(reason) \
121 throw cuproj::logic_error("cuProj failure at: " __FILE__ ":" CUPROJ_STRINGIFY( \
122 __LINE__) ":" \
123 " " reason)
124
125namespace cuproj {
126namespace detail {
127
128inline void throw_cuda_error(cudaError_t error, const char* file, unsigned int line)
129{
130 throw cuproj::cuda_error(std::string{"CUDA error encountered at: " + std::string{file} + ":" +
131 std::to_string(line) + ": " + std::to_string(error) + " " +
132 cudaGetErrorName(error) + " " + cudaGetErrorString(error)});
133}
134
135} // namespace detail
136} // namespace cuproj
137
145#define CUPROJ_CUDA_TRY(call) \
146 do { \
147 cudaError_t const status = (call); \
148 if (cudaSuccess != status) { \
149 cudaGetLastError(); \
150 cuproj::detail::throw_cuda_error(status, __FILE__, __LINE__); \
151 } \
152 } while (0);
153
167#ifndef NDEBUG
168#define CUPROJ_CHECK_CUDA(stream) \
169 do { \
170 CUPROJ_CUDA_TRY(cudaStreamSynchronize(stream)); \
171 CUPROJ_CUDA_TRY(cudaPeekAtLastError()); \
172 } while (0);
173#else
174#define CUPROJ_CHECK_CUDA(stream) CUPROJ_CUDA_TRY(cudaPeekAtLastError());
175#endif
Exception thrown when a CUDA error is encountered.
Definition error.hpp:48
Exception thrown when logical precondition is violated.
Definition error.hpp:40