Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosBlockGmresIter.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_BLOCK_GMRES_ITER_HPP
43 #define BELOS_BLOCK_GMRES_ITER_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 #include "BelosGmresIteration.hpp"
52 
53 #include "BelosLinearProblem.hpp"
54 #include "BelosMatOrthoManager.hpp"
55 #include "BelosOutputManager.hpp"
56 #include "BelosStatusTest.hpp"
57 #include "BelosOperatorTraits.hpp"
58 #include "BelosMultiVecTraits.hpp"
59 
60 #include "Teuchos_BLAS.hpp"
63 #include "Teuchos_ScalarTraits.hpp"
65 #include "Teuchos_TimeMonitor.hpp"
66 
80 namespace Belos {
81 
82 template<class ScalarType, class MV, class OP>
83 class BlockGmresIter : virtual public GmresIteration<ScalarType,MV,OP> {
84 
85  public:
86 
87  //
88  // Convenience typedefs
89  //
95 
97 
98 
109  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
112  Teuchos::ParameterList &params );
113 
115  virtual ~BlockGmresIter() {};
117 
118 
120 
121 
143  void iterate();
144 
167 
171  void initialize()
172  {
174  initializeGmres(empty);
175  }
176 
186  state.curDim = curDim_;
187  state.V = V_;
188  state.H = H_;
189  state.R = R_;
190  state.z = z_;
191  return state;
192  }
193 
195 
196 
198 
199 
201  int getNumIters() const { return iter_; }
202 
204  void resetNumIters( int iter = 0 ) { iter_ = iter; }
205 
208  Teuchos::RCP<const MV> getNativeResiduals( std::vector<MagnitudeType> *norms ) const;
209 
211 
217 
219 
222  void updateLSQR( int dim = -1 );
223 
225  int getCurSubspaceDim() const {
226  if (!initialized_) return 0;
227  return curDim_;
228  };
229 
231  int getMaxSubspaceDim() const { return blockSize_*numBlocks_; }
232 
234 
235 
237 
238 
240  const LinearProblem<ScalarType,MV,OP>& getProblem() const { return *lp_; }
241 
243  int getBlockSize() const { return blockSize_; }
244 
246  void setBlockSize(int blockSize) { setSize( blockSize, numBlocks_ ); }
247 
249  int getNumBlocks() const { return numBlocks_; }
250 
252  void setNumBlocks(int numBlocks) { setSize( blockSize_, numBlocks ); }
253 
260  void setSize(int blockSize, int numBlocks);
261 
263  bool isInitialized() { return initialized_; }
264 
266 
267  private:
268 
269  //
270  // Internal methods
271  //
273  void setStateSize();
274 
275  //
276  // Classes inputed through constructor that define the linear problem to be solved.
277  //
282 
283  //
284  // Algorithmic parameters
285  //
286  // blockSize_ is the solver block size.
287  // It controls the number of vectors added to the basis on each iteration.
288  int blockSize_;
289  // numBlocks_ is the size of the allocated space for the Krylov basis, in blocks.
290  int numBlocks_;
291 
292  // Storage for QR factorization of the least squares system.
295 
296  //
297  // Current solver state
298  //
299  // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
300  // is capable of running; _initialize is controlled by the initialize() member method
301  // For the implications of the state of initialized_, please see documentation for initialize()
302  bool initialized_;
303 
304  // stateStorageInitialized_ specifies that the state storage has be initialized to the current
305  // blockSize_ and numBlocks_. This initialization may be postponed if the linear problem was
306  // generated without the right-hand side or solution vectors.
307  bool stateStorageInitialized_;
308 
309  // keepHessenberg_ specifies that the iteration must keep the Hessenberg matrix formed via the
310  // Arnoldi factorization and the upper triangular matrix that is the Hessenberg matrix reduced via
311  // QR factorization separate.
312  bool keepHessenberg_;
313 
314  // initHessenberg_ specifies that the iteration should reinitialize the Hessenberg matrix by zeroing
315  // out all entries before an iteration is started.
316  bool initHessenberg_;
317 
318  // Current subspace dimension, and number of iterations performed.
319  int curDim_, iter_;
320 
321  //
322  // State Storage
323  //
324  Teuchos::RCP<MV> V_;
325  //
326  // Projected matrices
327  // H_ : Projected matrix from the Krylov factorization AV = VH + FE^T
328  //
330  //
331  // QR decomposition of Projected matrices for solving the least squares system HY = B.
332  // R_: Upper triangular reduction of H
333  // z_: Q applied to right-hand side of the least squares system
336 };
337 
339  // Constructor.
340  template<class ScalarType, class MV, class OP>
342  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
345  Teuchos::ParameterList &params ):
346  lp_(problem),
347  om_(printer),
348  stest_(tester),
349  ortho_(ortho),
350  blockSize_(0),
351  numBlocks_(0),
352  initialized_(false),
353  stateStorageInitialized_(false),
354  keepHessenberg_(false),
355  initHessenberg_(false),
356  curDim_(0),
357  iter_(0)
358  {
359  // Find out whether we are saving the Hessenberg matrix.
360  keepHessenberg_ = params.get("Keep Hessenberg", false);
361 
362  // Find out whether we are initializing the Hessenberg matrix.
363  initHessenberg_ = params.get("Initialize Hessenberg", false);
364 
365  // Get the maximum number of blocks allowed for this Krylov subspace
366  TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter("Num Blocks"), std::invalid_argument,
367  "Belos::BlockGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
368  int nb = Teuchos::getParameter<int>(params, "Num Blocks");
369 
370  // Set the block size and allocate data
371  int bs = params.get("Block Size", 1);
372  setSize( bs, nb );
373  }
374 
376  // Set the block size and make necessary adjustments.
377  template <class ScalarType, class MV, class OP>
378  void BlockGmresIter<ScalarType,MV,OP>::setSize (int blockSize, int numBlocks)
379  {
380  // This routine only allocates space; it doesn't not perform any computation
381  // any change in size will invalidate the state of the solver.
382 
383  TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0 || blockSize <= 0, std::invalid_argument, "Belos::BlockGmresIter::setSize was passed a non-positive argument.");
384  if (blockSize == blockSize_ && numBlocks == numBlocks_) {
385  // do nothing
386  return;
387  }
388 
389  if (blockSize!=blockSize_ || numBlocks!=numBlocks_)
390  stateStorageInitialized_ = false;
391 
392  blockSize_ = blockSize;
393  numBlocks_ = numBlocks;
394 
395  initialized_ = false;
396  curDim_ = 0;
397 
398  // Use the current blockSize_ and numBlocks_ to initialize the state storage.
399  setStateSize();
400 
401  }
402 
404  // Setup the state storage.
405  template <class ScalarType, class MV, class OP>
407  {
408  if (!stateStorageInitialized_) {
409 
410  // Check if there is any multivector to clone from.
411  Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
412  Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
413  if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
414  stateStorageInitialized_ = false;
415  return;
416  }
417  else {
418 
420  // blockSize*numBlocks dependent
421  //
422  int newsd = blockSize_*(numBlocks_+1);
423 
424  if (blockSize_==1) {
425  cs.resize( newsd );
426  sn.resize( newsd );
427  }
428  else {
429  beta.resize( newsd );
430  }
431 
432  // Initialize the state storage
433  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_*static_cast<ptrdiff_t>(numBlocks_) > MVText::GetGlobalLength(*rhsMV),std::invalid_argument,
434  "Belos::BlockGmresIter::setStateSize(): Cannot generate a Krylov basis with dimension larger the operator!");
435 
436  // If the subspace has not be initialized before, generate it using the LHS or RHS from lp_.
437  if (V_ == Teuchos::null) {
438  // Get the multivector that is not null.
439  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
440  TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
441  "Belos::BlockGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
442  V_ = MVT::Clone( *tmp, newsd );
443  }
444  else {
445  // Generate V_ by cloning itself ONLY if more space is needed.
446  if (MVT::GetNumberVecs(*V_) < newsd) {
447  Teuchos::RCP<const MV> tmp = V_;
448  V_ = MVT::Clone( *tmp, newsd );
449  }
450  }
451 
452  // Generate R_ only if it doesn't exist, otherwise resize it.
453  if (R_ == Teuchos::null) {
455  }
456  if (initHessenberg_) {
457  R_->shape( newsd, newsd-blockSize_ );
458  }
459  else {
460  if (R_->numRows() < newsd || R_->numCols() < newsd-blockSize_) {
461  R_->shapeUninitialized( newsd, newsd-blockSize_ );
462  }
463  }
464 
465  // Generate H_ only if it doesn't exist, and we are keeping the upper Hessenberg matrix.
466  if (keepHessenberg_) {
467  if (H_ == Teuchos::null) {
469  }
470  if (initHessenberg_) {
471  H_->shape( newsd, newsd-blockSize_ );
472  }
473  else {
474  if (H_->numRows() < newsd || H_->numCols() < newsd-blockSize_) {
475  H_->shapeUninitialized( newsd, newsd-blockSize_ );
476  }
477  }
478  }
479  else {
480  // Point H_ and R_ at the same object.
481  H_ = R_;
482  }
483 
484  // Generate z_ only if it doesn't exist, otherwise resize it.
485  if (z_ == Teuchos::null) {
487  }
488  if (z_-> numRows() < newsd || z_->numCols() < blockSize_) {
489  z_->shapeUninitialized( newsd, blockSize_ );
490  }
491 
492  // State storage has now been initialized.
493  stateStorageInitialized_ = true;
494  }
495  }
496  }
497 
499  // Get the current update from this subspace.
500  template <class ScalarType, class MV, class OP>
502  {
503  //
504  // If this is the first iteration of the Arnoldi factorization,
505  // there is no update, so return Teuchos::null.
506  //
507  Teuchos::RCP<MV> currentUpdate = Teuchos::null;
508  if (curDim_==0) {
509  return currentUpdate;
510  } else {
511  const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
512  const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
514  currentUpdate = MVT::Clone( *V_, blockSize_ );
515  //
516  // Make a view and then copy the RHS of the least squares problem. DON'T OVERWRITE IT!
517  //
518  Teuchos::SerialDenseMatrix<int,ScalarType> y( Teuchos::Copy, *z_, curDim_, blockSize_ );
519  //
520  // Solve the least squares problem.
521  //
523  Teuchos::NON_UNIT_DIAG, curDim_, blockSize_, one,
524  R_->values(), R_->stride(), y.values(), y.stride() );
525  //
526  // Compute the current update.
527  //
528  std::vector<int> index(curDim_);
529  for ( int i=0; i<curDim_; i++ ) {
530  index[i] = i;
531  }
532  Teuchos::RCP<const MV> Vjp1 = MVT::CloneView( *V_, index );
533  MVT::MvTimesMatAddMv( one, *Vjp1, y, zero, *currentUpdate );
534  }
535  return currentUpdate;
536  }
537 
538 
540  // Get the native residuals stored in this iteration.
541  // Note: No residual std::vector will be returned by Gmres.
542  template <class ScalarType, class MV, class OP>
544  {
545  //
546  // NOTE: Make sure the incoming std::vector is the correct size!
547  //
548  if ( norms && (int)norms->size() < blockSize_ )
549  norms->resize( blockSize_ );
550 
551  if (norms) {
553  for (int j=0; j<blockSize_; j++) {
554  (*norms)[j] = blas.NRM2( blockSize_, &(*z_)(curDim_, j), 1);
555  }
556  }
557  return Teuchos::null;
558  }
559 
560 
561 
563  // Initialize this iteration object
564  template <class ScalarType, class MV, class OP>
566  {
567  // Initialize the state storage if it isn't already.
568  if (!stateStorageInitialized_)
569  setStateSize();
570 
571  TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
572  "Belos::BlockGmresIter::initialize(): Cannot initialize state storage!");
573 
574  // NOTE: In BlockGmresIter, V and Z are required!!!
575  // inconsitent multivectors widths and lengths will not be tolerated, and
576  // will be treated with exceptions.
577  //
578  std::string errstr("Belos::BlockGmresIter::initialize(): Specified multivectors must have a consistent length and width.");
579 
580  if (newstate.V != Teuchos::null && newstate.z != Teuchos::null) {
581 
582  // initialize V_,z_, and curDim_
583 
584  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.V) != MVText::GetGlobalLength(*V_),
585  std::invalid_argument, errstr );
586  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.V) < blockSize_,
587  std::invalid_argument, errstr );
588  TEUCHOS_TEST_FOR_EXCEPTION( newstate.curDim > blockSize_*(numBlocks_+1),
589  std::invalid_argument, errstr );
590 
591  curDim_ = newstate.curDim;
592  int lclDim = MVT::GetNumberVecs(*newstate.V);
593 
594  // check size of Z
595  TEUCHOS_TEST_FOR_EXCEPTION(newstate.z->numRows() < curDim_ || newstate.z->numCols() < blockSize_, std::invalid_argument, errstr);
596 
597 
598  // copy basis vectors from newstate into V
599  if (newstate.V != V_) {
600  // only copy over the first block and print a warning.
601  if (curDim_ == 0 && lclDim > blockSize_) {
602  om_->stream(Warnings) << "Belos::BlockGmresIter::initialize(): the solver was initialized with a kernel of " << lclDim << std::endl
603  << "The block size however is only " << blockSize_ << std::endl
604  << "The last " << lclDim - blockSize_ << " vectors will be discarded." << std::endl;
605  }
606  std::vector<int> nevind(curDim_+blockSize_);
607  for (int i=0; i<curDim_+blockSize_; i++) nevind[i] = i;
608  Teuchos::RCP<const MV> newV = MVT::CloneView( *newstate.V, nevind );
609  Teuchos::RCP<MV> lclV = MVT::CloneViewNonConst( *V_, nevind );
610  MVT::MvAddMv( 1.0, *newV, 0.0, *newV, *lclV );
611 
612  // done with local pointers
613  lclV = Teuchos::null;
614  }
615 
616  // put data into z_, make sure old information is not still hanging around.
617  if (newstate.z != z_) {
618  z_->putScalar();
619  Teuchos::SerialDenseMatrix<int,ScalarType> newZ(Teuchos::View,*newstate.z,curDim_+blockSize_,blockSize_);
621  lclZ = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*z_,curDim_+blockSize_,blockSize_) );
622  lclZ->assign(newZ);
623 
624  // done with local pointers
625  lclZ = Teuchos::null;
626  }
627 
628  }
629  else {
630 
631  TEUCHOS_TEST_FOR_EXCEPTION(newstate.V == Teuchos::null,std::invalid_argument,
632  "Belos::BlockGmresIter::initialize(): BlockGmresStateIterState does not have initial kernel V_0.");
633 
634  TEUCHOS_TEST_FOR_EXCEPTION(newstate.z == Teuchos::null,std::invalid_argument,
635  "Belos::BlockGmresIter::initialize(): BlockGmresStateIterState does not have initial norms z_0.");
636  }
637 
638  // the solver is initialized
639  initialized_ = true;
640 
641  /*
642  if (om_->isVerbosity( Debug ) ) {
643  // Check almost everything here
644  CheckList chk;
645  chk.checkV = true;
646  chk.checkArn = true;
647  chk.checkAux = true;
648  om_->print( Debug, accuracyCheck(chk, ": after initialize()") );
649  }
650  */
651 
652  }
653 
654 
656  // Iterate until the status test informs us we should stop.
657  template <class ScalarType, class MV, class OP>
659  {
660  //
661  // Allocate/initialize data structures
662  //
663  if (initialized_ == false) {
664  initialize();
665  }
666 
667  // Compute the current search dimension.
668  int searchDim = blockSize_*numBlocks_;
669 
671  // iterate until the status test tells us to stop.
672  //
673  // also break if our basis is full
674  //
675  while (stest_->checkStatus(this) != Passed && curDim_+blockSize_ <= searchDim) {
676 
677  iter_++;
678 
679  // F can be found at the curDim_ block, but the next block is at curDim_ + blockSize_.
680  int lclDim = curDim_ + blockSize_;
681 
682  // Get the current part of the basis.
683  std::vector<int> curind(blockSize_);
684  for (int i=0; i<blockSize_; i++) { curind[i] = lclDim + i; }
685  Teuchos::RCP<MV> Vnext = MVT::CloneViewNonConst(*V_,curind);
686 
687  // Get a view of the previous vectors.
688  // This is used for orthogonalization and for computing V^H K H
689  for (int i=0; i<blockSize_; i++) { curind[i] = curDim_ + i; }
690  Teuchos::RCP<const MV> Vprev = MVT::CloneView(*V_,curind);
691 
692  // Compute the next std::vector in the Krylov basis: Vnext = Op*Vprev
693  lp_->apply(*Vprev,*Vnext);
694  Vprev = Teuchos::null;
695 
696  // Remove all previous Krylov basis vectors from Vnext
697  // Get a view of all the previous vectors
698  std::vector<int> prevind(lclDim);
699  for (int i=0; i<lclDim; i++) { prevind[i] = i; }
700  Vprev = MVT::CloneView(*V_,prevind);
701  Teuchos::Array<Teuchos::RCP<const MV> > AVprev(1, Vprev);
702 
703  // Get a view of the part of the Hessenberg matrix needed to hold the ortho coeffs.
706  ( Teuchos::View,*H_,lclDim,blockSize_,0,curDim_ ) );
708  AsubH.append( subH );
709 
710  // Get a view of the part of the Hessenberg matrix needed to hold the norm coeffs.
713  ( Teuchos::View,*H_,blockSize_,blockSize_,lclDim,curDim_ ) );
714  subH2->putScalar(); // Initialize subdiagonal to zero
715  int rank = ortho_->projectAndNormalize(*Vnext,AsubH,subH2,AVprev);
716 
717  // Copy over the coefficients if we are saving the upper Hessenberg matrix,
718  // just in case we run into an error.
719  if (keepHessenberg_) {
720  // Copy over the orthogonalization coefficients.
723  ( Teuchos::View,*R_,lclDim,blockSize_,0,curDim_ ) );
724  subR->assign(*subH);
725 
726  // Copy over the lower diagonal block of the Hessenberg matrix.
729  ( Teuchos::View,*R_,blockSize_,blockSize_,lclDim,curDim_ ) );
730  subR2->assign(*subH2);
731  }
732 
734  "Belos::BlockGmresIter::iterate(): couldn't generate basis of full rank.");
735  //
736  // V has been extended, and H has been extended.
737  //
738  // Update the QR factorization of the upper Hessenberg matrix
739  //
740  updateLSQR();
741  //
742  // Update basis dim and release all pointers.
743  //
744  Vnext = Teuchos::null;
745  curDim_ += blockSize_;
746  //
747  /*
748  // When required, monitor some orthogonalities
749  if (om_->isVerbosity( Debug ) ) {
750  // Check almost everything here
751  CheckList chk;
752  chk.checkV = true;
753  chk.checkArn = true;
754  om_->print( Debug, accuracyCheck(chk, ": after local update") );
755  }
756  else if (om_->isVerbosity( OrthoDetails ) ) {
757  CheckList chk;
758  chk.checkV = true;
759  om_->print( OrthoDetails, accuracyCheck(chk, ": after local update") );
760  }
761  */
762 
763  } // end while (statusTest == false)
764 
765  }
766 
767 
768  template<class ScalarType, class MV, class OP>
770  {
771  int i, j, maxidx;
772  ScalarType sigma, mu, vscale, maxelem;
773  const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
774 
775  // Get correct dimension based on input "dim"
776  // Remember that ortho failures result in an exit before updateLSQR() is called.
777  // Therefore, it is possible that dim == curDim_.
778  int curDim = curDim_;
779  if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
780  curDim = dim;
781  }
782 
784  //
785  // Apply previous transformations and compute new transformation to reduce upper-Hessenberg
786  // system to upper-triangular form.
787  //
788  if (blockSize_ == 1) {
789  //
790  // QR factorization of Least-Squares system with Givens rotations
791  //
792  for (i=0; i<curDim; i++) {
793  //
794  // Apply previous Givens rotations to new column of Hessenberg matrix
795  //
796  blas.ROT( 1, &(*R_)(i,curDim), 1, &(*R_)(i+1, curDim), 1, &cs[i], &sn[i] );
797  }
798  //
799  // Calculate new Givens rotation
800  //
801  blas.ROTG( &(*R_)(curDim,curDim), &(*R_)(curDim+1,curDim), &cs[curDim], &sn[curDim] );
802  (*R_)(curDim+1,curDim) = zero;
803  //
804  // Update RHS w/ new transformation
805  //
806  blas.ROT( 1, &(*z_)(curDim,0), 1, &(*z_)(curDim+1,0), 1, &cs[curDim], &sn[curDim] );
807  }
808  else {
809  //
810  // QR factorization of Least-Squares system with Householder reflectors
811  //
812  for (j=0; j<blockSize_; j++) {
813  //
814  // Apply previous Householder reflectors to new block of Hessenberg matrix
815  //
816  for (i=0; i<curDim+j; i++) {
817  sigma = blas.DOT( blockSize_, &(*R_)(i+1,i), 1, &(*R_)(i+1,curDim+j), 1);
818  sigma += (*R_)(i,curDim+j);
819  sigma *= beta[i];
820  blas.AXPY(blockSize_, -sigma, &(*R_)(i+1,i), 1, &(*R_)(i+1,curDim+j), 1);
821  (*R_)(i,curDim+j) -= sigma;
822  }
823  //
824  // Compute new Householder reflector
825  //
826  maxidx = blas.IAMAX( blockSize_+1, &(*R_)(curDim+j,curDim+j), 1 );
827  maxelem = (*R_)(curDim+j+maxidx-1,curDim+j);
828  for (i=0; i<blockSize_+1; i++)
829  (*R_)(curDim+j+i,curDim+j) /= maxelem;
830  sigma = blas.DOT( blockSize_, &(*R_)(curDim+j+1,curDim+j), 1,
831  &(*R_)(curDim+j+1,curDim+j), 1 );
832  if (sigma == zero) {
833  beta[curDim + j] = zero;
834  } else {
835  mu = Teuchos::ScalarTraits<ScalarType>::squareroot((*R_)(curDim+j,curDim+j)*(*R_)(curDim+j,curDim+j)+sigma);
836  if ( Teuchos::ScalarTraits<ScalarType>::real((*R_)(curDim+j,curDim+j))
838  vscale = (*R_)(curDim+j,curDim+j) - mu;
839  } else {
840  vscale = -sigma / ((*R_)(curDim+j,curDim+j) + mu);
841  }
842  beta[curDim+j] = 2.0*vscale*vscale/(sigma + vscale*vscale);
843  (*R_)(curDim+j,curDim+j) = maxelem*mu;
844  for (i=0; i<blockSize_; i++)
845  (*R_)(curDim+j+1+i,curDim+j) /= vscale;
846  }
847  //
848  // Apply new Householder reflector to rhs
849  //
850  for (i=0; i<blockSize_; i++) {
851  sigma = blas.DOT( blockSize_, &(*R_)(curDim+j+1,curDim+j),
852  1, &(*z_)(curDim+j+1,i), 1);
853  sigma += (*z_)(curDim+j,i);
854  sigma *= beta[curDim+j];
855  blas.AXPY(blockSize_, -sigma, &(*R_)(curDim+j+1,curDim+j),
856  1, &(*z_)(curDim+j+1,i), 1);
857  (*z_)(curDim+j,i) -= sigma;
858  }
859  }
860  } // end if (blockSize_ == 1)
861 
862  // If the least-squares problem is updated wrt "dim" then update the curDim_.
863  if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
864  curDim_ = dim + blockSize_;
865  }
866 
867  } // end updateLSQR()
868 
869 } // end Belos namespace
870 
871 #endif /* BELOS_BLOCK_GMRES_ITER_HPP */
ScalarType * values() const
Collection of types and exceptions used within the Belos solvers.
OrdinalType IAMAX(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
Array< T > & append(const T &x)
Class which manages the output and verbosity of the Belos solvers.
MultiVecTraitsExt< ScalarType, MV > MVText
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
This class implements the block GMRES iteration, where a block Krylov subspace is constructed...
Teuchos::RCP< const MV > V
The current Krylov basis.
T & get(const std::string &name, T def_value)
MultiVecTraits< ScalarType, MV > MVT
Pure virtual base class for defining the status testing capabilities of Belos.
static T squareroot(T x)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void updateLSQR(int dim=-1)
Method for updating QR factorization of upper Hessenberg matrix.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
Declaration of basic traits for the multivector type.
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > z
The current right-hand side of the least squares system RY = Z.
void iterate()
This method performs block Gmres iterations until the status test indicates the need to stop or an er...
Structure to contain pointers to GmresIteration state variables.
An extension of the MultiVecTraits class that adds a new vector length method.
void resetNumIters(int iter=0)
Reset the iteration count.
SCT::magnitudeType MagnitudeType
A pure virtual class for defining the status tests for the Belos iterative solvers.
Class which defines basic traits for the operator type.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
void setSize(int blockSize, int numBlocks)
Set the blocksize and number of blocks to be used by the iterative solver in solving this linear prob...
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
virtual ~BlockGmresIter()
Destructor.
Traits class which defines basic operations on multivectors.
bool isParameter(const std::string &name) const
bool isInitialized()
States whether the solver has been initialized or not.
void ROTG(ScalarType *da, ScalarType *db, MagnitudeType *c, ScalarType *s) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int curDim
The current dimension of the reduction.
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > R
The current upper-triangular matrix from the QR reduction of H.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void AXPY(const OrdinalType n, const alpha_type alpha, const x_type *x, const OrdinalType incx, ScalarType *y, const OrdinalType incy) const
void ROT(const OrdinalType n, ScalarType *dx, const OrdinalType incx, ScalarType *dy, const OrdinalType incy, MagnitudeType *c, ScalarType *s) const
int getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
Teuchos::ScalarTraits< ScalarType > SCT
GmresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
void initializeGmres(GmresIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
OrdinalType numCols() const
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
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
Class which defines basic traits for the operator type.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
BlockGmresIter(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)
BlockGmresIter constructor with linear problem, solver utilities, and parameter list of solver option...
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Pure virtual base class which augments the basic interface for a Gmres linear solver iteration...
GmresIterationOrthoFailure is thrown when the GmresIteration object is unable to compute independent ...
Belos header file which uses auto-configuration information to include necessary C++ headers...
ScalarType DOT(const OrdinalType n, const x_type *x, const OrdinalType incx, const y_type *y, const OrdinalType incy) const
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem...
void setBlockSize(int blockSize)
Set the blocksize.
OrdinalType stride() const
OrdinalType numRows() const
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
int getNumIters() const
Get the current iteration count.
ScalarTraits< ScalarType >::magnitudeType NRM2(const OrdinalType n, const ScalarType *x, const OrdinalType incx) const
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.