LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
exception.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_EXCEPTION_HPP_INCLUDED
28 #define LBANN_UTILS_EXCEPTION_HPP_INCLUDED
29 
30 #include "lbann/comm.hpp"
31 
32 #include <exception>
33 #include <iostream>
34 #include <sstream>
35 
36 // Macro to throw an LBANN exception
37 #define LBANN_ERROR(...) \
38  do { \
39  const int rank_LBANN_ERROR = lbann::get_rank_in_world(); \
40  throw ::lbann::exception(::lbann::build_string( \
41  "LBANN error", \
42  (rank_LBANN_ERROR >= 0 ? " on rank " + std::to_string(rank_LBANN_ERROR) \
43  : std::string()), \
44  " (", \
45  __FILE__, \
46  ":", \
47  __LINE__, \
48  "): ", \
49  __VA_ARGS__)); \
50  } while (0)
51 
52 // Macro to print a warning to standard error stream.
53 #define LBANN_WARNING(...) \
54  do { \
55  const int rank_LBANN_WARNING = lbann::get_rank_in_world(); \
56  std::cerr << lbann::build_string( \
57  "LBANN warning", \
58  (rank_LBANN_WARNING >= 0 \
59  ? " on rank " + std::to_string(rank_LBANN_WARNING) \
60  : std::string()), \
61  " (", \
62  __FILE__, \
63  ":", \
64  __LINE__, \
65  "): ", \
66  __VA_ARGS__) \
67  << std::endl; \
68  } while (0)
69 
70 #define LBANN_WARNING_WORLD_ROOT(...) \
71  do { \
72  if (lbann::get_rank_in_world() == 0) { \
73  LBANN_WARNING(__VA_ARGS__); \
74  } \
75  } while (0)
76 
77 // Macro to print a message to standard cout stream.
78 #define LBANN_MSG(...) \
79  do { \
80  const int rank_LBANN_MSG = lbann::get_rank_in_world(); \
81  if (rank_LBANN_MSG == 0) { \
82  std::cout << lbann::build_string( \
83  "LBANN message", \
84  (rank_LBANN_MSG >= 0 \
85  ? " on rank " + std::to_string(rank_LBANN_MSG) \
86  : std::string()), \
87  " (", \
88  __FILE__, \
89  ":", \
90  __LINE__, \
91  "): ", \
92  __VA_ARGS__) \
93  << std::endl; \
94  } \
95  } while (0)
96 
97 #define LBANN_ASSERT(cond) \
98  if (!(cond)) \
99  LBANN_ERROR("The assertion " #cond " failed.")
100 
101 #ifdef LBANN_DEBUG
102 #define LBANN_ASSERT_DEBUG(cond) LBANN_ASSERT(cond)
103 #else
104 #define LBANN_ASSERT_DEBUG(cond)
105 #endif
106 
107 #define LBANN_ASSERT_WARNING(cond) \
108  if (!(cond)) \
109  LBANN_WARNING("The assertion " #cond " failed.")
110 
111 namespace lbann {
112 
118 class exception : public std::exception
119 {
120 public:
125  exception();
126 
134  exception(std::string message);
135 
136  char const* what() const noexcept override;
137 
139  void print_report(std::ostream& os = std::cerr) const;
140 
141 private:
143  std::string m_message;
144 };
146 
156 template <typename... Args>
157 std::string build_string(Args&&... args)
158 {
159  std::ostringstream oss;
160  int dummy[] = {(oss << args, 0)...};
161  (void)dummy; // silence compiler warnings
162  return oss.str();
163 }
164 
165 } // namespace lbann
166 
167 #endif // LBANN_UTILS_EXCEPTION_HPP_INCLUDED
The base exception for LBANN errors.
Definition: exception.hpp:118
char const * what() const noexcept override
std::string m_message
Definition: exception.hpp:143
std::string build_string(Args &&... args)
Build a string from the arguments.
Definition: exception.hpp:157
void print_report(std::ostream &os=std::cerr) const
Print the what() string to the stream.
exception()
Default constructor.