LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
mild_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.
25 //
26 // lbann_mild_exception .hpp - LBANN mild exception reporting macros
28 
29 #ifndef LBANN_MILD_EXCEPTION_HPP_INCLUDED
30 #define LBANN_MILD_EXCEPTION_HPP_INCLUDED
31 
32 // evaluate a rare exception condition
33 #if 1
34 #define _BUILTIN_FALSE(_COND_) (__builtin_expect((_COND_), false))
35 #else
36 #define _BUILTIN_FALSE(_COND_) (_COND_)
37 #endif
38 
39 #ifdef LBANN_DEBUG
41 //-----------------------------------------------------------------------------
42 #define _LBANN_DEBUG_MSG(_MSG_) \
43  std::cerr << __FILE__ << " " << __LINE__ << " : " << _MSG_ << std::endl;
44 
45 #define _LBANN_CRITICAL_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
46  if (_BUILTIN_FALSE(_COND_)) { \
47  std::stringstream err; \
48  err << __FILE__ << " " << __LINE__ << " : " << _MSG_ << std::endl; \
49  throw lbann_exception(err.str()); \
50  }
51 
52 #define _LBANN_MILD_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
53  if (_BUILTIN_FALSE(_COND_)) { \
54  _LBANN_DEBUG_MSG(_MSG_) \
55  return (_RETVAL_); \
56  }
57 
58 #define _LBANN_SILENT_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
59  if (_BUILTIN_FALSE(_COND_)) { \
60  return (_RETVAL_); \
61  }
62 //-----------------------------------------------------------------------------
63 #else
64 //-----------------------------------------------------------------------------
65 #include <iostream>
66 #if 1
67 #define _LBANN_DEBUG_MSG(_MSG_) \
68  std::cerr << __FILE__ << " " << __LINE__ << " : " << _MSG_ << std::endl;
69 #else
70 // disable message
71 #define _LBANN_DEBUG_MSG(_MSG_)
72 #endif
73 
74 #define _LBANN_CRITICAL_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
75  if (_BUILTIN_FALSE(_COND_)) { \
76  std::cerr << __FILE__ << " " << __LINE__ << " : " << _MSG_ << std::endl; \
77  return (_RETVAL_); \
78  }
79 
80 #if 1
81 // print out a message and exit the current function if an exception condition
82 // is met
83 #define _LBANN_MILD_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
84  if (_BUILTIN_FALSE(_COND_)) { \
85  _LBANN_DEBUG_MSG(_MSG_) \
86  return (_RETVAL_); \
87  }
88 
89 // exit the current function if an exception condition is met
90 #define _LBANN_SILENT_EXCEPTION(_COND_, _MSG_, _RETVAL_) \
91  if (_BUILTIN_FALSE(_COND_)) { \
92  return (_RETVAL_); \
93  }
94 #else
95 // skip mild exception condition checking when it is sure that it is not going
96 // to happen
97 #define _LBANN_MILD_EXCEPTION(_COND_, _MSG_, _RETVAL_)
98 #define _LBANN_SILENT_EXCEPTION(_COND_, _MSG_, _RETVAL_)
99 #endif
100 //-----------------------------------------------------------------------------
101 #endif
102 
103 #endif // LBANN_MILD_EXCEPTION_HPP_INCLUDED