Loading...
Searching...
No Matches
error.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020-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 <cuspatial/assert.cuh>
20
21#include <cuda_runtime_api.h>
22
23#include <stdexcept>
24#include <string>
25
26namespace cuspatial {
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 cuspatial
57
58#define CUSPATIAL_STRINGIFY_DETAIL(x) #x
59#define CUSPATIAL_STRINGIFY(x) CUSPATIAL_STRINGIFY_DETAIL(x)
60
76#define CUSPATIAL_EXPECTS(cond, reason) \
77 (!!(cond)) ? static_cast<void>(0) \
78 : throw cuspatial::logic_error("cuSpatial failure at: " __FILE__ \
79 ":" CUSPATIAL_STRINGIFY(__LINE__) ": " reason)
80
100#ifndef __CUDA_ARCH__
101#define CUSPATIAL_HOST_DEVICE_EXPECTS(cond, reason) CUSPATIAL_EXPECTS(cond, reason)
102#else
103#define CUSPATIAL_HOST_DEVICE_EXPECTS(cond, reason) cuspatial_assert(cond&& reason)
104#endif
105
119#define CUSPATIAL_FAIL(reason) \
120 throw cuspatial::logic_error("cuSpatial failure at: " __FILE__ \
121 ":" CUSPATIAL_STRINGIFY(__LINE__) ": " reason)
122
123namespace cuspatial {
124namespace detail {
125
126inline void throw_cuda_error(cudaError_t error, const char* file, unsigned int line)
127{
128 throw cuspatial::cuda_error(std::string{
129 "CUDA error encountered at: " + std::string{file} + ":" + std::to_string(line) + ": " +
130 std::to_string(error) + " " + cudaGetErrorName(error) + " " + cudaGetErrorString(error)});
131}
132
133} // namespace detail
134} // namespace cuspatial
135
143#define CUSPATIAL_CUDA_TRY(call) \
144 do { \
145 cudaError_t const status = (call); \
146 if (cudaSuccess != status) { \
147 cudaGetLastError(); \
148 cuspatial::detail::throw_cuda_error(status, __FILE__, __LINE__); \
149 } \
150 } while (0);
151
165#ifndef NDEBUG
166#define CUSPATIAL_CHECK_CUDA(stream) \
167 do { \
168 CUSPATIAL_CUDA_TRY(cudaStreamSynchronize(stream)); \
169 CUSPATIAL_CUDA_TRY(cudaPeekAtLastError()); \
170 } while (0);
171#else
172#define CUSPATIAL_CHECK_CUDA(stream) CUSPATIAL_CUDA_TRY(cudaPeekAtLastError());
173#endif
Exception thrown when a CUDA error is encountered.
Definition error.hpp:48
Exception thrown when logical precondition is violated.
Definition error.hpp:40