Belos  Version of the Day
BelosPCPGIter.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef BELOS_PCPG_ITER_HPP
43 #define BELOS_PCPG_ITER_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 
52 #include "BelosLinearProblem.hpp"
53 #include "BelosMatOrthoManager.hpp"
54 #include "BelosOutputManager.hpp"
55 #include "BelosStatusTest.hpp"
56 #include "BelosOperatorTraits.hpp"
57 #include "BelosMultiVecTraits.hpp"
58 
59 #include "Teuchos_BLAS.hpp"
60 #include "Teuchos_LAPACK.hpp"
61 #include "Teuchos_SerialDenseMatrix.hpp"
62 #include "Teuchos_SerialDenseVector.hpp"
63 #include "Teuchos_ScalarTraits.hpp"
64 #include "Teuchos_ParameterList.hpp"
65 #include "Teuchos_TimeMonitor.hpp"
66 
78 namespace Belos {
79 
81 
82 
87  template <class ScalarType, class MV>
88  struct PCPGIterState {
94  int curDim;
96  int prevUdim;
97 
99  Teuchos::RCP<MV> R;
100 
102  Teuchos::RCP<MV> Z;
103 
105  Teuchos::RCP<MV> P;
106 
108  Teuchos::RCP<MV> AP;
109 
111  Teuchos::RCP<MV> U;
112 
114  Teuchos::RCP<MV> C;
115 
120  Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType> > D;
121 
122  PCPGIterState() : curDim(0),
123  prevUdim(0),
124  R(Teuchos::null), Z(Teuchos::null),
125  P(Teuchos::null), AP(Teuchos::null),
126  U(Teuchos::null), C(Teuchos::null),
127  D(Teuchos::null)
128  {}
129  };
130 
132 
134 
135 
147  class PCPGIterInitFailure : public BelosError {public:
148  PCPGIterInitFailure(const std::string& what_arg) : BelosError(what_arg)
149  {}};
150 
155  class PCPGIterateFailure : public BelosError {public:
156  PCPGIterateFailure(const std::string& what_arg) : BelosError(what_arg)
157  {}};
158 
159 
160 
167  class PCPGIterOrthoFailure : public BelosError {public:
168  PCPGIterOrthoFailure(const std::string& what_arg) : BelosError(what_arg)
169  {}};
170 
177  class PCPGIterLAPACKFailure : public BelosError {public:
178  PCPGIterLAPACKFailure(const std::string& what_arg) : BelosError(what_arg)
179  {}};
180 
182 
183 
184  template<class ScalarType, class MV, class OP>
185  class PCPGIter : virtual public Iteration<ScalarType,MV,OP> {
186 
187  public:
188 
189  //
190  // Convenience typedefs
191  //
195  typedef Teuchos::ScalarTraits<ScalarType> SCT;
196  typedef typename SCT::magnitudeType MagnitudeType;
197 
199 
200 
208  PCPGIter( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
209  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
210  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
211  const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
212  Teuchos::ParameterList &params );
213 
215  virtual ~PCPGIter() {};
217 
218 
220 
221 
240  void iterate();
241 
260  void initialize(PCPGIterState<ScalarType,MV> newstate);
261 
265  void initialize()
266  {
268  initialize(empty);
269  }
270 
280  state.Z = Z_; // CG state
281  state.P = P_;
282  state.AP = AP_;
283  state.R = R_;
284  state.U = U_; // seed state
285  state.C = C_;
286  state.D = D_;
287  state.curDim = curDim_;
288  state.prevUdim = prevUdim_;
289  return state;
290  }
291 
293 
294 
296 
297 
299  int getNumIters() const { return iter_; }
300 
302  void resetNumIters( int iter = 0 ) { iter_ = iter; }
303 
306  Teuchos::RCP<const MV> getNativeResiduals( std::vector<MagnitudeType> *norms ) const { return R_; }
307 
309 
312  Teuchos::RCP<MV> getCurrentUpdate() const { return Teuchos::null; }
313 
315  int getCurSubspaceDim() const {
316  if (!initialized_) return 0;
317  return curDim_;
318  };
319 
321  int getPrevSubspaceDim() const {
322  if (!initialized_) return 0;
323  return prevUdim_;
324  };
325 
327 
328 
330 
331 
333  const LinearProblem<ScalarType,MV,OP>& getProblem() const { return *lp_; }
334 
336  int getBlockSize() const { return 1; }
337 
339  int getNumRecycledBlocks() const { return savedBlocks_; }
340 
342 
344  void setBlockSize(int blockSize) {
345  TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
346  "Belos::PCPGIter::setBlockSize(): Cannot use a block size that is not one.");
347  }
348 
350  void setSize( int savedBlocks );
351 
353  bool isInitialized() { return initialized_; }
354 
356  void resetState();
357 
359 
360  private:
361 
362  //
363  // Internal methods
364  //
366  void setStateSize();
367 
368  //
369  // Classes inputed through constructor that define the linear problem to be solved.
370  //
371  const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
372  const Teuchos::RCP<OutputManager<ScalarType> > om_;
373  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
374  const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
375 
376  //
377  // Algorithmic parameters
378  // savedBlocks_ is the number of blocks allocated for the reused subspace
379  int savedBlocks_;
380  //
381  //
382  // Current solver state
383  //
384  // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
385  // is capable of running; _initialize is controlled by the initialize() member method
386  // For the implications of the state of initialized_, please see documentation for initialize()
387  bool initialized_;
388 
389  // stateStorageInitialized_ indicates that the state storage has be initialized to the current
390  // savedBlocks_. State storage initialization may be postponed if the linear problem was
391  // generated without either the right-hand side or solution vectors.
392  bool stateStorageInitialized_;
393 
394  // keepDiagonal_ specifies that the iteration must keep the diagonal matrix of pivots
395  bool keepDiagonal_;
396 
397  // initDiagonal_ specifies that the iteration will reinitialize the diagonal matrix by zeroing
398  // out all entries before an iteration is started.
399  bool initDiagonal_;
400 
401  // Current subspace dimension
402  int curDim_;
403 
404  // Dimension of seed space used to solve current linear system
405  int prevUdim_;
406 
407  // Number of iterations performed
408  int iter_;
409  //
410  // State Storage ... of course this part is different for CG
411  //
412  // Residual
413  Teuchos::RCP<MV> R_;
414  //
415  // Preconditioned residual
416  Teuchos::RCP<MV> Z_;
417  //
418  // Direction std::vector
419  Teuchos::RCP<MV> P_;
420  //
421  // Operator applied to direction std::vector
422  Teuchos::RCP<MV> AP_;
423  //
424  // Recycled subspace vectors.
425  Teuchos::RCP<MV> U_;
426  //
427  // C = A * U, linear system is Ax=b
428  Teuchos::RCP<MV> C_;
429  //
430  // Projected matrices
431  // D_ : Diagonal matrix of pivots D = P'AP
432  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > D_;
433  };
434 
436  // Constructor.
437  template<class ScalarType, class MV, class OP>
439  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
440  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
441  const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
442  Teuchos::ParameterList &params ):
443  lp_(problem),
444  om_(printer),
445  stest_(tester),
446  ortho_(ortho),
447  savedBlocks_(0),
448  initialized_(false),
449  stateStorageInitialized_(false),
450  keepDiagonal_(false),
451  initDiagonal_(false),
452  curDim_(0),
453  prevUdim_(0),
454  iter_(0)
455  {
456  // Get the maximum number of blocks allowed for this Krylov subspace
457 
458  TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter("Saved Blocks"), std::invalid_argument,
459  "Belos::PCPGIter::constructor: mandatory parameter \"Saved Blocks\" is not specified.");
460  int rb = Teuchos::getParameter<int>(params, "Saved Blocks");
461 
462  // Find out whether we are saving the Diagonal matrix.
463  keepDiagonal_ = params.get("Keep Diagonal", false);
464 
465  // Find out whether we are initializing the Diagonal matrix.
466  initDiagonal_ = params.get("Initialize Diagonal", false);
467 
468  // Set the number of blocks and allocate data
469  setSize( rb );
470  }
471 
473  // Set the block size and adjust as necessary
474  template <class ScalarType, class MV, class OP>
475  void PCPGIter<ScalarType,MV,OP>::setSize( int savedBlocks )
476  {
477  // allocate space only; perform no computation
478  // Any change in size invalidates the state of the solver as implemented here.
479 
480  TEUCHOS_TEST_FOR_EXCEPTION(savedBlocks <= 0, std::invalid_argument, "Belos::PCPGIter::setSize() was passed a non-positive argument for \"Num Saved Blocks\".");
481 
482  if ( savedBlocks_ != savedBlocks) {
483  stateStorageInitialized_ = false;
484  savedBlocks_ = savedBlocks;
485  initialized_ = false;
486  curDim_ = 0;
487  prevUdim_ = 0;
488  setStateSize(); // Use the current savedBlocks_ to initialize the state storage.
489  }
490  }
491 
493  // Enable the reuse of a single solver object for completely different linear systems
494  template <class ScalarType, class MV, class OP>
496  {
497  stateStorageInitialized_ = false;
498  initialized_ = false;
499  curDim_ = 0;
500  prevUdim_ = 0;
501  setStateSize();
502  }
503 
505  // Setup the state storage. Called by either initialize or, if savedBlocks_ changes, setSize.
506  template <class ScalarType, class MV, class OP>
508  {
509  if (!stateStorageInitialized_) {
510 
511  // Check if there is any multivector to clone from.
512  Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
513  Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
514  if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
515  return; // postpone exception
516  }
517  else {
518 
520  // blockSize*recycledBlocks dependent
521  int newsd = savedBlocks_ ; //int newsd = blockSize_* savedBlocks_ ;
522  //
523  // Initialize the CG state storage
524  // If the subspace is not initialized, generate it using the LHS or RHS from lp_.
525  // Generate CG state only if it does not exist, otherwise resize it.
526  if (Z_ == Teuchos::null) {
527  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
528  Z_ = MVT::Clone( *tmp, 1 );
529  }
530  if (P_ == Teuchos::null) {
531  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
532  P_ = MVT::Clone( *tmp, 1 );
533  }
534  if (AP_ == Teuchos::null) {
535  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
536  AP_ = MVT::Clone( *tmp, 1 );
537  }
538 
539  if (C_ == Teuchos::null) {
540 
541  // Get the multivector that is not null.
542  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
543  TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
544  "Belos::PCPGIter::setStateSize(): linear problem does not specify multivectors to clone from.");
545  TEUCHOS_TEST_FOR_EXCEPTION( 0 != prevUdim_,std::invalid_argument,
546  "Belos::PCPGIter::setStateSize(): prevUdim not zero and C is null.");
547  C_ = MVT::Clone( *tmp, savedBlocks_ );
548  }
549  else {
550  // Generate C_ by cloning itself ONLY if more space is needed.
551  if (MVT::GetNumberVecs(*C_) < savedBlocks_ ) {
552  Teuchos::RCP<const MV> tmp = C_;
553  C_ = MVT::Clone( *tmp, savedBlocks_ );
554  }
555  }
556  if (U_ == Teuchos::null) {
557  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
558  TEUCHOS_TEST_FOR_EXCEPTION( 0 != prevUdim_,std::invalid_argument,
559  "Belos::PCPGIter::setStateSize(): prevUdim not zero and U is null.");
560  U_ = MVT::Clone( *tmp, savedBlocks_ );
561  }
562  else {
563  // Generate U_ by cloning itself ONLY if more space is needed.
564  if (MVT::GetNumberVecs(*U_) < savedBlocks_ ) {
565  Teuchos::RCP<const MV> tmp = U_;
566  U_ = MVT::Clone( *tmp, savedBlocks_ );
567  }
568  }
569  if (keepDiagonal_) {
570  if (D_ == Teuchos::null) {
571  D_ = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>() );
572  }
573  if (initDiagonal_) {
574  D_->shape( newsd, newsd );
575  }
576  else {
577  if (D_->numRows() < newsd || D_->numCols() < newsd) {
578  D_->shapeUninitialized( newsd, newsd );
579  }
580  }
581  }
582  // State storage has now been initialized.
583  stateStorageInitialized_ = true;
584  } // if there is a vector to clone from
585  } // if !stateStorageInitialized_
586  } // end of setStateSize
587 
589  // Initialize the iteration object
590  template <class ScalarType, class MV, class OP>
592  {
593 
594  TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
595  "Belos::PCPGIter::initialize(): Cannot initialize state storage!");
596 
597  // Requirements: R_ and consistent multivectors widths and lengths
598  //
599  std::string errstr("Belos::PCPGIter::initialize(): Specified multivectors must have a consistent length and width.");
600 
601  if (newstate.R != Teuchos::null){
602 
603  R_ = newstate.R; // SolverManager::R_ == newstate.R == Iterator::R_
604  if (newstate.U == Teuchos::null){
605  prevUdim_ = 0;
606  newstate.U = U_;
607  newstate.C = C_;
608  }
609  else {
610  prevUdim_ = newstate.curDim;
611  if (newstate.C == Teuchos::null){ // Stub for new feature
612  std::vector<int> index(prevUdim_);
613  for (int i=0; i< prevUdim_; ++i)
614  index[i] = i;
615  Teuchos::RCP<const MV> Ukeff = MVT::CloneView( *newstate.U, index );
616  newstate.C = MVT::Clone( *newstate.U, prevUdim_ );
617  Teuchos::RCP<MV> Ckeff = MVT::CloneViewNonConst( *newstate.C, index );
618  lp_->apply( *Ukeff, *Ckeff );
619  }
620  curDim_ = prevUdim_ ;
621  }
622 
623  // Initialize the state storage if not already allocated in the constructor
624  if (!stateStorageInitialized_)
625  setStateSize();
626 
627  //TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.V) != MVText::GetGlobalLength(*V_), std::invalid_argument, errstr );
628  //TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.V) < 1, std::invalid_argument, errstr );
629 
630  newstate.prevUdim = prevUdim_; // big change in functionality from GCRODR
631  newstate.curDim = curDim_;
632 
633  //TEUCHOS_TEST_FOR_EXCEPTION(newstate.z->numRows() < curDim_ || newstate.z->numCols() < 1, std::invalid_argument, errstr);
634 
635  std::vector<int> zero_index(1);
636  zero_index[0] = 0;
637  if ( lp_->getLeftPrec() != Teuchos::null ) { // Compute the initial search direction
638  lp_->applyLeftPrec( *R_, *Z_ );
639  MVT::SetBlock( *Z_, zero_index , *P_ ); // P(:,zero_index) := Z
640  } else {
641  Z_ = R_;
642  MVT::SetBlock( *R_, zero_index, *P_ );
643  }
644 
645  std::vector<int> nextind(1);
646  nextind[0] = curDim_;
647 
648  MVT::SetBlock( *P_, nextind, *newstate.U ); // U(:,curDim_ ) := P_
649 
650  ++curDim_;
651  newstate.curDim = curDim_;
652 
653  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.U) != savedBlocks_ ,
654  std::invalid_argument, errstr );
655  if (newstate.U != U_) { // Why this is needed?
656  U_ = newstate.U;
657  }
658 
659  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.C) != savedBlocks_ ,
660  std::invalid_argument, errstr );
661  if (newstate.C != C_) {
662  C_ = newstate.C;
663  }
664  }
665  else {
666 
667  TEUCHOS_TEST_FOR_EXCEPTION(newstate.R == Teuchos::null,std::invalid_argument,
668  "Belos::PCPGIter::initialize(): PCPGStateIterState does not have initial kernel R_0.");
669  }
670 
671  // the solver is initialized
672  initialized_ = true;
673  }
674 
675 
677  // Iterate until the status test informs us we should stop.
678  template <class ScalarType, class MV, class OP>
680  {
681  //
682  // Allocate/initialize data structures
683  //
684  if (initialized_ == false) {
685  initialize();
686  }
687  bool debug = false;
688 
689  // Allocate memory for scalars.
690  Teuchos::SerialDenseMatrix<int,ScalarType> alpha( 1, 1 );
691  Teuchos::SerialDenseMatrix<int,ScalarType> pAp( 1, 1 );
692  Teuchos::SerialDenseMatrix<int,ScalarType> beta( 1, 1 );
693  Teuchos::SerialDenseMatrix<int,ScalarType> rHz( 1, 1 ), rHz_old( 1, 1 );
694 
695  if( iter_ != 0 )
696  std::cout << " Iterate Warning: begin from nonzero iter_ ?" << std::endl; //DMD
697 
698  // GenOrtho Project Stubs
699  std::vector<int> prevInd;
700  Teuchos::RCP<const MV> Uprev;
701  Teuchos::RCP<const MV> Cprev;
702  Teuchos::SerialDenseMatrix<int,ScalarType> CZ;
703 
704  if( prevUdim_ ){
705  prevInd.resize( prevUdim_ );
706  for( int i=0; i<prevUdim_ ; i++) prevInd[i] = i;
707  CZ.reshape( prevUdim_ , 1 );
708  Uprev = MVT::CloneView(*U_, prevInd);
709  Cprev = MVT::CloneView(*C_, prevInd);
710  }
711 
712  // Get the current solution std::vector.
713  Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
714 
715  // Check that the current solution std::vector only has one column.
716  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*cur_soln_vec) != 1, PCPGIterInitFailure,
717  "Belos::CGIter::iterate(): current linear system has more than one std::vector!" );
718 
719  //Check that the input is correctly set up
720  TEUCHOS_TEST_FOR_EXCEPTION( curDim_ != prevUdim_ + 1, PCPGIterInitFailure,
721  "Belos::CGIter::iterate(): mistake in initialization !" );
722 
723 
724  const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
725  const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
726 
727 
728  std::vector<int> curind(1);
729  std::vector<ScalarType> rnorm(MVT::GetNumberVecs(*cur_soln_vec));
730  if (prevUdim_ > 0){ // A-orthonalize P=Z to Uprev
731  Teuchos::RCP<MV> P;
732  curind[0] = curDim_ - 1; // column = dimension - 1
733  P = MVT::CloneViewNonConst(*U_,curind);
734  MVT::MvTransMv( one, *Cprev, *P, CZ );
735  MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *P ); // P -= U*(C'Z)
736 
737  if( debug ){
738  MVT::MvTransMv( one, *Cprev, *P, CZ );
739  std::cout << " Input CZ post ortho " << std::endl;
740  CZ.print( std::cout );
741  }
742  if( curDim_ == savedBlocks_ ){
743  std::vector<int> zero_index(1);
744  zero_index[0] = 0;
745  MVT::SetBlock( *P, zero_index, *P_ );
746  }
747  P = Teuchos::null;
748  }
749 
750  // Compute first <r,z> a.k.a. rHz
751  MVT::MvTransMv( one, *R_, *Z_, rHz );
752 
754  // iterate until the status test is satisfied
755  //
756  while (stest_->checkStatus(this) != Passed ) {
757  Teuchos::RCP<const MV> P;
758  Teuchos::RCP<MV> AP;
759  iter_++; // The next iteration begins.
760  //std::vector<int> curind(1);
761  curind[0] = curDim_ - 1; // column = dimension - 1
762  if( debug ){
763  MVT::MvNorm(*R_, rnorm);
764  std::cout << iter_ << " " << curDim_ << " " << rnorm[0] << std::endl;
765  }
766  if( prevUdim_ + iter_ < savedBlocks_ ){
767  P = MVT::CloneView(*U_,curind);
768  AP = MVT::CloneViewNonConst(*C_,curind);
769  lp_->applyOp( *P, *AP );
770  MVT::MvTransMv( one, *P, *AP, pAp );
771  }else{
772  if( prevUdim_ + iter_ == savedBlocks_ ){
773  AP = MVT::CloneViewNonConst(*C_,curind);
774  lp_->applyOp( *P_, *AP );
775  MVT::MvTransMv( one, *P_, *AP, pAp );
776  }else{
777  lp_->applyOp( *P_, *AP_ );
778  MVT::MvTransMv( one, *P_, *AP_, pAp );
779  }
780  }
781 
782  if( keepDiagonal_ && prevUdim_ + iter_ <= savedBlocks_ )
783  (*D_)(iter_ -1 ,iter_ -1 ) = pAp(0,0);
784 
785  // positive pAp required
786  TEUCHOS_TEST_FOR_EXCEPTION( pAp(0,0) <= zero, PCPGIterateFailure,
787  "Belos::CGIter::iterate(): non-positive value for p^H*A*p encountered!" );
788 
789  // alpha := <R_,Z_> / <P,AP>
790  alpha(0,0) = rHz(0,0) / pAp(0,0);
791 
792  // positive alpha required
793  TEUCHOS_TEST_FOR_EXCEPTION( alpha(0,0) <= zero, PCPGIterateFailure,
794  "Belos::CGIter::iterate(): non-positive value for alpha encountered!" );
795 
796  // solution update x += alpha * P
797  if( curDim_ < savedBlocks_ ){
798  MVT::MvAddMv( one, *cur_soln_vec, alpha(0,0), *P, *cur_soln_vec );
799  }else{
800  MVT::MvAddMv( one, *cur_soln_vec, alpha(0,0), *P_, *cur_soln_vec );
801  }
802  //lp_->updateSolution(); ... does nothing.
803  //
804  // The denominator of beta is saved before residual is updated [ old <R_, Z_> ].
805  //
806  rHz_old(0,0) = rHz(0,0);
807  //
808  // residual update R_ := R_ - alpha * AP
809  //
810  if( prevUdim_ + iter_ <= savedBlocks_ ){
811  MVT::MvAddMv( one, *R_, -alpha(0,0), *AP, *R_ );
812  AP = Teuchos::null;
813  }else{
814  MVT::MvAddMv( one, *R_, -alpha(0,0), *AP_, *R_ );
815  }
816  //
817  // update beta := [ new <R_, Z_> ] / [ old <R_, Z_> ] and the search direction p.
818  //
819  if ( lp_->getLeftPrec() != Teuchos::null ) {
820  lp_->applyLeftPrec( *R_, *Z_ );
821  } else {
822  Z_ = R_;
823  }
824  //
825  MVT::MvTransMv( one, *R_, *Z_, rHz );
826  //
827  beta(0,0) = rHz(0,0) / rHz_old(0,0);
828  //
829  if( curDim_ < savedBlocks_ ){
830  curDim_++; // update basis dim
831  curind[0] = curDim_ - 1;
832  Teuchos::RCP<MV> Pnext = MVT::CloneViewNonConst(*U_,curind);
833  MVT::MvAddMv( one, *Z_, beta(0,0), *P, *Pnext );
834  if( prevUdim_ ){ // Deflate seed space
835  MVT::MvTransMv( one, *Cprev, *Z_, CZ );
836  MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *Pnext ); // Pnext -= U*(C'Z)
837  if( debug ){
838  std::cout << " Check CZ " << std::endl;
839  MVT::MvTransMv( one, *Cprev, *Pnext, CZ );
840  CZ.print( std::cout );
841  }
842  }
843  P = Teuchos::null;
844  if( curDim_ == savedBlocks_ ){
845  std::vector<int> zero_index(1);
846  zero_index[0] = 0;
847  MVT::SetBlock( *Pnext, zero_index, *P_ );
848  }
849  Pnext = Teuchos::null;
850  }else{
851  MVT::MvAddMv( one, *Z_, beta(0,0), *P_, *P_ );
852  if( prevUdim_ ){ // Deflate seed space
853  MVT::MvTransMv( one, *Cprev, *Z_, CZ );
854  MVT::MvTimesMatAddMv( -one, *Uprev, CZ, one, *P_ ); // P_ -= U*(C'Z)
855 
856  if( debug ){
857  std::cout << " Check CZ " << std::endl;
858  MVT::MvTransMv( one, *Cprev, *P_, CZ );
859  CZ.print( std::cout );
860  }
861  }
862  }
863  // CGB: 5/26/2010
864  // this RCP<const MV> P was previously a variable outside the loop. however, it didn't appear to be see any use between
865  // loop iterations. therefore, I moved it inside to avoid scoping errors with previously used variables named P.
866  // to ensure that this wasn't a bug, I verify below that we have set P == null, i.e., that we are not going to use it again
867  // same for AP
868  TEUCHOS_TEST_FOR_EXCEPTION( AP != Teuchos::null || P != Teuchos::null, std::logic_error, "Loop recurrence violated. Please contact Belos team.");
869  } // end coupled two-term recursion
870  if( prevUdim_ + iter_ < savedBlocks_ ) --curDim_; // discard negligible search direction
871  }
872 
873 } // end Belos namespace
874 
875 #endif /* BELOS_PCPG_ITER_HPP */
PCPGIterateFailure is thrown when the PCPGIter object breaks down.
Collection of types and exceptions used within the Belos solvers.
int prevUdim
Number of block columns in matrices C and U before current iteration.
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
bool isInitialized()
States whether the solver has been initialized or not.
Class which manages the output and verbosity of the Belos solvers.
Teuchos::RCP< MV > R
The current residual.
PCPGIterState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Pure virtual base class for defining the status testing capabilities of Belos.
Teuchos::RCP< MV > C
C = AU, U spans recycled subspace.
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).
int getCurSubspaceDim() const
Get the current dimension of the whole seed subspace.
Declaration of basic traits for the multivector type.
void setSize(int savedBlocks)
Set the maximum number of saved or recycled blocks used by the iterative solver.
int curDim
The current dimension of the reduction.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::RCP< MV > P
The current decent direction std::vector.
Teuchos::RCP< MV > AP
The matrix A applied to current decent direction std::vector.
An extension of the MultiVecTraits class that adds a new vector length method.
Structure to contain pointers to PCPGIter state variables.
OperatorTraits< ScalarType, MV, OP > OPT
int getPrevSubspaceDim() const
Get the dimension of the search subspace used to solve the current solution to the linear 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 .
A pure virtual class for defining the status tests for the Belos iterative solvers.
int getBlockSize() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
Class which defines basic traits for the operator type.
PCPGIterLAPACKFailure is thrown when a nonzero return value is passed back from an LAPACK routine...
PCPGIterLAPACKFailure(const std::string &what_arg)
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 .
MultiVecTraitsExt< ScalarType, MV > MVText
Traits class which defines basic operations on multivectors.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > D
The current Hessenberg matrix.
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...
virtual ~PCPGIter()
Destructor.
void setBlockSize(int blockSize)
Get the blocksize to be used by the iterative solver in solving this linear problem.
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).
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
int getNumIters() const
Get the current iteration count.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
void initialize()
Initialize the solver with the initial vectors from the linear problem. An exception is thrown if ini...
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
PCPGIterOrthoFailure(const std::string &what_arg)
int getNumRecycledBlocks() const
Get the maximum number of recycled blocks used by the iterative solver in solving this linear problem...
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
SCT::magnitudeType MagnitudeType
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
Teuchos::RCP< MV > U
The recycled subspace.
void resetState()
tell the Iterator to "reset" itself; delete and rebuild the seed space.
PCPGIterOrthoFailure is thrown when the PCPGIter object is unable to compute independent direction ve...
void iterate()
PCPGIter iterates CG until the status test either requests a stop or detects an error. In the latter case, std::exception is thrown.
PCPGIterateFailure(const std::string &what_arg)
Teuchos::RCP< MV > Z
The current preconditioned residual.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
Class which defines basic traits for the operator type.
PCPGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, 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)
PCPGIter constructor with linear problem, solver utilities, and parameter list of solver options...
Parent class to all Belos exceptions.
Definition: BelosTypes.hpp:60
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
This class implements the PCPG iteration, where a single-std::vector Krylov subspace is constructed...
Belos header file which uses auto-configuration information to include necessary C++ headers...
MultiVecTraits< ScalarType, MV > MVT
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system solution?.
PCPGIterInitFailure is thrown when the PCPGIter object is unable to generate an initial iterate in th...
PCPGIterInitFailure(const std::string &what_arg)
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::ScalarTraits< ScalarType > SCT