Block-Structured AMR Software Framework
AMReX.H
Go to the documentation of this file.
1 
2 #ifndef BL_AMREX_H
3 #define BL_AMREX_H
4 #include <AMReX_Config.H>
5 
6 #include <AMReX_GpuQualifiers.H>
7 #include <AMReX_GpuPrint.H>
8 #include <AMReX_GpuAssert.H>
9 #include <AMReX_ccse-mpi.H>
10 #include <AMReX_Exception.H>
11 #include <AMReX_Extension.H>
12 
13 #include <cstdint>
14 #include <functional>
15 #include <iostream>
16 #include <memory>
17 #include <string>
18 #include <type_traits>
19 #include <vector>
20 
21 //
22 // Initialize, Finalize, Error Reporting, and Version String Functions
23 
24 /*
25  This class consists of initialize, finalize, error-reporting, and version
26  functions that are used throughout AMReX. Note that all the output functions
27  add a terminating exclamation mark, so there is no need to add any punctuation
28  to sentences that get output explicitly.
29 */
30 
31 namespace amrex
32 {
33  class AMReX;
34 
35  using PTR_TO_VOID_FUNC = void (*)();
36  using ErrorHandler = void (*)(const char*);
37 
38  namespace system
39  {
40 #ifndef AMREX_DEBUG
41  constexpr bool NDebug = true;
42  constexpr bool Debug = false;
43 #else
44  constexpr bool NDebug = false;
45  constexpr bool Debug = true;
46 #endif
47 
48  extern AMREX_EXPORT std::string exename;
49 
50  extern AMREX_EXPORT int verbose;
51 
52  extern AMREX_EXPORT bool signal_handling;
53  extern AMREX_EXPORT bool handle_sigsegv;
54  extern AMREX_EXPORT bool handle_sigterm;
55  extern AMREX_EXPORT bool handle_sigint;
56  extern AMREX_EXPORT bool handle_sigabrt;
57  extern AMREX_EXPORT bool handle_sigfpe;
58 
59  extern AMREX_EXPORT bool call_addr2line;
60  extern AMREX_EXPORT bool throw_exception;
61 
62  extern AMREX_EXPORT bool regtest_reduction;
63 
64  extern AMREX_EXPORT std::ostream* osout;
65  extern AMREX_EXPORT std::ostream* oserr;
66 
69 
70  extern AMREX_EXPORT bool init_snan;
71  }
72 
74  [[nodiscard]] std::string Version ();
75 
76  // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
77  AMReX* Initialize (MPI_Comm mpi_comm,
78  std::ostream& a_osout = std::cout,
79  std::ostream& a_oserr = std::cerr,
80  ErrorHandler a_errhandler = nullptr);
81 
82  // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
83  AMReX* Initialize (int& argc, char**& argv, bool build_parm_parse=true,
84  MPI_Comm mpi_comm = MPI_COMM_WORLD,
85  const std::function<void()>& func_parm_parse = {},
86  std::ostream& a_osout = std::cout,
87  std::ostream& a_oserr = std::cerr,
88  ErrorHandler a_errhandler = nullptr);
89 
95  [[nodiscard]] bool Initialized ();
96 
97  void Finalize (AMReX* pamrex);
98  void Finalize (); // Finalize the current top
107 
109  template <class... Ts>
111  void ignore_unused (const Ts&...) {}
112 
114  void Error (const std::string& msg);
115 
116  void Error_host (const char* type, const char* msg);
117 
119  void Error (const char* msg = nullptr) {
120 #if defined(NDEBUG)
122 #else
124  if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
126  ))
127 #endif
128  AMREX_IF_ON_HOST((Error_host("Error", msg);))
129  }
130 
132  void Warning (const std::string& msg);
133 
134  void Warning_host (const char * msg);
135 
137  void Warning (const char * msg) {
138 #if defined(NDEBUG)
140 #else
141  AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
142 #endif
144  }
145 
147  void Abort (const std::string& msg);
148 
150  void Abort (const char * msg = nullptr) {
151 #if defined(NDEBUG)
153 #else
155  if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
157  ))
158 #endif
159  AMREX_IF_ON_HOST((Error_host("Abort", msg);))
160  }
161 
168  void Assert_host (const char* EX, const char* file, int line, const char* msg);
169 
171  void Assert (const char* EX, const char* file, int line, const char* msg = nullptr) {
172 #if defined(NDEBUG)
173  AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
174 #else
176  if (msg) {
177  AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
178  EX, file, line, msg);
179  } else {
180  AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
181  EX, file, line);
182  }
184  ))
185 #endif
186  AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg);))
187  }
188 
194  void write_to_stderr_without_buffering (const char* str);
195 
197 
198  std::ostream& OutStream ();
199  std::ostream& ErrorStream ();
200 
201  [[nodiscard]] int Verbose () noexcept;
202  void SetVerbose (int v) noexcept;
203 
204  [[nodiscard]] bool InitSNaN () noexcept;
205  void SetInitSNaN (bool v) noexcept;
206 
207  // ! Get the entire command line including the executable
208  [[nodiscard]] std::string get_command ();
209 
210  // ! Get number of command line arguments after the executable
211  [[nodiscard]] int command_argument_count ();
212 
218  [[nodiscard]] std::string get_command_argument (int number);
219 
220 #ifndef _MSC_VER
221  inline void GccPlacater ()
222  {
223  std::allocator<bool> a_b;
224  std::allocator<char> a_c;
225  std::allocator<int> a_i;
226  std::allocator<long> a_l;
227  std::allocator<long long> a_ll;
228  std::allocator<unsigned char> a_uc;
229  std::allocator<unsigned int> a_ui;
230  std::allocator<unsigned long> a_ul;
231  std::allocator<unsigned long long> a_ull;
232  std::allocator<float> a_f;
233  std::allocator<double> a_d;
234  std::allocator<std::string> a_s;
235 
240  amrex::ignore_unused(a_ll);
241  amrex::ignore_unused(a_uc);
242  amrex::ignore_unused(a_ui);
243  amrex::ignore_unused(a_ul);
244  amrex::ignore_unused(a_ull);
245  amrex::ignore_unused(a_ll);
249  }
250 #endif
251 
252  class Geometry;
253 
254  class AMReX
255  {
256  public:
257  AMReX ();
258  ~AMReX ();
259  AMReX (AMReX const&) = delete;
260  AMReX (AMReX &&) = delete;
261  AMReX& operator= (AMReX const&) = delete;
262  AMReX& operator= (AMReX &&) = delete;
263 
264  static bool empty () noexcept { return m_instance.empty(); }
265 
266  static int size () noexcept { return static_cast<int>(m_instance.size()); }
267 
268  static AMReX* top () noexcept { return m_instance.back().get(); }
269 
270  // Thisfunction will take the ownership of the AMReX pointer,
271  // and put it on the top of the stack (i.e., back of the
272  // vector). If the pointer is already in the stack, it will
273  // be moved to the top.
274  static void push (AMReX* pamrex);
275 
276  // This erases `pamrex` from the stack.
277  static void erase (AMReX* pamrex);
278 
279  [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
280 
281  private:
282 
283  static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
284 
285  Geometry* m_geom = nullptr;
286  };
287 
288  enum struct FPExcept : std::uint8_t {
289  none = 0B0000,
290  invalid = 0B0001,
291  zero = 0B0010,
292  overflow = 0B0100,
293  all = 0B0111
294  };
295 
296  [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
297 
298  [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
299  {
300  using T = std::underlying_type_t<FPExcept>;
301  return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
302  }
303 
304  [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
305  {
306  using T = std::underlying_type_t<FPExcept>;
307  return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
308  }
309 
311  [[nodiscard]] FPExcept getFPExcept ();
312 
315  FPExcept setFPExcept (FPExcept excepts);
316 
330  [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
331 
345  [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
346 }
347 
348 #endif /*BL_AMREX_H*/
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_EXPORT
Definition: AMReX_Extension.H:191
#define AMREX_DEVICE_ASSERT(flag)
Definition: AMReX_GpuAssert.H:16
#define AMREX_DEVICE_PRINTF(...)
Definition: AMReX_GpuPrint.H:21
#define AMREX_IF_ON_DEVICE(CODE)
Definition: AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition: AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
int MPI_Comm
Definition: AMReX_ccse-mpi.H:47
static constexpr int MPI_COMM_WORLD
Definition: AMReX_ccse-mpi.H:54
Definition: AMReX.H:255
static bool empty() noexcept
Definition: AMReX.H:264
static int size() noexcept
Definition: AMReX.H:266
AMReX & operator=(AMReX const &)=delete
Geometry * getDefaultGeometry() noexcept
Definition: AMReX.H:279
AMReX(AMReX const &)=delete
Geometry * m_geom
Definition: AMReX.H:285
static void erase(AMReX *pamrex)
Definition: AMReX.cpp:924
static AMReX * top() noexcept
Definition: AMReX.H:268
static void push(AMReX *pamrex)
Definition: AMReX.cpp:911
~AMReX()
Definition: AMReX.cpp:905
AMReX()
Definition: AMReX.cpp:900
static AMREX_EXPORT std::vector< std::unique_ptr< AMReX > > m_instance
Definition: AMReX.H:283
AMReX(AMReX &&)=delete
Rectangular problem domain geometry.
Definition: AMReX_Geometry.H:73
static int f(amrex::Real t, N_Vector y_data, N_Vector y_rhs, void *user_data)
Definition: AMReX_SundialsIntegrator.H:44
std::ostream * oserr
Definition: AMReX.cpp:114
bool init_snan
Definition: AMReX.cpp:119
ErrorHandler error_handler
Definition: AMReX.cpp:115
bool abort_on_unused_inputs
Definition: AMReX.cpp:112
bool throw_exception
Definition: AMReX.cpp:110
bool signal_handling
Definition: AMReX.cpp:102
bool handle_sigint
Definition: AMReX.cpp:105
std::ostream * osout
Definition: AMReX.cpp:113
bool handle_sigterm
Definition: AMReX.cpp:104
bool call_addr2line
Definition: AMReX.cpp:109
std::string exename
Definition: AMReX.cpp:100
bool regtest_reduction
Definition: AMReX.cpp:111
bool handle_sigsegv
Definition: AMReX.cpp:103
bool handle_sigabrt
Definition: AMReX.cpp:106
constexpr bool NDebug
Definition: AMReX.H:41
int verbose
Definition: AMReX.cpp:101
constexpr bool Debug
Definition: AMReX.H:42
bool handle_sigfpe
Definition: AMReX.cpp:107
Definition: AMReX_Amr.cpp:49
std::string get_command()
Definition: AMReX.cpp:876
void write_to_stderr_without_buffering(const char *str)
This is used by amrex::Error(), amrex::Abort(), and amrex::Assert() to ensure that when writing the m...
Definition: AMReX.cpp:179
int command_argument_count()
Definition: AMReX.cpp:882
void SetVerbose(int v) noexcept
Definition: AMReX.cpp:162
AMReX * Initialize(MPI_Comm mpi_comm, std::ostream &a_osout=std::cout, std::ostream &a_oserr=std::cerr, ErrorHandler a_errhandler=nullptr)
Definition: AMReX.cpp:322
std::ostream & ErrorStream()
Definition: AMReX.cpp:870
FPExcept
Definition: AMReX.H:288
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void Assert(const char *EX, const char *file, int line, const char *msg=nullptr)
Definition: AMReX.H:171
void(*)() PTR_TO_VOID_FUNC
Definition: AMReX.H:35
void SetErrorHandler(ErrorHandler f)
Definition: AMReX.cpp:168
bool InitSNaN() noexcept
Definition: AMReX.cpp:164
std::string get_command_argument(int number)
Get command line arguments. The executable name is the zero-th argument. Return empty string if there...
Definition: AMReX.cpp:888
bool any(FPExcept a)
Definition: AMReX.H:296
FPExcept operator|(FPExcept a, FPExcept b)
Definition: AMReX.H:298
void Error_host(const char *type, const char *msg)
Definition: AMReX.cpp:233
void ExecOnInitialize(PTR_TO_VOID_FUNC)
Definition: AMReX.cpp:316
void Assert_host(const char *EX, const char *file, int line, const char *msg)
Prints assertion failed messages to cerr and exits via abort(). Intended for use by the BL_ASSERT() m...
Definition: AMReX.cpp:263
FPExcept setFPExcept(FPExcept excepts)
Definition: AMReX.cpp:946
void Finalize(AMReX *pamrex)
Definition: AMReX.cpp:731
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition: AMReX.H:111
void GccPlacater()
Definition: AMReX.H:221
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition: AMReX.cpp:215
void Warning_host(const char *msg)
Definition: AMReX.cpp:255
void SetInitSNaN(bool v) noexcept
Definition: AMReX.cpp:166
FPExcept operator&(FPExcept a, FPExcept b)
Definition: AMReX.H:304
int Verbose() noexcept
Definition: AMReX.cpp:160
void Warning(const std::string &msg)
Print out warning message to cerr.
Definition: AMReX.cpp:227
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition: AMReX.cpp:221
std::string Version()
Definition: AMReX_Version.cpp:9
std::ostream & OutStream()
Definition: AMReX.cpp:864
FPExcept getFPExcept()
Return currently enabled FP exceptions. Linux only.
Definition: AMReX.cpp:934
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux Only.
Definition: AMReX.cpp:963
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux Only.
Definition: AMReX.cpp:978
void ExecOnFinalize(PTR_TO_VOID_FUNC)
We maintain a stack of functions that need to be called in Finalize(). The functions are called in LI...
Definition: AMReX.cpp:310
void(*)(const char *) ErrorHandler
Definition: AMReX.H:36
bool Initialized()
Returns true if there are any currently-active and initialized AMReX instances (i....
Definition: AMReX.cpp:719