/usr/include/trilinos/AnasaziRTRSolMgr.hpp is in libtrilinos-anasazi-dev 12.12.1-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | // @HEADER
// ***********************************************************************
//
// Anasazi: Block Eigensolvers Package
// Copyright 2004 Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef ANASAZI_RTR_SOLMGR_HPP
#define ANASAZI_RTR_SOLMGR_HPP
/*! \file AnasaziRTRSolMgr.hpp
\brief The Anasazi::RTRSolMgr provides a simple solver manager over the IRTR
eigensolvers.
*/
#include "AnasaziConfigDefs.hpp"
#include "AnasaziTypes.hpp"
#include "AnasaziEigenproblem.hpp"
#include "AnasaziSolverManager.hpp"
#include "AnasaziSolverUtils.hpp"
#include "AnasaziIRTR.hpp"
#include "AnasaziSIRTR.hpp"
#include "AnasaziBasicSort.hpp"
#include "AnasaziICGSOrthoManager.hpp"
#include "AnasaziStatusTestMaxIters.hpp"
#include "AnasaziStatusTestResNorm.hpp"
#include "AnasaziStatusTestWithOrdering.hpp"
#include "AnasaziStatusTestCombo.hpp"
#include "AnasaziStatusTestOutput.hpp"
#include "AnasaziBasicOutputManager.hpp"
#include <Teuchos_TimeMonitor.hpp>
#include <Teuchos_FancyOStream.hpp>
/*! \class Anasazi::RTRSolMgr
\brief The Anasazi::RTRSolMgr provides a simple solver
manager over the RTR eigensolver. For more information, see the discussion for RTRBase.
\ingroup anasazi_solver_framework
\author Chris Baker
*/
namespace Anasazi {
template<class ScalarType, class MV, class OP>
class RTRSolMgr : public SolverManager<ScalarType,MV,OP> {
private:
typedef MultiVecTraits<ScalarType,MV> MVT;
typedef OperatorTraits<ScalarType,MV,OP> OPT;
typedef Teuchos::ScalarTraits<ScalarType> SCT;
typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
typedef Teuchos::ScalarTraits<MagnitudeType> MT;
public:
//! @name Constructors/Destructor
//@{
/*! \brief Basic constructor for RTRSolMgr.
*
* This constructor accepts the Eigenproblem to be solved in addition
* to a parameter list of options for the solver manager. These options include the following:
* - Solver parameters
* - \c "Skinny Solver" - a \c bool specifying whether a non-caching ("skinny") solver implementation is used. Determines whether the underlying solver is IRTR or SIRTR.
* - \c "Which" - a \c string specifying the desired eigenvalues: SR or LR, i.e., smallest or largest algebraic eigenvalues.
* - \c "Block Size" - a \c int specifying the block size to be used by the underlying RTR solver. Default: problem->getNEV()
* - \c "Verbosity" - a sum of MsgType specifying the verbosity. Default: ::Errors
* - Convergence parameters
* - \c "Maximum Iterations" - a \c int specifying the maximum number of iterations the underlying solver is allowed to perform. Default: 100
* - \c "Convergence Tolerance" - a \c MagnitudeType specifying the level that residual norms must reach to decide convergence. Default: machine precision.
* - \c "Relative Convergence Tolerance" - a \c bool specifying whether residuals norms should be scaled by their eigenvalues for the purposing of deciding convergence. Default: true
* - \c "Convergence Norm" - a \c string specifying the norm for convergence testing: "2" or "M"
*/
RTRSolMgr( const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
Teuchos::ParameterList &pl );
//! Destructor.
virtual ~RTRSolMgr() {};
//@}
//! @name Accessor methods
//@{
//! Return the eigenvalue problem.
const Eigenproblem<ScalarType,MV,OP>& getProblem() const {
return *problem_;
}
/*! \brief Return the timers for this object.
*
* The timers are ordered as follows:
* - time spent in solve() routine
*/
Teuchos::Array<Teuchos::RCP<Teuchos::Time> > getTimers() const {
return Teuchos::tuple(_timerSolve);
}
//! Get the iteration count for the most recent call to solve.
int getNumIters() const {
return numIters_;
}
//@}
//! @name Solver application methods
//@{
/*! \brief This method performs possibly repeated calls to the underlying eigensolver's iterate() routine
* until the problem has been solved (as decided by the solver manager) or the solver manager decides to
* quit.
*
* \returns ::ReturnType specifying:
* - ::Converged: the eigenproblem was solved to the specification required by the solver manager.
* - ::Unconverged: the eigenproblem was not solved to the specification desired by the solver manager.
*/
ReturnType solve();
//@}
private:
Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > problem_;
std::string whch_;
std::string ortho_;
bool skinny_;
MagnitudeType convtol_;
int maxIters_;
bool relconvtol_;
enum ResType convNorm_;
int numIters_;
int numICGS_;
Teuchos::RCP<Teuchos::Time> _timerSolve;
Teuchos::RCP<BasicOutputManager<ScalarType> > printer_;
Teuchos::ParameterList pl_;
};
////////////////////////////////////////////////////////////////////////////////////////
template<class ScalarType, class MV, class OP>
RTRSolMgr<ScalarType,MV,OP>::RTRSolMgr(
const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
Teuchos::ParameterList &pl ) :
problem_(problem),
whch_("SR"),
skinny_(true),
convtol_(MT::prec()),
maxIters_(100),
relconvtol_(true),
numIters_(-1),
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
_timerSolve(Teuchos::TimeMonitor::getNewTimer("Anasazi: RTRSolMgr::solve()")),
#endif
pl_(pl)
{
TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument, "Problem does not contain initial vectors to clone from.");
std::string strtmp;
whch_ = pl_.get("Which","SR");
TEUCHOS_TEST_FOR_EXCEPTION(whch_ != "SR" && whch_ != "LR",
std::invalid_argument, "Anasazi::RTRSolMgr: Invalid sorting string. RTR solvers compute only LR or SR.");
// convergence tolerance
convtol_ = pl_.get("Convergence Tolerance",convtol_);
relconvtol_ = pl_.get("Relative Convergence Tolerance",relconvtol_);
strtmp = pl_.get("Convergence Norm",std::string("2"));
if (strtmp == "2") {
convNorm_ = RES_2NORM;
}
else if (strtmp == "M") {
convNorm_ = RES_ORTH;
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
"Anasazi::RTRSolMgr: Invalid Convergence Norm.");
}
// maximum number of (outer) iterations
maxIters_ = pl_.get("Maximum Iterations",maxIters_);
// skinny solver or not
skinny_ = pl_.get("Skinny Solver",skinny_);
// number if ICGS iterations
numICGS_ = pl_.get("Num ICGS",2);
// output stream
std::string fntemplate = "";
bool allProcs = false;
if (pl_.isParameter("Output on all processors")) {
if (Teuchos::isParameterType<bool>(pl_,"Output on all processors")) {
allProcs = pl_.get("Output on all processors",allProcs);
} else {
allProcs = ( Teuchos::getParameter<int>(pl_,"Output on all processors") != 0 );
}
}
fntemplate = pl_.get("Output filename template",fntemplate);
int MyPID;
# ifdef HAVE_MPI
// Initialize MPI
int mpiStarted = 0;
MPI_Initialized(&mpiStarted);
if (mpiStarted) MPI_Comm_rank(MPI_COMM_WORLD, &MyPID);
else MyPID=0;
# else
MyPID = 0;
# endif
if (fntemplate != "") {
std::ostringstream MyPIDstr;
MyPIDstr << MyPID;
// replace %d in fntemplate with MyPID
int pos, start=0;
while ( (pos = fntemplate.find("%d",start)) != -1 ) {
fntemplate.replace(pos,2,MyPIDstr.str());
start = pos+2;
}
}
Teuchos::RCP<ostream> osp;
if (fntemplate != "") {
osp = Teuchos::rcp( new std::ofstream(fntemplate.c_str(),std::ios::out | std::ios::app) );
if (!*osp) {
osp = Teuchos::rcpFromRef(std::cout);
std::cout << "Anasazi::RTRSolMgr::constructor(): Could not open file for write: " << fntemplate << std::endl;
}
}
else {
osp = Teuchos::rcpFromRef(std::cout);
}
// Output manager
int verbosity = Anasazi::Errors;
if (pl_.isParameter("Verbosity")) {
if (Teuchos::isParameterType<int>(pl_,"Verbosity")) {
verbosity = pl_.get("Verbosity", verbosity);
} else {
verbosity = (int)Teuchos::getParameter<Anasazi::MsgType>(pl_,"Verbosity");
}
}
if (allProcs) {
// print on all procs
printer_ = Teuchos::rcp( new BasicOutputManager<ScalarType>(verbosity,osp,MyPID) );
}
else {
// print only on proc 0
printer_ = Teuchos::rcp( new BasicOutputManager<ScalarType>(verbosity,osp,0) );
}
}
// solve()
template<class ScalarType, class MV, class OP>
ReturnType
RTRSolMgr<ScalarType,MV,OP>::solve() {
using std::endl;
// typedef SolverUtils<ScalarType,MV,OP> msutils; // unused
const int nev = problem_->getNEV();
// clear num iters
numIters_ = -1;
#ifdef TEUCHOS_DEBUG
Teuchos::RCP<Teuchos::FancyOStream>
out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(printer_->stream(Debug)));
out->setShowAllFrontMatter(false).setShowProcRank(true);
*out << "Entering Anasazi::RTRSolMgr::solve()\n";
#endif
//////////////////////////////////////////////////////////////////////////////////////
// Sort manager
Teuchos::RCP<BasicSort<MagnitudeType> > sorter = Teuchos::rcp( new BasicSort<MagnitudeType>(whch_) );
//////////////////////////////////////////////////////////////////////////////////////
// Status tests
//
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > maxtest;
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > ordertest;
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > combotest;
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convtest;
// maximum iters
if (maxIters_ > 0) {
maxtest = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>(maxIters_) );
}
else {
maxtest = Teuchos::null;
}
//
// convergence
convtest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(convtol_,nev,convNorm_,relconvtol_) );
ordertest = Teuchos::rcp( new StatusTestWithOrdering<ScalarType,MV,OP>(convtest,sorter,nev) );
//
// combo
Teuchos::Array<Teuchos::RCP<StatusTest<ScalarType,MV,OP> > > alltests;
alltests.push_back(ordertest);
if (maxtest != Teuchos::null) alltests.push_back(maxtest);
combotest = Teuchos::rcp( new StatusTestCombo<ScalarType,MV,OP>( StatusTestCombo<ScalarType,MV,OP>::OR, alltests) );
//
// printing StatusTest
Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputtest;
if ( printer_->isVerbosity(Debug) ) {
outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed+Failed+Undefined ) );
}
else {
outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer_,combotest,1,Passed ) );
}
//////////////////////////////////////////////////////////////////////////////////////
// Orthomanager
Teuchos::RCP<ICGSOrthoManager<ScalarType,MV,OP> > ortho
= Teuchos::rcp( new ICGSOrthoManager<ScalarType,MV,OP>(problem_->getM(),numICGS_) );
//////////////////////////////////////////////////////////////////////////////////////
// create an RTR solver
// leftmost or rightmost?
bool leftMost = true;
if (whch_ == "LR" || whch_ == "LM") {
leftMost = false;
}
pl_.set<bool>("Leftmost",leftMost);
Teuchos::RCP<RTRBase<ScalarType,MV,OP> > rtr_solver;
if (skinny_ == false) {
// "hefty" IRTR
rtr_solver = Teuchos::rcp( new IRTR<ScalarType,MV,OP>(problem_,sorter,printer_,outputtest,ortho,pl_) );
}
else {
// "skinny" IRTR
rtr_solver = Teuchos::rcp( new SIRTR<ScalarType,MV,OP>(problem_,sorter,printer_,outputtest,ortho,pl_) );
}
// set any auxiliary vectors defined in the problem
Teuchos::RCP< const MV > probauxvecs = problem_->getAuxVecs();
if (probauxvecs != Teuchos::null) {
rtr_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs) );
}
TEUCHOS_TEST_FOR_EXCEPTION(rtr_solver->getBlockSize() < problem_->getNEV(),std::logic_error,
"Anasazi::RTRSolMgr requires block size >= requested number of eigenvalues.");
int numfound = 0;
Teuchos::RCP<MV> foundvecs;
std::vector<MagnitudeType> foundvals;
// tell the solver to iterate
try {
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
Teuchos::TimeMonitor slvtimer(*_timerSolve);
#endif
rtr_solver->iterate();
numIters_ = rtr_solver->getNumIters();
}
catch (const std::exception &e) {
// we are a simple solver manager. we don't catch exceptions. set solution empty, then rethrow.
printer_->stream(Anasazi::Errors) << "Exception: " << e.what() << endl;
Eigensolution<ScalarType,MV> sol;
sol.numVecs = 0;
problem_->setSolution(sol);
throw;
}
// check the status tests
if (convtest->getStatus() == Passed || (maxtest != Teuchos::null && maxtest->getStatus() == Passed))
{
int num = convtest->howMany();
if (num > 0) {
std::vector<int> ind = convtest->whichVecs();
// copy the converged eigenvectors
foundvecs = MVT::CloneCopy(*rtr_solver->getRitzVectors(),ind);
// copy the converged eigenvalues
foundvals.resize(num);
std::vector<Value<ScalarType> > all = rtr_solver->getRitzValues();
for (int i=0; i<num; i++) {
foundvals[i] = all[ind[i]].realpart;
}
numfound = num;
}
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::RTRSolMgr::solve(): solver returned without satisfy status test.");
}
// create contiguous storage for all eigenvectors, eigenvalues
Eigensolution<ScalarType,MV> sol;
sol.numVecs = numfound;
sol.Evecs = foundvecs;
sol.Espace = sol.Evecs;
sol.Evals.resize(sol.numVecs);
for (int i=0; i<sol.numVecs; i++) {
sol.Evals[i].realpart = foundvals[i];
}
// all real eigenvalues: set index vectors [0,...,numfound-1]
sol.index.resize(numfound,0);
// print final summary
rtr_solver->currentStatus(printer_->stream(FinalSummary));
// print timing information
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
if ( printer_->isVerbosity( TimingDetails ) ) {
Teuchos::TimeMonitor::summarize( printer_->stream( TimingDetails ) );
}
#endif
// send the solution to the eigenproblem
problem_->setSolution(sol);
printer_->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << endl;
// return from SolMgr::solve()
if (sol.numVecs < nev) return Unconverged;
return Converged;
}
} // end Anasazi namespace
#endif /* ANASAZI_RTR_SOLMGR_HPP */
|