Ginkgo  Generated from pipelines/3136675824 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
exception.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_EXCEPTION_HPP_
6 #define GKO_PUBLIC_CORE_BASE_EXCEPTION_HPP_
7 
8 
9 #include <exception>
10 #include <string>
11 #include <typeinfo>
12 
13 #include <ginkgo/core/base/name_demangling.hpp>
14 #include <ginkgo/core/base/precision.hpp>
15 #include <ginkgo/core/base/types.hpp>
16 
17 
18 namespace gko {
19 
20 
59 class Error : public std::runtime_error {
60 public:
68  Error(const std::string& file, int line, const std::string& what)
69  : std::runtime_error(file + ":" + std::to_string(line) + ": " + what)
70  {}
71 };
72 
73 
78 class NotImplemented : public Error {
79 public:
87  NotImplemented(const std::string& file, int line, const std::string& func)
88  : Error(file, line, func + " is not implemented")
89  {}
90 };
91 
92 
97 class NotCompiled : public Error {
98 public:
107  NotCompiled(const std::string& file, int line, const std::string& func,
108  const std::string& module_name)
109  : Error(file, line,
110  "feature " + func + " is part of the " + module_name +
111  " module, which is not compiled on this system")
112  {}
113 };
114 
115 
120 class NotSupported : public Error {
121 public:
131  NotSupported(const std::string& file, int line, const std::string& func,
132  const std::string& obj_type)
133  : Error(file, line,
134  "Operation " + func + " does not support parameters of type " +
135  obj_type)
136  {}
137 };
138 
139 
143 class MpiError : public Error {
144 public:
152  MpiError(const std::string& file, int line, const std::string& func,
153  int64 error_code)
154  : Error(file, line, func + ": " + get_error(error_code))
155  {}
156 
157 private:
158  static std::string get_error(int64 error_code);
159 };
160 
161 
165 class CudaError : public Error {
166 public:
175  CudaError(const std::string& file, int line, const std::string& func,
176  int64 error_code)
177  : Error(file, line, func + ": " + get_error(error_code))
178  {}
179 
180 private:
181  static std::string get_error(int64 error_code);
182 };
183 
184 
188 class CublasError : public Error {
189 public:
198  CublasError(const std::string& file, int line, const std::string& func,
199  int64 error_code)
200  : Error(file, line, func + ": " + get_error(error_code))
201  {}
202 
203 private:
204  static std::string get_error(int64 error_code);
205 };
206 
207 
211 class CurandError : public Error {
212 public:
221  CurandError(const std::string& file, int line, const std::string& func,
222  int64 error_code)
223  : Error(file, line, func + ": " + get_error(error_code))
224  {}
225 
226 private:
227  static std::string get_error(int64 error_code);
228 };
229 
230 
234 class CusparseError : public Error {
235 public:
244  CusparseError(const std::string& file, int line, const std::string& func,
245  int64 error_code)
246  : Error(file, line, func + ": " + get_error(error_code))
247  {}
248 
249 private:
250  static std::string get_error(int64 error_code);
251 };
252 
253 
257 class CufftError : public Error {
258 public:
267  CufftError(const std::string& file, int line, const std::string& func,
268  int64 error_code)
269  : Error(file, line, func + ": " + get_error(error_code))
270  {}
271 
272 private:
273  static std::string get_error(int64 error_code);
274 };
275 
276 
280 class HipError : public Error {
281 public:
290  HipError(const std::string& file, int line, const std::string& func,
291  int64 error_code)
292  : Error(file, line, func + ": " + get_error(error_code))
293  {}
294 
295 private:
296  static std::string get_error(int64 error_code);
297 };
298 
299 
303 class HipblasError : public Error {
304 public:
313  HipblasError(const std::string& file, int line, const std::string& func,
314  int64 error_code)
315  : Error(file, line, func + ": " + get_error(error_code))
316  {}
317 
318 private:
319  static std::string get_error(int64 error_code);
320 };
321 
322 
326 class HiprandError : public Error {
327 public:
336  HiprandError(const std::string& file, int line, const std::string& func,
337  int64 error_code)
338  : Error(file, line, func + ": " + get_error(error_code))
339  {}
340 
341 private:
342  static std::string get_error(int64 error_code);
343 };
344 
345 
350 class HipsparseError : public Error {
351 public:
360  HipsparseError(const std::string& file, int line, const std::string& func,
361  int64 error_code)
362  : Error(file, line, func + ": " + get_error(error_code))
363  {}
364 
365 private:
366  static std::string get_error(int64 error_code);
367 };
368 
369 
373 class HipfftError : public Error {
374 public:
383  HipfftError(const std::string& file, int line, const std::string& func,
384  int64 error_code)
385  : Error(file, line, func + ": " + get_error(error_code))
386  {}
387 
388 private:
389  static std::string get_error(int64 error_code);
390 };
391 
392 
396 class MetisError : public Error {
397 public:
406  MetisError(const std::string& file, int line, const std::string& func,
407  const std::string& error)
408  : Error(file, line, func + ": " + error)
409  {}
410 };
411 
412 
417 class DimensionMismatch : public Error {
418 public:
433  DimensionMismatch(const std::string& file, int line,
434  const std::string& func, const std::string& first_name,
435  size_type first_rows, size_type first_cols,
436  const std::string& second_name, size_type second_rows,
437  size_type second_cols, const std::string& clarification)
438  : Error(file, line,
439  func + ": attempting to combine operators " + first_name +
440  " [" + std::to_string(first_rows) + " x " +
441  std::to_string(first_cols) + "] and " + second_name + " [" +
442  std::to_string(second_rows) + " x " +
443  std::to_string(second_cols) + "]: " + clarification)
444  {}
445 };
446 
447 
452 class BadDimension : public Error {
453 public:
465  BadDimension(const std::string& file, int line, const std::string& func,
466  const std::string& op_name, size_type op_num_rows,
467  size_type op_num_cols, const std::string& clarification)
468  : Error(file, line,
469  func + ": Object " + op_name + " has dimensions [" +
470  std::to_string(op_num_rows) + " x " +
471  std::to_string(op_num_cols) + "]: " + clarification)
472  {}
473 };
474 
475 
482 template <typename IndexType>
483 class BlockSizeError : public Error {
484 public:
491  BlockSizeError(const std::string& file, const int line,
492  const int block_size, const IndexType size)
493  : Error(file, line,
494  "block size = " + std::to_string(block_size) +
495  ", size = " + std::to_string(size))
496  {}
497 };
498 
499 
503 class ValueMismatch : public Error {
504 public:
515  ValueMismatch(const std::string& file, int line, const std::string& func,
516  size_type val1, size_type val2,
517  const std::string& clarification)
518  : Error(file, line,
519  func + ": Value mismatch : " + std::to_string(val1) + " and " +
520  std::to_string(val2) + " : " + clarification)
521  {}
522 };
523 
524 
528 class AllocationError : public Error {
529 public:
538  AllocationError(const std::string& file, int line,
539  const std::string& device, size_type bytes)
540  : Error(file, line,
541  device + ": failed to allocate memory block of " +
542  std::to_string(bytes) + "B")
543  {}
544 };
545 
546 
551 class OutOfBoundsError : public Error {
552 public:
561  OutOfBoundsError(const std::string& file, int line, size_type index,
562  size_type bound)
563  : Error(file, line,
564  "trying to access index " + std::to_string(index) +
565  " in a memory block of " + std::to_string(bound) +
566  " elements")
567  {}
568 };
569 
570 
575 class OverflowError : public Error {
576 public:
582  OverflowError(const std::string& file, const int line,
583  const std::string& index_type)
584  : Error(file, line, "Overflowing " + index_type)
585  {}
586 };
587 
588 
592 class StreamError : public Error {
593 public:
602  StreamError(const std::string& file, int line, const std::string& func,
603  const std::string& message)
604  : Error(file, line, func + ": " + message)
605  {}
606 };
607 
608 
613 class KernelNotFound : public Error {
614 public:
622  KernelNotFound(const std::string& file, int line, const std::string& func)
623  : Error(file, line, func + ": unable to find an eligible kernel")
624  {}
625 };
626 
627 
635 public:
643  UnsupportedMatrixProperty(const std::string& file, const int line,
644  const std::string& msg)
645  : Error(file, line, msg)
646  {}
647 };
648 
649 
651 class InvalidStateError : public Error {
652 public:
661  InvalidStateError(const std::string& file, int line,
662  const std::string& func, const std::string& clarification)
663  : Error(file, line,
664  func + ": Invalid state encountered : " + clarification)
665  {}
666 };
667 
668 
674 class InvalidData : public Error {
675 public:
684  InvalidData(const std::string& file, int line, const std::type_info& type,
685  const std::string& invariant)
686  : Error(file, line,
687  "Invariant violated in " +
688  name_demangling::get_type_name(type) + ": " + invariant)
689  {}
690 };
691 
692 
696 class PrecisionError : public Error {
697 public:
707  PrecisionError(const std::string& file, int line, const std::string& func,
708  precision expected, const std::string& op, precision p)
709  : Error(file, line,
710  func + ": expected precision " + to_string(expected) +
711  " but object " + op + " has precision " + to_string(p) +
712  ".")
713  {}
714 };
715 
716 
717 } // namespace gko
718 
719 
720 #endif // GKO_PUBLIC_CORE_BASE_EXCEPTION_HPP_
gko::CudaError
CudaError is thrown when a CUDA routine throws a non-zero error code.
Definition: exception.hpp:165
gko::ValueMismatch
ValueMismatch is thrown if two values are not equal.
Definition: exception.hpp:503
gko::BlockSizeError
Error that denotes issues between block sizes and matrix dimensions.
Definition: exception.hpp:483
gko::KernelNotFound::KernelNotFound
KernelNotFound(const std::string &file, int line, const std::string &func)
Initializes a KernelNotFound error.
Definition: exception.hpp:622
gko::KernelNotFound
KernelNotFound is thrown if Ginkgo cannot find a kernel which satisfies the criteria imposed by the i...
Definition: exception.hpp:613
gko::HipblasError
HipblasError is thrown when a hipBLAS routine throws a non-zero error code.
Definition: exception.hpp:303
gko::OutOfBoundsError
OutOfBoundsError is thrown if a memory access is detected to be out-of-bounds.
Definition: exception.hpp:551
gko::NotImplemented
NotImplemented is thrown in case an operation has not yet been implemented (but will be implemented i...
Definition: exception.hpp:78
gko::AllocationError::AllocationError
AllocationError(const std::string &file, int line, const std::string &device, size_type bytes)
Initializes an allocation error.
Definition: exception.hpp:538
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::OutOfBoundsError::OutOfBoundsError
OutOfBoundsError(const std::string &file, int line, size_type index, size_type bound)
Initializes an OutOfBoundsError.
Definition: exception.hpp:561
gko::InvalidStateError
Exception thrown if an object is in an invalid state.
Definition: exception.hpp:651
gko::UnsupportedMatrixProperty
Exception throws if a matrix does not have a property required by a numerical method.
Definition: exception.hpp:634
gko::NotCompiled
NotCompiled is thrown when attempting to call an operation which is a part of a module that was not c...
Definition: exception.hpp:97
gko::NotSupported::NotSupported
NotSupported(const std::string &file, int line, const std::string &func, const std::string &obj_type)
Initializes a NotSupported error.
Definition: exception.hpp:131
gko::HiprandError::HiprandError
HiprandError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a hipRAND error.
Definition: exception.hpp:336
gko::precision
precision
An enum to specify the precision of stored data.
Definition: precision.hpp:19
gko::NotSupported
NotSupported is thrown in case it is not possible to perform the requested operation on the given obj...
Definition: exception.hpp:120
gko::InvalidData::InvalidData
InvalidData(const std::string &file, int line, const std::type_info &type, const std::string &invariant)
Initializes an InvalidData error.
Definition: exception.hpp:684
gko::CublasError
CublasError is thrown when a cuBLAS routine throws a non-zero error code.
Definition: exception.hpp:188
gko::MetisError::MetisError
MetisError(const std::string &file, int line, const std::string &func, const std::string &error)
Initializes a METIS error.
Definition: exception.hpp:406
gko::CublasError::CublasError
CublasError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a cuBLAS error.
Definition: exception.hpp:198
gko::MpiError
MpiError is thrown when a MPI routine throws a non-zero error code.
Definition: exception.hpp:143
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::UnsupportedMatrixProperty::UnsupportedMatrixProperty
UnsupportedMatrixProperty(const std::string &file, const int line, const std::string &msg)
Initializes the UnsupportedMatrixProperty error.
Definition: exception.hpp:643
gko::PrecisionError::PrecisionError
PrecisionError(const std::string &file, int line, const std::string &func, precision expected, const std::string &op, precision p)
Definition: exception.hpp:707
gko::CudaError::CudaError
CudaError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a CUDA error.
Definition: exception.hpp:175
gko::StreamError::StreamError
StreamError(const std::string &file, int line, const std::string &func, const std::string &message)
Initializes a file access error.
Definition: exception.hpp:602
gko::AllocationError
AllocationError is thrown if a memory allocation fails.
Definition: exception.hpp:528
gko::CurandError::CurandError
CurandError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a cuRAND error.
Definition: exception.hpp:221
gko::HipfftError::HipfftError
HipfftError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a hipFFT error.
Definition: exception.hpp:383
gko::BadDimension::BadDimension
BadDimension(const std::string &file, int line, const std::string &func, const std::string &op_name, size_type op_num_rows, size_type op_num_cols, const std::string &clarification)
Initializes a bad dimension error.
Definition: exception.hpp:465
gko::MpiError::MpiError
MpiError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a MPI error.
Definition: exception.hpp:152
gko::OverflowError::OverflowError
OverflowError(const std::string &file, const int line, const std::string &index_type)
Definition: exception.hpp:582
gko::InvalidStateError::InvalidStateError
InvalidStateError(const std::string &file, int line, const std::string &func, const std::string &clarification)
Initializes an invalid state error.
Definition: exception.hpp:661
gko::StreamError
StreamError is thrown if accessing a stream failed.
Definition: exception.hpp:592
gko::CufftError::CufftError
CufftError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a cuFFT error.
Definition: exception.hpp:267
gko::DimensionMismatch
DimensionMismatch is thrown if an operation is being applied to LinOps of incompatible size.
Definition: exception.hpp:417
gko::CusparseError::CusparseError
CusparseError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a cuSPARSE error.
Definition: exception.hpp:244
gko::HipError
HipError is thrown when a HIP routine throws a non-zero error code.
Definition: exception.hpp:280
gko::BadDimension
BadDimension is thrown if an operation is being applied to a LinOp with bad dimensions.
Definition: exception.hpp:452
gko::CufftError
CufftError is thrown when a cuFFT routine throws a non-zero error code.
Definition: exception.hpp:257
gko::OverflowError
OverflowError is thrown when an index calculation for storage requirements overflows.
Definition: exception.hpp:575
gko::Error::Error
Error(const std::string &file, int line, const std::string &what)
Initializes an error.
Definition: exception.hpp:68
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:125
gko::HipblasError::HipblasError
HipblasError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a hipBLAS error.
Definition: exception.hpp:313
gko::HipError::HipError
HipError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a HIP error.
Definition: exception.hpp:290
gko::CurandError
CurandError is thrown when a cuRAND routine throws a non-zero error code.
Definition: exception.hpp:211
gko::HiprandError
HiprandError is thrown when a hipRAND routine throws a non-zero error code.
Definition: exception.hpp:326
gko::BlockSizeError::BlockSizeError
BlockSizeError(const std::string &file, const int line, const int block_size, const IndexType size)
Definition: exception.hpp:491
gko::HipsparseError
HipsparseError is thrown when a hipSPARSE routine throws a non-zero error code.
Definition: exception.hpp:350
gko::NotCompiled::NotCompiled
NotCompiled(const std::string &file, int line, const std::string &func, const std::string &module_name)
Initializes a NotCompiled error.
Definition: exception.hpp:107
gko::DimensionMismatch::DimensionMismatch
DimensionMismatch(const std::string &file, int line, const std::string &func, const std::string &first_name, size_type first_rows, size_type first_cols, const std::string &second_name, size_type second_rows, size_type second_cols, const std::string &clarification)
Initializes a dimension mismatch error.
Definition: exception.hpp:433
gko::HipfftError
HipfftError is thrown when a hipFFT routine throws a non-zero error code.
Definition: exception.hpp:373
gko::ValueMismatch::ValueMismatch
ValueMismatch(const std::string &file, int line, const std::string &func, size_type val1, size_type val2, const std::string &clarification)
Initializes a value mismatch error.
Definition: exception.hpp:515
gko::InvalidData
InvalidData is thrown in case an object in Ginkgo contains invalid data, either because the data was ...
Definition: exception.hpp:674
gko::CusparseError
CusparseError is thrown when a cuSPARSE routine throws a non-zero error code.
Definition: exception.hpp:234
gko::HipsparseError::HipsparseError
HipsparseError(const std::string &file, int line, const std::string &func, int64 error_code)
Initializes a hipSPARSE error.
Definition: exception.hpp:360
gko::MetisError
MetisError is thrown when METIS routine throws an error code.
Definition: exception.hpp:396
gko::Error
The Error class is used to report exceptional behaviour in library functions.
Definition: exception.hpp:59
gko::NotImplemented::NotImplemented
NotImplemented(const std::string &file, int line, const std::string &func)
Initializes a NotImplemented error.
Definition: exception.hpp:87
gko::PrecisionError
Exception thrown if an object with mismatched precision is used.
Definition: exception.hpp:696