OGS
EigenLisLinearSolver.cpp
Go to the documentation of this file.
1
12
13#ifdef _OPENMP
14#include <omp.h>
15#endif
16
17#include "BaseLib/Logging.h"
22
23namespace MathLib
24{
25EigenLisLinearSolver::EigenLisLinearSolver(std::string const& /*solver_name*/,
26 std::string const& lis_options)
27 : lis_options_(lis_options)
28{
29}
30
32 EigenVector& x_)
33{
34 static_assert(EigenMatrix::RawMatrixType::IsRowMajor,
35 "Sparse matrix is required to be in row major storage.");
36 auto& A = A_.getRawMatrix();
37 auto& b = b_.getRawVector();
38 auto& x = x_.getRawVector();
39
40 if (!A.isCompressed())
41 {
42 A.makeCompressed();
43 }
44 int nnz = A.nonZeros();
45 int* ptr = A.outerIndexPtr();
46 int* col = A.innerIndexPtr();
47 double* data = A.valuePtr();
48 LisMatrix lisA(A_.getNumberOfRows(), nnz, ptr, col, data);
49 LisVector lisb(b.rows(), b.data());
50 LisVector lisx(x.rows(), x.data());
51
52 bool const status = solve(lisA, lisb, lisx);
53
54 for (std::size_t i = 0; i < lisx.size(); i++)
55 {
56 x[i] = lisx[i];
57 }
58
59 return status;
60}
61
63{
65
66 INFO("------------------------------------------------------------------");
67 INFO("*** LIS solver computation");
68
69 // Create solver
70 LIS_SOLVER solver;
71 int ierr = lis_solver_create(&solver);
72 if (!checkLisError(ierr))
73 {
74 return false;
75 }
76 lis_solver_set_option(const_cast<char*>(lis_options_.c_str()), solver);
77#ifdef _OPENMP
78 INFO("-> number of threads: {:d}", (int)omp_get_max_threads());
79#endif
80 {
81 int precon;
82 ierr = lis_solver_get_precon(solver, &precon);
83 if (!checkLisError(ierr))
84 {
85 return false;
86 }
87 INFO("-> precon: {:d}", precon);
88 }
89 {
90 int slv;
91 ierr = lis_solver_get_solver(solver, &slv);
92 if (!checkLisError(ierr))
93 {
94 return false;
95 }
96 INFO("-> solver: {:d}", slv);
97 }
98
99 // solve
100 INFO("-> solve");
101 ierr =
102 lis_solve(A.getRawMatrix(), b.getRawVector(), x.getRawVector(), solver);
103 if (!checkLisError(ierr))
104 {
105 return false;
106 }
107
108 LIS_INT linear_solver_status;
109 ierr = lis_solver_get_status(solver, &linear_solver_status);
110 if (!checkLisError(ierr))
111 {
112 return false;
113 }
114
115 INFO("-> status: {:d}", linear_solver_status);
116
117 {
118 int iter = 0;
119 ierr = lis_solver_get_iter(solver, &iter);
120 if (!checkLisError(ierr))
121 {
122 return false;
123 }
124
125 INFO("-> iteration: {:d}", iter);
126 }
127 {
128 double resid = 0.0;
129 ierr = lis_solver_get_residualnorm(solver, &resid);
130 if (!checkLisError(ierr))
131 {
132 return false;
133 }
134 INFO("-> residual: {:g}", resid);
135 }
136 {
137 double time, itime, ptime, p_ctime, p_itime;
138 ierr = lis_solver_get_timeex(solver, &time, &itime, &ptime, &p_ctime,
139 &p_itime);
140 if (!checkLisError(ierr))
141 {
142 return false;
143 }
144 INFO("-> time total (s): {:g}", time);
145 INFO("-> time iterations (s): {:g}", itime);
146 INFO("-> time preconditioning (s): {:g}", ptime);
147 INFO("-> time precond. create (s): {:g}", p_ctime);
148 INFO("-> time precond. iter (s): {:g}", p_itime);
149 }
150
151 // Clear solver
152 ierr = lis_solver_destroy(solver);
153 if (!checkLisError(ierr))
154 {
155 return false;
156 }
157 INFO("------------------------------------------------------------------");
158
159 return linear_solver_status == LIS_SUCCESS;
160}
161
162} // namespace MathLib
Definition of the LisMatrix class.
Definition of the LisVector class.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
EigenLisLinearSolver(std::string const &solver_name, std::string const &lis_options)
bool solve(EigenMatrix &A, EigenVector &b, EigenVector &x)
IndexType getNumberOfRows() const
return the number of rows
Definition EigenMatrix.h:53
RawMatrixType & getRawMatrix()
Global vector based on Eigen vector.
Definition EigenVector.h:25
RawVectorType & getRawVector()
return a raw Eigen vector object
LisMatrix is a wrapper class for matrix types of the linear iterative solvers library.
Definition LisMatrix.h:43
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition LisMatrix.h:114
Lis vector wrapper class.
Definition LisVector.h:29
LIS_VECTOR & getRawVector()
return a raw Lis vector object
Definition LisVector.h:91
std::size_t size() const
return a vector length
Definition LisVector.cpp:46
bool checkLisError(int err)
Definition LisCheck.h:31
bool finalizeMatrixAssembly(MAT_T &)