| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
// Purpose |
| 11 |
// The example tests the successive right-hand sides capabilities of MueLu |
| 12 |
// and Belos on a heat flow u_t = u_xx problem. |
| 13 |
// |
| 14 |
// A sequence of linear systems with the same coefficient matrix and |
| 15 |
// different right-hand sides is solved. A seed space is generated dynamically, |
| 16 |
// and a deflated linear system is solved. After each solves, the first |
| 17 |
// few Krylov vectors are saved, and used to reduce the number of iterations |
| 18 |
// for later solves. |
| 19 |
// The optimal numbers of vectors to deflate and save are not known. |
| 20 |
// Presently, the maximum number of vectors to deflate (seed space dimension) |
| 21 |
// and to save are user paraemters. |
| 22 |
// The seed space dimension is less than or equal to total number of vectors saved. |
| 23 |
// The difference between the seed space dimension and the total number of vectors, |
| 24 |
// is the number of vectors used to update the seed space after each solve. |
| 25 |
// I guess that a seed space whose dimension is a small fraction of the total space |
| 26 |
// will be best. |
| 27 |
// |
| 28 |
// maxSave=1 and maxDeflate=0 uses no recycling (not tested ). |
| 29 |
// |
| 30 |
// TODO: Instrument with timers, so that we can tell what is going on besides |
| 31 |
// by counting the numbers of iterations. |
| 32 |
|
| 33 |
// Adapted from test_pcpg_epetraex.cpp by David M. Day (with original comments) |
| 34 |
|
| 35 |
// All preconditioning has been commented out |
| 36 |
|
| 37 |
// Belos |
| 38 |
#include <BelosPCPGSolMgr.hpp> |
| 39 |
#include <BelosLinearProblem.hpp> |
| 40 |
#include <BelosTpetraAdapter.hpp> |
| 41 |
|
| 42 |
// Tpetra |
| 43 |
#include <Tpetra_Core.hpp> |
| 44 |
#include <Tpetra_Map_fwd.hpp> |
| 45 |
#include <Tpetra_Vector_fwd.hpp> |
| 46 |
#include <Tpetra_CrsMatrix_fwd.hpp> |
| 47 |
#include <TpetraExt_MatrixMatrix.hpp> |
| 48 |
|
| 49 |
// MueLu |
| 50 |
// #include <MueLu_TpetraOperator.hpp> |
| 51 |
// #include <MueLu_CreateTpetraPreconditioner.hpp> |
| 52 |
|
| 53 |
// Teuchos |
| 54 |
#include <Teuchos_RCP.hpp> |
| 55 |
#include <Teuchos_Comm.hpp> |
| 56 |
#include <Teuchos_Tuple.hpp> |
| 57 |
#include <Teuchos_VerboseObject.hpp> |
| 58 |
#include <Teuchos_FancyOStream.hpp> |
| 59 |
#include <Teuchos_CommHelpers.hpp> |
| 60 |
#include <Teuchos_DefaultComm.hpp> |
| 61 |
#include <Teuchos_ParameterList.hpp> |
| 62 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 63 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 64 |
|
| 65 |
|
| 66 |
template<typename ScalarType> |
| 67 |
int run(int argc, char *argv[]) { |
| 68 |
using ST = typename Tpetra::Vector<ScalarType>::scalar_type; |
| 69 |
using LO = typename Tpetra::Vector<>::local_ordinal_type; |
| 70 |
using GO = typename Tpetra::Vector<>::global_ordinal_type; |
| 71 |
using NT = typename Tpetra::Vector<>::node_type; |
| 72 |
|
| 73 |
using SCT = typename Teuchos::ScalarTraits<ST>; |
| 74 |
using MT = typename SCT::magnitudeType; |
| 75 |
using MV = typename Tpetra::MultiVector<ST,LO,GO,NT>; |
| 76 |
using OP = typename Tpetra::Operator<ST,LO,GO,NT>; |
| 77 |
using MVT = typename Belos::MultiVecTraits<ST,MV>; |
| 78 |
using OPT = typename Belos::OperatorTraits<ST,MV,OP>; |
| 79 |
|
| 80 |
using tcrsmatrix_t = Tpetra::CrsMatrix<ST,LO,GO,NT>; |
| 81 |
using tmap_t = Tpetra::Map<LO,GO,NT>; |
| 82 |
using tvector_t = Tpetra::Vector<ST,LO,GO,NT>; |
| 83 |
using tmultivector_t = Tpetra::MultiVector<ST,LO,GO,NT>; |
| 84 |
|
| 85 |
using toperator_t = Tpetra::Operator<ST,LO,GO,NT>; |
| 86 |
// using mtoperator_t = MueLu::TpetraOperator<ST,LO,GO,NT>; |
| 87 |
|
| 88 |
using starray_t = Teuchos::Array<ST>; |
| 89 |
using goarray_t = Teuchos::Array<GO>; |
| 90 |
|
| 91 |
using Teuchos::ParameterList; // all of this may look fine but do not be fooled ... |
| 92 |
using Teuchos::RCP; // it is not so clear what any of this does |
| 93 |
using Teuchos::rcp; |
| 94 |
using Teuchos::Comm; |
| 95 |
using Teuchos::rcp_dynamic_cast; |
| 96 |
|
| 97 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &std::cout); |
| 98 |
const auto comm = Tpetra::getDefaultComm(); |
| 99 |
const int MyPID = comm->getRank(); |
| 100 |
const int numProc = comm->getSize(); |
| 101 |
|
| 102 |
// Laplace's equation, homogenous Dirichlet boundary counditions, [0,1]^2 |
| 103 |
// regular mesh, Q1 finite elements |
| 104 |
bool success = false; |
| 105 |
bool verbose = false; |
| 106 |
|
| 107 |
try { |
| 108 |
bool proc_verbose = false; |
| 109 |
int frequency = -1; // frequency of status test output. |
| 110 |
int blocksize = 1; // blocksize, PCPGIter |
| 111 |
int numrhs = 1; // number of right-hand sides to solve for |
| 112 |
int maxiters = 30; // maximum number of iterations allowed per linear system |
| 113 |
|
| 114 |
int maxDeflate = 4; // maximum number of vectors deflated from the linear system; |
| 115 |
// There is no overhead cost assoc with changing maxDeflate between solves |
| 116 |
int maxSave = 8; // maximum number of vectors saved from current and previous ."); |
| 117 |
// If maxSave changes between solves, then re-initialize (setSize). |
| 118 |
|
| 119 |
// Hypothesis: seed vectors are conjugate. |
| 120 |
// Initial versions allowed users to supply a seed space et cetera, but no longer. |
| 121 |
|
| 122 |
// The documentation is suitable for certain tasks, like defining a modules grammar, |
| 123 |
std::string ortho("ICGS"); // The Belos documentation obscures the fact that |
| 124 |
// IMGS is Iterated Modified Gram Schmidt, |
| 125 |
// ICGS is Iterated Classical Gram Schmidt, and |
| 126 |
// DKGS is another Iterated Classical Gram Schmidt. |
| 127 |
// Mathematical issues, such as the difference between ICGS and DKGS, are not documented at all. |
| 128 |
// UH tells me that Anasazi::SVQBOrthoManager is available; I need it for Belos |
| 129 |
MT tol = 1.0e-8; // relative residual tolerance |
| 130 |
// How do command line parsers work? |
| 131 |
Teuchos::CommandLineProcessor cmdp(false,true); |
| 132 |
|
| 133 |
cmdp.setOption("verbose","quiet",&verbose,"Print messages and results"); |
| 134 |
cmdp.setOption("frequency",&frequency,"Solvers frequency for printing residuals (#iters)"); |
| 135 |
cmdp.setOption("tol",&tol,"Relative residual tolerance used by PCPG solver"); |
| 136 |
cmdp.setOption("num-rhs",&numrhs,"Number of right-hand sides to be solved for"); |
| 137 |
cmdp.setOption("max-iters",&maxiters,"Maximum number of iterations per linear system (-1 = adapted to problem/block size)"); |
| 138 |
cmdp.setOption("num-deflate",&maxDeflate,"Number of vectors deflated from the linear system"); |
| 139 |
cmdp.setOption("num-save",&maxSave,"Number of vectors saved from old Krylov subspaces"); |
| 140 |
cmdp.setOption("ortho-type",&ortho,"Orthogonalization type, either DGKS, ICGS or IMGS"); |
| 141 |
|
| 142 |
if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { |
| 143 |
return -1; |
| 144 |
} |
| 145 |
if (!verbose) |
| 146 |
frequency = -1; // reset frequency if test is not verbose |
| 147 |
|
| 148 |
//////////////////////////////////////////////////// |
| 149 |
// Form the problem // |
| 150 |
//////////////////////////////////////////////////// |
| 151 |
|
| 152 |
int num_time_step = 4; |
| 153 |
GO numElePerDirection = 14 * numProc; |
| 154 |
size_t numNodes = (numElePerDirection - 1)*(numElePerDirection - 1); |
| 155 |
GO base = 0; |
| 156 |
|
| 157 |
//By the way, either matrix has (3*numElePerDirection - 2)^2 nonzeros. |
| 158 |
RCP<tmap_t> Map = rcp(new tmap_t(numNodes, base, comm)); |
| 159 |
RCP<tcrsmatrix_t> Stiff = rcp(new tcrsmatrix_t(Map, numNodes)); |
| 160 |
RCP<tcrsmatrix_t> Mass = rcp(new tcrsmatrix_t(Map, numNodes)); |
| 161 |
RCP<tvector_t> vecLHS = rcp(new tvector_t(Map)); |
| 162 |
RCP<tvector_t> vecRHS = rcp(new tvector_t(Map)); |
| 163 |
RCP<tmultivector_t> LHS, RHS; |
| 164 |
|
| 165 |
ST ko = 8.0 / 3.0, k1 = -1.0 / 3.0; |
| 166 |
starray_t k_arr(2); |
| 167 |
k_arr[0] = ko; |
| 168 |
k_arr[1] = k1; |
| 169 |
|
| 170 |
ST h = 1.0 / static_cast<ST>(numElePerDirection); // x=(iX,iY)h |
| 171 |
|
| 172 |
ST mo = h*h*4.0/9.0, m1 = h*h/9.0, m2 = h*h/36.0; |
| 173 |
starray_t m_arr(3); |
| 174 |
m_arr[0] = mo; |
| 175 |
m_arr[1] = m1; |
| 176 |
m_arr[2] = m2; |
| 177 |
|
| 178 |
ST pi = 4.0*atan(1.0), valueLHS; |
| 179 |
GO lid, node, iX, iY; |
| 180 |
|
| 181 |
goarray_t pos_arr(1); |
| 182 |
|
| 183 |
for (lid = Map->getMinLocalIndex(); lid <= Map->getMaxLocalIndex(); lid++) { |
| 184 |
|
| 185 |
node = Map->getGlobalElement(lid); |
| 186 |
iX = node % (numElePerDirection-1); |
| 187 |
iY = ( node - iX )/(numElePerDirection-1); |
| 188 |
pos_arr[0] = node; |
| 189 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(0,1)); // global row ID, global col ID, value |
| 190 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(0,1)); // init guess violates hom Dir bc |
| 191 |
valueLHS = sin( pi*h*((ST) iX+1) )*cos( 2.0 * pi*h*((ST) iY+1) ); |
| 192 |
vecLHS->replaceGlobalValue(node, valueLHS); |
| 193 |
|
| 194 |
if (iY > 0) { |
| 195 |
pos_arr[0] = iX + (iY-1)*(numElePerDirection-1); |
| 196 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); //North |
| 197 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(1,1)); |
| 198 |
} |
| 199 |
|
| 200 |
if (iY < numElePerDirection-2) { |
| 201 |
pos_arr[0] = iX + (iY+1)*(numElePerDirection-1); |
| 202 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); //South |
| 203 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(1,1)); |
| 204 |
} |
| 205 |
|
| 206 |
if (iX > 0) { |
| 207 |
pos_arr[0] = iX-1 + iY*(numElePerDirection-1); |
| 208 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // West |
| 209 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(1,1)); |
| 210 |
if (iY > 0) { |
| 211 |
pos_arr[0] = iX-1 + (iY-1)*(numElePerDirection-1); |
| 212 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // North West |
| 213 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(2,1)); |
| 214 |
} |
| 215 |
if (iY < numElePerDirection-2) { |
| 216 |
pos_arr[0] = iX-1 + (iY+1)*(numElePerDirection-1); |
| 217 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // South West |
| 218 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(2,1)); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (iX < numElePerDirection - 2) { |
| 223 |
pos_arr[0] = iX+1 + iY*(numElePerDirection-1); |
| 224 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // East |
| 225 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(1,1)); |
| 226 |
if (iY > 0) { |
| 227 |
pos_arr[0] = iX+1 + (iY-1)*(numElePerDirection-1); |
| 228 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // North East |
| 229 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(2,1)); |
| 230 |
} |
| 231 |
if (iY < numElePerDirection-2) { |
| 232 |
pos_arr[0] = iX+1 + (iY+1)*(numElePerDirection-1); |
| 233 |
Stiff->insertGlobalValues(node, pos_arr.view(0,1), k_arr.view(1,1)); // South East |
| 234 |
Mass->insertGlobalValues(node, pos_arr.view(0,1), m_arr.view(2,1)); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
Stiff->fillComplete(); |
| 240 |
Mass->fillComplete(); |
| 241 |
|
| 242 |
ST one = 1.0, hdt = .00005; // A = Mass+Stiff*dt/2 |
| 243 |
RCP<tcrsmatrix_t> A = Tpetra::MatrixMatrix::add(one, false, *Mass, hdt, false, *Stiff); |
| 244 |
|
| 245 |
hdt = -hdt; // B = Mass-Stiff*dt/2 |
| 246 |
RCP<tcrsmatrix_t> B = Tpetra::MatrixMatrix::add(one, false, *Mass, hdt, false, *Stiff); |
| 247 |
|
| 248 |
B->apply(*vecLHS, *vecRHS); // rhs_new := B*lhs_old, |
| 249 |
|
| 250 |
proc_verbose = verbose && (MyPID==0); /* Only print on the zero processor */ |
| 251 |
|
| 252 |
LHS = Teuchos::rcp_implicit_cast<tmultivector_t>(vecLHS); |
| 253 |
RHS = Teuchos::rcp_implicit_cast<tmultivector_t>(vecRHS); |
| 254 |
|
| 255 |
//////////////////////////////////////////////////// |
| 256 |
// Construct Preconditioner // |
| 257 |
//////////////////////////////////////////////////// |
| 258 |
|
| 259 |
// ParameterList MueLuList; // Set MueLuList for Smoothed Aggregation |
| 260 |
|
| 261 |
// MueLuList.set("smoother: type", "CHEBYSHEV"); |
| 262 |
// MueLuList.set("smoother: pre or post", "both"); // both pre- and post-smoothing |
| 263 |
|
| 264 |
// #ifdef HAVE_MUELU_AMESOS2 |
| 265 |
// MueLuList.set("coarse: type", "KLU2"); |
| 266 |
// #else |
| 267 |
// MueLuList.set("coarse: type", "none") |
| 268 |
// #endif |
| 269 |
// RCP<toperator_t> A_op = A; |
| 270 |
// RCP<mtoperator_t> Prec = MueLu::CreateTpetraPreconditioner(A_op, MueLuList); |
| 271 |
|
| 272 |
/////////////////////////////////////////////////// |
| 273 |
// Create Parameter List // |
| 274 |
/////////////////////////////////////////////////// |
| 275 |
|
| 276 |
const size_t NumGlobalElements = RHS->getGlobalLength(); |
| 277 |
|
| 278 |
if (maxiters == -1) |
| 279 |
maxiters = NumGlobalElements/blocksize - 1; // maximum number of iterations to run |
| 280 |
|
| 281 |
ParameterList belosList; |
| 282 |
belosList.set( "Block Size", blocksize ); // Blocksize to be used by iterative solver |
| 283 |
belosList.set( "Maximum Iterations", maxiters ); // Maximum number of iterations allowed |
| 284 |
belosList.set( "Convergence Tolerance", tol ); // Relative convergence tolerance requested |
| 285 |
belosList.set( "Num Deflated Blocks", maxDeflate ); // Number of vectors in seed space |
| 286 |
belosList.set( "Num Saved Blocks", maxSave ); // Number of vectors saved from old spaces |
| 287 |
belosList.set( "Orthogonalization", ortho ); // Orthogonalization type |
| 288 |
|
| 289 |
if (numrhs > 1) { |
| 290 |
belosList.set( "Show Maximum Residual Norm Only", true ); // although numrhs = 1. |
| 291 |
} |
| 292 |
if (verbose) { |
| 293 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + |
| 294 |
Belos::TimingDetails + Belos::FinalSummary + Belos::StatusTestDetails ); |
| 295 |
if (frequency > 0) |
| 296 |
belosList.set( "Output Frequency", frequency ); |
| 297 |
} |
| 298 |
else |
| 299 |
belosList.set( "Verbosity", Belos::Errors + Belos::Warnings + Belos::FinalSummary ); |
| 300 |
|
| 301 |
/////////////////////////////////////////////////// |
| 302 |
// Construct /*Preconditioned*/ Linear Problem // |
| 303 |
/////////////////////////////////////////////////// |
| 304 |
|
| 305 |
RCP<Belos::LinearProblem<ST,MV,OP> > problem |
| 306 |
= rcp( new Belos::LinearProblem<ST,MV,OP>( A, LHS, RHS ) ); |
| 307 |
|
| 308 |
// problem->setLeftPrec( Prec ); // for Preconditioned Problem |
| 309 |
|
| 310 |
bool set = problem->setProblem(); |
| 311 |
if (set == false) { |
| 312 |
if (proc_verbose) { |
| 313 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 314 |
} |
| 315 |
return -1; |
| 316 |
} |
| 317 |
|
| 318 |
// Create an iterative solver manager. |
| 319 |
RCP< Belos::SolverManager<ST,MV,OP> > solver |
| 320 |
= rcp( new Belos::PCPGSolMgr<ST,MV,OP>(problem, rcp(&belosList,false)) ); |
| 321 |
|
| 322 |
//////////////////////////////////////////////////// |
| 323 |
// Iterate PCPG // |
| 324 |
//////////////////////////////////////////////////// |
| 325 |
|
| 326 |
if (proc_verbose) { |
| 327 |
std::cout << std::endl << std::endl; |
| 328 |
std::cout << "Dimension of matrix: " << NumGlobalElements << std::endl; |
| 329 |
std::cout << "Number of right-hand sides: " << numrhs << std::endl; |
| 330 |
std::cout << "Block size used by solver: " << blocksize << std::endl; |
| 331 |
std::cout << "Maximum number of iterations allowed: " << maxiters << std::endl; |
| 332 |
std::cout << "Relative residual tolerance: " << tol << std::endl; |
| 333 |
std::cout << std::endl; |
| 334 |
} |
| 335 |
bool badRes; |
| 336 |
for( int time_step = 0; time_step < num_time_step; time_step++){ |
| 337 |
if (time_step) { |
| 338 |
B->apply(*LHS, *RHS); // rhs_new := B*lhs_old, |
| 339 |
set = problem->setProblem(LHS, RHS); |
| 340 |
if (set == false) { |
| 341 |
if (proc_verbose) |
| 342 |
std::cout << std::endl << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 343 |
return -1; |
| 344 |
} |
| 345 |
} // if time_step |
| 346 |
std::vector<ST> rhs_norm(numrhs); |
| 347 |
MVT::MvNorm(*RHS, rhs_norm); |
| 348 |
std::cout << "\t\t\t\tRHS norm is ... " << rhs_norm[0] << std::endl; |
| 349 |
|
| 350 |
// Perform solve |
| 351 |
|
| 352 |
Belos::ReturnType ret = solver->solve(); |
| 353 |
|
| 354 |
// Compute actual residuals. |
| 355 |
|
| 356 |
badRes = false; |
| 357 |
std::vector<ST> actual_resids(numrhs); |
| 358 |
tmultivector_t resid(Map, numrhs); |
| 359 |
OPT::Apply( *A, *LHS, resid ); |
| 360 |
MVT::MvAddMv( -1.0, resid, 1.0, *RHS, resid ); |
| 361 |
MVT::MvNorm( resid, actual_resids ); |
| 362 |
MVT::MvNorm( *RHS, rhs_norm ); |
| 363 |
std::cout << "\t\t\t\tRHS norm is ... " << rhs_norm[0] << std::endl; |
| 364 |
|
| 365 |
if (proc_verbose) { |
| 366 |
std::cout<< "---------- Actual Residuals (normalized) ----------"<<std::endl<<std::endl; |
| 367 |
for ( int i=0; i<numrhs; i++) { |
| 368 |
ST actRes = actual_resids[i]/rhs_norm[i]; |
| 369 |
std::cout<<"Problem "<<i<<" : \t"<< actRes <<std::endl; |
| 370 |
if (actRes > tol) badRes = true; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
success = ret==Belos::Converged && !badRes; |
| 375 |
if (!success) |
| 376 |
break; |
| 377 |
} // for time_step |
| 378 |
|
| 379 |
if (success) { |
| 380 |
if (proc_verbose) |
| 381 |
std::cout << "End Result: TEST PASSED" << std::endl; |
| 382 |
} else { |
| 383 |
if (proc_verbose) |
| 384 |
std::cout << "End Result: TEST FAILED" << std::endl; |
| 385 |
} |
| 386 |
} // try block |
| 387 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 388 |
|
| 389 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 390 |
} // run |
| 391 |
|
| 392 |
int main(int argc, char *argv[]) { |
| 393 |
// run with different ST |
| 394 |
run<double>(argc, argv); |
| 395 |
// run<float>(argc, argv); // FAILS -- will need to change tolerance |
| 396 |
} |