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> 65 #define XFORM1 TPETRA_UNARY_TRANSFORM 66 #define XFORM2 TPETRA_BINARY_TRANSFORM 67 #define XFORM3 TPETRA_TERTIARY_TRANSFORM 69 #define REDUCE1(in, gexp) \ 70 Tpetra::RTI::reduce( *in, \ 71 Tpetra::RTI::reductionGlob<Tpetra::RTI::ZeroOp<decltype((in)->meanValue())>>( \ 72 [=]( decltype((in)->meanValue()) in ){ return gexp; }, \ 73 std::plus<decltype((in)->meanValue())>() ) ) 75 #define REDUCE2(in1, in2, gexp) \ 76 Tpetra::RTI::reduce( *in1, *in2, \ 77 Tpetra::RTI::reductionGlob<Tpetra::RTI::ZeroOp<decltype((in1)->meanValue())>>( \ 78 [=]( decltype((in1)->meanValue()) in1, \ 79 decltype((in2)->meanValue()) in2 ) { return gexp; },\ 80 std::plus<decltype((in1)->meanValue())>() ) ) 82 #define XFORM2RED(out,in,texp,gexp) \ 83 Tpetra::RTI::binary_pre_transform_reduce( *out, *in, \ 84 Tpetra::RTI::reductionGlob<Tpetra::RTI::ZeroOp<decltype((out)->meanValue())>>( \ 85 [=]( decltype((out)->meanValue()) out, \ 86 decltype((in)->meanValue()) in ) { return texp; }, \ 87 [=]( decltype((out)->meanValue()) out, \ 88 decltype((in)->meanValue()) in ) { return gexp; }, \ 89 std::plus<decltype((out)->meanValue())>() ) ) 91 #define XFORM3RED(out,in2,in3, texp, gexp) \ 92 Tpetra::RTI::tertiary_pre_transform_reduce( *out, *in2, *in3, \ 93 Tpetra::RTI::reductionGlob<Tpetra::RTI::ZeroOp<decltype((out)->meanValue())>>( \ 94 [=]( decltype((out)->meanValue()) out, \ 95 decltype((in2)->meanValue()) in2, \ 96 decltype((in3)->meanValue()) in3 ) \ 98 [=]( decltype((out)->meanValue()) out, \ 99 decltype((in2)->meanValue()) in2, \ 100 decltype((in3)->meanValue()) in3 ) \ 102 std::plus<decltype((out)->meanValue())>() ) ) 111 template <
class Tout,
class Tin,
class LO,
class GO,
class Node>
115 return A->template convert<Tout>();
120 template <
class T,
class LO,
class GO,
class Node>
134 template <
class S,
class Souter,
class LO,
class GO,
class Node>
160 const S alpha = XFORM3RED(z,D,x, z/D, x*z ) / xiDx;
161 return XFORM3RED(z,iDx,r, z - iDx*alpha, z*r );
164 template <
class S,
class LO,
class GO,
class Node>
165 struct tCG_workspace {
177 template <
class S,
class Souter,
class LO,
class GO,
class Node>
183 const Souter &lx_outer,
185 const tCG_workspace<S,LO,GO,Node> &workspace,
186 double rho_prime,
double kappa,
double theta,
int maxItersCG,
double outertol)
193 const S ZERO = ST::zero();
194 const S ONE = ST::one();
195 const S D2 = ONE/rho_prime - ONE;
196 const S lx = as<S>(lx_outer);
197 decltype(eta) r = workspace.r,
203 if (timer == Teuchos::null) {
210 auto xinit = createVector<S>(r->getMap());
211 XFORM2(xinit,x_outer, as<S>(x_outer));
218 const S xiDx = XFORM3RED(iDx,diag,x, x/diag, x*iDx );
222 S r_r = XFORM2RED( r,grad, as<S>(grad), r*r );
224 S z_r = diagPrecond<S,Souter,LO,GO,Node>(diag,x,iDx,xiDx,r,z);
226 const S rnrm0 = ST::squareroot(r_r);
227 const S kconv = rnrm0*kappa;
228 const S tconv = ST::pow(rnrm0, (theta+1));
229 const S futile_tol = TEUCHOS_MAX(outertol, 100*rnrm0*ST::eps());
230 const S convtol = TEUCHOS_MAX(TEUCHOS_MIN(kconv,tconv), futile_tol);
232 if (kconv < tconv) convstr =
"non-local convergence";
233 else convstr =
"local convergence";
236 if (verbose > 2) *out <<
"starting tCG with |res| = " << rnrm0 << std::endl;
237 if (verbose > 2) *out <<
"targeting tol = " << convtol << std::endl;
244 const S xHd = XFORM3RED(Hd,x,d, Hd - d*lx, x*Hd );
245 const S dHd = XFORM3RED(Hd,x,d, Hd - x*xHd, d*Hd );
246 if (verbose > 19) *out <<
"curvature: " << dHd << std::endl;
261 const S alpha = z_r / dHd;
262 if (verbose > 19) *out <<
"alpha: " << alpha << std::endl;
263 if (verbose > 19) *out <<
"z_r: " << z_r << std::endl;
266 const S e_d = REDUCE2(eta,d, eta*d),
267 d_d = REDUCE1(d, d*d );
268 const S new_e_e = e_e + 2.0*alpha*e_d + alpha*alpha*d_d;
271 if (dHd <= ZERO || new_e_e > D2)
280 const S alpha_prime = (-e_d + ST::squareroot(e_d*e_d + d_d*(D2-e_e))) / d_d;
281 if (verbose > 19) *out <<
"alpha_prime: " << alpha_prime << std::endl;
282 XFORM2(eta,d, eta + alpha_prime*d );
283 if (dHd <= ZERO) reason=
"negative curvature";
284 else reason=
"exceeded trust-region";
290 XFORM2(eta,d, eta + alpha*d );
294 const S x_r = XFORM3RED(r,Hd,x, r + alpha*Hd, x*r );
296 r_r = XFORM2RED(r,x, r - x*x_r, r*r );
298 S rnrm = ST::squareroot(r_r);
299 if (verbose > 9) *out <<
"|r| = " << rnrm << std::endl;
302 if (rnrm <= convtol) {reason = convstr;
break;}
305 const S zold_rold = z_r;
307 z_r = diagPrecond<S,Souter,LO,GO,Node>(diag,x,iDx,xiDx,r,z);
309 const S beta = z_r / zold_rold;
310 XFORM2(d,z, -z + beta*d );
313 if (k >= maxItersCG) {reason=
"maximum number of inner iterations";
break;}
317 *out <<
"leaving tCG after " << k <<
" iterations: " << reason << std::endl;
322 template <
class Sinner,
class S,
class LO,
class GO,
class Node>
335 const int maxIters = params.
get<
int >(
"maxIters", 100);
336 const int maxItersCG = params.
get<
int >(
"maxItersCG",A->getDomainMap()->getGlobalNumElements());
337 const int verbose = params.
get<
int >(
"verbose",0);
338 const double tolerance = params.
get<
double>(
"tolerance");
339 const double kappa = params.
get<
double>(
"kappa",0.1);
340 const double theta = params.
get<
double>(
"theta",2.0);
341 const double rho_prime = params.
get<
double>(
"rho_prime",.1);
342 const bool precond = params.
get<
int> (
"precond",1);
343 if (verbose > 10) *out << params << std::endl;
344 if (verbose > 10) *out <<
"maxIters: " << maxIters << std::endl;
348 auto map = A->getDomainMap();
349 auto Ax = createVector<S>(map),
350 r = createVector<S>(map),
351 eta = createVector<Sinner>(map),
352 diag = createVector<Sinner>(map);
353 tCG_workspace<Sinner,LO,GO,Node> workspace;
354 workspace.r = createVector<Sinner>(map);
355 workspace.z = createVector<Sinner>(map);
356 workspace.iDx = createVector<Sinner>(map);
357 workspace.d = createVector<Sinner>(map);
358 workspace.Hd = createVector<Sinner>(map);
361 Ainner->getLocalDiagCopy(*diag);
369 S x_x = REDUCE1(x, x*x );
370 S xnrm = ST::squareroot(x_x);
373 S lambda = REDUCE2(x,Ax, x*Ax );
374 S rr = XFORM3RED(r,x,Ax, Ax - x*lambda, r*r );
375 S rnrm = ST::squareroot(rr);
377 *out <<
"Beginning " << methName <<
" with λ = " << lambda << std::endl;
382 while (k < maxIters && rnrm > tolerance) {
385 truncateCG<Sinner,S,LO,GO,Node>(out,verbose,Ainner,diag,
388 rho_prime,kappa,theta,maxItersCG,tolerance);
391 x_x = XFORM2RED(x,eta, x + eta, x*x );
392 xnrm = ST::squareroot(x_x);
395 lambda = REDUCE2(x,Ax, x*Ax );
396 rr = XFORM3RED(r,x,Ax, Ax - x*lambda, r*r );
397 rnrm = ST::squareroot(rr) / ST::magnitude(lambda);
400 *out <<
"After iteration " << k <<
", λ = " << std::setprecision(2) << std::scientific << lambda <<
", |Ax-xλ|/|λ| = " << rnrm << std::endl;
405 *out <<
"Leaving " << methName <<
" after " << k <<
" iterations." << std::endl;
412 #endif // MULTIPRECCG_HPP_ Sparse matrix that presents a compressed sparse row interface.
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Namespace for Tpetra example classes and methods.
S IRTR(const RCP< Teuchos::FancyOStream > &out, ParameterList ¶ms, const RCP< const Tpetra::CrsMatrix< S, LO, GO, Node > > &A, const RCP< Tpetra::Vector< S, LO, GO, Node > > &x)
Templated IRTR solver.
RCP< Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > createVector(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
Non-member function to create a Vector from a specified Map.
static RCP< Time > getNewTimer(const std::string &name)
void start(bool reset=false)
void truncateCG(const RCP< Teuchos::FancyOStream > &out, int verbose, const RCP< const Tpetra::Operator< S, LO, GO, Node > > &A, const RCP< const Tpetra::Vector< S, LO, GO, Node > > &diag, const RCP< const Tpetra::Vector< Souter, LO, GO, Node > > &x_outer, const RCP< const Tpetra::Vector< Souter, LO, GO, Node > > &grad, const Souter &lx_outer, const RCP< Tpetra::Vector< S, LO, GO, Node > > &eta, const tCG_workspace< S, LO, GO, Node > &workspace, double rho_prime, double kappa, double theta, int maxItersCG, double outertol)
Model trust-region subproblem CG solver.
Helper function to call CrsMatrix<T1>::convert<T2> if T1 != T2, and return the original matrix otherw...
TypeTo as(const TypeFrom &t)
A distributed dense vector.