21 typedef AlgebraT AlgebraType;
22 typedef typename AlgebraType::Matrix MatrixType;
23 typedef typename AlgebraType::Vector VectorType;
24 typedef typename MatrixType::ValueType ValueType;
25 typedef typename AlgebraType::FutureType FutureType;
28 CG(AlgebraType& algebra, ITraceMng* trace_mng =
nullptr)
30 , m_trace_mng(trace_mng)
36 void setOutputLevel(
int level)
38 m_output_level = level;
41 template <
typename PrecondT,
typename iterT>
42 int solve(PrecondT& precond,
50 ValueType rho(0), rho1(0), alpha(0);
51 VectorType p, z, q, r;
53 m_algebra.allocate(AlgebraType::resource(A), p, z, q, r);
58 m_algebra.mult(A, x, p);
59 m_algebra.axpy(-1., p, r);
72 m_algebra.exec(precond, r, z);
73 rho1 = m_algebra.dot(r, z);
75 m_algebra.mult(A, p, q);
76 alpha = m_algebra.dot(p, q);
77 if (m_output_level > 2)
78 _print(0,
"Seq 1",
"rho", rho1,
"alpha", alpha);
82 m_algebra.free(p, z, q, r);
86 throw typename AlgebraType::NullValueException(
"alpha");
89 m_algebra.axpy(alpha, p, x);
90 m_algebra.axpy(-alpha, q, r);
94 while (!iter.stop(r)) {
107 m_algebra.exec(precond, r, z);
108 rho1 = m_algebra.dot(r, z);
110 m_algebra.axpy(alpha, p, z);
111 m_algebra.copy(z, p);
112 m_algebra.mult(A, p, q);
113 alpha = m_algebra.dot(q, p);
117 m_algebra.free(p, z, q, r);
121 throw typename AlgebraType::NullValueException(
"alpha");
123 alpha = rho1 / alpha;
124 m_algebra.axpy(alpha, p, x);
125 m_algebra.axpy(-alpha, q, r);
130 m_algebra.free(p, z, q, r);
135 template <
typename PrecondT,
typename iterT>
136 int solve2(PrecondT& precond,
145 ValueType rho(0), rho1(0), alpha(0);
146 FutureType frho(rho), frho1(rho1), falpha(alpha);
147 VectorType p, z, q, r;
149 m_algebra.allocate(AlgebraType::resource(A), p, z, q, r);
153 m_algebra.copy(b, r);
154 m_algebra.mult(A, x, p);
155 m_algebra.axpy(-1., p, r);
168 m_algebra.exec(precond, r, z);
169 m_algebra.copy(z, p);
170 m_algebra.mult(A, p, q);
171 m_algebra.dot(r, p, frho1);
172 m_algebra.dot(p, q, falpha);
173 if (falpha.get() == 0) {
176 m_algebra.free(p, z, q, r);
180 throw typename AlgebraType::NullValueException(
"alpha");
182 alpha = frho1.get() / alpha;
183 m_algebra.axpy(alpha, p, x);
184 m_algebra.axpy(-alpha, q, r);
188 while (!iter.stop(r)) {
201 m_algebra.exec(precond, r, z);
202 m_algebra.dot(r, z, frho1);
203 alpha = frho1.get() / rho;
204 m_algebra.axpy(alpha, p, z);
205 m_algebra.copy(z, p);
206 m_algebra.mult(A, p, q);
207 m_algebra.dot(p, q, falpha);
208 if (falpha.get() == 0) {
211 m_algebra.free(p, z, q, r);
215 throw typename AlgebraType::NullValueException(
"alpha");
217 alpha = rho1 / alpha;
218 m_algebra.axpy(alpha, p, x);
219 m_algebra.axpy(-alpha, q, r);
224 m_algebra.free(p, z, q, r);
231 _print(
int iter, std::string
const& msg, std::string
const& label0,
235 m_trace_mng->info() << msg;
236 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label0 <<
" "
242 _print(
int iter, std::string
const& msg, std::string
const& label0,
243 ValueType value0, std::string
const& label1, ValueType value1)
246 _print(iter, msg, label0, value0);
247 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label1 <<
" "
253 _print(
int iter, std::string
const& msg, std::string
const& label0,
254 ValueType value0, std::string
const& label1, ValueType value1,
255 std::string
const& label2, ValueType value2)
258 _print(iter, msg, label0, value0, label1, value1);
259 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label2 <<
" "
265 _print(
int iter, std::string
const& msg, std::string
const& label0,
266 ValueType value0, std::string
const& label1, ValueType value1,
267 std::string
const& label2, ValueType value2,
268 std::string
const& label3, ValueType value3)
271 _print(iter, msg, label0, value0, label1, value1, label2, value2);
272 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label3 <<
" "
277 AlgebraType& m_algebra;
278 ITraceMng* m_trace_mng =
nullptr;
279 int m_output_level = 0;