Fenix
@develop
Loading...
Searching...
No Matches
fenix_util.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_UTIL__
58
#define __FENIX_UTIL__
59
60
#include <mpi.h>
61
#include "fenix.hpp"
62
#include "fenix_opt.hpp"
63
64
namespace
fenix::util {
65
66
int
resume_application(
bool
new_exception =
false
);
67
68
// ScopedOptions hold the old option and revert to it in their destructors, so
69
// changes revert even when exceptions are thrown.
70
struct
ScopedOption
{
71
ScopedOption
(
SettingName
m_setting,
int
new_option) : setting(m_setting) {
72
set_option(setting, new_option);
73
}
74
~ScopedOption
() {
75
if
(fenix::initialized()) set_option(setting, old);
76
}
77
78
// No moving or copying scoped options, things would get complicated.
79
ScopedOption
(
const
ScopedOption
&) =
delete
;
80
ScopedOption
(
ScopedOption
&&) =
delete
;
81
82
const
SettingName
setting;
83
const
int
old = get_option(setting);
84
};
85
86
// Default error handling for inside the Fenix runtime.
87
struct
ScopedDefaultRuntimeOptions
{
88
ScopedOption
resume{RESUME_MODE, THROW}, unhandled{UNHANDLED_MODE, ABORT};
89
};
90
91
// Helper for MPI_ERRORS_RETURN-like error handling
92
struct
ScopedIgnoreAndReturn
{
93
ScopedOption
recovery{RECOVERY_MODE, IGNORE}, resume{RESUME_MODE, RETURN};
94
};
95
96
struct
ScopedActiveMlog
{
97
ScopedActiveMlog
(
int
id
) : old_mlog(mlog::active()) { mlog::activate(
id
); }
98
~ScopedActiveMlog
() {
99
if
(fenix::initialized()) mlog::activate(old_mlog);
100
}
101
const
int
old_mlog;
102
const
bool
old_inline_recovery = old_mlog != FENIX_MLOG_NONE &&
103
get_option(MLOG_RECOVERY_MODE) != MANUAL &&
104
get_option(RECOVERY_MODE) != IGNORE;
105
};
106
107
}
// namespace fenix::util
108
109
// clang-format off
110
#define RUNTIME_EXCEPTION_HANDLER \
111
} catch (const fenix::RuntimeException& e) { \
112
debug_print("%s\n", e.what()); \
113
return e.error;
114
115
#define COMM_EXCEPTION_HANDLER \
116
} catch (const fenix::CommException& e) { \
117
return fenix::util::resume_application(); \
118
}
119
120
#define FENIX_CPP_API_BEGIN \
121
try { \
122
fenix::util::ScopedDefaultRuntimeOptions _scoped_dro; \
123
if (!fenix::initialized()) FENIX_THROW(FENIX_ERROR_UNINITIALIZED);
124
125
// clang-format on
126
127
#define FENIX_C_API_BEGIN FENIX_CPP_API_BEGIN
128
129
#ifdef FENIX_C_CATCH_RUNTIME_EXCEPTIONS
130
#define FENIX_C_API_END RUNTIME_EXCEPTION_HANDLER COMM_EXCEPTION_HANDLER
131
#else
132
#define FENIX_C_API_END COMM_EXCEPTION_HANDLER
133
#endif
134
135
#ifdef FENIX_CPP_CATCH_RUNTIME_EXCEPTIONS
136
#define FENIX_CPP_API_END RUNTIME_EXCEPTION_HANDLER COMM_EXCEPTION_HANDLER
137
#else
138
#define FENIX_CPP_API_END COMM_EXCEPTION_HANDLER
139
#endif
140
141
// Local functions are ones that can be called before Fenix is initialized
142
#define FENIX_LOCAL_CPP_API_BEGIN try {
143
#define FENIX_LOCAL_CPP_API_END FENIX_CPP_API_END
144
145
#define FENIX_LOCAL_C_API_BEGIN FENIX_LOCAL_CPP_API_BEGIN
146
#define FENIX_LOCAL_C_API_END FENIX_C_API_END
147
148
#endif
Fenix_Setting_name
Fenix_Setting_name
Global Fenix settings.
Definition
fenix.h:185
fenix::util::ScopedActiveMlog
Definition
fenix_util.hpp:96
fenix::util::ScopedDefaultRuntimeOptions
Definition
fenix_util.hpp:87
fenix::util::ScopedIgnoreAndReturn
Definition
fenix_util.hpp:92
fenix::util::ScopedOption
Definition
fenix_util.hpp:70
include
fenix_util.hpp
Generated by
1.12.0