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
11#include <AMReX_GpuQualifiers.H>
12#include <AMReX_GpuPrint.H>
13#include <AMReX_GpuAssert.H>
14#include <AMReX_ccse-mpi.H>
15#include <AMReX_Exception.H>
16#include <AMReX_Extension.H>
17
18#include <cstdint>
19#include <cstring>
20#include <functional>
21#include <iostream>
22#include <memory>
23#include <string>
24#include <type_traits>
25#include <vector>
26
27//
28// Initialize, Finalize, Error Reporting, and Version String Functions
29
30/*
31 This class consists of initialize, finalize, error-reporting, and version
32 functions that are used throughout AMReX. Note that all the output functions
33 add a terminating exclamation mark, so there is no need to add any punctuation
34 to sentences that get output explicitly.
35*/
36
37namespace amrex
38{
39 class AMReX;
40
41 using PTR_TO_VOID_FUNC = void (*)();
42 using ErrorHandler = void (*)(const char*);
43
45 namespace system
46 {
47#ifndef AMREX_DEBUG
48 constexpr bool NDebug = true;
49 constexpr bool Debug = false;
50#else
51 constexpr bool NDebug = false;
52 constexpr bool Debug = true;
53#endif
54
55 extern AMREX_EXPORT std::string exename;
56
57 extern AMREX_EXPORT int verbose;
58
59 extern AMREX_EXPORT bool signal_handling;
60 extern AMREX_EXPORT bool handle_sigsegv;
61 extern AMREX_EXPORT bool handle_sigterm;
62 extern AMREX_EXPORT bool handle_sigint;
63 extern AMREX_EXPORT bool handle_sigabrt;
64 extern AMREX_EXPORT bool handle_sigfpe;
65 extern AMREX_EXPORT bool handle_sigill;
66
67 extern AMREX_EXPORT bool call_addr2line;
68 extern AMREX_EXPORT bool throw_exception;
69
71
72 extern AMREX_EXPORT std::ostream* osout;
73 extern AMREX_EXPORT std::ostream* oserr;
74
77
78 extern AMREX_EXPORT bool init_snan;
79 }
80
86 [[nodiscard]] std::string Version ();
87
109 AMReX* Initialize (MPI_Comm mpi_comm,
110 std::ostream& a_osout = std::cout,
111 std::ostream& a_oserr = std::cerr,
112 ErrorHandler a_errhandler = nullptr,
113 int a_device_id = -1);
114
145 AMReX* Initialize (int& argc, char**& argv,
146 const std::function<void()>& func_parm_parse,
147 std::ostream& a_osout = std::cout,
148 std::ostream& a_oserr = std::cerr,
149 ErrorHandler a_errhandler = nullptr,
150 int a_device_id = -1);
151
187 AMReX* Initialize (int& argc, char**& argv, bool build_parm_parse=true,
188 MPI_Comm mpi_comm = MPI_COMM_WORLD,
189 const std::function<void()>& func_parm_parse = {},
190 std::ostream& a_osout = std::cout,
191 std::ostream& a_oserr = std::cerr,
192 ErrorHandler a_errhandler = nullptr,
193 int a_device_id = -1);
194
209 void Init_minimal (MPI_Comm mpi_comm = MPI_COMM_WORLD);
210
216 [[nodiscard]] bool Initialized ();
217
227 void Finalize (AMReX* pamrex);
229 void Finalize ();
231 void Finalize_minimal();
232
242 void ExecOnFinalize (std::function<void()> func);
251 void ExecOnInitialize (std::function<void()> func);
252
257 template <class... Ts>
259 void ignore_unused (const Ts&...) {}
260
265 void Error (const std::string& msg);
266
277 void Error_host (const char* type, const char* msg);
278
281 void Error (const char* msg = nullptr) {
282#if defined(NDEBUG)
284#else
286 if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
288 ))
289#endif
290 AMREX_IF_ON_HOST((Error_host("Error", msg);))
291 }
292
297 void Warning (const std::string& msg);
298
303 void Warning_host (const char * msg);
304
307 void Warning (const char * msg) {
308#if defined(NDEBUG)
310#else
311 AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
312#endif
314 }
315
320 void Abort (const std::string& msg);
321
324 void Abort (const char * msg = nullptr) {
325#if defined(NDEBUG)
327#else
329 if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
331 ))
332#endif
333 AMREX_IF_ON_HOST((Error_host("Abort", msg);))
334 }
335
348 void Assert_host (const char* EX, const char* file, int line, const char* msg,
349 std::size_t msg_size=0);
350
364 void Assert (const char* EX, const char* file, int line) {
365#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
367#else
369 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
370 EX, file, line);
372 ))
373#endif
374 AMREX_IF_ON_HOST((Assert_host(EX,file,line,nullptr,0U);))
375 }
376
390 void Assert (const char* EX, const char* file, int line, const char* msg) {
391#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
392 AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
393#else
395 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
396 EX, file, line, msg);
398 ))
399#endif
400 AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg,std::strlen(msg));))
401 }
402
405 void Assert (const char* EX, const char* file, int line, const std::string& msg) {
406 Assert_host(EX,file,line,msg.c_str(),msg.size());
407 }
408
417 void write_to_stderr_without_buffering (const char* str);
418
424
426 std::ostream& OutStream ();
428 std::ostream& ErrorStream ();
429
431 [[nodiscard]] int Verbose () noexcept;
436 void SetVerbose (int v) noexcept;
437
439 [[nodiscard]] bool InitSNaN () noexcept;
444 void SetInitSNaN (bool v) noexcept;
445
447 [[nodiscard]] std::string get_command ();
448
450 [[nodiscard]] int command_argument_count ();
451
457 [[nodiscard]] std::string get_command_argument (int number);
458
459#ifndef _MSC_VER
460 inline void GccPlacater ()
461 {
462 std::allocator<bool> a_b;
463 std::allocator<char> a_c;
464 std::allocator<int> a_i;
465 std::allocator<long> a_l;
466 std::allocator<long long> a_ll;
467 std::allocator<unsigned char> a_uc;
468 std::allocator<unsigned int> a_ui;
469 std::allocator<unsigned long> a_ul;
470 std::allocator<unsigned long long> a_ull;
471 std::allocator<float> a_f;
472 std::allocator<double> a_d;
473 std::allocator<std::string> a_s;
474
488 }
489#endif
490
491 class Geometry;
492
499 class AMReX
500 {
501 public:
502 AMReX ();
503 ~AMReX ();
504 AMReX (AMReX const&) = delete;
505 AMReX (AMReX &&) = delete;
506 AMReX& operator= (AMReX const&) = delete;
507 AMReX& operator= (AMReX &&) = delete;
508
510 static bool empty () noexcept { return m_instance.empty(); }
511
513 static int size () noexcept { return static_cast<int>(m_instance.size()); }
514
516 static AMReX* top () noexcept { return m_instance.back().get(); }
517
525 static void push (AMReX* pamrex);
526
528 static void push (std::unique_ptr<AMReX> pamrex);
529
531 static void erase (AMReX* pamrex);
532
534 [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
535
536 private:
537
538 static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
539
540 Geometry* m_geom = nullptr;
541 };
542
543 enum struct FPExcept : std::uint8_t {
544 none = 0B0000,
545 invalid = 0B0001,
546 zero = 0B0010,
547 overflow = 0B0100,
548 all = 0B0111
549 };
550
552 [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
553
555 [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
556 {
557 using T = std::underlying_type_t<FPExcept>;
558 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
559 return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
560 }
561
563 [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
564 {
565 using T = std::underlying_type_t<FPExcept>;
566 return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
567 }
568
570 [[nodiscard]] FPExcept getFPExcept ();
571
575 FPExcept setFPExcept (FPExcept excepts);
576
591 [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
592
607 [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
608}
609
610#endif /*BL_AMREX_H*/
AMReX exception type.
Compiler- and backend-specific extension macros (e.g., restrict, SIMD, inline).
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:124
#define AMREX_EXPORT
Definition AMReX_Extension.H:196
#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
Runtime context stored for each active amrex::Initialize() call.
Definition AMReX.H:500
static bool empty() noexcept
True if no AMReX instances are registered on the stack.
Definition AMReX.H:510
static int size() noexcept
Number of managed instances currently tracked (Initialize depth).
Definition AMReX.H:513
AMReX(AMReX const &)=delete
static void erase(AMReX *pamrex)
Remove a specific instance pointer from the internal stack.
Definition AMReX.cpp:1030
Geometry * getDefaultGeometry() noexcept
Geometry owned by this context (constructed with the AMReX object).
Definition AMReX.H:534
static void push(AMReX *pamrex)
Transfer ownership of an existing AMReX* into the stack.
Definition AMReX.cpp:1011
~AMReX()
Definition AMReX.cpp:1005
AMReX()
Definition AMReX.cpp:1000
AMReX(AMReX &&)=delete
AMReX & operator=(AMReX const &)=delete
static AMReX * top() noexcept
Pointer to the most recently pushed instance.
Definition AMReX.H:516
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:75
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:123
bool abort_on_unused_inputs
Definition AMReX.cpp:122
bool handle_sigfpe
Definition AMReX.cpp:117
bool call_addr2line
Definition AMReX.cpp:119
bool signal_handling
Definition AMReX.cpp:112
bool throw_exception
Definition AMReX.cpp:120
bool handle_sigsegv
Definition AMReX.cpp:113
bool handle_sigint
Definition AMReX.cpp:115
std::ostream * oserr
Definition AMReX.cpp:124
bool regtest_reduction
Definition AMReX.cpp:121
bool handle_sigterm
Definition AMReX.cpp:114
ErrorHandler error_handler
Definition AMReX.cpp:125
bool handle_sigabrt
Definition AMReX.cpp:116
int verbose
Definition AMReX.cpp:111
bool handle_sigill
Definition AMReX.cpp:118
std::string exename
Definition AMReX.cpp:110
constexpr bool NDebug
Definition AMReX.H:48
constexpr bool Debug
Definition AMReX.H:49
bool init_snan
Definition AMReX.cpp:129
Definition AMReX_Amr.cpp:50
__host__ __device__ void Assert(const char *EX, const char *file, int line)
Device/host assertion helper used by BL_ASSERT.
Definition AMReX.H:364
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:259
std::string get_command()
Get the entire command line including the executable path.
Definition AMReX.cpp:976
void write_to_stderr_without_buffering(const char *str)
Write directly to stderr after flushing standard I/O buffers.
Definition AMReX.cpp:200
int command_argument_count()
Get the number of command line arguments excluding argv[0].
Definition AMReX.cpp:982
void SetVerbose(int v) noexcept
Override the verbosity level used by amrex::Print().
Definition AMReX.cpp:183
std::ostream & ErrorStream()
Stream that receives diagnostic output (defaults to std::cerr).
Definition AMReX.cpp:970
FPExcept
Definition AMReX.H:543
void Init_minimal(MPI_Comm mpi_comm)
Minimal initialization for utilities that need only part of AMReX.
Definition AMReX.cpp:1099
void(*)() PTR_TO_VOID_FUNC
Definition AMReX.H:41
void SetErrorHandler(ErrorHandler f)
Install a custom error handler invoked by amrex::Error_host.
Definition AMReX.cpp:189
bool InitSNaN() noexcept
Whether new allocations are filled with signaling NaNs when enabled.
Definition AMReX.cpp:185
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)
Initialize AMReX using an existing MPI communicator.
Definition AMReX.cpp:343
std::string get_command_argument(int number)
Fetch a copy of argv[number] (argv[0] is the executable path).
Definition AMReX.cpp:988
void Finalize()
Convenience overload that finalizes the instance at the top of the stack.
Definition AMReX.cpp:818
void Finalize_minimal()
Finalize a run that was initialized via Init_minimal().
Definition AMReX.cpp:1109
bool any(FPExcept a)
Return true when a is not FPExcept::none (i.e., the mask is non-empty).
Definition AMReX.H:552
FPExcept operator|(FPExcept a, FPExcept b)
Bitwise union of two floating-point exception masks.
Definition AMReX.H:555
void Error_host(const char *type, const char *msg)
Host-side error sink invoked by GPU-friendly overloads.
Definition AMReX.cpp:253
FPExcept setFPExcept(FPExcept excepts)
Definition AMReX.cpp:1052
void GccPlacater()
Definition AMReX.H:460
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:235
void Warning_host(const char *msg)
Print a warning message to the diagnostic stream (host only).
Definition AMReX.cpp:275
void SetInitSNaN(bool v) noexcept
Toggle whether new allocations are initialized with signaling NaNs.
Definition AMReX.cpp:187
FPExcept operator&(FPExcept a, FPExcept b)
Bitwise intersection of two floating-point exception masks.
Definition AMReX.H:563
int Verbose() noexcept
Return the verbosity level configured via ParmParse or SetVerbose().
Definition AMReX.cpp:181
void Warning(const std::string &msg)
Print a warning message to the diagnostic stream and keep running.
Definition AMReX.cpp:247
void ExecOnInitialize(std::function< void()> func)
Register a callback invoked early in Initialize().
Definition AMReX.cpp:337
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:241
std::string Version()
Return the AMReX version string reported by git describe.
Definition AMReX_Version.cpp:9
std::ostream & OutStream()
Stream that receives informational output (defaults to std::cout).
Definition AMReX.cpp:964
FPExcept getFPExcept()
Return currently enabled FP exceptions. Linux only.
Definition AMReX.cpp:1040
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux Only.
Definition AMReX.cpp:1069
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux Only.
Definition AMReX.cpp:1084
void Assert_host(const char *EX, const char *file, int line, const char *msg, std::size_t msg_size=0)
Host-side assertion helper used by BL_ASSERT().
Definition AMReX.cpp:283
void(*)(const char *) ErrorHandler
Definition AMReX.H:42
bool Initialized()
Returns true if there are any currently-active and initialized AMReX instances (i....
Definition AMReX.cpp:812
void ExecOnFinalize(std::function< void()> func)
Register a callback invoked during Finalize() (LIFO order).
Definition AMReX.cpp:331