LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
ctmc_gmres_multi.h
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2012-2026, QORE Lab, Imperial College London
3
* All rights reserved.
4
*/
5
#ifndef LINE_API_MC_CTMC_GMRES_MULTI_H
6
#define LINE_API_MC_CTMC_GMRES_MULTI_H
7
8
/**
9
* @file
10
* @ingroup api_mc
11
* Restarted GMRES for a block of right-hand sides sharing one coefficient
12
* matrix.
13
*
14
* Templated port of matlab/src/api/mc/ctmc_gmres_multi.m and the multi-column
15
* overload of jar/src/main/java/jline/api/mc/Ctmc_gmres.java. Every column is
16
* solved against the SAME equilibration, reordering and ILUT factorization, and
17
* each column starts from the previous column's solution: this is the shape of
18
* the stochastic complement, whose right-hand side is a whole block of the
19
* generator, and refactorizing per column would cost more than the direct solve
20
* the method replaces.
21
*
22
* FLAG is zero only when every column converged. On any other value the result
23
* matrix is empty and the caller must fall back to the direct solve; returning
24
* a partially converged block would leave the fallback ambiguous, which is the
25
* reference's rule and is kept.
26
*
27
* GATED ON TRANSCENDENTAL ARITHMETIC, for the reason given in ctmc_gmres: the
28
* iteration stops on a residual tolerance and normalizes by a Euclidean norm.
29
*/
30
31
#include <cstddef>
32
#include <vector>
33
34
#include "
line/api/mc/ctmc_gmres.h
"
35
#include "
line/num/number.h
"
36
#include "
line/util/error.h
"
37
#include "
line/util/matrix.h
"
38
39
namespace
line
{
40
namespace
mc
{
41
42
template
<
class
T>
43
struct
GmresMultiResult
{
44
Matrix<T>
X
;
///< solution block, empty unless flag == 0
45
int
flag
;
///< 0 all columns converged, otherwise the first failing flag
46
};
47
48
/**
49
* @brief Restarted GMRES for a block of right-hand sides sharing one
50
* coefficient matrix.
51
*
52
* @param A coefficient matrix
53
* @param B right-hand sides, one per column
54
* @param tol relative residual tolerance (default 1e-12)
55
* @param restart restart length; <= 0 selects min(n, 50)
56
* @param maxit outer cycles; <= 0 selects ceil(n / restart)
57
*/
58
template
<
class
T>
59
GmresMultiResult<T>
ctmc_gmres_multi
(
const
Matrix<T>
& A,
const
Matrix<T>
& B,
double
tol = 1e-12,
60
long
restart = 0,
long
maxit = 0) {
61
static_assert
(
num_traits<T>::has_transcendental
,
62
"ctmc_gmres_multi requires transcendental arithmetic: see ctmc_gmres, the "
63
"iteration stops on a residual tolerance rather than reaching an exact value"
);
64
const
std::size_t n = A.
rows
();
65
if
(A.
cols
() != n)
throw
InputError
(
"ctmc_gmres_multi: matrix is not square"
);
66
if
(B.
rows
() != n)
throw
InputError
(
"ctmc_gmres_multi: right-hand side block has the wrong height"
);
67
68
const
detail::GmresPrepared<T> prep(A);
69
const
std::size_t nrhs = B.
cols
();
70
Matrix<T>
X(n, nrhs,
num_traits<T>::from_int
(0));
71
std::vector<T> guess(n,
num_traits<T>::from_int
(1) /
num_traits<T>::from_int
(
static_cast<
long
>
(n)));
72
std::vector<T> rhs(n);
73
74
GmresMultiResult<T>
out;
75
for
(std::size_t c = 0; c < nrhs; ++c) {
76
for
(std::size_t i = 0; i < n; ++i) rhs[i] = B(i, c);
77
const
GmresResult<T>
r = detail::gmres_solve(prep, rhs, guess, tol, restart, maxit);
78
if
(r.
flag
!= 0) {
79
out.
X
=
Matrix<T>
();
80
out.
flag
= r.
flag
;
81
return
out;
82
}
83
for
(std::size_t i = 0; i < n; ++i) X(i, c) = r.
x
[i];
84
guess = r.
x
;
85
}
86
out.
X
= X;
87
out.
flag
= 0;
88
return
out;
89
}
90
91
}
// namespace mc
92
}
// namespace line
93
94
#endif
// LINE_API_MC_CTMC_GMRES_MULTI_H
line::InputError::InputError
InputError(const std::string &what)
Definition
error.h:39
line::Matrix
Definition
matrix.h:56
line::Matrix::cols
std::size_t cols() const
Definition
matrix.h:90
line::Matrix::Matrix
Matrix()
Definition
matrix.h:58
line::Matrix::rows
std::size_t rows() const
Definition
matrix.h:89
ctmc_gmres.h
Restarted GMRES with an ILUT preconditioner, for the linear systems a generator produces.
error.h
The exception types the port throws.
matrix.h
Dense matrix and non-owning view.
line::mc
Definition
ctmc_bicgstab.h:59
line::mc::ctmc_gmres_multi
GmresMultiResult< T > ctmc_gmres_multi(const Matrix< T > &A, const Matrix< T > &B, double tol=1e-12, long restart=0, long maxit=0)
Restarted GMRES for a block of right-hand sides sharing one coefficient matrix.
Definition
ctmc_gmres_multi.h:59
line
Definition
aoi_dist2ph.h:52
number.h
Number-type abstraction for the templated API port.
line::mc::GmresMultiResult
Definition
ctmc_gmres_multi.h:43
line::mc::GmresMultiResult::flag
int flag
0 all columns converged, otherwise the first failing flag
Definition
ctmc_gmres_multi.h:45
line::mc::GmresMultiResult::X
Matrix< T > X
solution block, empty unless flag == 0
Definition
ctmc_gmres_multi.h:44
line::mc::GmresResult
Definition
ctmc_gmres.h:78
line::mc::GmresResult::flag
int flag
0 converged, 1 iteration limit, 3 stagnation/divergence
Definition
ctmc_gmres.h:80
line::mc::GmresResult::x
std::vector< T > x
solution
Definition
ctmc_gmres.h:79
line::num_traits
Definition
number.h:111
include
line
api
mc
ctmc_gmres_multi.h
Generated by
1.18.0