29 #ifndef ANASAZI_LOBPCG_SOLMGR_HPP 30 #define ANASAZI_LOBPCG_SOLMGR_HPP 108 template<
class ScalarType,
class MV,
class OP>
174 return Teuchos::tuple(_timerSolve, _timerLocking);
234 std::string whch_, ortho_;
236 MagnitudeType convtol_, locktol_;
237 int maxIters_, numIters_;
239 bool relconvtol_, rellocktol_;
258 template<
class ScalarType,
class MV,
class OP>
265 convtol_(
MT::prec()),
277 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
278 , _timerSolve(
Teuchos::TimeMonitor::getNewTimer(
"Anasazi: LOBPCGSolMgr::solve()")),
279 _timerLocking(
Teuchos::TimeMonitor::getNewTimer(
"Anasazi: LOBPCGSolMgr locking"))
285 TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument,
"Problem does not contain initial vectors to clone from.");
291 whch_ = pl.
get(
"Which",whch_);
293 std::invalid_argument,
"Anasazi::LOBPCGSolMgr: Invalid sorting string.");
296 ortho_ = pl.
get(
"Orthogonalization",ortho_);
297 if (ortho_ !=
"DGKS" && ortho_ !=
"SVQB") {
302 convtol_ = pl.
get(
"Convergence Tolerance",convtol_);
303 relconvtol_ = pl.
get(
"Relative Convergence Tolerance",relconvtol_);
304 strtmp = pl.
get(
"Convergence Norm",std::string(
"2"));
308 else if (strtmp ==
"M") {
313 "Anasazi::LOBPCGSolMgr: Invalid Convergence Norm.");
318 useLocking_ = pl.
get(
"Use Locking",useLocking_);
319 rellocktol_ = pl.
get(
"Relative Locking Tolerance",rellocktol_);
321 locktol_ = convtol_/10;
322 locktol_ = pl.
get(
"Locking Tolerance",locktol_);
323 strtmp = pl.
get(
"Locking Norm",std::string(
"2"));
327 else if (strtmp ==
"M") {
332 "Anasazi::LOBPCGSolMgr: Invalid Locking Norm.");
336 maxIters_ = pl.
get(
"Maximum Iterations",maxIters_);
339 blockSize_ = pl.
get(
"Block Size",problem_->getNEV());
341 "Anasazi::LOBPCGSolMgr: \"Block Size\" must be strictly positive.");
345 maxLocked_ = pl.
get(
"Max Locked",problem_->getNEV());
350 if (maxLocked_ == 0) {
354 "Anasazi::LOBPCGSolMgr: \"Max Locked\" must be positive.");
356 std::invalid_argument,
357 "Anasazi::LOBPCGSolMgr: Not enough storage space for requested number of eigenpairs.");
360 lockQuorum_ = pl.
get(
"Locking Quorum",lockQuorum_);
362 std::invalid_argument,
363 "Anasazi::LOBPCGSolMgr: \"Locking Quorum\" must be strictly positive.");
367 fullOrtho_ = pl.
get(
"Full Ortho",fullOrtho_);
371 if (Teuchos::isParameterType<int>(pl,
"Verbosity")) {
372 verbosity_ = pl.
get(
"Verbosity", verbosity_);
374 verbosity_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,
"Verbosity");
379 recover_ = pl.
get(
"Recover",recover_);
383 state_ = Teuchos::getParameter<Teuchos::RCP<Anasazi::LOBPCGState<ScalarType,MV> > >(pl,
"Init");
389 template<
class ScalarType,
class MV,
class OP>
395 const int nev = problem_->getNEV();
417 if (globalTest_ == Teuchos::null) {
421 convtest = globalTest_;
428 if (lockingTest_ == Teuchos::null) {
432 locktest = lockingTest_;
438 if (locktest != Teuchos::null) alltests.
push_back(locktest);
439 if (debugTest_ != Teuchos::null) alltests.
push_back(debugTest_);
440 if (maxtest != Teuchos::null) alltests.
push_back(maxtest);
445 if ( printer->isVerbosity(
Debug) ) {
455 if (ortho_==
"SVQB") {
457 }
else if (ortho_==
"DGKS") {
460 TEUCHOS_TEST_FOR_EXCEPTION(ortho_!=
"SVQB"&&ortho_!=
"DGKS",std::logic_error,
"Anasazi::LOBPCGSolMgr::solve(): Invalid orthogonalization type.");
466 plist.
set(
"Block Size",blockSize_);
467 plist.
set(
"Full Ortho",fullOrtho_);
475 if (probauxvecs != Teuchos::null) {
483 int curNumLocked = 0;
486 lockvecs =
MVT::Clone(*problem_->getInitVec(),maxLocked_);
488 std::vector<MagnitudeType> lockvals;
498 if (fullOrtho_ ==
false && recover_ ==
true) {
499 workMV =
MVT::Clone(*problem_->getInitVec(),2*3*blockSize_);
501 else if (useLocking_) {
502 if (problem_->getM() != Teuchos::null) {
503 workMV =
MVT::Clone(*problem_->getInitVec(),4*blockSize_);
506 workMV =
MVT::Clone(*problem_->getInitVec(),2*blockSize_);
513 problem_->setSolution(sol);
516 if (state_ != Teuchos::null) {
517 lobpcg_solver->initialize(*state_);
522 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR 529 lobpcg_solver->iterate();
536 if (debugTest_ != Teuchos::null && debugTest_->getStatus() ==
Passed) {
537 throw AnasaziError(
"Anasazi::LOBPCGSolMgr::solve(): User-specified debug status test returned Passed.");
544 else if (ordertest->getStatus() ==
Passed || (maxtest != Teuchos::null && maxtest->getStatus() ==
Passed) ) {
555 else if (locktest != Teuchos::null && locktest->getStatus() ==
Passed) {
557 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR 563 "Anasazi::LOBPCGSolMgr::solve(): status test mistake: howMany() non-positive.");
565 "Anasazi::LOBPCGSolMgr::solve(): status test mistake: howMany() not consistent with whichVecs().");
567 "Anasazi::LOBPCGSolMgr::solve(): status test mistake: locking not deactivated.");
569 int numnew = locktest->howMany();
570 std::vector<int> indnew = locktest->whichVecs();
573 if (curNumLocked + numnew > maxLocked_) {
574 numnew = maxLocked_ - curNumLocked;
575 indnew.resize(numnew);
580 bool hadP = lobpcg_solver->hasP();
584 printer->print(
Debug,
"Locking vectors: ");
585 for (
unsigned int i=0; i<indnew.size(); i++) {printer->stream(
Debug) <<
" " << indnew[i];}
586 printer->print(
Debug,
"\n");
588 std::vector<MagnitudeType> newvals(numnew);
593 newvecs =
MVT::CloneView(*lobpcg_solver->getRitzVectors(),indnew);
595 std::vector<Value<ScalarType> > allvals = lobpcg_solver->getRitzValues();
596 for (
int i=0; i<numnew; i++) {
597 newvals[i] = allvals[indnew[i]].realpart;
602 std::vector<int> indlock(numnew);
603 for (
int i=0; i<numnew; i++) indlock[i] = curNumLocked+i;
605 newvecs = Teuchos::null;
608 lockvals.insert(lockvals.end(),newvals.begin(),newvals.end());
609 curNumLocked += numnew;
612 std::vector<int> indlock(curNumLocked);
613 for (
int i=0; i<curNumLocked; i++) indlock[i] = i;
615 if (probauxvecs != Teuchos::null) {
623 ordertest->setAuxVals(lockvals);
632 std::vector<int> bsind(blockSize_);
633 for (
int i=0; i<blockSize_; i++) bsind[i] = i;
637 if (state.
MX != Teuchos::null) {
638 std::vector<int> block3(blockSize_);
639 for (
int i=0; i<blockSize_; i++) block3[i] = 2*blockSize_+i;
649 if (newstateMX != Teuchos::null) {
658 ortho->projectAndNormalizeMat(*newstateX,curauxvecs,dummyC,Teuchos::null,newstateMX);
663 std::vector<int> block2(blockSize_);
664 for (
int i=0; i<blockSize_; i++) block2[i] = blockSize_+i;
668 if (state.
MP != Teuchos::null) {
669 std::vector<int> block4(blockSize_);
670 for (
int i=0; i<blockSize_; i++) block4[i] = 3*blockSize_+i;
677 curauxvecs.push_back(newstateX);
678 ortho->projectAndNormalizeMat(*newstateP,curauxvecs,dummyC,Teuchos::null,newstateMP);
682 ortho->projectAndNormalizeMat(*newstateP,curauxvecs,dummyC,Teuchos::null,newstateMP);
687 newstate.
X = newstateX;
688 newstate.
MX = newstateMX;
689 newstate.
P = newstateP;
690 newstate.
MP = newstateMP;
691 lobpcg_solver->initialize(newstate);
694 if (curNumLocked == maxLocked_) {
696 combotest->removeTest(locktest);
700 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::logic_error,
"Anasazi::LOBPCGSolMgr::solve(): Invalid return from lobpcg_solver::iterate().");
709 if (fullOrtho_==
true || recover_==
false) {
713 printer->stream(
Warnings) <<
"Error! Caught LOBPCGRitzFailure at iteration " << lobpcg_solver->getNumIters() << std::endl
714 <<
"Will not try to recover." << std::endl;
717 printer->stream(
Warnings) <<
"Error! Caught LOBPCGRitzFailure at iteration " << lobpcg_solver->getNumIters() << std::endl
718 <<
"Full orthogonalization is off; will try to recover." << std::endl;
725 int localsize = lobpcg_solver->hasP() ? 3*blockSize_ : 2*blockSize_;
726 bool hasM = problem_->getM() != Teuchos::null;
728 std::vector<int> recind(localsize);
729 for (
int i=0; i<localsize; i++) recind[i] = i;
733 std::vector<int> recind(localsize);
734 for (
int i=0; i<localsize; i++) recind[i] = localsize+i;
748 std::vector<int> blk1(blockSize_);
749 for (
int i=0; i < blockSize_; i++) blk1[i] = i;
760 std::vector<int> blk2(blockSize_);
761 for (
int i=0; i < blockSize_; i++) blk2[i] = blockSize_+i;
770 if (localsize == 3*blockSize_) {
771 std::vector<int> blk3(blockSize_);
772 for (
int i=0; i < blockSize_; i++) blk3[i] = 2*blockSize_+i;
784 if (curNumLocked > 0) {
785 std::vector<int> indlock(curNumLocked);
786 for (
int i=0; i<curNumLocked; i++) indlock[i] = i;
790 if (probauxvecs != Teuchos::null) {
794 int rank = ortho->projectAndNormalizeMat(*restart,Q,dummyC,Teuchos::null,Mrestart);
795 if (rank < blockSize_) {
797 printer->stream(
Errors) <<
"Error! Recovered basis only rank " << rank <<
". Block size is " << blockSize_ <<
".\n" 798 <<
"Recovery failed." << std::endl;
802 if (rank < localsize) {
804 std::vector<int> redind(localsize);
805 for (
int i=0; i<localsize; i++) redind[i] = i;
817 std::vector<MagnitudeType> theta(localsize);
824 OPT::Apply(*problem_->getOperator(),*restart,*Krestart);
829 msutils::directSolver(localsize,KK,Teuchos::rcpFromRef(MM),S,theta,rank,1);
830 if (rank < blockSize_) {
831 printer->stream(
Errors) <<
"Error! Recovered basis of rank " << rank <<
" produced only " << rank <<
"ritz vectors.\n" 832 <<
"Block size is " << blockSize_ <<
".\n" 833 <<
"Recovery failed." << std::endl;
841 std::vector<int> order(rank);
843 sorter->sort( theta, Teuchos::rcpFromRef(order),rank );
846 msutils::permuteVectors(order,curS);
855 std::vector<int> bsind(blockSize_);
856 for (
int i=0; i<blockSize_; i++) bsind[i] = i;
862 theta.resize(blockSize_);
863 newstate.
T = Teuchos::rcpFromRef(theta);
865 lobpcg_solver->initialize(newstate);
869 <<
"Anasazi::LOBPCGSolMgr::solve() caught unexpected exception from Anasazi::LOBPCG::iterate() at iteration " << lobpcg_solver->getNumIters() << std::endl
870 << err.what() << std::endl
871 <<
"Anasazi::LOBPCGSolMgr::solve() returning Unconverged with no solutions." << std::endl;
877 sol.numVecs = ordertest->howMany();
878 if (sol.numVecs > 0) {
879 sol.Evecs =
MVT::Clone(*problem_->getInitVec(),sol.numVecs);
880 sol.Espace = sol.Evecs;
881 sol.Evals.resize(sol.numVecs);
882 std::vector<MagnitudeType> vals(sol.numVecs);
885 std::vector<int> which = ordertest->whichVecs();
889 std::vector<int> inlocked(0), insolver(0);
890 for (
unsigned int i=0; i<which.size(); i++) {
892 TEUCHOS_TEST_FOR_EXCEPTION(which[i] >= blockSize_,std::logic_error,
"Anasazi::LOBPCGSolMgr::solve(): positive indexing mistake from ordertest.");
893 insolver.push_back(which[i]);
897 TEUCHOS_TEST_FOR_EXCEPTION(which[i] < -curNumLocked,std::logic_error,
"Anasazi::LOBPCGSolMgr::solve(): negative indexing mistake from ordertest.");
898 inlocked.push_back(which[i] + curNumLocked);
902 TEUCHOS_TEST_FOR_EXCEPTION(insolver.size() + inlocked.size() != (
unsigned int)sol.numVecs,std::logic_error,
"Anasazi::LOBPCGSolMgr::solve(): indexing mistake.");
905 if (insolver.size() > 0) {
907 int lclnum = insolver.size();
908 std::vector<int> tosol(lclnum);
909 for (
int i=0; i<lclnum; i++) tosol[i] = i;
913 std::vector<Value<ScalarType> > fromsolver = lobpcg_solver->getRitzValues();
914 for (
unsigned int i=0; i<insolver.size(); i++) {
915 vals[i] = fromsolver[insolver[i]].realpart;
920 if (inlocked.size() > 0) {
921 int solnum = insolver.size();
923 int lclnum = inlocked.size();
924 std::vector<int> tosol(lclnum);
925 for (
int i=0; i<lclnum; i++) tosol[i] = solnum + i;
929 for (
unsigned int i=0; i<inlocked.size(); i++) {
930 vals[i+solnum] = lockvals[inlocked[i]];
936 std::vector<int> order(sol.numVecs);
937 sorter->sort( vals, Teuchos::rcpFromRef(order), sol.numVecs);
939 for (
int i=0; i<sol.numVecs; i++) {
940 sol.Evals[i].realpart = vals[i];
944 msutils::permuteVectors(sol.numVecs,order,*sol.Evecs);
948 sol.index.resize(sol.numVecs,0);
953 lobpcg_solver->currentStatus(printer->stream(
FinalSummary));
956 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR 962 problem_->setSolution(sol);
963 printer->stream(
Debug) <<
"Returning " << sol.numVecs <<
" eigenpairs to eigenproblem." << std::endl;
966 numIters_ = lobpcg_solver->getNumIters();
968 if (sol.numVecs < nev) {
975 template <
class ScalarType,
class MV,
class OP>
980 globalTest_ = global;
983 template <
class ScalarType,
class MV,
class OP>
990 template <
class ScalarType,
class MV,
class OP>
998 template <
class ScalarType,
class MV,
class OP>
1005 template <
class ScalarType,
class MV,
class OP>
1010 lockingTest_ = locking;
1013 template <
class ScalarType,
class MV,
class OP>
1017 return lockingTest_;
Pure virtual base class which describes the basic interface for a solver manager. ...
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getLockingStatusTest() const
Get the status test defining locking.
A special StatusTest for printing other status tests.
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getDebugStatusTest() const
Get the status test for debugging.
This class defines the interface required by an eigensolver and status test class to compute solution...
LOBPCGSolMgr(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, Teuchos::ParameterList &pl)
Basic constructor for LOBPCGSolMgr.
An implementation of the Anasazi::SortManager that performs a collection of common sorting techniques...
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
This class provides the Locally Optimal Block Preconditioned Conjugate Gradient (LOBPCG) iteration...
Virtual base class which defines basic traits for the operator type.
ReturnType solve()
This method performs possibly repeated calls to the underlying eigensolver's iterate() routine until ...
Status test for forming logical combinations of other status tests.
Teuchos::RCP< const MultiVector > H
The current preconditioned residual vectors.
The Anasazi::SolverManager is a templated virtual base class that defines the basic interface that an...
Teuchos::RCP< const MultiVector > P
The current search direction.
static void Apply(const OP &Op, const MV &x, MV &y)
Application method which performs operation y = Op*x. An OperatorError exception is thrown if there i...
Basic implementation of the Anasazi::SortManager class.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
An exception class parent to all Anasazi exceptions.
Implementation of the locally-optimal block preconditioned conjugate gradient (LOBPCG) method...
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
Teuchos::RCP< const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > T
The current Ritz values.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
Anasazi's templated, static class providing utilities for the solvers.
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="")
bool isParameter(const std::string &name) const
int numVecs
The number of computed eigenpairs.
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
Basic output manager for sending information of select verbosity levels to the appropriate output str...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > & getGlobalStatusTest() const
Get the status test defining global convergence.
Anasazi's basic output manager for sending information of select verbosity levels to the appropriate ...
Abstract base class which defines the interface required by an eigensolver and status test class to c...
ReturnType
Enumerated type used to pass back information from a solver manager.
A status test for testing the norm of the eigenvectors residuals.
Traits class which defines basic operations on multivectors.
Teuchos::RCP< const MultiVector > MX
The image of the current eigenvectors under M, or Teuchos::null if M was not specified.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
void setDebugStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &debug)
Set the status test for debugging.
Orthogonalization manager based on the SVQB technique described in "A Block Orthogonalization Procedu...
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
Struct for storing an eigenproblem solution.
void setLockingStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &locking)
Set the status test defining locking.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
Teuchos::RCP< const MultiVector > MH
The image of the current preconditioned residual vectors under M, or Teuchos::null if M was not speci...
A status test for testing the number of iterations.
Status test for testing the number of iterations.
void push_back(const value_type &x)
Special StatusTest for printing status tests.
A status test for testing the norm of the eigenvectors residuals along with a set of auxiliary eigenv...
LOBPCGRitzFailure is thrown when the LOBPCG solver is unable to continue a call to LOBPCG::iterate() ...
Status test for forming logical combinations of other status tests.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using (potentially)...
Types and exceptions used within Anasazi solvers and interfaces.
void setGlobalStatusTest(const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &global)
Set the status test defining global convergence.
virtual ~LOBPCGSolMgr()
Destructor.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< const MultiVector > MP
The image of the current search direction under M, or Teuchos::null if M was not specified.
int getNumIters() const
Get the iteration count for the most recent call to solve().
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
A status test for testing the norm of the eigenvectors residuals.
Basic implementation of the Anasazi::OrthoManager class.
The LOBPCGSolMgr provides a powerful solver manager over the LOBPCG eigensolver.
Structure to contain pointers to Anasazi state variables.
Teuchos::RCP< const MultiVector > X
The current eigenvectors.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Return the eigenvalue problem.
Class which provides internal utilities for the Anasazi solvers.