Anasazi  Version of the Day
AnasaziLOBPCG.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 
34 /*
35  LOBPCG contains local storage of up to 10*blockSize_ vectors, representing 10 entities
36  X,H,P,R
37  KX,KH,KP (product of K and the above)
38  MX,MH,MP (product of M and the above, not allocated if we don't have an M matrix)
39  If full orthogonalization is enabled, one extra multivector of blockSize_ vectors is required to
40  compute the local update of X and P.
41 
42  A solver is bound to an eigenproblem at declaration.
43  Other solver parameters (e.g., block size, auxiliary vectors) can be changed dynamically.
44 
45  The orthogonalization manager is used to project away from the auxiliary vectors.
46  If full orthogonalization is enabled, the orthogonalization manager is also used to construct an M orthonormal basis.
47  The orthogonalization manager is subclass of MatOrthoManager, which LOBPCG assumes to be defined by the M inner product.
48  LOBPCG will not work correctly if the orthomanager uses a different inner product.
49  */
50 
51 
52 #ifndef ANASAZI_LOBPCG_HPP
53 #define ANASAZI_LOBPCG_HPP
54 
55 #include "AnasaziTypes.hpp"
56 
57 #include "AnasaziEigensolver.hpp"
60 #include "Teuchos_ScalarTraits.hpp"
61 
63 #include "AnasaziSolverUtils.hpp"
64 
65 #include "Teuchos_LAPACK.hpp"
66 #include "Teuchos_BLAS.hpp"
69 #include "Teuchos_TimeMonitor.hpp"
70 
91 namespace Anasazi {
92 
94 
95 
100  template <class ScalarType, class MultiVector>
101  struct LOBPCGState {
108 
115 
122 
132 
135 
138 
139  LOBPCGState() :
140  V(Teuchos::null),KV(Teuchos::null),MV(Teuchos::null),
141  X(Teuchos::null),KX(Teuchos::null),MX(Teuchos::null),
142  P(Teuchos::null),KP(Teuchos::null),MP(Teuchos::null),
143  H(Teuchos::null),KH(Teuchos::null),MH(Teuchos::null),
144  R(Teuchos::null),T(Teuchos::null) {};
145  };
146 
148 
150 
151 
165  class LOBPCGRitzFailure : public AnasaziError {public:
166  LOBPCGRitzFailure(const std::string& what_arg) : AnasaziError(what_arg)
167  {}};
168 
181  class LOBPCGInitFailure : public AnasaziError {public:
182  LOBPCGInitFailure(const std::string& what_arg) : AnasaziError(what_arg)
183  {}};
184 
201  class LOBPCGOrthoFailure : public AnasaziError {public:
202  LOBPCGOrthoFailure(const std::string& what_arg) : AnasaziError(what_arg)
203  {}};
204 
206 
207 
208  template <class ScalarType, class MV, class OP>
209  class LOBPCG : public Eigensolver<ScalarType,MV,OP> {
210  public:
211 
213 
214 
224  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
227  Teuchos::ParameterList &params
228  );
229 
231  virtual ~LOBPCG() {};
232 
234 
236 
237 
264  void iterate();
265 
291  void initialize(LOBPCGState<ScalarType,MV> newstate);
292 
296  void initialize();
297 
317  bool isInitialized() const;
318 
329  LOBPCGState<ScalarType,MV> getState() const;
330 
332 
334 
335 
337  int getNumIters() const;
338 
340  void resetNumIters();
341 
349  Teuchos::RCP<const MV> getRitzVectors();
350 
356  std::vector<Value<ScalarType> > getRitzValues();
357 
365  std::vector<int> getRitzIndex();
366 
367 
373  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getResNorms();
374 
375 
381  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRes2Norms();
382 
383 
391  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRitzRes2Norms();
392 
393 
402  int getCurSubspaceDim() const;
403 
407  int getMaxSubspaceDim() const;
408 
410 
412 
413 
415  void setStatusTest(Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test);
416 
418  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > getStatusTest() const;
419 
421  const Eigenproblem<ScalarType,MV,OP>& getProblem() const;
422 
423 
432  void setBlockSize(int blockSize);
433 
434 
436  int getBlockSize() const;
437 
438 
450  void setAuxVecs(const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs);
451 
453  Teuchos::Array<Teuchos::RCP<const MV> > getAuxVecs() const;
454 
456 
458 
459 
465  void setFullOrtho(bool fullOrtho);
466 
468  bool getFullOrtho() const;
469 
471  bool hasP();
472 
474 
476 
477 
479  void currentStatus(std::ostream &os);
480 
482 
483  private:
484  //
485  //
486  //
487  void setupViews();
488  //
489  // Convenience typedefs
490  //
496  typedef typename SCT::magnitudeType MagnitudeType;
497  const MagnitudeType ONE;
498  const MagnitudeType ZERO;
499  const MagnitudeType NANVAL;
500  //
501  // Internal structs
502  //
503  struct CheckList {
504  bool checkX, checkMX, checkKX;
505  bool checkH, checkMH;
506  bool checkP, checkMP, checkKP;
507  bool checkR, checkQ;
508  CheckList() : checkX(false),checkMX(false),checkKX(false),
509  checkH(false),checkMH(false),
510  checkP(false),checkMP(false),checkKP(false),
511  checkR(false),checkQ(false) {};
512  };
513  //
514  // Internal methods
515  //
516  std::string accuracyCheck(const CheckList &chk, const std::string &where) const;
517  //
518  // Classes inputed through constructor that define the eigenproblem to be solved.
519  //
525  //
526  // Information obtained from the eigenproblem
527  //
531  bool hasM_;
532  //
533  // Internal timers
534  //
535  Teuchos::RCP<Teuchos::Time> timerOp_, timerMOp_, timerPrec_,
536  timerSort_,
537  timerLocalProj_, timerDS_,
538  timerLocalUpdate_, timerCompRes_,
539  timerOrtho_, timerInit_;
540  //
541  // Counters
542  //
543  // Number of operator applications
544  int count_ApplyOp_, count_ApplyM_, count_ApplyPrec_;
545 
546  //
547  // Algorithmic parameters.
548  //
549  // blockSize_ is the solver block size
550  int blockSize_;
551  //
552  // fullOrtho_ dictates whether the orthogonalization procedures specified by Hetmaniuk and Lehoucq should
553  // be activated (see citations at the top of this file)
554  bool fullOrtho_;
555 
556  //
557  // Current solver state
558  //
559  // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
560  // is capable of running; _initialize is controlled by the initialize() member method
561  // For the implications of the state of initialized_, please see documentation for initialize()
562  bool initialized_;
563  //
564  // nevLocal_ reflects how much of the current basis is valid (0 <= nevLocal_ <= 3*blockSize_)
565  // this tells us how many of the values in theta_ are valid Ritz values
566  int nevLocal_;
567  //
568  // hasP_ tells us whether there is valid data in P (and KP,MP)
569  bool hasP_;
570  //
571  // State Multivecs
572  // V_, KV_ MV_ and R_ are primary pointers to allocated multivectors
573  Teuchos::RCP<MV> V_, KV_, MV_, R_;
574  // the rest are multivector views into V_, KV_ and MV_
575  Teuchos::RCP<MV> X_, KX_, MX_,
576  H_, KH_, MH_,
577  P_, KP_, MP_;
578 
579  //
580  // if fullOrtho_ == true, then we must produce the following on every iteration:
581  // [newX newP] = [X H P] [CX;CP]
582  // the structure of [CX;CP] when using full orthogonalization does not allow us to
583  // do this in situ, and R_ does not have enough storage for newX and newP. therefore,
584  // we must allocate additional storage for this.
585  // otherwise, when not using full orthogonalization, the structure
586  // [newX newP] = [X H P] [CX1 0 ]
587  // [CX2 CP2] allows us to work using only R as work space
588  // [CX3 CP3]
589  Teuchos::RCP<MV> tmpmvec_;
590  //
591  // auxiliary vectors
593  int numAuxVecs_;
594  //
595  // Number of iterations that have been performed.
596  int iter_;
597  //
598  // Current eigenvalues, residual norms
599  std::vector<MagnitudeType> theta_, Rnorms_, R2norms_;
600  //
601  // are the residual norms current with the residual?
602  bool Rnorms_current_, R2norms_current_;
603 
604  };
605 
606 
607 
608 
610  // Constructor
611  template <class ScalarType, class MV, class OP>
615  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
618  Teuchos::ParameterList &params
619  ) :
620  ONE(Teuchos::ScalarTraits<MagnitudeType>::one()),
621  ZERO(Teuchos::ScalarTraits<MagnitudeType>::zero()),
622  NANVAL(Teuchos::ScalarTraits<MagnitudeType>::nan()),
623  // problem, tools
624  problem_(problem),
625  sm_(sorter),
626  om_(printer),
627  tester_(tester),
628  orthman_(ortho),
629  // timers, counters
630 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
631  timerOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation Op*x")),
632  timerMOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation M*x")),
633  timerPrec_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation Prec*x")),
634  timerSort_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Sorting eigenvalues")),
635  timerLocalProj_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Local projection")),
636  timerDS_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Direct solve")),
637  timerLocalUpdate_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Local update")),
638  timerCompRes_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Computing residuals")),
639  timerOrtho_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Orthogonalization")),
640  timerInit_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Initialization")),
641 #endif
642  count_ApplyOp_(0),
643  count_ApplyM_(0),
644  count_ApplyPrec_(0),
645  // internal data
646  blockSize_(0),
647  fullOrtho_(params.get("Full Ortho", true)),
648  initialized_(false),
649  nevLocal_(0),
650  hasP_(false),
651  auxVecs_( Teuchos::Array<Teuchos::RCP<const MV> >(0) ),
652  numAuxVecs_(0),
653  iter_(0),
654  Rnorms_current_(false),
655  R2norms_current_(false)
656  {
657  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,std::invalid_argument,
658  "Anasazi::LOBPCG::constructor: user passed null problem pointer.");
659  TEUCHOS_TEST_FOR_EXCEPTION(sm_ == Teuchos::null,std::invalid_argument,
660  "Anasazi::LOBPCG::constructor: user passed null sort manager pointer.");
661  TEUCHOS_TEST_FOR_EXCEPTION(om_ == Teuchos::null,std::invalid_argument,
662  "Anasazi::LOBPCG::constructor: user passed null output manager pointer.");
663  TEUCHOS_TEST_FOR_EXCEPTION(tester_ == Teuchos::null,std::invalid_argument,
664  "Anasazi::LOBPCG::constructor: user passed null status test pointer.");
665  TEUCHOS_TEST_FOR_EXCEPTION(orthman_ == Teuchos::null,std::invalid_argument,
666  "Anasazi::LOBPCG::constructor: user passed null orthogonalization manager pointer.");
667  TEUCHOS_TEST_FOR_EXCEPTION(problem_->isProblemSet() == false, std::invalid_argument,
668  "Anasazi::LOBPCG::constructor: problem is not set.");
669  TEUCHOS_TEST_FOR_EXCEPTION(problem_->isHermitian() == false, std::invalid_argument,
670  "Anasazi::LOBPCG::constructor: problem is not Hermitian; LOBPCG requires Hermitian problem.");
671 
672  // get the problem operators
673  Op_ = problem_->getOperator();
674  TEUCHOS_TEST_FOR_EXCEPTION(Op_ == Teuchos::null, std::invalid_argument,
675  "Anasazi::LOBPCG::constructor: problem provides no operator.");
676  MOp_ = problem_->getM();
677  Prec_ = problem_->getPrec();
678  hasM_ = (MOp_ != Teuchos::null);
679 
680  // set the block size and allocate data
681  int bs = params.get("Block Size", problem_->getNEV());
682  setBlockSize(bs);
683  }
684 
685 
687  // Set the block size and make necessary adjustments.
688  template <class ScalarType, class MV, class OP>
690  {
691  // time spent here counts towards timerInit_
692 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
693  Teuchos::TimeMonitor lcltimer( *timerInit_ );
694 #endif
695 
696  // This routine only allocates space; it doesn't not perform any computation
697  // if size is decreased, take the first newBS vectors of all and leave state as is
698  // otherwise, grow/allocate space and set solver to unitialized
699 
701  // grab some Multivector to Clone
702  // in practice, getInitVec() should always provide this, but it is possible to use a
703  // Eigenproblem with nothing in getInitVec() by manually initializing with initialize();
704  // in case of that strange scenario, we will try to Clone from R_ because it is smaller
705  // than V_, and we don't want to keep V_ around longer than necessary
706  if (blockSize_ > 0) {
707  tmp = R_;
708  }
709  else {
710  tmp = problem_->getInitVec();
711  TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::logic_error,
712  "Anasazi::LOBPCG::setBlockSize(): eigenproblem did not specify initial vectors to clone from.");
713  }
714 
715  TEUCHOS_TEST_FOR_EXCEPTION(newBS <= 0 || static_cast<ptrdiff_t>(newBS) > MVText::GetGlobalLength(*tmp),
716  std::invalid_argument, "Anasazi::LOBPCG::setBlockSize(): block size must be strictly positive.");
717  if (newBS == blockSize_) {
718  // do nothing
719  return;
720  }
721  else if (newBS < blockSize_ && initialized_) {
722  //
723  // shrink vectors
724 
725  // release views so we can modify the bases
726  X_ = Teuchos::null;
727  KX_ = Teuchos::null;
728  MX_ = Teuchos::null;
729  H_ = Teuchos::null;
730  KH_ = Teuchos::null;
731  MH_ = Teuchos::null;
732  P_ = Teuchos::null;
733  KP_ = Teuchos::null;
734  MP_ = Teuchos::null;
735 
736  // make new indices vectors
737  std::vector<int> newind(newBS), oldind(newBS);
738  for (int i=0; i<newBS; i++) {
739  newind[i] = i;
740  oldind[i] = i;
741  }
742 
743  Teuchos::RCP<MV> newV, newMV, newKV, newR;
745  // allocate R and newV
746  newR = MVT::Clone(*tmp,newBS);
747  newV = MVT::Clone(*tmp,newBS*3);
748  newKV = MVT::Clone(*tmp,newBS*3);
749  if (hasM_) {
750  newMV = MVT::Clone(*tmp,newBS*3);
751  }
752 
753  //
754  // if we are initialized, we want to pull the data from V_ into newV:
755  // bs | bs | bs
756  // newV = [newX | **** |newP ]
757  // newKV = [newKX| **** |newKP]
758  // newMV = [newMX| **** |newMP]
759  // where
760  // oldbs | oldbs | oldbs
761  // V_ = [newX *** | ******* | newP ***]
762  // KV_ = [newKX *** | ******* | newKP ***]
763  // MV_ = [newMX *** | ******* | newMP ***]
764  //
765  // we don't care to copy the data corresponding to H
766  // we will not copy the M data if !hasM_, because it doesn't exist
767  //
768 
769  // these are shrink operations which preserve their data
770  theta_.resize(3*newBS);
771  Rnorms_.resize(newBS);
772  R2norms_.resize(newBS);
773 
774  // copy residual vectors: oldind,newind currently contains [0,...,newBS-1]
775  src = MVT::CloneView(*R_,newind);
776  MVT::SetBlock(*src,newind,*newR);
777  // free old memory and point to new memory
778  R_ = newR;
779 
780  // copy in order: newX newKX newMX, then newP newKP newMP
781  // for X: [0,bs-1] <-- [0,bs-1]
782  src = MVT::CloneView(*V_,oldind);
783  MVT::SetBlock(*src,newind,*newV);
784  src = MVT::CloneView(*KV_,oldind);
785  MVT::SetBlock(*src,newind,*newKV);
786  if (hasM_) {
787  src = MVT::CloneView(*MV_,oldind);
788  MVT::SetBlock(*src,newind,*newMV);
789  }
790  // for P: [2*bs, 3*bs-1] <-- [2*oldbs, 2*oldbs+bs-1]
791  for (int i=0; i<newBS; i++) {
792  newind[i] += 2*newBS;
793  oldind[i] += 2*blockSize_;
794  }
795  src = MVT::CloneView(*V_,oldind);
796  MVT::SetBlock(*src,newind,*newV);
797  src = MVT::CloneView(*KV_,oldind);
798  MVT::SetBlock(*src,newind,*newKV);
799  if (hasM_) {
800  src = MVT::CloneView(*MV_,oldind);
801  MVT::SetBlock(*src,newind,*newMV);
802  }
803 
804  // release temp view
805  src = Teuchos::null;
806 
807  // release old allocations and point at new memory
808  V_ = newV;
809  KV_ = newKV;
810  if (hasM_) {
811  MV_ = newMV;
812  }
813  else {
814  MV_ = V_;
815  }
816  }
817  else {
818  // newBS > blockSize_ or not initialized
819  // this is also the scenario for our initial call to setBlockSize(), in the constructor
820  initialized_ = false;
821  hasP_ = false;
822 
823  // release views
824  X_ = Teuchos::null;
825  KX_ = Teuchos::null;
826  MX_ = Teuchos::null;
827  H_ = Teuchos::null;
828  KH_ = Teuchos::null;
829  MH_ = Teuchos::null;
830  P_ = Teuchos::null;
831  KP_ = Teuchos::null;
832  MP_ = Teuchos::null;
833 
834  // free allocated storage
835  R_ = Teuchos::null;
836  V_ = Teuchos::null;
837 
838  // allocate scalar vectors
839  theta_.resize(3*newBS,NANVAL);
840  Rnorms_.resize(newBS,NANVAL);
841  R2norms_.resize(newBS,NANVAL);
842 
843  // clone multivectors off of tmp
844  R_ = MVT::Clone(*tmp,newBS);
845  V_ = MVT::Clone(*tmp,newBS*3);
846  KV_ = MVT::Clone(*tmp,newBS*3);
847  if (hasM_) {
848  MV_ = MVT::Clone(*tmp,newBS*3);
849  }
850  else {
851  MV_ = V_;
852  }
853  }
854 
855  // allocate tmp space
856  tmpmvec_ = Teuchos::null;
857  if (fullOrtho_) {
858  tmpmvec_ = MVT::Clone(*tmp,newBS);
859  }
860 
861  // set new block size
862  blockSize_ = newBS;
863 
864  // setup new views
865  setupViews();
866  }
867 
868 
870  // Setup views into V,KV,MV
871  template <class ScalarType, class MV, class OP>
873  {
874  std::vector<int> ind(blockSize_);
875 
876  for (int i=0; i<blockSize_; i++) {
877  ind[i] = i;
878  }
879  X_ = MVT::CloneViewNonConst(*V_,ind);
880  KX_ = MVT::CloneViewNonConst(*KV_,ind);
881  if (hasM_) {
882  MX_ = MVT::CloneViewNonConst(*MV_,ind);
883  }
884  else {
885  MX_ = X_;
886  }
887 
888  for (int i=0; i<blockSize_; i++) {
889  ind[i] += blockSize_;
890  }
891  H_ = MVT::CloneViewNonConst(*V_,ind);
892  KH_ = MVT::CloneViewNonConst(*KV_,ind);
893  if (hasM_) {
894  MH_ = MVT::CloneViewNonConst(*MV_,ind);
895  }
896  else {
897  MH_ = H_;
898  }
899 
900  for (int i=0; i<blockSize_; i++) {
901  ind[i] += blockSize_;
902  }
903  P_ = MVT::CloneViewNonConst(*V_,ind);
904  KP_ = MVT::CloneViewNonConst(*KV_,ind);
905  if (hasM_) {
906  MP_ = MVT::CloneViewNonConst(*MV_,ind);
907  }
908  else {
909  MP_ = P_;
910  }
911  }
912 
913 
915  // Set the auxiliary vectors
916  template <class ScalarType, class MV, class OP>
918  typedef typename Teuchos::Array<Teuchos::RCP<const MV> >::iterator tarcpmv;
919 
920  // set new auxiliary vectors
921  auxVecs_ = auxvecs;
922 
923  numAuxVecs_ = 0;
924  for (tarcpmv i=auxVecs_.begin(); i != auxVecs_.end(); i++) {
925  numAuxVecs_ += MVT::GetNumberVecs(**i);
926  }
927 
928  // If the solver has been initialized, X and P are not necessarily orthogonal to new auxiliary vectors
929  if (numAuxVecs_ > 0 && initialized_) {
930  initialized_ = false;
931  hasP_ = false;
932  }
933 
934  if (om_->isVerbosity( Debug ) ) {
935  // Check almost everything here
936  CheckList chk;
937  chk.checkQ = true;
938  om_->print( Debug, accuracyCheck(chk, ": in setAuxVecs()") );
939  }
940  }
941 
942 
944  /* Initialize the state of the solver
945  *
946  * POST-CONDITIONS:
947  *
948  * initialized_ == true
949  * X is orthonormal, orthogonal to auxVecs_
950  * KX = Op*X
951  * MX = M*X if hasM_
952  * theta_ contains Ritz values of X
953  * R = KX - MX*diag(theta_)
954  * if hasP() == true,
955  * P orthogonal to auxVecs_
956  * if fullOrtho_ == true,
957  * P orthonormal and orthogonal to X
958  * KP = Op*P
959  * MP = M*P
960  */
961  template <class ScalarType, class MV, class OP>
963  {
964  // NOTE: memory has been allocated by setBlockSize(). Use SetBlock below; do not Clone
965  // NOTE: Overall time spent in this routine is counted to timerInit_; portions will also be counted towards other primitives
966 
967 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
968  Teuchos::TimeMonitor inittimer( *timerInit_ );
969 #endif
970 
971  std::vector<int> bsind(blockSize_);
972  for (int i=0; i<blockSize_; i++) bsind[i] = i;
973 
974  // in LOBPCG, X (the subspace iterate) is primary
975  // the order of dependence follows like so.
976  // --init-> X
977  // --op apply-> MX,KX
978  // --ritz analysis-> theta
979  // --optional-> P,MP,KP
980  //
981  // if the user specifies all data for a level, we will accept it.
982  // otherwise, we will generate the whole level, and all subsequent levels.
983  //
984  // the data members are ordered based on dependence, and the levels are
985  // partitioned according to the amount of work required to produce the
986  // items in a level.
987  //
988  // inconsitent multivectors widths and lengths will not be tolerated, and
989  // will be treated with exceptions.
990 
991  // set up X, KX, MX: get them from "state" if user specified them
992 
993  //----------------------------------------
994  // set up X, MX, KX
995  //----------------------------------------
996  if (newstate.X != Teuchos::null) {
998  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.X not correct." );
999  // newstate.X must have blockSize_ vectors; any more will be ignored
1000  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.X) < blockSize_,
1001  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.X must have at least block size vectors.");
1002 
1003  // put X data in X_
1004  MVT::SetBlock(*newstate.X,bsind,*X_);
1005 
1006  // put MX data in MX_
1007  if (hasM_) {
1008  if (newstate.MX != Teuchos::null) {
1010  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.MX not correct." );
1011  // newstate.MX must have blockSize_ vectors; any more will be ignored
1012  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.MX) < blockSize_,
1013  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.MX must have at least block size vectors.");
1014  MVT::SetBlock(*newstate.MX,bsind,*MX_);
1015  }
1016  else {
1017  // user didn't specify MX, compute it
1018  {
1019 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1020  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1021 #endif
1022  OPT::Apply(*MOp_,*X_,*MX_);
1023  count_ApplyM_ += blockSize_;
1024  }
1025  // we generated MX; we will generate R as well
1026  newstate.R = Teuchos::null;
1027  }
1028  }
1029 
1030  // put data in KX
1031  if (newstate.KX != Teuchos::null) {
1033  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.KX not correct." );
1034  // newstate.KX must have blockSize_ vectors; any more will be ignored
1035  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.KX) < blockSize_,
1036  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.KX must have at least block size vectors.");
1037  MVT::SetBlock(*newstate.KX,bsind,*KX_);
1038  }
1039  else {
1040  // user didn't specify KX, compute it
1041  {
1042 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1043  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1044 #endif
1045  OPT::Apply(*Op_,*X_,*KX_);
1046  count_ApplyOp_ += blockSize_;
1047  }
1048  // we generated KX; we will generate R as well
1049  newstate.R = Teuchos::null;
1050  }
1051  }
1052  else {
1053  // user did not specify X
1054  // we will initialize X, compute KX and MX, and compute R
1055  //
1056  // clear state so we won't use any data from it below
1057  newstate.P = Teuchos::null;
1058  newstate.KP = Teuchos::null;
1059  newstate.MP = Teuchos::null;
1060  newstate.R = Teuchos::null;
1061  newstate.T = Teuchos::null;
1062 
1063  // generate a basis and projectAndNormalize
1064  Teuchos::RCP<const MV> ivec = problem_->getInitVec();
1065  TEUCHOS_TEST_FOR_EXCEPTION(ivec == Teuchos::null,std::logic_error,
1066  "Anasazi::LOBPCG::initialize(): Eigenproblem did not specify initial vectors to clone from.");
1067 
1068  int initSize = MVT::GetNumberVecs(*ivec);
1069  if (initSize > blockSize_) {
1070  // we need only the first blockSize_ vectors from ivec; get a view of them
1071  initSize = blockSize_;
1072  std::vector<int> ind(blockSize_);
1073  for (int i=0; i<blockSize_; i++) ind[i] = i;
1074  ivec = MVT::CloneView(*ivec,ind);
1075  }
1076 
1077  // assign ivec to first part of X_
1078  if (initSize > 0) {
1079  std::vector<int> ind(initSize);
1080  for (int i=0; i<initSize; i++) ind[i] = i;
1081  MVT::SetBlock(*ivec,ind,*X_);
1082  }
1083  // fill the rest of X_ with random
1084  if (blockSize_ > initSize) {
1085  std::vector<int> ind(blockSize_ - initSize);
1086  for (int i=0; i<blockSize_ - initSize; i++) ind[i] = initSize + i;
1088  MVT::MvRandom(*rX);
1089  rX = Teuchos::null;
1090  }
1091 
1092  // put data in MX
1093  if (hasM_) {
1094 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1095  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1096 #endif
1097  OPT::Apply(*MOp_,*X_,*MX_);
1098  count_ApplyM_ += blockSize_;
1099  }
1100 
1101  // remove auxVecs from X_ and normalize it
1102  if (numAuxVecs_ > 0) {
1103 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1104  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1105 #endif
1107  int rank = orthman_->projectAndNormalizeMat(*X_,auxVecs_,dummy,Teuchos::null,MX_);
1108  TEUCHOS_TEST_FOR_EXCEPTION(rank != blockSize_, LOBPCGInitFailure,
1109  "Anasazi::LOBPCG::initialize(): Couldn't generate initial basis of full rank.");
1110  }
1111  else {
1112 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1113  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1114 #endif
1115  int rank = orthman_->normalizeMat(*X_,Teuchos::null,MX_);
1116  TEUCHOS_TEST_FOR_EXCEPTION(rank != blockSize_, LOBPCGInitFailure,
1117  "Anasazi::LOBPCG::initialize(): Couldn't generate initial basis of full rank.");
1118  }
1119 
1120  // put data in KX
1121  {
1122 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1123  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1124 #endif
1125  OPT::Apply(*Op_,*X_,*KX_);
1126  count_ApplyOp_ += blockSize_;
1127  }
1128  } // end if (newstate.X != Teuchos::null)
1129 
1130 
1131  //----------------------------------------
1132  // set up Ritz values
1133  //----------------------------------------
1134  theta_.resize(3*blockSize_,NANVAL);
1135  if (newstate.T != Teuchos::null) {
1136  TEUCHOS_TEST_FOR_EXCEPTION( (signed int)(newstate.T->size()) < blockSize_,
1137  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.T must contain at least block size Ritz values.");
1138  for (int i=0; i<blockSize_; i++) {
1139  theta_[i] = (*newstate.T)[i];
1140  }
1141  nevLocal_ = blockSize_;
1142  }
1143  else {
1144  // get ritz vecs/vals
1145  Teuchos::SerialDenseMatrix<int,ScalarType> KK(blockSize_,blockSize_),
1146  MM(blockSize_,blockSize_),
1147  S(blockSize_,blockSize_);
1148  {
1149 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1150  Teuchos::TimeMonitor lcltimer( *timerLocalProj_ );
1151 #endif
1152  // project K
1153  MVT::MvTransMv(ONE,*X_,*KX_,KK);
1154  // project M
1155  MVT::MvTransMv(ONE,*X_,*MX_,MM);
1156  nevLocal_ = blockSize_;
1157  }
1158 
1159  // solve the projected problem
1160  {
1161 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1162  Teuchos::TimeMonitor lcltimer( *timerDS_ );
1163 #endif
1164  Utils::directSolver(blockSize_, KK, Teuchos::rcpFromRef(MM), S, theta_, nevLocal_, 1);
1165  TEUCHOS_TEST_FOR_EXCEPTION(nevLocal_ != blockSize_,LOBPCGInitFailure,
1166  "Anasazi::LOBPCG::initialize(): Initial Ritz analysis did not produce enough Ritz pairs to initialize algorithm.");
1167  }
1168 
1169  // We only have blockSize_ ritz pairs, ergo we do not need to select.
1170  // However, we still require them to be ordered correctly
1171  {
1172 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1173  Teuchos::TimeMonitor lcltimer( *timerSort_ );
1174 #endif
1175 
1176  std::vector<int> order(blockSize_);
1177  //
1178  // sort the first blockSize_ values in theta_
1179  sm_->sort(theta_, Teuchos::rcpFromRef(order), blockSize_); // don't catch exception
1180  //
1181  // apply the same ordering to the primitive ritz vectors
1182  Utils::permuteVectors(order,S);
1183  }
1184 
1185  // update the solution, use R for storage
1186  {
1187 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1188  Teuchos::TimeMonitor lcltimer( *timerLocalUpdate_ );
1189 #endif
1190  // X <- X*S
1191  MVT::MvAddMv( ONE, *X_, ZERO, *X_, *R_ );
1192  MVT::MvTimesMatAddMv( ONE, *R_, S, ZERO, *X_ );
1193  // KX <- KX*S
1194  MVT::MvAddMv( ONE, *KX_, ZERO, *KX_, *R_ );
1195  MVT::MvTimesMatAddMv( ONE, *R_, S, ZERO, *KX_ );
1196  if (hasM_) {
1197  // MX <- MX*S
1198  MVT::MvAddMv( ONE, *MX_, ZERO, *MX_, *R_ );
1199  MVT::MvTimesMatAddMv( ONE, *R_, S, ZERO, *MX_ );
1200  }
1201  }
1202  }
1203 
1204  //----------------------------------------
1205  // compute R
1206  //----------------------------------------
1207  if (newstate.R != Teuchos::null) {
1209  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.R not correct." );
1210  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.R) < blockSize_,
1211  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.R must have blockSize number of vectors." );
1212  MVT::SetBlock(*newstate.R,bsind,*R_);
1213  }
1214  else {
1215 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1216  Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1217 #endif
1218  // form R <- KX - MX*T
1219  MVT::MvAddMv(ZERO,*KX_,ONE,*KX_,*R_);
1220  Teuchos::SerialDenseMatrix<int,ScalarType> T(blockSize_,blockSize_);
1221  for (int i=0; i<blockSize_; i++) T(i,i) = theta_[i];
1222  MVT::MvTimesMatAddMv(-ONE,*MX_,T,ONE,*R_);
1223  }
1224 
1225  // R has been updated; mark the norms as out-of-date
1226  Rnorms_current_ = false;
1227  R2norms_current_ = false;
1228 
1229  // put data in P,KP,MP: P is not used to set theta
1230  if (newstate.P != Teuchos::null) {
1231  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.P) < blockSize_ ,
1232  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.P must have blockSize number of vectors." );
1234  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.P not correct." );
1235  hasP_ = true;
1236 
1237  // set P_
1238  MVT::SetBlock(*newstate.P,bsind,*P_);
1239 
1240  // set/compute KP_
1241  if (newstate.KP != Teuchos::null) {
1242  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.KP) < blockSize_,
1243  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.KP must have blockSize number of vectors." );
1245  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.KP not correct." );
1246  MVT::SetBlock(*newstate.KP,bsind,*KP_);
1247  }
1248  else {
1249 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1250  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1251 #endif
1252  OPT::Apply(*Op_,*P_,*KP_);
1253  count_ApplyOp_ += blockSize_;
1254  }
1255 
1256  // set/compute MP_
1257  if (hasM_) {
1258  if (newstate.MP != Teuchos::null) {
1259  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.MP) < blockSize_,
1260  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.MP must have blockSize number of vectors." );
1262  std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.MP not correct." );
1263  MVT::SetBlock(*newstate.MP,bsind,*MP_);
1264  }
1265  else {
1266 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1267  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1268 #endif
1269  OPT::Apply(*MOp_,*P_,*MP_);
1270  count_ApplyM_ += blockSize_;
1271  }
1272  }
1273  }
1274  else {
1275  hasP_ = false;
1276  }
1277 
1278  // finally, we are initialized
1279  initialized_ = true;
1280 
1281  if (om_->isVerbosity( Debug ) ) {
1282  // Check almost everything here
1283  CheckList chk;
1284  chk.checkX = true;
1285  chk.checkKX = true;
1286  chk.checkMX = true;
1287  chk.checkP = true;
1288  chk.checkKP = true;
1289  chk.checkMP = true;
1290  chk.checkR = true;
1291  chk.checkQ = true;
1292  om_->print( Debug, accuracyCheck(chk, ": after initialize()") );
1293  }
1294 
1295  }
1296 
1297  template <class ScalarType, class MV, class OP>
1299  {
1301  initialize(empty);
1302  }
1303 
1304 
1306  // Instruct the solver to use full orthogonalization
1307  template <class ScalarType, class MV, class OP>
1309  {
1310  if ( fullOrtho_ == true || initialized_ == false || fullOrtho == fullOrtho_ ) {
1311  // state is already orthogonalized or solver is not initialized
1312  fullOrtho_ = fullOrtho;
1313  }
1314  else {
1315  // solver is initialized, state is not fully orthogonalized, and user has requested full orthogonalization
1316  // ergo, we must throw away data in P
1317  fullOrtho_ = true;
1318  hasP_ = false;
1319  }
1320 
1321  // the user has called setFullOrtho, so the class has been instantiated
1322  // ergo, the data has already been allocated, i.e., setBlockSize() has been called
1323  // if it is already allocated, it should be the proper size
1324  if (fullOrtho_ && tmpmvec_ == Teuchos::null) {
1325  // allocated the workspace
1326  tmpmvec_ = MVT::Clone(*X_,blockSize_);
1327  }
1328  else if (fullOrtho_==false) {
1329  // free the workspace
1330  tmpmvec_ = Teuchos::null;
1331  }
1332  }
1333 
1334 
1336  // Perform LOBPCG iterations until the StatusTest tells us to stop.
1337  template <class ScalarType, class MV, class OP>
1339  {
1340  //
1341  // Allocate/initialize data structures
1342  //
1343  if (initialized_ == false) {
1344  initialize();
1345  }
1346 
1347  //
1348  // Miscellaneous definitions
1349  const int oneBlock = blockSize_;
1350  const int twoBlocks = 2*blockSize_;
1351  const int threeBlocks = 3*blockSize_;
1352 
1353  std::vector<int> indblock1(blockSize_), indblock2(blockSize_), indblock3(blockSize_);
1354  for (int i=0; i<blockSize_; i++) {
1355  indblock1[i] = i;
1356  indblock2[i] = i + blockSize_;
1357  indblock3[i] = i + 2*blockSize_;
1358  }
1359 
1360  //
1361  // Define dense projected/local matrices
1362  // KK = Local stiffness matrix (size: 3*blockSize_ x 3*blockSize_)
1363  // MM = Local mass matrix (size: 3*blockSize_ x 3*blockSize_)
1364  // S = Local eigenvectors (size: 3*blockSize_ x 3*blockSize_)
1365  Teuchos::SerialDenseMatrix<int,ScalarType> KK( threeBlocks, threeBlocks ),
1366  MM( threeBlocks, threeBlocks ),
1367  S( threeBlocks, threeBlocks );
1368 
1369  while (tester_->checkStatus(this) != Passed) {
1370 
1371  // Print information on current status
1372  if (om_->isVerbosity(Debug)) {
1373  currentStatus( om_->stream(Debug) );
1374  }
1375  else if (om_->isVerbosity(IterationDetails)) {
1376  currentStatus( om_->stream(IterationDetails) );
1377  }
1378 
1379  // increment iteration counter
1380  iter_++;
1381 
1382  // Apply the preconditioner on the residuals: H <- Prec*R
1383  if (Prec_ != Teuchos::null) {
1384 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1385  Teuchos::TimeMonitor lcltimer( *timerPrec_ );
1386 #endif
1387  OPT::Apply( *Prec_, *R_, *H_ ); // don't catch the exception
1388  count_ApplyPrec_ += blockSize_;
1389  }
1390  else {
1391  MVT::MvAddMv(ONE,*R_,ZERO,*R_,*H_);
1392  }
1393 
1394  // Apply the mass matrix on H
1395  if (hasM_) {
1396 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1397  Teuchos::TimeMonitor lcltimer( *timerMOp_ );
1398 #endif
1399  OPT::Apply( *MOp_, *H_, *MH_); // don't catch the exception
1400  count_ApplyM_ += blockSize_;
1401  }
1402 
1403  // orthogonalize H against the auxiliary vectors
1404  // optionally: orthogonalize H against X and P ([X P] is already orthonormal)
1406  Q = auxVecs_;
1407  if (fullOrtho_) {
1408  // X and P are not contiguous, so there is not much point in putting them under
1409  // a single multivector view
1410  Q.push_back(X_);
1411  if (hasP_) {
1412  Q.push_back(P_);
1413  }
1414  }
1415  {
1416 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1417  Teuchos::TimeMonitor lcltimer( *timerOrtho_ );
1418 #endif
1420  Teuchos::tuple<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > >(Teuchos::null);
1421  int rank = orthman_->projectAndNormalizeMat(*H_,Q,dummyC,Teuchos::null,MH_);
1422  // our views are currently in place; it is safe to throw an exception
1424  "Anasazi::LOBPCG::iterate(): unable to compute orthonormal basis for H.");
1425  }
1426 
1427  if (om_->isVerbosity( Debug ) ) {
1428  CheckList chk;
1429  chk.checkH = true;
1430  chk.checkMH = true;
1431  om_->print( Debug, accuracyCheck(chk, ": after ortho H") );
1432  }
1433  else if (om_->isVerbosity( OrthoDetails ) ) {
1434  CheckList chk;
1435  chk.checkH = true;
1436  chk.checkMH = true;
1437  om_->print( OrthoDetails, accuracyCheck(chk,": after ortho H") );
1438  }
1439 
1440  // Apply the stiffness matrix to H
1441  {
1442 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1443  Teuchos::TimeMonitor lcltimer( *timerOp_ );
1444 #endif
1445  OPT::Apply( *Op_, *H_, *KH_); // don't catch the exception
1446  count_ApplyOp_ += blockSize_;
1447  }
1448 
1449  if (hasP_) {
1450  nevLocal_ = threeBlocks;
1451  }
1452  else {
1453  nevLocal_ = twoBlocks;
1454  }
1455 
1456  //
1457  // we need bases: [X H P] and [H P] (only need the latter if fullOrtho == false)
1458  // we need to perform the following operations:
1459  // X' [KX KH KP]
1460  // X' [MX MH MP]
1461  // H' [KH KP]
1462  // H' [MH MP]
1463  // P' [KP]
1464  // P' [MP]
1465  // [X H P] CX
1466  // [X H P] CP if fullOrtho
1467  // [H P] CP if !fullOrtho
1468  //
1469  // since M[X H P] is potentially the same memory as [X H P], and
1470  // because we are not allowed to have overlapping non-const views of
1471  // a multivector, we will now abandon our non-const views in favor of
1472  // const views
1473  //
1474  X_ = Teuchos::null;
1475  KX_ = Teuchos::null;
1476  MX_ = Teuchos::null;
1477  H_ = Teuchos::null;
1478  KH_ = Teuchos::null;
1479  MH_ = Teuchos::null;
1480  P_ = Teuchos::null;
1481  KP_ = Teuchos::null;
1482  MP_ = Teuchos::null;
1483  Teuchos::RCP<const MV> cX, cH, cXHP, cHP, cK_XHP, cK_HP, cM_XHP, cM_HP, cP, cK_P, cM_P;
1484  {
1485  cX = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(V_),indblock1);
1486  cH = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(V_),indblock2);
1487 
1488  std::vector<int> indXHP(nevLocal_);
1489  for (int i=0; i<nevLocal_; i++) {
1490  indXHP[i] = i;
1491  }
1492  cXHP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(V_),indXHP);
1493  cK_XHP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(KV_),indXHP);
1494  if (hasM_) {
1495  cM_XHP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(MV_),indXHP);
1496  }
1497  else {
1498  cM_XHP = cXHP;
1499  }
1500 
1501  std::vector<int> indHP(nevLocal_-blockSize_);
1502  for (int i=blockSize_; i<nevLocal_; i++) {
1503  indHP[i-blockSize_] = i;
1504  }
1505  cHP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(V_),indHP);
1506  cK_HP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(KV_),indHP);
1507  if (hasM_) {
1508  cM_HP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(MV_),indHP);
1509  }
1510  else {
1511  cM_HP = cHP;
1512  }
1513 
1514  if (nevLocal_ == threeBlocks) {
1515  cP = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(V_),indblock3);
1516  cK_P = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(KV_),indblock3);
1517  if (hasM_) {
1518  cM_P = MVT::CloneView(*Teuchos::rcp_implicit_cast<const MV>(MV_),indblock3);
1519  }
1520  else {
1521  cM_P = cP;
1522  }
1523  }
1524  }
1525 
1526  //
1527  //----------------------------------------
1528  // Form "local" mass and stiffness matrices
1529  //----------------------------------------
1530  {
1531  // We will form only the block upper triangular part of
1532  // [X H P]' K [X H P] and [X H P]' M [X H P]
1533  // Get the necessary views into KK and MM:
1534  // [--K1--] [--M1--]
1535  // KK = [ -K2-] MM = [ -M2-]
1536  // [ K3] [ M3]
1537  //
1538  // It is okay to declare a zero-area view of a Teuchos::SerialDenseMatrix
1539  //
1541  K1(Teuchos::View,KK,blockSize_,nevLocal_ ,0*blockSize_,0*blockSize_),
1542  K2(Teuchos::View,KK,blockSize_,nevLocal_-1*blockSize_,1*blockSize_,1*blockSize_),
1543  K3(Teuchos::View,KK,blockSize_,nevLocal_-2*blockSize_,2*blockSize_,2*blockSize_),
1544  M1(Teuchos::View,MM,blockSize_,nevLocal_ ,0*blockSize_,0*blockSize_),
1545  M2(Teuchos::View,MM,blockSize_,nevLocal_-1*blockSize_,1*blockSize_,1*blockSize_),
1546  M3(Teuchos::View,MM,blockSize_,nevLocal_-2*blockSize_,2*blockSize_,2*blockSize_);
1547  {
1548 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1549  Teuchos::TimeMonitor lcltimer( *timerLocalProj_ );
1550 #endif
1551  MVT::MvTransMv( ONE, *cX, *cK_XHP, K1 );
1552  MVT::MvTransMv( ONE, *cX, *cM_XHP, M1 );
1553  MVT::MvTransMv( ONE, *cH, *cK_HP , K2 );
1554  MVT::MvTransMv( ONE, *cH, *cM_HP , M2 );
1555  if (nevLocal_ == threeBlocks) {
1556  MVT::MvTransMv( ONE, *cP, *cK_P, K3 );
1557  MVT::MvTransMv( ONE, *cP, *cM_P, M3 );
1558  }
1559  }
1560  }
1561  // below, we only need bases [X H P] and [H P] and friends
1562  // furthermore, we only need [H P] and friends if fullOrtho == false
1563  // clear the others now
1564  cX = Teuchos::null;
1565  cH = Teuchos::null;
1566  cP = Teuchos::null;
1567  cK_P = Teuchos::null;
1568  cM_P = Teuchos::null;
1569  if (fullOrtho_ == true) {
1570  cHP = Teuchos::null;
1571  cK_HP = Teuchos::null;
1572  cM_HP = Teuchos::null;
1573  }
1574 
1575  //
1576  //---------------------------------------------------
1577  // Perform a spectral decomposition of (KK,MM)
1578  //---------------------------------------------------
1579  //
1580  // Get pointers to relevant part of KK, MM and S for Rayleigh-Ritz analysis
1581  Teuchos::SerialDenseMatrix<int,ScalarType> lclKK(Teuchos::View,KK,nevLocal_,nevLocal_),
1582  lclMM(Teuchos::View,MM,nevLocal_,nevLocal_),
1583  lclS(Teuchos::View, S,nevLocal_,nevLocal_);
1584  {
1585 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1586  Teuchos::TimeMonitor lcltimer( *timerDS_ );
1587 #endif
1588  int localSize = nevLocal_;
1589  Utils::directSolver(localSize, lclKK, Teuchos::rcpFromRef(lclMM), lclS, theta_, nevLocal_, 0);
1590  // localSize tells directSolver() how big KK,MM are
1591  // however, directSolver() may choose to use only the principle submatrices of KK,MM
1592  // because of loss of MM-orthogonality in the projected eigenvectors
1593  // nevLocal_ tells us how much it used, telling us the effective localSize
1594  // (i.e., how much of KK,MM used by directSolver)
1595  // we will not tolerate any indefiniteness, and will throw an exception if it was
1596  // detected by directSolver
1597  //
1598  if (nevLocal_ != localSize) {
1599  // before throwing the exception, and thereby leaving iterate(), setup the views again
1600  // first, clear the const views
1601  cXHP = Teuchos::null;
1602  cK_XHP = Teuchos::null;
1603  cM_XHP = Teuchos::null;
1604  cHP = Teuchos::null;
1605  cK_HP = Teuchos::null;
1606  cM_HP = Teuchos::null;
1607  setupViews();
1608  }
1609  TEUCHOS_TEST_FOR_EXCEPTION(nevLocal_ != localSize, LOBPCGRitzFailure,
1610  "Anasazi::LOBPCG::iterate(): indefiniteness detected in projected mass matrix." );
1611  }
1612 
1613  //
1614  //---------------------------------------------------
1615  // Sort the ritz values using the sort manager
1616  //---------------------------------------------------
1619  {
1620 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1621  Teuchos::TimeMonitor lcltimer( *timerSort_ );
1622 #endif
1623 
1624  std::vector<int> order(nevLocal_);
1625  //
1626  // Sort the first nevLocal_ values in theta_
1627  sm_->sort(theta_, Teuchos::rcpFromRef(order), nevLocal_); // don't catch exception
1628  //
1629  // Sort the primitive ritz vectors
1630  Utils::permuteVectors(order,lclS);
1631  }
1632 
1633  //
1634  //----------------------------------------
1635  // Compute coefficients for X and P under [X H P]
1636  //----------------------------------------
1637  // Before computing X,P, optionally perform orthogonalization per Hetmaniuk,Lehoucq paper
1638  // CX will be the coefficients of [X,H,P] for new X, CP for new P
1639  // The paper suggests orthogonalizing CP against CX and orthonormalizing CP, w.r.t. MM
1640  // Here, we will also orthonormalize CX.
1641  // This is accomplished using the Cholesky factorization of [CX CP]^H lclMM [CX CP]
1643  if (fullOrtho_) {
1644  // build orthonormal basis for ( 0 ) that is MM orthogonal to ( S11 )
1645  // ( S21 ) ( S21 )
1646  // ( S31 ) ( S31 )
1647  // Do this using Cholesky factorization of ( S11 0 )
1648  // ( S21 S21 )
1649  // ( S31 S31 )
1650  // ( S11 0 )
1651  // Build C = ( S21 S21 )
1652  // ( S31 S31 )
1653  Teuchos::SerialDenseMatrix<int,ScalarType> C(nevLocal_,twoBlocks),
1654  MMC(nevLocal_,twoBlocks),
1655  CMMC(twoBlocks ,twoBlocks);
1656 
1657  // first block of rows: ( S11 0 )
1658  for (int j=0; j<oneBlock; j++) {
1659  for (int i=0; i<oneBlock; i++) {
1660  // CX
1661  C(i,j) = lclS(i,j);
1662  // CP
1663  C(i,j+oneBlock) = ZERO;
1664  }
1665  }
1666  // second block of rows: (S21 S21)
1667  for (int j=0; j<oneBlock; j++) {
1668  for (int i=oneBlock; i<twoBlocks; i++) {
1669  // CX
1670  C(i,j) = lclS(i,j);
1671  // CP
1672  C(i,j+oneBlock) = lclS(i,j);
1673  }
1674  }
1675  // third block of rows
1676  if (nevLocal_ == threeBlocks) {
1677  for (int j=0; j<oneBlock; j++) {
1678  for (int i=twoBlocks; i<threeBlocks; i++) {
1679  // CX
1680  C(i,j) = lclS(i,j);
1681  // CP
1682  C(i,j+oneBlock) = lclS(i,j);
1683  }
1684  }
1685  }
1686 
1687  // compute MMC = lclMM*C
1688  {
1689  int teuchosret;
1690  teuchosret = MMC.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,lclMM,C,ZERO);
1691  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1692  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1693  // compute CMMC = C^H*MMC == C^H*lclMM*C
1694  teuchosret = CMMC.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,C,MMC,ZERO);
1695  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1696  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1697  }
1698 
1699  // compute R (cholesky) of CMMC
1700  {
1701  int info;
1702  lapack.POTRF('U',twoBlocks,CMMC.values(),CMMC.stride(),&info);
1703  // our views ARE NOT currently in place; we must reestablish them before throwing an exception
1704  if (info != 0) {
1705  // Check symmetry of H'*K*H, this might be the first place a non-Hermitian operator will cause a problem.
1706  Teuchos::SerialDenseMatrix<int,ScalarType> K22(Teuchos::View,KK,blockSize_,blockSize_,1*blockSize_,1*blockSize_);
1708  K22_trans -= K22;
1709  MagnitudeType symNorm = K22_trans.normOne();
1710  MagnitudeType tol = SCT::magnitude(SCT::squareroot(SCT::eps()));
1711 
1712  cXHP = Teuchos::null;
1713  cHP = Teuchos::null;
1714  cK_XHP = Teuchos::null;
1715  cK_HP = Teuchos::null;
1716  cM_XHP = Teuchos::null;
1717  cM_HP = Teuchos::null;
1718  setupViews();
1719 
1720  std::string errMsg = "Anasazi::LOBPCG::iterate(): Cholesky factorization failed during full orthogonalization.";
1721  if ( symNorm < tol )
1722  {
1723  TEUCHOS_TEST_FOR_EXCEPTION(info != 0, LOBPCGOrthoFailure, errMsg );
1724  }
1725  else
1726  {
1727  errMsg += " Check the operator A (or K), it may not be Hermitian!";
1728  TEUCHOS_TEST_FOR_EXCEPTION(info != 0, LOBPCGOrthoFailure, errMsg );
1729  }
1730  }
1731  }
1732  // compute C = C inv(R)
1734  nevLocal_,twoBlocks,ONE,CMMC.values(),CMMC.stride(),C.values(),C.stride());
1735  // put C(:,0:oneBlock-1) into CX
1736  CX = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy,C,nevLocal_,oneBlock,0,0) );
1737  // put C(:,oneBlock:twoBlocks-1) into CP
1738  CP = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy,C,nevLocal_,oneBlock,0,oneBlock) );
1739 
1740  // check the results
1741  if (om_->isVerbosity( Debug ) ) {
1742  Teuchos::SerialDenseMatrix<int,ScalarType> tmp1(nevLocal_,oneBlock),
1743  tmp2(oneBlock,oneBlock);
1744  MagnitudeType tmp;
1745  int teuchosret;
1746  std::stringstream os;
1747  os.precision(2);
1748  os.setf(std::ios::scientific, std::ios::floatfield);
1749 
1750  os << " Checking Full Ortho: iteration " << iter_ << std::endl;
1751 
1752  // check CX^T MM CX == I
1753  // compute tmp1 = MM*CX
1754  teuchosret = tmp1.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,lclMM,*CX,ZERO);
1755  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1756  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1757  // compute tmp2 = CX^H*tmp1 == CX^H*MM*CX
1758  teuchosret = tmp2.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,*CX,tmp1,ZERO);
1759  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1760  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1761  // subtrace tmp2 - I == CX^H * MM * CX - I
1762  for (int i=0; i<oneBlock; i++) tmp2(i,i) -= ONE;
1763  tmp = tmp2.normFrobenius();
1764  os << " >> Error in CX^H MM CX == I : " << tmp << std::endl;
1765 
1766  // check CP^T MM CP == I
1767  // compute tmp1 = MM*CP
1768  teuchosret = tmp1.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,lclMM,*CP,ZERO);
1769  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1770  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1771  // compute tmp2 = CP^H*tmp1 == CP^H*MM*CP
1772  teuchosret = tmp2.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,*CP,tmp1,ZERO);
1773  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,
1774  "Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1775  // subtrace tmp2 - I == CP^H * MM * CP - I
1776  for (int i=0; i<oneBlock; i++) tmp2(i,i) -= ONE;
1777  tmp = tmp2.normFrobenius();
1778  os << " >> Error in CP^H MM CP == I : " << tmp << std::endl;
1779 
1780  // check CX^T MM CP == 0
1781  // compute tmp1 = MM*CP
1782  teuchosret = tmp1.multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,lclMM,*CP,ZERO);
1783  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,"Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1784  // compute tmp2 = CX^H*tmp1 == CX^H*MM*CP
1785  teuchosret = tmp2.multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,*CX,tmp1,ZERO);
1786  TEUCHOS_TEST_FOR_EXCEPTION(teuchosret != 0,std::logic_error,"Anasazi::LOBPCG::iterate(): Logic error calling SerialDenseMatrix::multiply");
1787  // subtrace tmp2 == CX^H * MM * CP
1788  tmp = tmp2.normFrobenius();
1789  os << " >> Error in CX^H MM CP == 0 : " << tmp << std::endl;
1790 
1791  os << std::endl;
1792  om_->print(Debug,os.str());
1793  }
1794  }
1795  else {
1796  // [S11 ... ...]
1797  // S = [S21 ... ...]
1798  // [S31 ... ...]
1799  //
1800  // CX = [S11]
1801  // [S21]
1802  // [S31] -> X = [X H P] CX
1803  //
1804  // CP = [S21] -> P = [H P] CP
1805  // [S31]
1806  //
1807  CX = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy,lclS,nevLocal_ ,oneBlock,0 ,0) );
1808  CP = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy,lclS,nevLocal_-oneBlock,oneBlock,oneBlock,0) );
1809  }
1810 
1811  //
1812  //----------------------------------------
1813  // Compute new X and new P
1814  //----------------------------------------
1815  // Note: Use R as a temporary work space and (if full ortho) tmpMV as well
1816  {
1817 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1818  Teuchos::TimeMonitor lcltimer( *timerLocalUpdate_ );
1819 #endif
1820 
1821  // if full ortho, then CX and CP are dense
1822  // we multiply [X H P]*CX into tmpMV
1823  // [X H P]*CP into R
1824  // then put V(:,firstblock) <- tmpMV
1825  // V(:,thirdblock) <- R
1826  //
1827  // if no full ortho, then [H P]*CP doesn't reference first block (X)
1828  // of V, so that we can modify it before computing P
1829  // so we multiply [X H P]*CX into R
1830  // V(:,firstblock) <- R
1831  // multiply [H P]*CP into R
1832  // V(:,thirdblock) <- R
1833  //
1834  // mutatis mutandis for K[XP] and M[XP]
1835  //
1836  // use SetBlock to do the assignments into V_
1837  //
1838  // in either case, views are only allowed to be overlapping
1839  // if they are const, and it should be assume that SetBlock
1840  // creates a view of the associated part
1841  //
1842  // we have from above const-pointers to [KM]XHP, [KM]HP and (if hasP) [KM]P
1843  //
1844  if (fullOrtho_) {
1845  // X,P
1846  MVT::MvTimesMatAddMv(ONE,*cXHP,*CX,ZERO,*tmpmvec_);
1847  MVT::MvTimesMatAddMv(ONE,*cXHP,*CP,ZERO,*R_);
1848  cXHP = Teuchos::null;
1849  MVT::SetBlock(*tmpmvec_,indblock1,*V_);
1850  MVT::SetBlock(*R_ ,indblock3,*V_);
1851  // KX,KP
1852  MVT::MvTimesMatAddMv(ONE,*cK_XHP,*CX,ZERO,*tmpmvec_);
1853  MVT::MvTimesMatAddMv(ONE,*cK_XHP,*CP,ZERO,*R_);
1854  cK_XHP = Teuchos::null;
1855  MVT::SetBlock(*tmpmvec_,indblock1,*KV_);
1856  MVT::SetBlock(*R_ ,indblock3,*KV_);
1857  // MX,MP
1858  if (hasM_) {
1859  MVT::MvTimesMatAddMv(ONE,*cM_XHP,*CX,ZERO,*tmpmvec_);
1860  MVT::MvTimesMatAddMv(ONE,*cM_XHP,*CP,ZERO,*R_);
1861  cM_XHP = Teuchos::null;
1862  MVT::SetBlock(*tmpmvec_,indblock1,*MV_);
1863  MVT::SetBlock(*R_ ,indblock3,*MV_);
1864  }
1865  else {
1866  cM_XHP = Teuchos::null;
1867  }
1868  }
1869  else {
1870  // X,P
1871  MVT::MvTimesMatAddMv(ONE,*cXHP,*CX,ZERO,*R_);
1872  cXHP = Teuchos::null;
1873  MVT::SetBlock(*R_,indblock1,*V_);
1874  MVT::MvTimesMatAddMv(ONE,*cHP,*CP,ZERO,*R_);
1875  cHP = Teuchos::null;
1876  MVT::SetBlock(*R_,indblock3,*V_);
1877  // KX,KP
1878  MVT::MvTimesMatAddMv(ONE,*cK_XHP,*CX,ZERO,*R_);
1879  cK_XHP = Teuchos::null;
1880  MVT::SetBlock(*R_,indblock1,*KV_);
1881  MVT::MvTimesMatAddMv(ONE,*cK_HP,*CP,ZERO,*R_);
1882  cK_HP = Teuchos::null;
1883  MVT::SetBlock(*R_,indblock3,*KV_);
1884  // MX,MP
1885  if (hasM_) {
1886  MVT::MvTimesMatAddMv(ONE,*cM_XHP,*CX,ZERO,*R_);
1887  cM_XHP = Teuchos::null;
1888  MVT::SetBlock(*R_,indblock1,*MV_);
1889  MVT::MvTimesMatAddMv(ONE,*cM_HP,*CP,ZERO,*R_);
1890  cM_HP = Teuchos::null;
1891  MVT::SetBlock(*R_,indblock3,*MV_);
1892  }
1893  else {
1894  cM_XHP = Teuchos::null;
1895  cM_HP = Teuchos::null;
1896  }
1897  }
1898  } // end timing block
1899  // done with coefficient matrices
1900  CX = Teuchos::null;
1901  CP = Teuchos::null;
1902 
1903  //
1904  // we now have a P direction
1905  hasP_ = true;
1906 
1907  // debugging check: all of our const views should have been cleared by now
1908  // if not, we have a logic error above
1909  TEUCHOS_TEST_FOR_EXCEPTION( cXHP != Teuchos::null || cK_XHP != Teuchos::null || cM_XHP != Teuchos::null
1910  || cHP != Teuchos::null || cK_HP != Teuchos::null || cM_HP != Teuchos::null
1911  || cP != Teuchos::null || cK_P != Teuchos::null || cM_P != Teuchos::null,
1912  std::logic_error,
1913  "Anasazi::BlockKrylovSchur::iterate(): const views were not all cleared! Something went wrong!" );
1914 
1915  //
1916  // recreate our const MV views of X,H,P and friends
1917  setupViews();
1918 
1919  //
1920  // Compute the new residuals, explicitly
1921  {
1922 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
1923  Teuchos::TimeMonitor lcltimer( *timerCompRes_ );
1924 #endif
1925  MVT::MvAddMv( ONE, *KX_, ZERO, *KX_, *R_ );
1926  Teuchos::SerialDenseMatrix<int,ScalarType> T( blockSize_, blockSize_ );
1927  for (int i = 0; i < blockSize_; i++) {
1928  T(i,i) = theta_[i];
1929  }
1930  MVT::MvTimesMatAddMv( -ONE, *MX_, T, ONE, *R_ );
1931  }
1932 
1933  // R has been updated; mark the norms as out-of-date
1934  Rnorms_current_ = false;
1935  R2norms_current_ = false;
1936 
1937  // When required, monitor some orthogonalities
1938  if (om_->isVerbosity( Debug ) ) {
1939  // Check almost everything here
1940  CheckList chk;
1941  chk.checkX = true;
1942  chk.checkKX = true;
1943  chk.checkMX = true;
1944  chk.checkP = true;
1945  chk.checkKP = true;
1946  chk.checkMP = true;
1947  chk.checkR = true;
1948  om_->print( Debug, accuracyCheck(chk, ": after local update") );
1949  }
1950  else if (om_->isVerbosity( OrthoDetails )) {
1951  CheckList chk;
1952  chk.checkX = true;
1953  chk.checkP = true;
1954  chk.checkR = true;
1955  om_->print( OrthoDetails, accuracyCheck(chk, ": after local update") );
1956  }
1957  } // end while (statusTest == false)
1958  }
1959 
1960 
1962  // compute/return residual M-norms
1963  template <class ScalarType, class MV, class OP>
1964  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1966  if (Rnorms_current_ == false) {
1967  // Update the residual norms
1968  orthman_->norm(*R_,Rnorms_);
1969  Rnorms_current_ = true;
1970  }
1971  return Rnorms_;
1972  }
1973 
1974 
1976  // compute/return residual 2-norms
1977  template <class ScalarType, class MV, class OP>
1978  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
1980  if (R2norms_current_ == false) {
1981  // Update the residual 2-norms
1982  MVT::MvNorm(*R_,R2norms_);
1983  R2norms_current_ = true;
1984  }
1985  return R2norms_;
1986  }
1987 
1988 
1989 
1990 
1992  // Check accuracy, orthogonality, and other debugging stuff
1993  //
1994  // bools specify which tests we want to run (instead of running more than we actually care about)
1995  //
1996  // we don't bother checking the following because they are computed explicitly:
1997  // H == Prec*R
1998  // KH == K*H
1999  //
2000  //
2001  // checkX : X orthonormal
2002  // orthogonal to auxvecs
2003  // checkMX: check MX == M*X
2004  // checkKX: check KX == K*X
2005  // checkP : if fullortho P orthonormal and orthogonal to X
2006  // orthogonal to auxvecs
2007  // checkMP: check MP == M*P
2008  // checkKP: check KP == K*P
2009  // checkH : if fullortho H orthonormal and orthogonal to X and P
2010  // orthogonal to auxvecs
2011  // checkMH: check MH == M*H
2012  // checkR : check R orthogonal to X
2013  // checkQ : check that auxiliary vectors are actually orthonormal
2014  //
2015  // TODO:
2016  // add checkTheta
2017  //
2018  template <class ScalarType, class MV, class OP>
2019  std::string LOBPCG<ScalarType,MV,OP>::accuracyCheck( const CheckList &chk, const std::string &where ) const
2020  {
2021  using std::endl;
2022 
2023  std::stringstream os;
2024  os.precision(2);
2025  os.setf(std::ios::scientific, std::ios::floatfield);
2026  MagnitudeType tmp;
2027 
2028  os << " Debugging checks: iteration " << iter_ << where << endl;
2029 
2030  // X and friends
2031  if (chk.checkX && initialized_) {
2032  tmp = orthman_->orthonormError(*X_);
2033  os << " >> Error in X^H M X == I : " << tmp << endl;
2034  for (Array_size_type i=0; i<auxVecs_.size(); i++) {
2035  tmp = orthman_->orthogError(*X_,*auxVecs_[i]);
2036  os << " >> Error in X^H M Q[" << i << "] == 0 : " << tmp << endl;
2037  }
2038  }
2039  if (chk.checkMX && hasM_ && initialized_) {
2040  tmp = Utils::errorEquality(*X_, *MX_, MOp_);
2041  os << " >> Error in MX == M*X : " << tmp << endl;
2042  }
2043  if (chk.checkKX && initialized_) {
2044  tmp = Utils::errorEquality(*X_, *KX_, Op_);
2045  os << " >> Error in KX == K*X : " << tmp << endl;
2046  }
2047 
2048  // P and friends
2049  if (chk.checkP && hasP_ && initialized_) {
2050  if (fullOrtho_) {
2051  tmp = orthman_->orthonormError(*P_);
2052  os << " >> Error in P^H M P == I : " << tmp << endl;
2053  tmp = orthman_->orthogError(*P_,*X_);
2054  os << " >> Error in P^H M X == 0 : " << tmp << endl;
2055  }
2056  for (Array_size_type i=0; i<auxVecs_.size(); i++) {
2057  tmp = orthman_->orthogError(*P_,*auxVecs_[i]);
2058  os << " >> Error in P^H M Q[" << i << "] == 0 : " << tmp << endl;
2059  }
2060  }
2061  if (chk.checkMP && hasM_ && hasP_ && initialized_) {
2062  tmp = Utils::errorEquality(*P_, *MP_, MOp_);
2063  os << " >> Error in MP == M*P : " << tmp << endl;
2064  }
2065  if (chk.checkKP && hasP_ && initialized_) {
2066  tmp = Utils::errorEquality(*P_, *KP_, Op_);
2067  os << " >> Error in KP == K*P : " << tmp << endl;
2068  }
2069 
2070  // H and friends
2071  if (chk.checkH && initialized_) {
2072  if (fullOrtho_) {
2073  tmp = orthman_->orthonormError(*H_);
2074  os << " >> Error in H^H M H == I : " << tmp << endl;
2075  tmp = orthman_->orthogError(*H_,*X_);
2076  os << " >> Error in H^H M X == 0 : " << tmp << endl;
2077  if (hasP_) {
2078  tmp = orthman_->orthogError(*H_,*P_);
2079  os << " >> Error in H^H M P == 0 : " << tmp << endl;
2080  }
2081  }
2082  for (Array_size_type i=0; i<auxVecs_.size(); i++) {
2083  tmp = orthman_->orthogError(*H_,*auxVecs_[i]);
2084  os << " >> Error in H^H M Q[" << i << "] == 0 : " << tmp << endl;
2085  }
2086  }
2087  if (chk.checkMH && hasM_ && initialized_) {
2088  tmp = Utils::errorEquality(*H_, *MH_, MOp_);
2089  os << " >> Error in MH == M*H : " << tmp << endl;
2090  }
2091 
2092  // R: this is not M-orthogonality, but standard euclidean orthogonality
2093  if (chk.checkR && initialized_) {
2094  Teuchos::SerialDenseMatrix<int,ScalarType> xTx(blockSize_,blockSize_);
2095  MVT::MvTransMv(ONE,*X_,*R_,xTx);
2096  tmp = xTx.normFrobenius();
2097  MVT::MvTransMv(ONE,*R_,*R_,xTx);
2098  double normR = xTx.normFrobenius();
2099  os << " >> RelError in X^H R == 0: " << tmp/normR << endl;
2100  }
2101 
2102  // Q
2103  if (chk.checkQ) {
2104  for (Array_size_type i=0; i<auxVecs_.size(); i++) {
2105  tmp = orthman_->orthonormError(*auxVecs_[i]);
2106  os << " >> Error in Q[" << i << "]^H M Q[" << i << "] == I : " << tmp << endl;
2107  for (Array_size_type j=i+1; j<auxVecs_.size(); j++) {
2108  tmp = orthman_->orthogError(*auxVecs_[i],*auxVecs_[j]);
2109  os << " >> Error in Q[" << i << "]^H M Q[" << j << "] == 0 : " << tmp << endl;
2110  }
2111  }
2112  }
2113 
2114  os << endl;
2115 
2116  return os.str();
2117  }
2118 
2119 
2121  // Print the current status of the solver
2122  template <class ScalarType, class MV, class OP>
2123  void
2125  {
2126  using std::endl;
2127 
2128  os.setf(std::ios::scientific, std::ios::floatfield);
2129  os.precision(6);
2130  os <<endl;
2131  os <<"================================================================================" << endl;
2132  os << endl;
2133  os <<" LOBPCG Solver Status" << endl;
2134  os << endl;
2135  os <<"The solver is "<<(initialized_ ? "initialized." : "not initialized.") << endl;
2136  os <<"The number of iterations performed is " << iter_ << endl;
2137  os <<"The current block size is " << blockSize_ << endl;
2138  os <<"The number of auxiliary vectors is " << numAuxVecs_ << endl;
2139  os <<"The number of operations Op*x is " << count_ApplyOp_ << endl;
2140  os <<"The number of operations M*x is " << count_ApplyM_ << endl;
2141  os <<"The number of operations Prec*x is " << count_ApplyPrec_ << endl;
2142 
2143  os.setf(std::ios_base::right, std::ios_base::adjustfield);
2144 
2145  if (initialized_) {
2146  os << endl;
2147  os <<"CURRENT EIGENVALUE ESTIMATES "<<endl;
2148  os << std::setw(20) << "Eigenvalue"
2149  << std::setw(20) << "Residual(M)"
2150  << std::setw(20) << "Residual(2)"
2151  << endl;
2152  os <<"--------------------------------------------------------------------------------"<<endl;
2153  for (int i=0; i<blockSize_; i++) {
2154  os << std::setw(20) << theta_[i];
2155  if (Rnorms_current_) os << std::setw(20) << Rnorms_[i];
2156  else os << std::setw(20) << "not current";
2157  if (R2norms_current_) os << std::setw(20) << R2norms_[i];
2158  else os << std::setw(20) << "not current";
2159  os << endl;
2160  }
2161  }
2162  os <<"================================================================================" << endl;
2163  os << endl;
2164  }
2165 
2167  // are we initialized or not?
2168  template <class ScalarType, class MV, class OP>
2170  return initialized_;
2171  }
2172 
2173 
2175  // is P valid or not?
2176  template <class ScalarType, class MV, class OP>
2178  return hasP_;
2179  }
2180 
2182  // is full orthogonalization enabled or not?
2183  template <class ScalarType, class MV, class OP>
2185  return(fullOrtho_);
2186  }
2187 
2188 
2190  // return the current auxilliary vectors
2191  template <class ScalarType, class MV, class OP>
2193  return auxVecs_;
2194  }
2195 
2197  // return the current block size
2198  template <class ScalarType, class MV, class OP>
2200  return(blockSize_);
2201  }
2202 
2204  // return the current eigenproblem
2205  template <class ScalarType, class MV, class OP>
2207  return(*problem_);
2208  }
2209 
2210 
2212  // return the max subspace dimension
2213  template <class ScalarType, class MV, class OP>
2215  return 3*blockSize_;
2216  }
2217 
2219  // return the current subspace dimension
2220  template <class ScalarType, class MV, class OP>
2222  if (!initialized_) return 0;
2223  return nevLocal_;
2224  }
2225 
2226 
2228  // return the current ritz residual norms
2229  template <class ScalarType, class MV, class OP>
2230  std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
2232  {
2233  return this->getRes2Norms();
2234  }
2235 
2236 
2238  // return the current compression indices
2239  template <class ScalarType, class MV, class OP>
2241  std::vector<int> ret(nevLocal_,0);
2242  return ret;
2243  }
2244 
2245 
2247  // return the current ritz values
2248  template <class ScalarType, class MV, class OP>
2249  std::vector<Value<ScalarType> > LOBPCG<ScalarType,MV,OP>::getRitzValues() {
2250  std::vector<Value<ScalarType> > ret(nevLocal_);
2251  for (int i=0; i<nevLocal_; i++) {
2252  ret[i].realpart = theta_[i];
2253  ret[i].imagpart = ZERO;
2254  }
2255  return ret;
2256  }
2257 
2259  // Set a new StatusTest for the solver.
2260  template <class ScalarType, class MV, class OP>
2262  TEUCHOS_TEST_FOR_EXCEPTION(test == Teuchos::null,std::invalid_argument,
2263  "Anasazi::LOBPCG::setStatusTest() was passed a null StatusTest.");
2264  tester_ = test;
2265  }
2266 
2268  // Get the current StatusTest used by the solver.
2269  template <class ScalarType, class MV, class OP>
2271  return tester_;
2272  }
2273 
2275  // return the current ritz vectors
2276  template <class ScalarType, class MV, class OP>
2278  return X_;
2279  }
2280 
2281 
2283  // reset the iteration counter
2284  template <class ScalarType, class MV, class OP>
2286  iter_=0;
2287  }
2288 
2289 
2291  // return the number of iterations
2292  template <class ScalarType, class MV, class OP>
2294  return(iter_);
2295  }
2296 
2297 
2299  // return the state
2300  template <class ScalarType, class MV, class OP>
2303  state.V = V_;
2304  state.KV = KV_;
2305  state.X = X_;
2306  state.KX = KX_;
2307  state.P = P_;
2308  state.KP = KP_;
2309  state.H = H_;
2310  state.KH = KH_;
2311  state.R = R_;
2312  state.T = Teuchos::rcp(new std::vector<MagnitudeType>(theta_));
2313  if (hasM_) {
2314  state.MV = MV_;
2315  state.MX = MX_;
2316  state.MP = MP_;
2317  state.MH = MH_;
2318  }
2319  else {
2320  state.MX = Teuchos::null;
2321  state.MP = Teuchos::null;
2322  state.MH = Teuchos::null;
2323  }
2324  return state;
2325  }
2326 
2327 } // end Anasazi namespace
2328 
2329 #endif // ANASAZI_LOBPCG_HPP
ScalarTraits< ScalarType >::magnitudeType normOne() const
ScalarType * values() const
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 .
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::Array< Teuchos::RCP< const MV > > getAuxVecs() const
Get the current auxiliary vectors.
LOBPCG(const Teuchos::RCP< Eigenproblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< SortManager< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > &sorter, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, const Teuchos::RCP< MatOrthoManager< ScalarType, MV, OP > > &ortho, Teuchos::ParameterList &params)
LOBPCG constructor with eigenproblem, solver utilities, and parameter list of solver options...
This class defines the interface required by an eigensolver and status test class to compute solution...
static ptrdiff_t GetGlobalLength(const MV &mv)
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this eigenproblem.
void setStatusTest(Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test)
Set a new StatusTest for the solver.
Declaration of basic traits for the multivector type.
T & get(const std::string &name, T def_value)
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
static ScalarType squareroot(ScalarType x)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
This class provides the Locally Optimal Block Preconditioned Conjugate Gradient (LOBPCG) iteration...
Teuchos::RCP< const MultiVector > V
The current test basis.
virtual ~LOBPCG()
LOBPCG destructor
Virtual base class which defines basic traits for the operator type.
Teuchos::RCP< const MultiVector > H
The current preconditioned residual vectors.
Teuchos::RCP< const MultiVector > P
The current search direction.
static Teuchos::ScalarTraits< ScalarType >::magnitudeType errorEquality(const MV &X, const MV &MX, Teuchos::RCP< const OP > M=Teuchos::null)
Return the maximum coefficient of the matrix scaled by the maximum coefficient of MX...
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...
An exception class parent to all Anasazi exceptions.
Teuchos::RCP< const MultiVector > MV
The image of the current test basis under M, or Teuchos::null if M was not specified.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Pure virtual base class which describes the basic interface to the iterative eigensolver.
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
Teuchos::RCP< const MultiVector > R
The current residual vectors.
void initialize()
Initialize the solver with the initial vectors from the eigenproblem or random data.
Teuchos::RCP< const std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > > T
The current Ritz values.
bool isInitialized() const
Indicates whether the solver has been initialized or not.
Anasazi&#39;s templated, static class providing utilities for the solvers.
const Eigenproblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the eigenvalue problem.
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 .
void POTRF(const char UPLO, const OrdinalType n, ScalarType *A, const OrdinalType lda, OrdinalType *info) const
Anasazi&#39;s templated virtual class for providing routines for orthogonalization and orthonormalization...
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this eigenproblem.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Output managers remove the need for the eigensolver to know any information about the required output...
static int directSolver(int size, const Teuchos::SerialDenseMatrix< int, ScalarType > &KK, Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > MM, Teuchos::SerialDenseMatrix< int, ScalarType > &EV, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &theta, int &nev, int esType=0)
Routine for computing the first NEV generalized eigenpairs of the Hermitian pencil (KK...
Traits class which defines basic operations on multivectors.
int getNumIters() const
Get the current iteration count.
Virtual base class which defines basic traits for the operator type.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRes2Norms()
Get the current residual 2-norms.
Teuchos::RCP< const MultiVector > MX
The image of the current eigenvectors under M, or Teuchos::null if M was not specified.
Teuchos::RCP< const MultiVector > KX
The image of the current eigenvectors under K.
bool getFullOrtho() const
Determine if the LOBPCG iteration is using full orthogonality.
Teuchos::RCP< const MultiVector > KH
The image of the current preconditioned residual vectors under K.
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).
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getResNorms()
Get the current residual norms.
std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > getRitzRes2Norms()
Get the 2-norms of the residuals.
std::vector< int > getRitzIndex()
Get the index used for extracting Ritz vectors from getRitzVectors().
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
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).
iterator end()
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...
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace. For LOBPCG, this always returns 3*getBlo...
Teuchos::RCP< const MultiVector > MH
The image of the current preconditioned residual vectors under M, or Teuchos::null if M was not speci...
Teuchos::RCP< const MultiVector > KP
The image of the current search direction under K.
static magnitudeType magnitude(ScalarType a)
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
Teuchos::RCP< StatusTest< ScalarType, MV, OP > > getStatusTest() const
Get the current StatusTest used by the solver.
void push_back(const value_type &x)
void currentStatus(std::ostream &os)
This method requests that the solver print out its current status to screen.
void iterate()
This method performs LOBPCG iterations until the status test indicates the need to stop or an error o...
static void permuteVectors(const int n, const std::vector< int > &perm, MV &Q, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > *resids=0)
Permute the vectors in a multivector according to the permutation vector perm, and optionally the res...
LOBPCGRitzFailure is thrown when the LOBPCG solver is unable to continue a call to LOBPCG::iterate() ...
std::vector< Value< ScalarType > > getRitzValues()
Get the Ritz values from the previous iteration.
Teuchos::RCP< const MultiVector > KV
The image of the current test basis under K.
size_type size() const
void setAuxVecs(const Teuchos::Array< Teuchos::RCP< const MV > > &auxvecs)
Set the auxiliary vectors for the solver.
Types and exceptions used within Anasazi solvers and interfaces.
void resetNumIters()
Reset the iteration count.
void TRSM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const OrdinalType m, const OrdinalType n, const alpha_type alpha, const A_type *A, const OrdinalType lda, ScalarType *B, const OrdinalType ldb) const
Teuchos::RCP< const MultiVector > MP
The image of the current search direction under M, or Teuchos::null if M was not specified.
Anasazi&#39;s templated pure virtual class for managing the sorting of approximate eigenvalues computed b...
A pure virtual class for defining the status tests for the Anasazi iterative solvers.
iterator begin()
LOBPCGOrthoFailure is thrown when an orthogonalization attempt fails.
LOBPCGInitFailure is thrown when the LOBPCG solver is unable to generate an initial iterate in the LO...
LOBPCGState< ScalarType, MV > getState() const
Get the current state of the eigensolver.
The Eigensolver is a templated virtual base class that defines the basic interface that any eigensolv...
void setFullOrtho(bool fullOrtho)
Instruct the LOBPCG iteration to use full orthogonality.
Structure to contain pointers to Anasazi state variables.
Teuchos::RCP< const MultiVector > X
The current eigenvectors.
OrdinalType stride() const
Teuchos::RCP< const MV > getRitzVectors()
Get the Ritz vectors from the previous iteration.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
bool hasP()
Indicates whether the search direction given by getState() is valid.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current eigenvectors and eigenvalues...
Class which provides internal utilities for the Anasazi solvers.