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
60 extern AMREX_EXPORT bool call_addr2line;
61 extern AMREX_EXPORT bool throw_exception;
62
64
65 extern AMREX_EXPORT std::ostream* osout;
66 extern AMREX_EXPORT std::ostream* oserr;
67
70
71 extern AMREX_EXPORT bool init_snan;
72 }
73
75 [[nodiscard]] std::string Version ();
76
77 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
78 AMReX* Initialize (MPI_Comm mpi_comm,
79 std::ostream& a_osout = std::cout,
80 std::ostream& a_oserr = std::cerr,
81 ErrorHandler a_errhandler = nullptr,
82 int a_device_id = -1);
83
84 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
85 AMReX* Initialize (int& argc, char**& argv,
86 const std::function<void()>& func_parm_parse,
87 std::ostream& a_osout = std::cout,
88 std::ostream& a_oserr = std::cerr,
89 ErrorHandler a_errhandler = nullptr,
90 int a_device_id = -1);
91
92 // The returned AMReX* is non-owning! To delete it, call Finalize(AMReX*).
93 AMReX* Initialize (int& argc, char**& argv, bool build_parm_parse=true,
94 MPI_Comm mpi_comm = MPI_COMM_WORLD,
95 const std::function<void()>& func_parm_parse = {},
96 std::ostream& a_osout = std::cout,
97 std::ostream& a_oserr = std::cerr,
98 ErrorHandler a_errhandler = nullptr,
99 int a_device_id = -1);
100
101 // \brief Minimal version of initialization.
102 //
103 // This version is intended for users who only need AMReX for some
104 // specific functionalities such as FFT. It's the user's responsibility
105 // to initialize MPI. For multiple-GPU systems, it's the user's
106 // responsibility to properly set the GPU devices to be used. We will not
107 // try to pre-allocate memory arenas. We will not install a signal
108 // handler. Functionalities like random number generator and async I/O
109 // will be work. However, functionalities like FFT and linear solvers do
110 // work.
111 void Init_minimal (MPI_Comm mpi_comm = MPI_COMM_WORLD);
112
118 [[nodiscard]] bool Initialized ();
119
120 void Finalize (AMReX* pamrex);
121 void Finalize (); // Finalize the current top
122 // For initialization with Init_minimal, Finalize_minimal should be used
123 // for finalization.
124 void Finalize_minimal();
125
132 void ExecOnFinalize (std::function<void()>);
133 void ExecOnInitialize (std::function<void()>);
134
136 template <class... Ts>
138 void ignore_unused (const Ts&...) {}
139
141 void Error (const std::string& msg);
142
143 void Error_host (const char* type, const char* msg);
144
146 void Error (const char* msg = nullptr) {
147#if defined(NDEBUG)
149#else
151 if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
153 ))
154#endif
155 AMREX_IF_ON_HOST((Error_host("Error", msg);))
156 }
157
159 void Warning (const std::string& msg);
160
161 void Warning_host (const char * msg);
162
164 void Warning (const char * msg) {
165#if defined(NDEBUG)
167#else
168 AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
169#endif
171 }
172
174 void Abort (const std::string& msg);
175
177 void Abort (const char * msg = nullptr) {
178#if defined(NDEBUG)
180#else
182 if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
184 ))
185#endif
186 AMREX_IF_ON_HOST((Error_host("Abort", msg);))
187 }
188
195 void Assert_host (const char* EX, const char* file, int line, const char* msg,
196 std::size_t msg_size=0);
197
199 void Assert (const char* EX, const char* file, int line) {
200#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
202#else
204 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
205 EX, file, line);
207 ))
208#endif
209 AMREX_IF_ON_HOST((Assert_host(EX,file,line,nullptr,0U);))
210 }
211
213 void Assert (const char* EX, const char* file, int line, const char* msg) {
214#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
215 AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
216#else
218 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
219 EX, file, line, msg);
221 ))
222#endif
223 AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg,std::strlen(msg));))
224 }
225
227 void Assert (const char* EX, const char* file, int line, const std::string& msg) {
228 Assert_host(EX,file,line,msg.c_str(),msg.size());
229 }
230
236 void write_to_stderr_without_buffering (const char* str);
237
239
240 std::ostream& OutStream ();
241 std::ostream& ErrorStream ();
242
243 [[nodiscard]] int Verbose () noexcept;
244 void SetVerbose (int v) noexcept;
245
246 [[nodiscard]] bool InitSNaN () noexcept;
247 void SetInitSNaN (bool v) noexcept;
248
249 // ! Get the entire command line including the executable
250 [[nodiscard]] std::string get_command ();
251
252 // ! Get number of command line arguments after the executable
253 [[nodiscard]] int command_argument_count ();
254
260 [[nodiscard]] std::string get_command_argument (int number);
261
262#ifndef _MSC_VER
263 inline void GccPlacater ()
264 {
265 std::allocator<bool> a_b;
266 std::allocator<char> a_c;
267 std::allocator<int> a_i;
268 std::allocator<long> a_l;
269 std::allocator<long long> a_ll;
270 std::allocator<unsigned char> a_uc;
271 std::allocator<unsigned int> a_ui;
272 std::allocator<unsigned long> a_ul;
273 std::allocator<unsigned long long> a_ull;
274 std::allocator<float> a_f;
275 std::allocator<double> a_d;
276 std::allocator<std::string> a_s;
277
291 }
292#endif
293
294 class Geometry;
295
296 class AMReX
297 {
298 public:
299 AMReX ();
300 ~AMReX ();
301 AMReX (AMReX const&) = delete;
302 AMReX (AMReX &&) = delete;
303 AMReX& operator= (AMReX const&) = delete;
304 AMReX& operator= (AMReX &&) = delete;
305
306 static bool empty () noexcept { return m_instance.empty(); }
307
308 static int size () noexcept { return static_cast<int>(m_instance.size()); }
309
310 static AMReX* top () noexcept { return m_instance.back().get(); }
311
312 // Thisfunction will take the ownership of the AMReX pointer,
313 // and put it on the top of the stack (i.e., back of the
314 // vector). If the pointer is already in the stack, it will
315 // be moved to the top.
316 static void push (AMReX* pamrex);
317
318 // This erases `pamrex` from the stack.
319 static void erase (AMReX* pamrex);
320
321 [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
322
323 private:
324
325 static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
326
327 Geometry* m_geom = nullptr;
328 };
329
330 enum struct FPExcept : std::uint8_t {
331 none = 0B0000,
332 invalid = 0B0001,
333 zero = 0B0010,
334 overflow = 0B0100,
335 all = 0B0111
336 };
337
338 [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
339
340 [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
341 {
342 using T = std::underlying_type_t<FPExcept>;
343 return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
344 }
345
346 [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
347 {
348 using T = std::underlying_type_t<FPExcept>;
349 return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
350 }
351
353 [[nodiscard]] FPExcept getFPExcept ();
354
357 FPExcept setFPExcept (FPExcept excepts);
358
372 [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
373
387 [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
388}
389
390#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:297
static bool empty() noexcept
Definition AMReX.H:306
static int size() noexcept
Definition AMReX.H:308
AMReX(AMReX const &)=delete
Geometry * m_geom
Definition AMReX.H:327
static void erase(AMReX *pamrex)
Definition AMReX.cpp:985
Geometry * getDefaultGeometry() noexcept
Definition AMReX.H:321
static void push(AMReX *pamrex)
Definition AMReX.cpp:972
~AMReX()
Definition AMReX.cpp:966
AMReX()
Definition AMReX.cpp:961
static std::vector< std::unique_ptr< AMReX > > m_instance
Definition AMReX.H:325
AMReX(AMReX &&)=delete
AMReX & operator=(AMReX const &)=delete
static AMReX * top() noexcept
Definition AMReX.H:310
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
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:199
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:138
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:330
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:1064
bool any(FPExcept a)
Definition AMReX.H:338
FPExcept operator|(FPExcept a, FPExcept b)
Definition AMReX.H:340
void Error_host(const char *type, const char *msg)
Definition AMReX.cpp:242
FPExcept setFPExcept(FPExcept excepts)
Definition AMReX.cpp:1007
void GccPlacater()
Definition AMReX.H:263
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:346
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:995
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux Only.
Definition AMReX.cpp:1024
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux Only.
Definition AMReX.cpp:1039
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:1054
bool Initialized()
Returns true if there are any currently-active and initialized AMReX instances (i....
Definition AMReX.cpp:780