Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
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
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 <cstring>
15#include <functional>
16#include <iostream>
17#include <memory>
18#include <string>
19#include <type_traits>
20#include <vector>
21
22//
23// Initialize, Finalize, Error Reporting, and Version String Functions
24
25/*
26 This class consists of initialize, finalize, error-reporting, and version
27 functions that are used throughout AMReX. Note that all the output functions
28 add a terminating exclamation mark, so there is no need to add any punctuation
29 to sentences that get output explicitly.
30*/
31
32namespace amrex
33{
34 class AMReX;
35
36 using PTR_TO_VOID_FUNC = void (*)();
37 using ErrorHandler = void (*)(const char*);
38
39 namespace system
40 {
41#ifndef AMREX_DEBUG
42 constexpr bool NDebug = true;
43 constexpr bool Debug = false;
44#else
45 constexpr bool NDebug = false;
46 constexpr bool Debug = true;
47#endif
48
49 extern AMREX_EXPORT std::string exename;
50
51 extern AMREX_EXPORT int verbose;
52
53 extern AMREX_EXPORT bool signal_handling;
54 extern AMREX_EXPORT bool handle_sigsegv;
55 extern AMREX_EXPORT bool handle_sigterm;
56 extern AMREX_EXPORT bool handle_sigint;
57 extern AMREX_EXPORT bool handle_sigabrt;
58 extern AMREX_EXPORT bool handle_sigfpe;
59 extern AMREX_EXPORT bool handle_sigill;
60
61 extern AMREX_EXPORT bool call_addr2line;
62 extern AMREX_EXPORT bool throw_exception;
63
65
66 extern AMREX_EXPORT std::ostream* osout;
67 extern AMREX_EXPORT std::ostream* oserr;
68
71
72 extern AMREX_EXPORT bool init_snan;
73 }
74
76 [[nodiscard]] std::string Version ();
77
78 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
79 AMReX* Initialize (MPI_Comm mpi_comm,
80 std::ostream& a_osout = std::cout,
81 std::ostream& a_oserr = std::cerr,
82 ErrorHandler a_errhandler = nullptr,
83 int a_device_id = -1);
84
85 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
86 AMReX* Initialize (int& argc, char**& argv,
87 const std::function<void()>& func_parm_parse,
88 std::ostream& a_osout = std::cout,
89 std::ostream& a_oserr = std::cerr,
90 ErrorHandler a_errhandler = nullptr,
91 int a_device_id = -1);
92
93 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
94 AMReX* Initialize (int& argc, char**& argv, bool build_parm_parse=true,
95 MPI_Comm mpi_comm = MPI_COMM_WORLD,
96 const std::function<void()>& func_parm_parse = {},
97 std::ostream& a_osout = std::cout,
98 std::ostream& a_oserr = std::cerr,
99 ErrorHandler a_errhandler = nullptr,
100 int a_device_id = -1);
101
102 // \brief Minimal version of initialization.
103 //
104 // This version is intended for users who only need AMReX for some
105 // specific functionalities such as FFT. It's the user's responsibility
106 // to initialize MPI. For multiple-GPU systems, it's the user's
107 // responsibility to properly set the GPU devices to be used. We will not
108 // try to pre-allocate memory arenas. We will not install a signal
109 // handler. Functionalities like random number generator and async I/O
110 // will be work. However, functionalities like FFT and linear solvers do
111 // work.
112 void Init_minimal (MPI_Comm mpi_comm = MPI_COMM_WORLD);
113
119 [[nodiscard]] bool Initialized ();
120
121 void Finalize (AMReX* pamrex);
122 void Finalize (); // Finalize the current top
123 // For initialization with Init_minimal, Finalize_minimal should be used
124 // for finalization.
125 void Finalize_minimal();
126
133 void ExecOnFinalize (std::function<void()>);
134 void ExecOnInitialize (std::function<void()>);
135
137 template <class... Ts>
139 void ignore_unused (const Ts&...) {}
140
142 void Error (const std::string& msg);
143
144 void Error_host (const char* type, const char* msg);
145
147 void Error (const char* msg = nullptr) {
148#if defined(NDEBUG)
150#else
152 if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
154 ))
155#endif
156 AMREX_IF_ON_HOST((Error_host("Error", msg);))
157 }
158
160 void Warning (const std::string& msg);
161
162 void Warning_host (const char * msg);
163
165 void Warning (const char * msg) {
166#if defined(NDEBUG)
168#else
169 AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
170#endif
172 }
173
175 void Abort (const std::string& msg);
176
178 void Abort (const char * msg = nullptr) {
179#if defined(NDEBUG)
181#else
183 if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
185 ))
186#endif
187 AMREX_IF_ON_HOST((Error_host("Abort", msg);))
188 }
189
196 void Assert_host (const char* EX, const char* file, int line, const char* msg,
197 std::size_t msg_size=0);
198
200 void Assert (const char* EX, const char* file, int line) {
201#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
203#else
205 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
206 EX, file, line);
208 ))
209#endif
210 AMREX_IF_ON_HOST((Assert_host(EX,file,line,nullptr,0U);))
211 }
212
214 void Assert (const char* EX, const char* file, int line, const char* msg) {
215#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
216 AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
217#else
219 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
220 EX, file, line, msg);
222 ))
223#endif
224 AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg,std::strlen(msg));))
225 }
226
228 void Assert (const char* EX, const char* file, int line, const std::string& msg) {
229 Assert_host(EX,file,line,msg.c_str(),msg.size());
230 }
231
237 void write_to_stderr_without_buffering (const char* str);
238
240
241 std::ostream& OutStream ();
242 std::ostream& ErrorStream ();
243
244 [[nodiscard]] int Verbose () noexcept;
245 void SetVerbose (int v) noexcept;
246
247 [[nodiscard]] bool InitSNaN () noexcept;
248 void SetInitSNaN (bool v) noexcept;
249
250 // ! Get the entire command line including the executable
251 [[nodiscard]] std::string get_command ();
252
253 // ! Get number of command line arguments after the executable
254 [[nodiscard]] int command_argument_count ();
255
261 [[nodiscard]] std::string get_command_argument (int number);
262
263#ifndef _MSC_VER
264 inline void GccPlacater ()
265 {
266 std::allocator<bool> a_b;
267 std::allocator<char> a_c;
268 std::allocator<int> a_i;
269 std::allocator<long> a_l;
270 std::allocator<long long> a_ll;
271 std::allocator<unsigned char> a_uc;
272 std::allocator<unsigned int> a_ui;
273 std::allocator<unsigned long> a_ul;
274 std::allocator<unsigned long long> a_ull;
275 std::allocator<float> a_f;
276 std::allocator<double> a_d;
277 std::allocator<std::string> a_s;
278
292 }
293#endif
294
295 class Geometry;
296
297 class AMReX
298 {
299 public:
300 AMReX ();
301 ~AMReX ();
302 AMReX (AMReX const&) = delete;
303 AMReX (AMReX &&) = delete;
304 AMReX& operator= (AMReX const&) = delete;
305 AMReX& operator= (AMReX &&) = delete;
306
307 static bool empty () noexcept { return m_instance.empty(); }
308
309 static int size () noexcept { return static_cast<int>(m_instance.size()); }
310
311 static AMReX* top () noexcept { return m_instance.back().get(); }
312
313 // Thisfunction will take the ownership of the AMReX pointer,
314 // and put it on the top of the stack (i.e., back of the
315 // vector). If the pointer is already in the stack, it will
316 // be moved to the top.
317 static void push (AMReX* pamrex);
318
319 static void push (std::unique_ptr<AMReX> pamrex);
320
321 // This erases `pamrex` from the stack.
322 static void erase (AMReX* pamrex);
323
324 [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
325
326 private:
327
328 static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
329
330 Geometry* m_geom = nullptr;
331 };
332
333 enum struct FPExcept : std::uint8_t {
334 none = 0B0000,
335 invalid = 0B0001,
336 zero = 0B0010,
337 overflow = 0B0100,
338 all = 0B0111
339 };
340
341 [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
342
343 [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
344 {
345 using T = std::underlying_type_t<FPExcept>;
346 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
347 return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
348 }
349
350 [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
351 {
352 using T = std::underlying_type_t<FPExcept>;
353 return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
354 }
355
357 [[nodiscard]] FPExcept getFPExcept ();
358
361 FPExcept setFPExcept (FPExcept excepts);
362
376 [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
377
391 [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
392}
393
394#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
Definition AMReX.H:298
static bool empty() noexcept
Definition AMReX.H:307
static int size() noexcept
Definition AMReX.H:309
AMReX(AMReX const &)=delete
static void erase(AMReX *pamrex)
Definition AMReX.cpp:991
Geometry * getDefaultGeometry() noexcept
Definition AMReX.H:324
static void push(AMReX *pamrex)
Definition AMReX.cpp:972
~AMReX()
Definition AMReX.cpp:966
AMReX()
Definition AMReX.cpp:961
AMReX(AMReX &&)=delete
AMReX & operator=(AMReX const &)=delete
static AMReX * top() noexcept
Definition AMReX.H:311
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:74
int MPI_Comm
Definition AMReX_ccse-mpi.H:51
static constexpr int MPI_COMM_WORLD
Definition AMReX_ccse-mpi.H:58
std::ostream * osout
Definition AMReX.cpp:117
bool abort_on_unused_inputs
Definition AMReX.cpp:116
bool handle_sigfpe
Definition AMReX.cpp:111
bool call_addr2line
Definition AMReX.cpp:113
bool signal_handling
Definition AMReX.cpp:106
bool throw_exception
Definition AMReX.cpp:114
bool handle_sigsegv
Definition AMReX.cpp:107
bool handle_sigint
Definition AMReX.cpp:109
std::ostream * oserr
Definition AMReX.cpp:118
bool regtest_reduction
Definition AMReX.cpp:115
bool handle_sigterm
Definition AMReX.cpp:108
ErrorHandler error_handler
Definition AMReX.cpp:119
bool handle_sigabrt
Definition AMReX.cpp:110
int verbose
Definition AMReX.cpp:105
bool handle_sigill
Definition AMReX.cpp:112
std::string exename
Definition AMReX.cpp:104
constexpr bool NDebug
Definition AMReX.H:42
constexpr bool Debug
Definition AMReX.H:43
bool init_snan
Definition AMReX.cpp:123
Definition AMReX_Amr.cpp:49
__host__ __device__ void Assert(const char *EX, const char *file, int line)
Definition AMReX.H:200
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
std::string get_command()
Definition AMReX.cpp:937
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:188
int command_argument_count()
Definition AMReX.cpp:943
void SetVerbose(int v) noexcept
Definition AMReX.cpp:171
std::ostream & ErrorStream()
Definition AMReX.cpp:931
FPExcept
Definition AMReX.H:333
void ExecOnInitialize(std::function< void()>)
Definition AMReX.cpp:326
void(*)() PTR_TO_VOID_FUNC
Definition AMReX.H:36
void SetErrorHandler(ErrorHandler f)
Definition AMReX.cpp:177
bool InitSNaN() noexcept
Definition AMReX.cpp:173
AMReX * Initialize(MPI_Comm mpi_comm, std::ostream &a_osout=std::cout, std::ostream &a_oserr=std::cerr, ErrorHandler a_errhandler=nullptr, int a_device_id=-1)
Definition AMReX.cpp:332
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:949
void Finalize()
Definition AMReX.cpp:786
void Finalize_minimal()
Definition AMReX.cpp:1070
bool any(FPExcept a)
Definition AMReX.H:341
FPExcept operator|(FPExcept a, FPExcept b)
Definition AMReX.H:343
void Error_host(const char *type, const char *msg)
Definition AMReX.cpp:242
FPExcept setFPExcept(FPExcept excepts)
Definition AMReX.cpp:1013
void GccPlacater()
Definition AMReX.H:264
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:224
void Warning_host(const char *msg)
Definition AMReX.cpp:264
void SetInitSNaN(bool v) noexcept
Definition AMReX.cpp:175
FPExcept operator&(FPExcept a, FPExcept b)
Definition AMReX.H:350
int Verbose() noexcept
Definition AMReX.cpp:169
void Warning(const std::string &msg)
Print out warning message to cerr.
Definition AMReX.cpp:236
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
void ExecOnFinalize(std::function< void()>)
We maintain a stack of functions that need to be called in Finalize(). The functions are called in LI...
Definition AMReX.cpp:320
std::string Version()
Definition AMReX_Version.cpp:9
std::ostream & OutStream()
Definition AMReX.cpp:925
FPExcept getFPExcept()
Return currently enabled FP exceptions. Linux only.
Definition AMReX.cpp:1001
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux Only.
Definition AMReX.cpp:1030
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux Only.
Definition AMReX.cpp:1045
void Assert_host(const char *EX, const char *file, int line, const char *msg, std::size_t msg_size=0)
Prints assertion failed messages to cerr and exits via abort(). Intended for use by the BL_ASSERT() m...
Definition AMReX.cpp:272
void(*)(const char *) ErrorHandler
Definition AMReX.H:37
void Init_minimal(MPI_Comm mpi_comm)
Definition AMReX.cpp:1060
bool Initialized()
Returns true if there are any currently-active and initialized AMReX instances (i....
Definition AMReX.cpp:780