Block-Structured AMR Software Framework
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <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
31namespace 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
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
90 // \brief Minimal version of initialization.
91 //
92 // This version is intended for users who only need AMReX for some
93 // specific functionalities such as FFT. It's the user's responsibility
94 // to initialize MPI. For multiple-GPU systems, it's the user's
95 // responsibility to properly set the GPU devices to be used. We will not
96 // try to pre-allocate memory arenas. We will not install a signal
97 // handler. Functionalities like random number generator and async I/O
98 // will be work. However, functionalities like FFT and linear solvers do
99 // work.
100 void Init_minimal (MPI_Comm mpi_comm = MPI_COMM_WORLD);
101
107 [[nodiscard]] bool Initialized ();
108
109 void Finalize (AMReX* pamrex);
110 void Finalize (); // Finalize the current top
111 // For initialization with Init_minimal, Finalize_minimal should be used
112 // for finalization.
113 void Finalize_minimal();
114
121 void ExecOnFinalize (std::function<void()>);
122 void ExecOnInitialize (std::function<void()>);
123
125 template <class... Ts>
127 void ignore_unused (const Ts&...) {}
128
130 void Error (const std::string& msg);
131
132 void Error_host (const char* type, const char* msg);
133
135 void Error (const char* msg = nullptr) {
136#if defined(NDEBUG)
138#else
140 if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
142 ))
143#endif
144 AMREX_IF_ON_HOST((Error_host("Error", msg);))
145 }
146
148 void Warning (const std::string& msg);
149
150 void Warning_host (const char * msg);
151
153 void Warning (const char * msg) {
154#if defined(NDEBUG)
156#else
157 AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
158#endif
160 }
161
163 void Abort (const std::string& msg);
164
166 void Abort (const char * msg = nullptr) {
167#if defined(NDEBUG)
169#else
171 if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
173 ))
174#endif
175 AMREX_IF_ON_HOST((Error_host("Abort", msg);))
176 }
177
184 void Assert_host (const char* EX, const char* file, int line, const char* msg);
185
187 void Assert (const char* EX, const char* file, int line, const char* msg = nullptr) {
188#if defined(NDEBUG)
189 AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
190#else
192 if (msg) {
193 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
194 EX, file, line, msg);
195 } else {
196 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
197 EX, file, line);
198 }
200 ))
201#endif
202 AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg);))
203 }
204
210 void write_to_stderr_without_buffering (const char* str);
211
213
214 std::ostream& OutStream ();
215 std::ostream& ErrorStream ();
216
217 [[nodiscard]] int Verbose () noexcept;
218 void SetVerbose (int v) noexcept;
219
220 [[nodiscard]] bool InitSNaN () noexcept;
221 void SetInitSNaN (bool v) noexcept;
222
223 // ! Get the entire command line including the executable
224 [[nodiscard]] std::string get_command ();
225
226 // ! Get number of command line arguments after the executable
227 [[nodiscard]] int command_argument_count ();
228
234 [[nodiscard]] std::string get_command_argument (int number);
235
236#ifndef _MSC_VER
237 inline void GccPlacater ()
238 {
239 std::allocator<bool> a_b;
240 std::allocator<char> a_c;
241 std::allocator<int> a_i;
242 std::allocator<long> a_l;
243 std::allocator<long long> a_ll;
244 std::allocator<unsigned char> a_uc;
245 std::allocator<unsigned int> a_ui;
246 std::allocator<unsigned long> a_ul;
247 std::allocator<unsigned long long> a_ull;
248 std::allocator<float> a_f;
249 std::allocator<double> a_d;
250 std::allocator<std::string> a_s;
251
265 }
266#endif
267
268 class Geometry;
269
270 class AMReX
271 {
272 public:
273 AMReX ();
274 ~AMReX ();
275 AMReX (AMReX const&) = delete;
276 AMReX (AMReX &&) = delete;
277 AMReX& operator= (AMReX const&) = delete;
278 AMReX& operator= (AMReX &&) = delete;
279
280 static bool empty () noexcept { return m_instance.empty(); }
281
282 static int size () noexcept { return static_cast<int>(m_instance.size()); }
283
284 static AMReX* top () noexcept { return m_instance.back().get(); }
285
286 // Thisfunction will take the ownership of the AMReX pointer,
287 // and put it on the top of the stack (i.e., back of the
288 // vector). If the pointer is already in the stack, it will
289 // be moved to the top.
290 static void push (AMReX* pamrex);
291
292 // This erases `pamrex` from the stack.
293 static void erase (AMReX* pamrex);
294
295 [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
296
297 private:
298
299 static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
300
301 Geometry* m_geom = nullptr;
302 };
303
304 enum struct FPExcept : std::uint8_t {
305 none = 0B0000,
306 invalid = 0B0001,
307 zero = 0B0010,
308 overflow = 0B0100,
309 all = 0B0111
310 };
311
312 [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
313
314 [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
315 {
316 using T = std::underlying_type_t<FPExcept>;
317 return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
318 }
319
320 [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
321 {
322 using T = std::underlying_type_t<FPExcept>;
323 return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
324 }
325
327 [[nodiscard]] FPExcept getFPExcept ();
328
331 FPExcept setFPExcept (FPExcept excepts);
332
346 [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
347
361 [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
362}
363
364#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:271
static bool empty() noexcept
Definition AMReX.H:280
static int size() noexcept
Definition AMReX.H:282
AMReX(AMReX const &)=delete
Geometry * m_geom
Definition AMReX.H:301
static void erase(AMReX *pamrex)
Definition AMReX.cpp:959
Geometry * getDefaultGeometry() noexcept
Definition AMReX.H:295
static void push(AMReX *pamrex)
Definition AMReX.cpp:946
~AMReX()
Definition AMReX.cpp:940
AMReX()
Definition AMReX.cpp:935
static AMREX_EXPORT std::vector< std::unique_ptr< AMReX > > m_instance
Definition AMReX.H:299
AMReX(AMReX &&)=delete
AMReX & operator=(AMReX const &)=delete
static AMReX * top() noexcept
Definition AMReX.H:284
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:73
std::ostream * oserr
Definition AMReX.cpp:118
bool init_snan
Definition AMReX.cpp:123
ErrorHandler error_handler
Definition AMReX.cpp:119
bool abort_on_unused_inputs
Definition AMReX.cpp:116
bool throw_exception
Definition AMReX.cpp:114
bool signal_handling
Definition AMReX.cpp:106
bool handle_sigint
Definition AMReX.cpp:109
std::ostream * osout
Definition AMReX.cpp:117
bool handle_sigterm
Definition AMReX.cpp:108
bool call_addr2line
Definition AMReX.cpp:113
std::string exename
Definition AMReX.cpp:104
bool regtest_reduction
Definition AMReX.cpp:115
bool handle_sigsegv
Definition AMReX.cpp:107
bool handle_sigabrt
Definition AMReX.cpp:110
constexpr bool NDebug
Definition AMReX.H:41
int verbose
Definition AMReX.cpp:105
constexpr bool Debug
Definition AMReX.H:42
bool handle_sigfpe
Definition AMReX.cpp:111
Definition AMReX_Amr.cpp:49
std::string get_command()
Definition AMReX.cpp:911
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:917
void SetVerbose(int v) noexcept
Definition AMReX.cpp:171
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:331
std::ostream & ErrorStream()
Definition AMReX.cpp:905
FPExcept
Definition AMReX.H:304
void ExecOnInitialize(std::function< void()>)
Definition AMReX.cpp:325
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void Assert(const char *EX, const char *file, int line, const char *msg=nullptr)
Definition AMReX.H:187
void(*)() PTR_TO_VOID_FUNC
Definition AMReX.H:35
void SetErrorHandler(ErrorHandler f)
Definition AMReX.cpp:177
bool InitSNaN() noexcept
Definition AMReX.cpp:173
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:923
void Finalize()
Definition AMReX.cpp:760
void Finalize_minimal()
Definition AMReX.cpp:1038
bool any(FPExcept a)
Definition AMReX.H:312
FPExcept operator|(FPExcept a, FPExcept b)
Definition AMReX.H:314
void Error_host(const char *type, const char *msg)
Definition AMReX.cpp:242
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:272
FPExcept setFPExcept(FPExcept excepts)
Definition AMReX.cpp:981
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:127
void GccPlacater()
Definition AMReX.H:237
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:320
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:319
std::string Version()
Definition AMReX_Version.cpp:9
std::ostream & OutStream()
Definition AMReX.cpp:899
FPExcept getFPExcept()
Return currently enabled FP exceptions. Linux only.
Definition AMReX.cpp:969
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux Only.
Definition AMReX.cpp:998
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux Only.
Definition AMReX.cpp:1013
void(*)(const char *) ErrorHandler
Definition AMReX.H:36
void Init_minimal(MPI_Comm mpi_comm)
Definition AMReX.cpp:1028
bool Initialized()
Returns true if there are any currently-active and initialized AMReX instances (i....
Definition AMReX.cpp:754