Anasazi  Version of the Day
AnasaziLOBPCGSolMgr.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 #ifndef ANASAZI_LOBPCG_SOLMGR_HPP
30 #define ANASAZI_LOBPCG_SOLMGR_HPP
31 
36 #include "AnasaziConfigDefs.hpp"
37 #include "AnasaziTypes.hpp"
38 
39 #include "AnasaziEigenproblem.hpp"
40 #include "AnasaziSolverManager.hpp"
41 #include "AnasaziSolverUtils.hpp"
42 
43 #include "AnasaziLOBPCG.hpp"
44 #include "AnasaziBasicSort.hpp"
53 #include "Teuchos_BLAS.hpp"
54 #include "Teuchos_TimeMonitor.hpp"
55 
56 
70 namespace Anasazi {
71 
72 
108 template<class ScalarType, class MV, class OP>
109 class LOBPCGSolMgr : public SolverManager<ScalarType,MV,OP> {
110 
111  private:
115  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
117 
118  public:
119 
121 
122 
149 
151  virtual ~LOBPCGSolMgr() {};
153 
155 
156 
159  return *problem_;
160  }
161 
163  int getNumIters() const {
164  return numIters_;
165  }
166 
174  return Teuchos::tuple(_timerSolve, _timerLocking);
175  }
176 
177 
179 
181 
182 
209  ReturnType solve();
210 
213 
216 
219 
222 
225 
228 
230 
231  private:
233 
234  std::string whch_, ortho_;
235 
236  MagnitudeType convtol_, locktol_;
237  int maxIters_, numIters_;
238  bool useLocking_;
239  bool relconvtol_, rellocktol_;
240  int blockSize_;
241  bool fullOrtho_;
242  int maxLocked_;
243  int verbosity_;
244  int lockQuorum_;
245  bool recover_;
247  enum StatusTestResNorm<ScalarType,MV,OP>::ResType convNorm_, lockNorm_;
248 
249  Teuchos::RCP<Teuchos::Time> _timerSolve, _timerLocking;
250 
254 };
255 
256 
257 // Constructor
258 template<class ScalarType, class MV, class OP>
261  Teuchos::ParameterList &pl ) :
262  problem_(problem),
263  whch_("SR"),
264  ortho_("SVQB"),
265  convtol_(MT::prec()),
266  maxIters_(100),
267  numIters_(0),
268  useLocking_(false),
269  relconvtol_(true),
270  rellocktol_(true),
271  blockSize_(0),
272  fullOrtho_(true),
273  maxLocked_(0),
274  verbosity_(Anasazi::Errors),
275  lockQuorum_(1),
276  recover_(true)
277 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
278  , _timerSolve(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCGSolMgr::solve()")),
279  _timerLocking(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCGSolMgr locking"))
280 #endif
281 {
282  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
283  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(), std::invalid_argument, "Problem not set.");
284  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isHermitian(), std::invalid_argument, "Problem not symmetric.");
285  TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument, "Problem does not contain initial vectors to clone from.");
286 
287 
288  std::string strtmp;
289 
290  // which values to solve for
291  whch_ = pl.get("Which",whch_);
292  TEUCHOS_TEST_FOR_EXCEPTION(whch_ != "SM" && whch_ != "LM" && whch_ != "SR" && whch_ != "LR",
293  std::invalid_argument, "Anasazi::LOBPCGSolMgr: Invalid sorting string.");
294 
295  // which orthogonalization to use
296  ortho_ = pl.get("Orthogonalization",ortho_);
297  if (ortho_ != "DGKS" && ortho_ != "SVQB") {
298  ortho_ = "SVQB";
299  }
300 
301  // convergence tolerance
302  convtol_ = pl.get("Convergence Tolerance",convtol_);
303  relconvtol_ = pl.get("Relative Convergence Tolerance",relconvtol_);
304  strtmp = pl.get("Convergence Norm",std::string("2"));
305  if (strtmp == "2") {
307  }
308  else if (strtmp == "M") {
310  }
311  else {
312  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
313  "Anasazi::LOBPCGSolMgr: Invalid Convergence Norm.");
314  }
315 
316 
317  // locking tolerance
318  useLocking_ = pl.get("Use Locking",useLocking_);
319  rellocktol_ = pl.get("Relative Locking Tolerance",rellocktol_);
320  // default: should be less than convtol_
321  locktol_ = convtol_/10;
322  locktol_ = pl.get("Locking Tolerance",locktol_);
323  strtmp = pl.get("Locking Norm",std::string("2"));
324  if (strtmp == "2") {
326  }
327  else if (strtmp == "M") {
329  }
330  else {
331  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
332  "Anasazi::LOBPCGSolMgr: Invalid Locking Norm.");
333  }
334 
335  // maximum number of iterations
336  maxIters_ = pl.get("Maximum Iterations",maxIters_);
337 
338  // block size: default is nev()
339  blockSize_ = pl.get("Block Size",problem_->getNEV());
340  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ <= 0, std::invalid_argument,
341  "Anasazi::LOBPCGSolMgr: \"Block Size\" must be strictly positive.");
342 
343  // max locked: default is nev(), must satisfy maxLocked_ + blockSize_ >= nev
344  if (useLocking_) {
345  maxLocked_ = pl.get("Max Locked",problem_->getNEV());
346  }
347  else {
348  maxLocked_ = 0;
349  }
350  if (maxLocked_ == 0) {
351  useLocking_ = false;
352  }
353  TEUCHOS_TEST_FOR_EXCEPTION(maxLocked_ < 0, std::invalid_argument,
354  "Anasazi::LOBPCGSolMgr: \"Max Locked\" must be positive.");
355  TEUCHOS_TEST_FOR_EXCEPTION(maxLocked_ + blockSize_ < problem_->getNEV(),
356  std::invalid_argument,
357  "Anasazi::LOBPCGSolMgr: Not enough storage space for requested number of eigenpairs.");
358 
359  if (useLocking_) {
360  lockQuorum_ = pl.get("Locking Quorum",lockQuorum_);
361  TEUCHOS_TEST_FOR_EXCEPTION(lockQuorum_ <= 0,
362  std::invalid_argument,
363  "Anasazi::LOBPCGSolMgr: \"Locking Quorum\" must be strictly positive.");
364  }
365 
366  // full orthogonalization: default true
367  fullOrtho_ = pl.get("Full Ortho",fullOrtho_);
368 
369  // verbosity level
370  if (pl.isParameter("Verbosity")) {
371  if (Teuchos::isParameterType<int>(pl,"Verbosity")) {
372  verbosity_ = pl.get("Verbosity", verbosity_);
373  } else {
374  verbosity_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,"Verbosity");
375  }
376  }
377 
378  // recover from LOBPCGRitzFailure
379  recover_ = pl.get("Recover",recover_);
380 
381  // get (optionally) an initial state
382  if (pl.isParameter("Init")) {
383  state_ = Teuchos::getParameter<Teuchos::RCP<Anasazi::LOBPCGState<ScalarType,MV> > >(pl,"Init");
384  }
385 }
386 
387 
388 // solve()
389 template<class ScalarType, class MV, class OP>
390 ReturnType
392 
393  typedef SolverUtils<ScalarType,MV,OP> msutils;
394 
395  const int nev = problem_->getNEV();
396 
397 
398 
400  // Sort manager
402 
404  // Output manager
406 
408  // Status tests
409  //
410  // maximum number of iterations: optional test
412  if (maxIters_ > 0) {
413  maxtest = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>(maxIters_) );
414  }
415  // convergence
417  if (globalTest_ == Teuchos::null) {
418  convtest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(convtol_,nev,convNorm_,relconvtol_) );
419  }
420  else {
421  convtest = globalTest_;
422  }
424  = Teuchos::rcp( new StatusTestWithOrdering<ScalarType,MV,OP>(convtest,sorter,nev) );
425  // locking
427  if (useLocking_) {
428  if (lockingTest_ == Teuchos::null) {
429  locktest = Teuchos::rcp( new StatusTestResNorm<ScalarType,MV,OP>(locktol_,lockQuorum_,lockNorm_,rellocktol_) );
430  }
431  else {
432  locktest = lockingTest_;
433  }
434  }
435  // for a non-short-circuited OR test, the order doesn't matter
437  alltests.push_back(ordertest);
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);
443  // printing StatusTest
445  if ( printer->isVerbosity(Debug) ) {
446  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer,combotest,1,Passed+Failed+Undefined ) );
447  }
448  else {
449  outputtest = Teuchos::rcp( new StatusTestOutput<ScalarType,MV,OP>( printer,combotest,1,Passed ) );
450  }
451 
453  // Orthomanager
455  if (ortho_=="SVQB") {
456  ortho = Teuchos::rcp( new SVQBOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
457  } else if (ortho_=="DGKS") {
458  ortho = Teuchos::rcp( new BasicOrthoManager<ScalarType,MV,OP>(problem_->getM()) );
459  } else {
460  TEUCHOS_TEST_FOR_EXCEPTION(ortho_!="SVQB"&&ortho_!="DGKS",std::logic_error,"Anasazi::LOBPCGSolMgr::solve(): Invalid orthogonalization type.");
461  }
462 
464  // Parameter list
466  plist.set("Block Size",blockSize_);
467  plist.set("Full Ortho",fullOrtho_);
468 
470  // LOBPCG solver
472  = Teuchos::rcp( new LOBPCG<ScalarType,MV,OP>(problem_,sorter,printer,outputtest,ortho,plist) );
473  // set any auxiliary vectors defined in the problem
474  Teuchos::RCP< const MV > probauxvecs = problem_->getAuxVecs();
475  if (probauxvecs != Teuchos::null) {
476  lobpcg_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs) );
477  }
478 
480  // Storage
481  //
482  // lockvecs will contain eigenvectors that have been determined "locked" by the status test
483  int curNumLocked = 0;
484  Teuchos::RCP<MV> lockvecs;
485  if (useLocking_) {
486  lockvecs = MVT::Clone(*problem_->getInitVec(),maxLocked_);
487  }
488  std::vector<MagnitudeType> lockvals;
489  // workMV will be used as work space for LOBPCGRitzFailure recovery and locking
490  // it will be partitioned in these cases as follows:
491  // for LOBPCGRitzFailure recovery:
492  // workMV = [X H P OpX OpH OpP], where OpX OpH OpP will be used for K and M
493  // total size: 2*3*blocksize
494  // for locking
495  // workMV = [X P MX MP], with MX,MP needing storage only if hasM==true
496  // total size: 2*blocksize or 4*blocksize
497  Teuchos::RCP<MV> workMV;
498  if (fullOrtho_ == false && recover_ == true) {
499  workMV = MVT::Clone(*problem_->getInitVec(),2*3*blockSize_);
500  }
501  else if (useLocking_) {
502  if (problem_->getM() != Teuchos::null) {
503  workMV = MVT::Clone(*problem_->getInitVec(),4*blockSize_);
504  }
505  else {
506  workMV = MVT::Clone(*problem_->getInitVec(),2*blockSize_);
507  }
508  }
509 
510  // initialize the solution to nothing in case we throw an exception
512  sol.numVecs = 0;
513  problem_->setSolution(sol);
514 
515  // initialize the solver if the user specified a state
516  if (state_ != Teuchos::null) {
517  lobpcg_solver->initialize(*state_);
518  }
519 
520  // enter solve() iterations
521  {
522 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
523  Teuchos::TimeMonitor slvtimer(*_timerSolve);
524 #endif
525 
526  // tell the lobpcg_solver to iterate
527  while (1) {
528  try {
529  lobpcg_solver->iterate();
530 
532  //
533  // check user-specified debug test; if it passed, return an exception
534  //
536  if (debugTest_ != Teuchos::null && debugTest_->getStatus() == Passed) {
537  throw AnasaziError("Anasazi::LOBPCGSolMgr::solve(): User-specified debug status test returned Passed.");
538  }
540  //
541  // check convergence first
542  //
544  else if (ordertest->getStatus() == Passed || (maxtest != Teuchos::null && maxtest->getStatus() == Passed) ) {
545  // we have convergence or not
546  // ordertest->whichVecs() tells us which vectors from lockvecs and solver state are the ones we want
547  // ordertest->howMany() will tell us how many
548  break;
549  }
551  //
552  // check locking if we didn't converge
553  //
555  else if (locktest != Teuchos::null && locktest->getStatus() == Passed) {
556 
557 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
558  Teuchos::TimeMonitor lcktimer(*_timerLocking);
559 #endif
560 
561  // remove the locked vectors,values from lobpcg_solver: put them in newvecs, newvals
562  TEUCHOS_TEST_FOR_EXCEPTION(locktest->howMany() <= 0,std::logic_error,
563  "Anasazi::LOBPCGSolMgr::solve(): status test mistake: howMany() non-positive.");
564  TEUCHOS_TEST_FOR_EXCEPTION(locktest->howMany() != (int)locktest->whichVecs().size(),std::logic_error,
565  "Anasazi::LOBPCGSolMgr::solve(): status test mistake: howMany() not consistent with whichVecs().");
566  TEUCHOS_TEST_FOR_EXCEPTION(curNumLocked == maxLocked_,std::logic_error,
567  "Anasazi::LOBPCGSolMgr::solve(): status test mistake: locking not deactivated.");
568  // get the indices
569  int numnew = locktest->howMany();
570  std::vector<int> indnew = locktest->whichVecs();
571 
572  // don't lock more than maxLocked_; we didn't allocate enough space.
573  if (curNumLocked + numnew > maxLocked_) {
574  numnew = maxLocked_ - curNumLocked;
575  indnew.resize(numnew);
576  }
577 
578  // the call below to lobpcg_solver->setAuxVecs() will reset the solver to unitialized with hasP() == false
579  // store the hasP() state for use below
580  bool hadP = lobpcg_solver->hasP();
581 
582  {
583  // debug printing
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");
587  }
588  std::vector<MagnitudeType> newvals(numnew);
589  Teuchos::RCP<const MV> newvecs;
590  {
591  // work in a local scope, to hide the variabes needed for extracting this info
592  // get the vectors
593  newvecs = MVT::CloneView(*lobpcg_solver->getRitzVectors(),indnew);
594  // get the values
595  std::vector<Value<ScalarType> > allvals = lobpcg_solver->getRitzValues();
596  for (int i=0; i<numnew; i++) {
597  newvals[i] = allvals[indnew[i]].realpart;
598  }
599  }
600  // put newvecs into lockvecs
601  {
602  std::vector<int> indlock(numnew);
603  for (int i=0; i<numnew; i++) indlock[i] = curNumLocked+i;
604  MVT::SetBlock(*newvecs,indlock,*lockvecs);
605  newvecs = Teuchos::null;
606  }
607  // put newvals into lockvals
608  lockvals.insert(lockvals.end(),newvals.begin(),newvals.end());
609  curNumLocked += numnew;
610  // add locked vecs as aux vecs, along with aux vecs from problem
611  {
612  std::vector<int> indlock(curNumLocked);
613  for (int i=0; i<curNumLocked; i++) indlock[i] = i;
614  Teuchos::RCP<const MV> curlocked = MVT::CloneView(*lockvecs,indlock);
615  if (probauxvecs != Teuchos::null) {
616  lobpcg_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(probauxvecs,curlocked) );
617  }
618  else {
619  lobpcg_solver->setAuxVecs( Teuchos::tuple< Teuchos::RCP<const MV> >(curlocked) );
620  }
621  }
622  // add locked vals to ordertest
623  ordertest->setAuxVals(lockvals);
624  // fill out the empty state in the solver
625  {
626  LOBPCGState<ScalarType,MV> state = lobpcg_solver->getState();
627  Teuchos::RCP<MV> newstateX, newstateMX, newstateP, newstateMP;
628  //
629  // workMV will be partitioned as follows: workMV = [X P MX MP],
630  //
631  // make a copy of the current X,MX state
632  std::vector<int> bsind(blockSize_);
633  for (int i=0; i<blockSize_; i++) bsind[i] = i;
634  newstateX = MVT::CloneViewNonConst(*workMV,bsind);
635  MVT::SetBlock(*state.X,bsind,*newstateX);
636 
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;
640  newstateMX = MVT::CloneViewNonConst(*workMV,block3);
641  MVT::SetBlock(*state.MX,bsind,*newstateMX);
642  }
643  //
644  // get select part, set to random, apply M
645  {
646  Teuchos::RCP<MV> newX = MVT::CloneViewNonConst(*newstateX,indnew);
647  MVT::MvRandom(*newX);
648 
649  if (newstateMX != Teuchos::null) {
650  Teuchos::RCP<MV> newMX = MVT::CloneViewNonConst(*newstateMX,indnew);
651  OPT::Apply(*problem_->getM(),*newX,*newMX);
652  }
653  }
654 
655  Teuchos::Array<Teuchos::RCP<const MV> > curauxvecs = lobpcg_solver->getAuxVecs();
657  // ortho X against the aux vectors
658  ortho->projectAndNormalizeMat(*newstateX,curauxvecs,dummyC,Teuchos::null,newstateMX);
659 
660  if (hadP) {
661  //
662  // get P and optionally MP, orthogonalize against X and auxiliary vectors
663  std::vector<int> block2(blockSize_);
664  for (int i=0; i<blockSize_; i++) block2[i] = blockSize_+i;
665  newstateP = MVT::CloneViewNonConst(*workMV,block2);
666  MVT::SetBlock(*state.P,bsind,*newstateP);
667 
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;
671  newstateMP = MVT::CloneViewNonConst(*workMV,block4);
672  MVT::SetBlock(*state.MP,bsind,*newstateMP);
673  }
674 
675  if (fullOrtho_) {
676  // ortho P against the new aux vectors and new X
677  curauxvecs.push_back(newstateX);
678  ortho->projectAndNormalizeMat(*newstateP,curauxvecs,dummyC,Teuchos::null,newstateMP);
679  }
680  else {
681  // ortho P against the new aux vectors
682  ortho->projectAndNormalizeMat(*newstateP,curauxvecs,dummyC,Teuchos::null,newstateMP);
683  }
684  }
685  // set the new state
687  newstate.X = newstateX;
688  newstate.MX = newstateMX;
689  newstate.P = newstateP;
690  newstate.MP = newstateMP;
691  lobpcg_solver->initialize(newstate);
692  }
693 
694  if (curNumLocked == maxLocked_) {
695  // disable locking now; remove locking test from combo test
696  combotest->removeTest(locktest);
697  }
698  }
699  else {
700  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,"Anasazi::LOBPCGSolMgr::solve(): Invalid return from lobpcg_solver::iterate().");
701  }
702  }
704  //
705  // check Ritz Failure
706  //
708  catch (const LOBPCGRitzFailure &re) {
709  if (fullOrtho_==true || recover_==false) {
710  // if we are already using full orthogonalization, there isn't much we can do here.
711  // the most recent information in the status tests is still valid, and can be used to extract/return the
712  // eigenpairs that have converged.
713  printer->stream(Warnings) << "Error! Caught LOBPCGRitzFailure at iteration " << lobpcg_solver->getNumIters() << std::endl
714  << "Will not try to recover." << std::endl;
715  break; // while(1)
716  }
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;
719  // get the current "basis" from the solver, orthonormalize it, do a rayleigh-ritz, and restart with the ritz vectors
720  // if there aren't enough, break and quit with what we have
721  //
722  // workMV = [X H P OpX OpH OpP], where OpX OpH OpP will be used for K and M
723  LOBPCGState<ScalarType,MV> curstate = lobpcg_solver->getState();
724  Teuchos::RCP<MV> restart, Krestart, Mrestart;
725  int localsize = lobpcg_solver->hasP() ? 3*blockSize_ : 2*blockSize_;
726  bool hasM = problem_->getM() != Teuchos::null;
727  {
728  std::vector<int> recind(localsize);
729  for (int i=0; i<localsize; i++) recind[i] = i;
730  restart = MVT::CloneViewNonConst(*workMV,recind);
731  }
732  {
733  std::vector<int> recind(localsize);
734  for (int i=0; i<localsize; i++) recind[i] = localsize+i;
735  Krestart = MVT::CloneViewNonConst(*workMV,recind);
736  }
737  if (hasM) {
738  Mrestart = Krestart;
739  }
740  else {
741  Mrestart = restart;
742  }
743  //
744  // set restart = [X H P] and Mrestart = M*[X H P]
745  //
746  // put X into [0 , blockSize)
747  {
748  std::vector<int> blk1(blockSize_);
749  for (int i=0; i < blockSize_; i++) blk1[i] = i;
750  MVT::SetBlock(*curstate.X,blk1,*restart);
751 
752  // put MX into [0 , blockSize)
753  if (hasM) {
754  MVT::SetBlock(*curstate.MX,blk1,*Mrestart);
755  }
756  }
757  //
758  // put H into [blockSize_ , 2*blockSize)
759  {
760  std::vector<int> blk2(blockSize_);
761  for (int i=0; i < blockSize_; i++) blk2[i] = blockSize_+i;
762  MVT::SetBlock(*curstate.H,blk2,*restart);
763 
764  // put MH into [blockSize_ , 2*blockSize)
765  if (hasM) {
766  MVT::SetBlock(*curstate.MH,blk2,*Mrestart);
767  }
768  }
769  // optionally, put P into [2*blockSize,3*blockSize)
770  if (localsize == 3*blockSize_) {
771  std::vector<int> blk3(blockSize_);
772  for (int i=0; i < blockSize_; i++) blk3[i] = 2*blockSize_+i;
773  MVT::SetBlock(*curstate.P,blk3,*restart);
774 
775  // put MP into [2*blockSize,3*blockSize)
776  if (hasM) {
777  MVT::SetBlock(*curstate.MP,blk3,*Mrestart);
778  }
779  }
780  // project against auxvecs and locked vecs, and orthonormalize the basis
783  {
784  if (curNumLocked > 0) {
785  std::vector<int> indlock(curNumLocked);
786  for (int i=0; i<curNumLocked; i++) indlock[i] = i;
787  Teuchos::RCP<const MV> curlocked = MVT::CloneView(*lockvecs,indlock);
788  Q.push_back(curlocked);
789  }
790  if (probauxvecs != Teuchos::null) {
791  Q.push_back(probauxvecs);
792  }
793  }
794  int rank = ortho->projectAndNormalizeMat(*restart,Q,dummyC,Teuchos::null,Mrestart);
795  if (rank < blockSize_) {
796  // quit
797  printer->stream(Errors) << "Error! Recovered basis only rank " << rank << ". Block size is " << blockSize_ << ".\n"
798  << "Recovery failed." << std::endl;
799  break;
800  }
801  // reduce multivec size if necessary
802  if (rank < localsize) {
803  localsize = rank;
804  std::vector<int> redind(localsize);
805  for (int i=0; i<localsize; i++) redind[i] = i;
806  // grab the first part of restart,Krestart
807  restart = MVT::CloneViewNonConst(*restart,redind);
808  Krestart = MVT::CloneViewNonConst(*Krestart,redind);
809  if (hasM) {
810  Mrestart = Krestart;
811  }
812  else {
813  Mrestart = restart;
814  }
815  }
816  Teuchos::SerialDenseMatrix<int,ScalarType> KK(localsize,localsize), MM(localsize,localsize), S(localsize,localsize);
817  std::vector<MagnitudeType> theta(localsize);
818  // project the matrices
819  //
820  // MM = restart^H M restart
821  MVT::MvTransMv(1.0,*restart,*Mrestart,MM);
822  //
823  // compute Krestart = K*restart
824  OPT::Apply(*problem_->getOperator(),*restart,*Krestart);
825  //
826  // KK = restart^H K restart
827  MVT::MvTransMv(1.0,*restart,*Krestart,KK);
828  rank = localsize;
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;
834  break;
835  }
836  theta.resize(rank);
837  //
838  // sort the ritz values using the sort manager
839  {
841  std::vector<int> order(rank);
842  // sort
843  sorter->sort( theta, Teuchos::rcpFromRef(order),rank ); // don't catch exception
844  // Sort the primitive ritz vectors
846  msutils::permuteVectors(order,curS);
847  }
848  //
850  //
851  // compute the ritz vectors: store them in Krestart
853  Teuchos::RCP<MV> newX;
854  {
855  std::vector<int> bsind(blockSize_);
856  for (int i=0; i<blockSize_; i++) bsind[i] = i;
857  newX = MVT::CloneViewNonConst(*Krestart,bsind);
858  }
859  MVT::MvTimesMatAddMv(1.0,*restart,S1,0.0,*newX);
860  // send X and theta into the solver
861  newstate.X = newX;
862  theta.resize(blockSize_);
863  newstate.T = Teuchos::rcpFromRef(theta);
864  // initialize
865  lobpcg_solver->initialize(newstate);
866  }
867  catch (const AnasaziError &err) {
868  printer->stream(Errors)
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;
872  return Unconverged;
873  }
874  // don't catch any other exceptions
875  }
876 
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);
883 
884  // copy them into the solution
885  std::vector<int> which = ordertest->whichVecs();
886  // indices between [0,blockSize) refer to vectors/values in the solver
887  // indices between [-curNumLocked,-1] refer to locked vectors/values [0,curNumLocked)
888  // everything has already been ordered by the solver; we just have to partition the two references
889  std::vector<int> inlocked(0), insolver(0);
890  for (unsigned int i=0; i<which.size(); i++) {
891  if (which[i] >= 0) {
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]);
894  }
895  else {
896  // sanity check
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);
899  }
900  }
901 
902  TEUCHOS_TEST_FOR_EXCEPTION(insolver.size() + inlocked.size() != (unsigned int)sol.numVecs,std::logic_error,"Anasazi::LOBPCGSolMgr::solve(): indexing mistake.");
903 
904  // set the vecs,vals in the solution
905  if (insolver.size() > 0) {
906  // set vecs
907  int lclnum = insolver.size();
908  std::vector<int> tosol(lclnum);
909  for (int i=0; i<lclnum; i++) tosol[i] = i;
910  Teuchos::RCP<const MV> v = MVT::CloneView(*lobpcg_solver->getRitzVectors(),insolver);
911  MVT::SetBlock(*v,tosol,*sol.Evecs);
912  // set vals
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;
916  }
917  }
918 
919  // get the vecs,vals from locked storage
920  if (inlocked.size() > 0) {
921  int solnum = insolver.size();
922  // set vecs
923  int lclnum = inlocked.size();
924  std::vector<int> tosol(lclnum);
925  for (int i=0; i<lclnum; i++) tosol[i] = solnum + i;
926  Teuchos::RCP<const MV> v = MVT::CloneView(*lockvecs,inlocked);
927  MVT::SetBlock(*v,tosol,*sol.Evecs);
928  // set vals
929  for (unsigned int i=0; i<inlocked.size(); i++) {
930  vals[i+solnum] = lockvals[inlocked[i]];
931  }
932  }
933 
934  // sort the eigenvalues and permute the eigenvectors appropriately
935  {
936  std::vector<int> order(sol.numVecs);
937  sorter->sort( vals, Teuchos::rcpFromRef(order), sol.numVecs);
938  // store the values in the Eigensolution
939  for (int i=0; i<sol.numVecs; i++) {
940  sol.Evals[i].realpart = vals[i];
941  sol.Evals[i].imagpart = MT::zero();
942  }
943  // now permute the eigenvectors according to order
944  msutils::permuteVectors(sol.numVecs,order,*sol.Evecs);
945  }
946 
947  // setup sol.index, remembering that all eigenvalues are real so that index = {0,...,0}
948  sol.index.resize(sol.numVecs,0);
949  }
950  }
951 
952  // print final summary
953  lobpcg_solver->currentStatus(printer->stream(FinalSummary));
954 
955  // print timing information
956 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
957  if ( printer->isVerbosity( TimingDetails ) ) {
958  Teuchos::TimeMonitor::summarize( printer->stream( TimingDetails ) );
959  }
960 #endif
961 
962  problem_->setSolution(sol);
963  printer->stream(Debug) << "Returning " << sol.numVecs << " eigenpairs to eigenproblem." << std::endl;
964 
965  // get the number of iterations performed in this call to solve.
966  numIters_ = lobpcg_solver->getNumIters();
967 
968  if (sol.numVecs < nev) {
969  return Unconverged; // return from LOBPCGSolMgr::solve()
970  }
971  return Converged; // return from LOBPCGSolMgr::solve()
972 }
973 
974 
975 template <class ScalarType, class MV, class OP>
976 void
978  const Teuchos::RCP< StatusTest<ScalarType,MV,OP> > &global)
979 {
980  globalTest_ = global;
981 }
982 
983 template <class ScalarType, class MV, class OP>
986 {
987  return globalTest_;
988 }
989 
990 template <class ScalarType, class MV, class OP>
991 void
994 {
995  debugTest_ = debug;
996 }
997 
998 template <class ScalarType, class MV, class OP>
1001 {
1002  return debugTest_;
1003 }
1004 
1005 template <class ScalarType, class MV, class OP>
1006 void
1008  const Teuchos::RCP< StatusTest<ScalarType,MV,OP> > &locking)
1009 {
1010  lockingTest_ = locking;
1011 }
1012 
1013 template <class ScalarType, class MV, class OP>
1016 {
1017  return lockingTest_;
1018 }
1019 
1020 } // end Anasazi namespace
1021 
1022 #endif /* ANASAZI_LOBPCG_SOLMGR_HPP */
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&#39;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&#39;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&#39;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.