Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
exception_helpers.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_EXCEPTION_HELPERS_HPP_
34 #define GKO_CORE_EXCEPTION_HELPERS_HPP_
35 
36 
37 #include <ginkgo/core/base/dim.hpp>
38 #include <ginkgo/core/base/exception.hpp>
39 #include <ginkgo/core/base/name_demangling.hpp>
40 
41 
42 #include <typeinfo>
43 
44 
45 namespace gko {
46 
47 
55 #define GKO_QUOTE(...) #__VA_ARGS__
56 
57 
64 #define GKO_NOT_IMPLEMENTED \
65  { \
66  throw ::gko::NotImplemented(__FILE__, __LINE__, __func__); \
67  } \
68  static_assert(true, \
69  "This assert is used to counter the false positive extra " \
70  "semi-colon warnings")
71 
72 
81 #define GKO_NOT_COMPILED(_module) \
82  { \
83  throw ::gko::NotCompiled(__FILE__, __LINE__, __func__, \
84  GKO_QUOTE(_module)); \
85  } \
86  static_assert(true, \
87  "This assert is used to counter the false positive extra " \
88  "semi-colon warnings")
89 
90 
100 #define GKO_NOT_SUPPORTED(_obj) \
101  ::gko::NotSupported(__FILE__, __LINE__, __func__, \
102  ::gko::name_demangling::get_type_name(typeid(_obj)))
103 
104 
105 namespace detail {
106 
107 
108 template <typename T>
109 inline dim<2> get_size(const T &op)
110 {
111  return op->get_size();
112 }
113 
114 inline dim<2> get_size(const dim<2> &size) { return size; }
115 
116 
117 } // namespace detail
118 
119 
126 #define GKO_ASSERT_IS_SQUARE_MATRIX(_op1) \
127  if (::gko::detail::get_size(_op1)[0] != \
128  ::gko::detail::get_size(_op1)[1]) { \
129  throw ::gko::DimensionMismatch( \
130  __FILE__, __LINE__, __func__, #_op1, \
131  ::gko::detail::get_size(_op1)[0], \
132  ::gko::detail::get_size(_op1)[1], #_op1, \
133  ::gko::detail::get_size(_op1)[0], \
134  ::gko::detail::get_size(_op1)[1], "expected square matrix"); \
135  }
136 
137 
143 #define GKO_ASSERT_CONFORMANT(_op1, _op2) \
144  if (::gko::detail::get_size(_op1)[1] != \
145  ::gko::detail::get_size(_op2)[0]) { \
146  throw ::gko::DimensionMismatch(__FILE__, __LINE__, __func__, #_op1, \
147  ::gko::detail::get_size(_op1)[0], \
148  ::gko::detail::get_size(_op1)[1], \
149  #_op2, \
150  ::gko::detail::get_size(_op2)[0], \
151  ::gko::detail::get_size(_op2)[1], \
152  "expected matching inner dimensions"); \
153  }
154 
155 
161 #define GKO_ASSERT_EQUAL_ROWS(_op1, _op2) \
162  if (::gko::detail::get_size(_op1)[0] != \
163  ::gko::detail::get_size(_op2)[0]) { \
164  throw ::gko::DimensionMismatch( \
165  __FILE__, __LINE__, __func__, #_op1, \
166  ::gko::detail::get_size(_op1)[0], \
167  ::gko::detail::get_size(_op1)[1], #_op2, \
168  ::gko::detail::get_size(_op2)[0], \
169  ::gko::detail::get_size(_op2)[1], "expected matching row length"); \
170  }
171 
172 
179 #define GKO_ASSERT_EQUAL_COLS(_op1, _op2) \
180  if (::gko::detail::get_size(_op1)[1] != \
181  ::gko::detail::get_size(_op2)[1]) { \
182  throw ::gko::DimensionMismatch(__FILE__, __LINE__, __func__, #_op1, \
183  ::gko::detail::get_size(_op1)[0], \
184  ::gko::detail::get_size(_op1)[1], \
185  #_op2, \
186  ::gko::detail::get_size(_op2)[0], \
187  ::gko::detail::get_size(_op2)[1], \
188  "expected matching column length"); \
189  }
190 
191 
198 #define GKO_ASSERT_EQUAL_DIMENSIONS(_op1, _op2) \
199  if (::gko::detail::get_size(_op1) != ::gko::detail::get_size(_op2)) { \
200  throw ::gko::DimensionMismatch( \
201  __FILE__, __LINE__, __func__, #_op1, \
202  ::gko::detail::get_size(_op1)[0], \
203  ::gko::detail::get_size(_op1)[1], #_op2, \
204  ::gko::detail::get_size(_op2)[0], \
205  ::gko::detail::get_size(_op2)[1], "expected equal dimensions"); \
206  }
207 
208 
214 #define GKO_CUDA_ERROR(_errcode) \
215  ::gko::CudaError(__FILE__, __LINE__, __func__, _errcode)
216 
217 
223 #define GKO_CUBLAS_ERROR(_errcode) \
224  ::gko::CublasError(__FILE__, __LINE__, __func__, _errcode)
225 
226 
232 #define GKO_CUSPARSE_ERROR(_errcode) \
233  ::gko::CusparseError(__FILE__, __LINE__, __func__, _errcode)
234 
235 
241 #define GKO_ASSERT_NO_CUDA_ERRORS(_cuda_call) \
242  do { \
243  auto _errcode = _cuda_call; \
244  if (_errcode != cudaSuccess) { \
245  throw GKO_CUDA_ERROR(_errcode); \
246  } \
247  } while (false)
248 
249 
255 #define GKO_ASSERT_NO_CUBLAS_ERRORS(_cublas_call) \
256  do { \
257  auto _errcode = _cublas_call; \
258  if (_errcode != CUBLAS_STATUS_SUCCESS) { \
259  throw GKO_CUBLAS_ERROR(_errcode); \
260  } \
261  } while (false)
262 
263 
269 #define GKO_ASSERT_NO_CUSPARSE_ERRORS(_cusparse_call) \
270  do { \
271  auto _errcode = _cusparse_call; \
272  if (_errcode != CUSPARSE_STATUS_SUCCESS) { \
273  throw GKO_CUSPARSE_ERROR(_errcode); \
274  } \
275  } while (false)
276 
277 
278 namespace detail {
279 
280 
281 template <typename T>
282 inline T ensure_allocated_impl(T ptr, const std::string &file, int line,
283  const std::string &dev, size_type size)
284 {
285  if (ptr == nullptr) {
286  throw AllocationError(file, line, dev, size);
287  }
288  return ptr;
289 }
290 
291 
292 } // namespace detail
293 
294 
309 #define GKO_ENSURE_ALLOCATED(_ptr, _dev, _size) \
310  ::gko::detail::ensure_allocated_impl(_ptr, __FILE__, __LINE__, _dev, _size)
311 
312 
321 #define GKO_ENSURE_IN_BOUNDS(_index, _bound) \
322  if (_index >= _bound) { \
323  throw ::gko::OutOfBoundsError(__FILE__, __LINE__, _index, _bound); \
324  } \
325  static_assert(true, \
326  "This assert is used to counter the false positive extra " \
327  "semi-colon warnings")
328 
329 
340 #define GKO_STREAM_ERROR(_message) \
341  ::gko::StreamError(__FILE__, __LINE__, __func__, _message)
342 
343 
350 #define GKO_KERNEL_NOT_FOUND \
351  { \
352  throw ::gko::KernelNotFound(__FILE__, __LINE__, __func__); \
353  } \
354  static_assert(true, \
355  "This assert is used to counter the false positive extra " \
356  "semi-colon warnings")
357 
358 
359 } // namespace gko
360 
361 
362 #endif // GKO_CORE_EXCEPTION_HELPERS_HPP_
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
The Ginkgo namespace.
Definition: abstract_factory.hpp:45