Fenix @develop
 
Loading...
Searching...
No Matches
fenix_opt.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5//
6// _|_|_|_| _|_|_|_| _| _| _|_|_| _| _|
7// _| _| _|_| _| _| _| _|
8// _|_|_| _|_|_| _| _| _| _| _|
9// _| _| _| _|_| _| _| _|
10// _| _|_|_|_| _| _| _|_|_| _| _|
11//
12//
13//
14//
15// Copyright (C) 2016 Rutgers University and Sandia Corporation
16//
17// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18// the U.S. Government retains certain rights in this software.
19//
20// Redistribution and use in source and binary forms, with or without
21// modification, are permitted provided that the following conditions are
22// met:
23//
24// 1. Redistributions of source code must retain the above copyright
25// notice, this list of conditions and the following disclaimer.
26//
27// 2. Redistributions in binary form must reproduce the above copyright
28// notice, this list of conditions and the following disclaimer in the
29// documentation and/or other materials provided with the distribution.
30//
31// 3. Neither the name of the Corporation nor the names of the
32// contributors may be used to endorse or promote products derived from
33// this software without specific prior written permission.
34//
35// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
36// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
39// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46//
47// Author Marc Gamell, Eric Valenzuela, Keita Teranishi, Manish Parashar
48// Michael Heroux, and Matthew Whitlock
49//
50// Questions? Contact Keita Teranishi (knteran@sandia.gov) and
51// Marc Gamell (mgamell@cac.rutgers.edu)
52//
53// ************************************************************************
54//@HEADER
55*/
56
57#ifndef __FENIX_OPT__
58#define __FENIX_OPT__
59
60#include <cstdio>
61#include <cstdlib>
62
63// FENIX_ABORT kills whole MPI job if MPI visible in current file, else just
64// aborts this process
65// Prefer fenix_assert or fatal_print instead of using this directly
66#ifdef MPI_VERSION
67#define FENIX_ABORT() \
68 do { \
69 int mpi_is_init; \
70 MPI_Initialized(&mpi_is_init); \
71 if (mpi_is_init) MPI_Abort(MPI_COMM_WORLD, 1); \
72 abort(); \
73 } while (0)
74#else
75#define FENIX_ABORT() abort()
76#endif
77
78// Helpers needing to support printing w/o any user-supplied format args
79// Supports up to 10 args
80// Functions should be named base_name_s for 1 args or base_name_a otherwise
81#define FN_SUFF_I(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME, ...) NAME
82#define FN_SUFF(...) \
83 FN_SUFF_I(__VA_ARGS__, _a, _a, _a, _a, _a, _a, _a, _a, _a, _s)
84#define FN_SUFF_MERGE_IMPL(fn, suff) fn##suff
85#define FN_SUFF_MERGE(fn, suff) FN_SUFF_MERGE_IMPL(fn, suff)
86#define FN_NAME(base_name, ...) FN_SUFF_MERGE(base_name, FN_SUFF(__VA_ARGS__))
87
88#define TRACE_PRINT_FMT "%s:%d %s(): "
89#define TRACE_PRINT_ARG __FILE__, __LINE__, __func__
90
91#define traced_print_s(file, fmt) \
92 fprintf(file, TRACE_PRINT_FMT fmt "\n", TRACE_PRINT_ARG)
93#define traced_print_a(file, fmt, ...) \
94 fprintf(file, TRACE_PRINT_FMT fmt "\n", TRACE_PRINT_ARG, __VA_ARGS__)
95#define traced_print(file, ...) \
96 FN_NAME(traced_print, __VA_ARGS__)(file, __VA_ARGS__)
97
98#define debug_print(...) traced_print(stderr, __VA_ARGS__)
99#define verbose_print(...) traced_print(stdout, __VA_ARGS__)
100
101//Multi-line macro functions wrapped in do-while to maintain correct behavior
102//regardless of what surrounding code is
103#define fatal_print(...) \
104 do { \
105 traced_print(stderr, __VA_ARGS__); \
106 traced_print(stderr, "Fenix aborting due to fatal error!"); \
107 FENIX_ABORT(); \
108 } while (0)
109
110#define fenix_assert_a(predicate, ...) \
111 do { \
112 if (!(predicate)) { \
113 fatal_print(__VA_ARGS__); \
114 } \
115 } while (0)
116#define fenix_assert_s(predicate) \
117 fenix_assert_a( \
118 predicate, "internal error, failed assertion (%s)", #predicate \
119 );
120
121#ifdef NDEBUG
122//Disable assertions when NDEBUG
123#define fenix_assert(...) \
124 do { \
125 } while (0)
126#else
127#define fenix_assert(...) FN_NAME(fenix_assert, __VA_ARGS__)(__VA_ARGS__)
128#endif
129
130typedef struct __fenix_debug_opt_t {
131 int verbose = -1;
133
134void __fenix_init_opt(int argc, char** argv);
135
136#endif
Definition fenix_opt.hpp:130