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;
67
68 extern AMREX_EXPORT bool call_addr2line;
69 extern AMREX_EXPORT bool throw_exception;
70
72
73 extern AMREX_EXPORT std::ostream* osout;
74 extern AMREX_EXPORT std::ostream* oserr;
75
78
79 extern AMREX_EXPORT bool init_snan;
80 }
81
87 [[nodiscard]] std::string Version ();
88
110 AMReX* Initialize (MPI_Comm mpi_comm,
111 std::ostream& a_osout = std::cout,
112 std::ostream& a_oserr = std::cerr,
113 ErrorHandler a_errhandler = nullptr,
114 int a_device_id = -1);
115
146 AMReX* Initialize (int& argc, char**& argv,
147 const std::function<void()>& func_parm_parse,
148 std::ostream& a_osout = std::cout,
149 std::ostream& a_oserr = std::cerr,
150 ErrorHandler a_errhandler = nullptr,
151 int a_device_id = -1);
152
155 inline AMReX* Initialize (int& argc, char**& argv,
156 void (*func_parm_parse) (),
157 std::ostream& a_osout = std::cout,
158 std::ostream& a_oserr = std::cerr,
159 ErrorHandler a_errhandler = nullptr,
160 int a_device_id = -1)
161 {
162 return Initialize(argc, argv, std::function<void()>(func_parm_parse),
163 a_osout, a_oserr, a_errhandler, a_device_id);
164 }
165
201 AMReX* Initialize (int& argc, char**& argv, bool build_parm_parse=true,
202 MPI_Comm mpi_comm = MPI_COMM_WORLD,
203 const std::function<void()>& func_parm_parse = {},
204 std::ostream& a_osout = std::cout,
205 std::ostream& a_oserr = std::cerr,
206 ErrorHandler a_errhandler = nullptr,
207 int a_device_id = -1);
208
223 void Init_minimal (MPI_Comm mpi_comm = MPI_COMM_WORLD);
224
230 [[nodiscard]] bool Initialized ();
231
241 void Finalize (AMReX* pamrex);
243 void Finalize ();
245 void Finalize_minimal();
246
256 void ExecOnFinalize (std::function<void()> func);
265 void ExecOnInitialize (std::function<void()> func);
266
271 template <class... Ts>
273 void ignore_unused (const Ts&...) {}
274
279 void Error (const std::string& msg);
280
291 void Error_host (const char* type, const char* msg);
292
295 void Error (const char* msg = nullptr) {
296#if defined(NDEBUG)
298#else
300 if (msg) { AMREX_DEVICE_PRINTF("Error %s\n", msg); }
302 ))
303#endif
304 AMREX_IF_ON_HOST((Error_host("Error", msg);))
305 }
306
311 void Warning (const std::string& msg);
312
317 void Warning_host (const char * msg);
318
321 void Warning (const char * msg) {
322#if defined(NDEBUG)
324#else
325 AMREX_IF_ON_DEVICE((if (msg) { AMREX_DEVICE_PRINTF("Warning %s\n", msg); }))
326#endif
328 }
329
334 void Abort (const std::string& msg);
335
338 void Abort (const char * msg = nullptr) {
339#if defined(NDEBUG)
341#else
343 if (msg) { AMREX_DEVICE_PRINTF("Abort %s\n", msg); }
345 ))
346#endif
347 AMREX_IF_ON_HOST((Error_host("Abort", msg);))
348 }
349
362 void Assert_host (const char* EX, const char* file, int line, const char* msg,
363 std::size_t msg_size=0);
364
378 void Assert (const char* EX, const char* file, int line) {
379#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
381#else
383 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d",
384 EX, file, line);
386 ))
387#endif
388 AMREX_IF_ON_HOST((Assert_host(EX,file,line,nullptr,0U);))
389 }
390
404 void Assert (const char* EX, const char* file, int line, const char* msg) {
405#if defined(NDEBUG) && !defined(AMREX_USE_ASSERTION)
406 AMREX_IF_ON_DEVICE((amrex::ignore_unused(EX,file,line,msg);))
407#else
409 AMREX_DEVICE_PRINTF("Assertion `%s' failed, file \"%s\", line %d, Msg: %s",
410 EX, file, line, msg);
412 ))
413#endif
414 AMREX_IF_ON_HOST((Assert_host(EX,file,line,msg,std::strlen(msg));))
415 }
416
419 void Assert (const char* EX, const char* file, int line, const std::string& msg) {
420 Assert_host(EX,file,line,msg.c_str(),msg.size());
421 }
422
431 void write_to_stderr_without_buffering (const char* str);
432
438
440 std::ostream& OutStream ();
442 std::ostream& ErrorStream ();
443
445 [[nodiscard]] int Verbose () noexcept;
450 void SetVerbose (int v) noexcept;
451
453 [[nodiscard]] bool InitSNaN () noexcept;
458 void SetInitSNaN (bool v) noexcept;
459
461 [[nodiscard]] std::string get_command ();
462
464 [[nodiscard]] int command_argument_count ();
465
471 [[nodiscard]] std::string get_command_argument (int number);
472
473#ifndef _MSC_VER
474 inline void GccPlacater ()
475 {
476 std::allocator<bool> a_b;
477 std::allocator<char> a_c;
478 std::allocator<int> a_i;
479 std::allocator<long> a_l;
480 std::allocator<long long> a_ll;
481 std::allocator<unsigned char> a_uc;
482 std::allocator<unsigned int> a_ui;
483 std::allocator<unsigned long> a_ul;
484 std::allocator<unsigned long long> a_ull;
485 std::allocator<float> a_f;
486 std::allocator<double> a_d;
487 std::allocator<std::string> a_s;
488
502 }
503#endif
504
505 class Geometry;
506
513 class AMReX
514 {
515 public:
516 AMReX ();
517 ~AMReX ();
518 AMReX (AMReX const&) = delete;
519 AMReX (AMReX &&) = delete;
520 AMReX& operator= (AMReX const&) = delete;
521 AMReX& operator= (AMReX &&) = delete;
522
524 static bool empty () noexcept { return m_instance.empty(); }
525
527 static int size () noexcept { return static_cast<int>(m_instance.size()); }
528
530 static AMReX* top () noexcept { return m_instance.back().get(); }
531
539 static void push (AMReX* pamrex);
540
542 static void push (std::unique_ptr<AMReX> pamrex);
543
545 static void erase (AMReX* pamrex);
546
548 [[nodiscard]] Geometry* getDefaultGeometry () noexcept { return m_geom; }
549
550 private:
551
552 static AMREX_EXPORT std::vector<std::unique_ptr<AMReX> > m_instance;
553
554 Geometry* m_geom = nullptr;
555 };
556
557 enum struct FPExcept : std::uint8_t {
558 none = 0B0000,
559 invalid = 0B0001,
560 zero = 0B0010,
561 overflow = 0B0100,
562 all = 0B0111
563 };
564
566 [[nodiscard]] inline bool any (FPExcept a) { return a != FPExcept::none; }
567
569 [[nodiscard]] inline FPExcept operator| (FPExcept a, FPExcept b)
570 {
571 using T = std::underlying_type_t<FPExcept>;
572 // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
573 return static_cast<FPExcept>(static_cast<T>(a) | static_cast<T>(b));
574 }
575
577 [[nodiscard]] inline FPExcept operator& (FPExcept a, FPExcept b)
578 {
579 using T = std::underlying_type_t<FPExcept>;
580 return static_cast<FPExcept>(static_cast<T>(a) & static_cast<T>(b));
581 }
582
585 [[nodiscard]] FPExcept getFPExcept ();
586
592 FPExcept setFPExcept (FPExcept excepts);
593
617 [[nodiscard]] FPExcept disableFPExcept (FPExcept excepts);
618
634 [[nodiscard]] FPExcept enableFPExcept (FPExcept excepts);
635}
636
637#endif /*BL_AMREX_H*/
AMReX exception types.
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:223
Assertions for GPU device code.
#define AMREX_DEVICE_ASSERT(flag)
Assert flag in CUDA, HIP, or SYCL device code.
Definition AMReX_GpuAssert.H:27
#define AMREX_DEVICE_PRINTF(...)
Definition AMReX_GpuPrint.H:15
#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:514
static bool empty() noexcept
True if no AMReX instances are registered on the stack.
Definition AMReX.H:524
static int size() noexcept
Number of managed instances currently tracked (Initialize depth).
Definition AMReX.H:527
AMReX(AMReX const &)=delete
static void erase(AMReX *pamrex)
Remove a specific instance pointer from the internal stack.
Definition AMReX.cpp:1054
Geometry * getDefaultGeometry() noexcept
Geometry owned by this context (constructed with the AMReX object).
Definition AMReX.H:548
static void push(AMReX *pamrex)
Transfer ownership of an existing AMReX* into the stack.
Definition AMReX.cpp:1035
~AMReX()
Definition AMReX.cpp:1029
AMReX()
Definition AMReX.cpp:1024
AMReX(AMReX &&)=delete
AMReX & operator=(AMReX const &)=delete
static AMReX * top() noexcept
Pointer to the most recently pushed instance.
Definition AMReX.H:530
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:85
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:126
bool abort_on_unused_inputs
Definition AMReX.cpp:125
bool handle_crt_reports
Definition AMReX.cpp:121
bool handle_sigfpe
Definition AMReX.cpp:119
bool call_addr2line
Definition AMReX.cpp:122
bool signal_handling
Definition AMReX.cpp:114
bool throw_exception
Definition AMReX.cpp:123
bool handle_sigsegv
Definition AMReX.cpp:115
bool handle_sigint
Definition AMReX.cpp:117
std::ostream * oserr
Definition AMReX.cpp:127
bool regtest_reduction
Definition AMReX.cpp:124
bool handle_sigterm
Definition AMReX.cpp:116
ErrorHandler error_handler
Definition AMReX.cpp:128
bool handle_sigabrt
Definition AMReX.cpp:118
int verbose
Definition AMReX.cpp:113
bool handle_sigill
Definition AMReX.cpp:120
std::string exename
Definition AMReX.cpp:112
constexpr bool NDebug
Definition AMReX.H:48
constexpr bool Debug
Definition AMReX.H:49
bool init_snan
Definition AMReX.cpp:132
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:378
__host__ __device__ void ignore_unused(const Ts &...)
No-op helper that marks variables as intentionally unused.
Definition AMReX.H:273
std::string get_command()
Get the entire command line including the executable path.
Definition AMReX.cpp:1000
void write_to_stderr_without_buffering(const char *str)
Write directly to stderr after flushing standard I/O buffers.
Definition AMReX.cpp:203
int command_argument_count()
Get the number of command line arguments excluding argv[0].
Definition AMReX.cpp:1006
void SetVerbose(int v) noexcept
Override the verbosity level used by amrex::Print().
Definition AMReX.cpp:186
std::ostream & ErrorStream()
Stream that receives diagnostic output (defaults to std::cerr).
Definition AMReX.cpp:994
FPExcept
Definition AMReX.H:557
void Init_minimal(MPI_Comm mpi_comm)
Minimal initialization for utilities that need only part of AMReX.
Definition AMReX.cpp:1187
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:192
bool InitSNaN() noexcept
Whether new allocations are filled with signaling NaNs when enabled.
Definition AMReX.cpp:188
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:346
std::string get_command_argument(int number)
Fetch a copy of argv[number] (argv[0] is the executable path).
Definition AMReX.cpp:1012
void Finalize()
Convenience overload that finalizes the instance at the top of the stack.
Definition AMReX.cpp:835
void Finalize_minimal()
Finalize a run that was initialized via Init_minimal().
Definition AMReX.cpp:1197
bool any(FPExcept a)
Return true when a is not FPExcept::none (i.e., the mask is non-empty).
Definition AMReX.H:566
FPExcept operator|(FPExcept a, FPExcept b)
Bitwise union of two floating-point exception masks.
Definition AMReX.H:569
void Error_host(const char *type, const char *msg)
Host-side error sink invoked by GPU-friendly overloads.
Definition AMReX.cpp:256
FPExcept setFPExcept(FPExcept excepts)
Definition AMReX.cpp:1134
void GccPlacater()
Definition AMReX.H:474
void Error(const std::string &msg)
Print a message to stderr and abort the program.
Definition AMReX.cpp:238
void Warning_host(const char *msg)
Print a warning message to the diagnostic stream (host only).
Definition AMReX.cpp:278
void SetInitSNaN(bool v) noexcept
Toggle whether new allocations are initialized with signaling NaNs.
Definition AMReX.cpp:190
FPExcept operator&(FPExcept a, FPExcept b)
Bitwise intersection of two floating-point exception masks.
Definition AMReX.H:577
int Verbose() noexcept
Return the verbosity level configured via ParmParse or SetVerbose().
Definition AMReX.cpp:184
void Warning(const std::string &msg)
Print a warning message to the diagnostic stream and keep running.
Definition AMReX.cpp:250
void ExecOnInitialize(std::function< void()> func)
Register a callback invoked early in Initialize().
Definition AMReX.cpp:340
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:244
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:988
FPExcept getFPExcept()
Definition AMReX.cpp:1120
FPExcept disableFPExcept(FPExcept excepts)
Disable FP exceptions. Linux and macOS only; a no-op elsewhere, including musl-based Linux.
Definition AMReX.cpp:1153
FPExcept enableFPExcept(FPExcept excepts)
Enable FP exceptions. Linux and macOS only; a no-op elsewhere, including musl-based Linux.
Definition AMReX.cpp:1170
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:286
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:829
void ExecOnFinalize(std::function< void()> func)
Register a callback invoked during Finalize() (LIFO order).
Definition AMReX.cpp:334