38 typedef AlgebraT AlgebraType;
39 typedef typename AlgebraType::Matrix MatrixType;
40 typedef typename AlgebraType::Vector VectorType;
41 typedef typename MatrixType::ValueType ValueType;
42 typedef typename AlgebraType::FutureType FutureType;
45 CG(AlgebraType& algebra, ITraceMng* trace_mng =
nullptr)
47 , m_trace_mng(trace_mng)
53 void setOutputLevel(
int level)
55 m_output_level = level;
58 template <
typename PrecondT,
typename iterT>
59 int solve(PrecondT& precond,
67 ValueType rho(0), rho1(0), alpha(0);
68 VectorType p, z, q, r;
70 m_algebra.allocate(AlgebraType::resource(A), p, z, q, r);
75 m_algebra.mult(A, x, p);
76 m_algebra.axpy(-1., p, r);
89 m_algebra.exec(precond, r, z);
90 rho1 = m_algebra.dot(r, z);
92 m_algebra.mult(A, p, q);
93 alpha = m_algebra.dot(p, q);
94 if (m_output_level > 1)
95 _print(0,
"Seq 1",
"rho", rho1,
"alpha", alpha);
99 m_algebra.free(p, z, q, r);
103 throw typename AlgebraType::NullValueException(
"alpha");
105 alpha = rho1 / alpha;
106 m_algebra.axpy(alpha, p, x);
107 m_algebra.axpy(-alpha, q, r);
111 while (!iter.stop(r)) {
124 m_algebra.exec(precond, r, z);
125 rho1 = m_algebra.dot(r, z);
127 m_algebra.axpy(alpha, p, z);
128 m_algebra.copy(z, p);
129 m_algebra.mult(A, p, q);
130 alpha = m_algebra.dot(q, p);
134 m_algebra.free(p, z, q, r);
138 throw typename AlgebraType::NullValueException(
"alpha");
140 alpha = rho1 / alpha;
141 m_algebra.axpy(alpha, p, x);
142 m_algebra.axpy(-alpha, q, r);
147 m_algebra.free(p, z, q, r);
152 template <
typename PrecondT,
typename iterT>
153 int solve2(PrecondT& precond,
162 ValueType rho(0), rho1(0), alpha(0);
163 FutureType frho(rho), frho1(rho1), falpha(alpha);
164 VectorType p, z, q, r;
166 m_algebra.allocate(AlgebraType::resource(A), p, z, q, r);
170 m_algebra.copy(b, r);
171 m_algebra.mult(A, x, p);
172 m_algebra.axpy(-1., p, r);
185 m_algebra.exec(precond, r, z);
186 m_algebra.copy(z, p);
187 m_algebra.mult(A, p, q);
188 m_algebra.dot(r, p, frho1);
189 m_algebra.dot(p, q, falpha);
190 if (falpha.get() == 0) {
193 m_algebra.free(p, z, q, r);
197 throw typename AlgebraType::NullValueException(
"alpha");
199 alpha = frho1.get() / alpha;
200 m_algebra.axpy(alpha, p, x);
201 m_algebra.axpy(-alpha, q, r);
205 while (!iter.stop(r)) {
218 m_algebra.exec(precond, r, z);
219 m_algebra.dot(r, z, frho1);
220 alpha = frho1.get() / rho;
221 m_algebra.axpy(alpha, p, z);
222 m_algebra.copy(z, p);
223 m_algebra.mult(A, p, q);
224 m_algebra.dot(p, q, falpha);
225 if (falpha.get() == 0) {
228 m_algebra.free(p, z, q, r);
232 throw typename AlgebraType::NullValueException(
"alpha");
234 alpha = rho1 / alpha;
235 m_algebra.axpy(alpha, p, x);
236 m_algebra.axpy(-alpha, q, r);
241 m_algebra.free(p, z, q, r);
248 _print(
int iter, std::string
const& msg, std::string
const& label0,
252 m_trace_mng->info() << msg;
253 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label0 <<
" "
259 _print(
int iter, std::string
const& msg, std::string
const& label0,
260 ValueType value0, std::string
const& label1, ValueType value1)
263 _print(iter, msg, label0, value0);
264 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label1 <<
" "
270 _print(
int iter, std::string
const& msg, std::string
const& label0,
271 ValueType value0, std::string
const& label1, ValueType value1,
272 std::string
const& label2, ValueType value2)
275 _print(iter, msg, label0, value0, label1, value1);
276 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label2 <<
" "
282 _print(
int iter, std::string
const& msg, std::string
const& label0,
283 ValueType value0, std::string
const& label1, ValueType value1,
284 std::string
const& label2, ValueType value2,
285 std::string
const& label3, ValueType value3)
288 _print(iter, msg, label0, value0, label1, value1, label2, value2);
289 m_trace_mng->info() <<
"Iterate: " << iter <<
" " << label3 <<
" "
294 AlgebraType& m_algebra;
295 ITraceMng* m_trace_mng =
nullptr;
296 int m_output_level = 0;