alpaqa
1.0.0a18
Nonconvex constrained optimization
Toggle main menu visibility
Main Page
Related Pages
Topics
Namespaces
Namespace List
Namespace Members
All
a
b
c
d
e
f
g
i
j
l
m
n
o
p
r
s
t
u
v
w
Functions
a
b
c
d
e
f
g
i
j
l
m
n
o
p
r
s
t
u
w
Variables
Typedefs
a
c
d
e
f
i
l
m
p
r
s
t
u
v
Enumerations
Concepts
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
û
ŷ
α
γ
δ
ε
ζ
λ
ν
ρ
σ
τ
φ
ψ
ϵ
Functions
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
û
α
ρ
Variables
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
ŷ
α
γ
δ
ε
ζ
λ
ν
ρ
σ
τ
φ
ψ
ϵ
Typedefs
a
b
c
d
e
f
g
h
i
l
m
n
o
p
r
s
t
v
w
Enumerations
Enumerator
a
f
i
s
u
Related Symbols
Files
File List
File Members
All
a
c
d
e
f
g
l
m
p
r
s
t
u
w
Functions
a
c
d
e
f
g
l
m
p
r
s
t
w
Variables
Typedefs
Enumerations
Enumerator
Macros
a
c
e
p
u
Examples
Sphinx
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Modules
Pages
Concepts
Loading...
Searching...
No Matches
src
alpaqa
src
driver
cancel.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <atomic>
4
#include <csignal>
5
#include <memory>
6
#include <stdexcept>
7
8
namespace
alpaqa
{
9
10
namespace
detail
{
11
inline
std::atomic<void *>
solver_to_stop
;
12
}
13
14
/**
15
* Attach SIGINT and SIGTERM handlers to stop the given solver.
16
* @param solver
17
* The solver that should be stopped by the handler.
18
* @return A RAII object that detaches the handler when destroyed.
19
*/
20
template
<
class
Solver>
21
[[
nodiscard
]]
auto
attach_cancellation
(
Solver
&solver) {
22
using
detail::solver_to_stop
;
23
using
solver_to_stop_t
=
decltype
(solver_to_stop);
24
if
constexpr
(
requires
{ solver.stop(); }) {
25
auto
*
old
= solver_to_stop.exchange(&solver, std::memory_order_release);
26
if
(
old
) {
27
throw
std::logic_error(
28
"alpaqa-driver:attach_cancellation can only be used once"
);
29
}
30
auto
handler
= +[](
int
) {
31
if
(
auto
*s = solver_to_stop.load(std::memory_order::acquire))
32
reinterpret_cast<
Solver
*
>
(s)->stop();
33
};
34
#ifdef _WIN32
35
signal
(
SIGINT
,
handler
);
36
signal
(
SIGTERM
,
handler
);
37
#else
38
struct
sigaction
action
;
39
action
.sa_handler =
handler
;
40
sigemptyset
(&
action
.sa_mask);
41
action
.sa_flags = 0;
42
sigaction
(
SIGINT
, &
action
,
nullptr
);
43
sigaction
(
SIGTERM
, &
action
,
nullptr
);
44
#endif
45
auto
detach_solver
= +[](
solver_to_stop_t
*p) {
46
#ifdef _WIN32
47
signal
(
SIGINT
,
SIG_DFL
);
48
signal
(
SIGTERM
,
SIG_DFL
);
49
#else
50
struct
sigaction
action
;
51
action
.sa_handler =
SIG_DFL
;
52
sigemptyset
(&
action
.sa_mask);
53
action
.sa_flags = 0;
54
sigaction
(
SIGINT
, &
action
,
nullptr
);
55
sigaction
(
SIGTERM
, &
action
,
nullptr
);
56
#endif
57
p->store(
nullptr
, std::memory_order_relaxed);
58
// Don't reorder this store with subsequent destruction of solver
59
std::atomic_signal_fence(std::memory_order_release);
60
};
61
return
std::unique_ptr<
solver_to_stop_t
,
decltype
(
detach_solver
)>{
62
&solver_to_stop,
detach_solver
};
63
}
else
{
64
struct
[[
maybe_unused
]] empty {};
65
return
empty{};
66
}
67
}
21
[[
nodiscard
]]
auto
attach_cancellation
(
Solver
&solver) {
…
}
68
69
}
// namespace alpaqa
alpaqa::detail::solver_to_stop
std::atomic< void * > solver_to_stop
Definition
cancel.hpp:11
alpaqa
Definition
anderson.hpp:10
alpaqa::attach_cancellation
auto attach_cancellation(Solver &solver)
Attach SIGINT and SIGTERM handlers to stop the given solver.
Definition
cancel.hpp:21
alpaqa::inf
constexpr const auto inf
Definition
config.hpp:112
detail
Definition
results.hpp:127
Generated on Wed Mar 27 2024 for alpaqa by
1.9.8