44 #ifndef MULTIPRECCG_HPP_ 45 #define MULTIPRECCG_HPP_ 51 #include <Teuchos_FancyOStream.hpp> 53 #include <Tpetra_CrsMatrix.hpp> 54 #include <Tpetra_Vector.hpp> 55 #include <Tpetra_RTI.hpp> 56 #include <Tpetra_MatrixIO.hpp> 62 # include <qd/qd_real.h> 68 template <
class T1,
class T2>
69 class ZeroOp<std::pair<T1,T2>> {
71 static inline std::pair<T1,T2> identity() {
85 using std::binary_function;
89 using std::multiplies;
91 struct trivial_fpu_fix {
96 struct nontrivial_fpu_fix {
98 void fix() {fpu_fix_start(&old_cw);}
99 void unfix() {fpu_fix_end(&old_cw);}
103 template <
class T>
struct fpu_fix : trivial_fpu_fix {};
104 #ifdef HAVE_TPETRA_QD 105 template <>
struct fpu_fix<qd_real> : nontrivial_fpu_fix {};
106 template <>
struct fpu_fix<dd_real> : nontrivial_fpu_fix {};
110 template <
class Tout,
class Tin,
class LO,
class GO,
class Node>
114 return A->template convert<Tout>();
119 template <
class T,
class LO,
class GO,
class Node>
120 struct convertHelp<T,T,LO,GO,Node> {
131 template <
class T1,
class T2,
class Op>
132 class pair_op :
public binary_function<pair<T1,T2>,pair<T1,T2>,pair<T1,T2>> {
136 pair_op(Op op) : op_(op) {}
137 inline pair<T1,T2> operator()(
const pair<T1,T2>& a,
const pair<T1,T2>& b)
const {
138 return make_pair(op_(a.first,b.first),op_(a.second,b.second));
143 template <
class T1,
class T2,
class Op>
144 pair_op<T1,T2,Op>
make_pair_op(Op op) {
return pair_op<T1,T2,Op>(op); }
147 template <
class S,
class LO,
class GO,
class Node>
172 db->set(
"numIters", params.
get<
int>(
"numIters",A->getGlobalNumRows()) );
173 db->set(
"tolerance",params.
get<
double>(
"tolerance",1e-7));
174 db->set(
"verbose", params.
get<
int>(
"verbose",0) );
175 db->set(
"bx", Tpetra::createVector<T>(map) );
176 db->set(
"r", Tpetra::createVector<T>(map) );
177 db->set(
"z", Tpetra::createVector<T>(map) );
178 db->set(
"p", Tpetra::createVector<T>(map) );
179 db->set(
"Ap", Tpetra::createVector<T>(map) );
180 db->set(
"rold", Tpetra::createVector<T>(map) );
181 if (params.
get<
bool>(
"Extract Diagonal",
false)) {
183 AT->getLocalDiagCopy(*diag);
184 db->set(
"diag", diag);
210 template <
class TS,
class LO,
class GO,
class Node>
215 typedef typename TS::type T;
216 typedef typename TS::next::type T2;
222 const int numIters = db.
get<
int>(
"numIters");
234 const T tolerance = db.
get<
double>(
"tolerance", 0.0);
235 const int verbose = db.
get<
int>(
"verbose",0);
242 if (Atimer == null) {
258 const T r2 = TPETRA_BINARY_PRETRANSFORM_REDUCE(
261 r*r, ZeroOp<T>, plus<T>() );
263 TPETRA_UNARY_TRANSFORM( x, ST::zero() );
265 TPETRA_TERTIARY_TRANSFORM( z, diag, r, r/diag );
272 TPETRA_BINARY_TRANSFORM( bx_T2, r, as<T2>(r) );
273 recursiveFPCG<typename TS::next,LO,GO,Node>(out,db_T2);
274 TPETRA_BINARY_TRANSFORM( z, bx_T2, as<T>(bx_T2) );
278 T zr = TPETRA_TERTIARY_PRETRANSFORM_REDUCE(
281 z*r, ZeroOp<T>, plus<T>() );
284 for (k=0; k<numIters; ++k)
289 T pAp = TPETRA_REDUCE2( p, Ap,
290 p*Ap, ZeroOp<T>, plus<T>() );
291 const T alpha = zr / pAp;
292 TPETRA_BINARY_TRANSFORM( x, p, x + alpha*p );
293 TPETRA_BINARY_TRANSFORM( rold, r, r );
294 T rr = TPETRA_BINARY_PRETRANSFORM_REDUCE(
297 r*r, ZeroOp<T>, plus<T>() );
298 if (verbose > 1) *out <<
"|res|/|res_0|: " << ST::squareroot(rr/r2)
300 if (rr/r2 < tolerance*tolerance) {
302 *out <<
"Convergence detected!" << std::endl;
307 TPETRA_TERTIARY_TRANSFORM( z, diag, r, r/diag );
314 TPETRA_BINARY_TRANSFORM( bx_T2, r, as<T2>(r) );
315 recursiveFPCG<typename TS::next,LO,GO,Node>(out,db_T2);
316 TPETRA_BINARY_TRANSFORM( z, bx_T2, as<T>(bx_T2) );
321 typedef ZeroOp<pair<T,T>> ZeroPTT;
322 auto plusTT = make_pair_op<T,T>(plus<T>());
323 pair<T,T> both = TPETRA_REDUCE3( z, r, rold,
324 make_pair(z*r, z*rold), ZeroPTT, plusTT );
326 const T znro = both.second;
327 const T beta = (zr - znro) / zoro;
328 TPETRA_BINARY_TRANSFORM( p, z, z + beta*p );
334 <<
"> after " << k <<
" iterations." << std::endl;
339 template <
class TS,
class LO,
class GO,
class Node>
349 typedef typename TS::type T;
350 typedef typename TS::next::type T2;
356 const int numIters = db.
get<
int>(
"numIters");
374 const T tolerance = db.
get<
double>(
"tolerance", 0.0);
375 const int verbose = db.
get<
int>(
"verbose",0);
400 recursiveFPCGUnfused<typename TS::next,LO,GO,Node>(out,db_T2);
410 for (k=0; k<numIters; ++k)
415 const T alpha = zr / pAp;
421 if (verbose > 1) *out <<
"|res|/|res_0|: " << ST::squareroot(rr/r2) << std::endl;
422 if (rr/r2 < tolerance*tolerance) {
424 *out <<
"Convergence detected!" << std::endl;
437 recursiveFPCGUnfused<typename TS::next,LO,GO,Node>(out,db_T2);
447 const T beta = (zr - znro) / zoro;
459 #endif // MULTIPRECCG_HPP_ Initialization for a database stack for the recursiveFPCG() algorithm, to be used with Tpetra::Ext::i...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Sparse matrix that presents a compressed sparse row interface.
pair_op< T1, T2, Op > make_pair_op(Op op)
Non-member constructor to create a pair_op.
void binary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, OP op)
Transform values of vec_inout using vec_inout, vec_in2 and operator op.
T & get(const std::string &name, T def_value)
Namespace for Tpetra example classes and methods.
void recursiveFPCGUnfused(const RCP< Teuchos::FancyOStream > &out, ParameterList &db)
Recursive, self-preconditioning flexible CG.
static RCP< Time > getNewTimer(const std::string &name)
void recursiveFPCG(const RCP< Teuchos::FancyOStream > &out, ParameterList &db)
Recursive, self-preconditioning flexible CG.
Abstract interface for linear operators accepting Tpetra MultiVector objects.
Glob::RedOP::result_type reduce(const Vector< S, LO, GO, Node > &vec_in, Glob glob)
Reduce values of vec_in using the operators instantiated in glob.
ReductionGlob< GOP, ROP, IOP > reductionGlob(GOP gop, ROP rop)
Non-member constructor to instantiate a type glob of a static identity functor and generation and red...
Helper function to call CrsMatrix<T1>::convert<T2> if T1 != T2, and return the original matrix otherw...
Describes a parallel distribution of objects over processes.
TypeTo as(const TypeFrom &t)
A distributed dense vector.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
A static identity functor, providing a static method identity() that returns zero.
void unary_transform(Vector< S, LO, GO, Node > &vec_inout, OP op)
Transform values of vec_inout using via operator op.
void tertiary_transform(Vector< S1, LO, GO, Node > &vec_inout, const Vector< S2, LO, GO, Node > &vec_in2, const Vector< S3, LO, GO, Node > &vec_in3, OP op)
Transform values of vec_inout using vec_inout, vec_in2, vec_in3 and operator op.
static std::string name()