Belos  Version of the Day
BelosBlockFGmresIter.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_FGMRES_ITER_HPP
43 #define BELOS_BLOCK_FGMRES_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"
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 
80 namespace Belos {
81 
82 template<class ScalarType, class MV, class OP>
83 class BlockFGmresIter : virtual public GmresIteration<ScalarType,MV,OP> {
84 
85  public:
86 
87  //
88  // Convenience typedefs
89  //
93  typedef Teuchos::ScalarTraits<ScalarType> SCT;
94  typedef typename SCT::magnitudeType MagnitudeType;
95 
97 
98 
108  BlockFGmresIter( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
109  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
110  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
111  const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
112  Teuchos::ParameterList &params );
113 
115  virtual ~BlockFGmresIter() {};
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.Z = Z_;
189  state.H = H_;
190  state.R = R_;
191  state.z = z_;
192  return state;
193  }
194 
196 
197 
199 
200 
202  int getNumIters() const { return iter_; }
203 
205  void resetNumIters( int iter = 0 ) { iter_ = iter; }
206 
209  Teuchos::RCP<const MV> getNativeResiduals( std::vector<MagnitudeType> *norms ) const;
210 
212 
217  Teuchos::RCP<MV> getCurrentUpdate() const;
218 
220 
223  void updateLSQR( int dim = -1 );
224 
226  int getCurSubspaceDim() const {
227  if (!initialized_) return 0;
228  return curDim_;
229  };
230 
232  int getMaxSubspaceDim() const { return blockSize_*numBlocks_; }
233 
235 
236 
238 
239 
241  const LinearProblem<ScalarType,MV,OP>& getProblem() const { return *lp_; }
242 
244  int getBlockSize() const { return blockSize_; }
245 
247  void setBlockSize(int blockSize) { setSize( blockSize, numBlocks_ ); }
248 
250  int getNumBlocks() const { return numBlocks_; }
251 
253  void setNumBlocks(int numBlocks) { setSize( blockSize_, numBlocks ); }
254 
261  void setSize(int blockSize, int numBlocks);
262 
264  bool isInitialized() { return initialized_; }
265 
267 
268  private:
269 
270  //
271  // Internal methods
272  //
274  void setStateSize();
275 
276  //
277  // Classes inputed through constructor that define the linear problem to be solved.
278  //
279  const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
280  const Teuchos::RCP<OutputManager<ScalarType> > om_;
281  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
282  const Teuchos::RCP<OrthoManager<ScalarType,MV> > ortho_;
283 
284  //
285  // Algorithmic parameters
286  //
287  // blockSize_ is the solver block size.
288  // It controls the number of vectors added to the basis on each iteration.
289  int blockSize_;
290  // numBlocks_ is the size of the allocated space for the Krylov basis, in blocks.
291  int numBlocks_;
292 
293  // Storage for QR factorization of the least squares system.
294  Teuchos::SerialDenseVector<int,ScalarType> beta, sn;
295  Teuchos::SerialDenseVector<int,MagnitudeType> cs;
296 
297  //
298  // Current solver state
299  //
300  // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
301  // is capable of running; _initialize is controlled by the initialize() member method
302  // For the implications of the state of initialized_, please see documentation for initialize()
303  bool initialized_;
304 
305  // stateStorageInitialized_ specified that the state storage has be initialized to the current
306  // blockSize_ and numBlocks_. This initialization may be postponed if the linear problem was
307  // generated without the right-hand side or solution vectors.
308  bool stateStorageInitialized_;
309 
310  // Current subspace dimension, and number of iterations performed.
311  int curDim_, iter_;
312 
313  //
314  // State Storage
315  //
316  Teuchos::RCP<MV> V_;
317  Teuchos::RCP<MV> Z_;
318  //
319  // Projected matrices
320  // H_ : Projected matrix from the Krylov factorization AV = VH + FE^T
321  //
322  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > H_;
323  //
324  // QR decomposition of Projected matrices for solving the least squares system HY = B.
325  // R_: Upper triangular reduction of H
326  // z_: Q applied to right-hand side of the least squares system
327  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > R_;
328  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > z_;
329 };
330 
332  // Constructor.
333  template<class ScalarType, class MV, class OP>
335  const Teuchos::RCP<OutputManager<ScalarType> > &printer,
336  const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
337  const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
338  Teuchos::ParameterList &params ):
339  lp_(problem),
340  om_(printer),
341  stest_(tester),
342  ortho_(ortho),
343  blockSize_(0),
344  numBlocks_(0),
345  initialized_(false),
346  stateStorageInitialized_(false),
347  curDim_(0),
348  iter_(0)
349  {
350  // Get the maximum number of blocks allowed for this Krylov subspace
351  TEUCHOS_TEST_FOR_EXCEPTION(!params.isParameter("Num Blocks"), std::invalid_argument,
352  "Belos::BlockFGmresIter::constructor: mandatory parameter 'Num Blocks' is not specified.");
353  int nb = Teuchos::getParameter<int>(params, "Num Blocks");
354 
355  // Set the block size and allocate data
356  int bs = params.get("Block Size", 1);
357  setSize( bs, nb );
358  }
359 
361  // Set the block size and make necessary adjustments.
362  template <class ScalarType, class MV, class OP>
363  void BlockFGmresIter<ScalarType,MV,OP>::setSize (int blockSize, int numBlocks)
364  {
365  // This routine only allocates space; it doesn't not perform any computation
366  // any change in size will invalidate the state of the solver.
367 
368  TEUCHOS_TEST_FOR_EXCEPTION(numBlocks <= 0 || blockSize <= 0, std::invalid_argument, "Belos::BlockFGmresIter::setSize was passed a non-positive argument.");
369  if (blockSize == blockSize_ && numBlocks == numBlocks_) {
370  // do nothing
371  return;
372  }
373 
374  if (blockSize!=blockSize_ || numBlocks!=numBlocks_)
375  stateStorageInitialized_ = false;
376 
377  blockSize_ = blockSize;
378  numBlocks_ = numBlocks;
379 
380  initialized_ = false;
381  curDim_ = 0;
382 
383  // Use the current blockSize_ and numBlocks_ to initialize the state storage.
384  setStateSize();
385 
386  }
387 
389  // Setup the state storage.
390  template <class ScalarType, class MV, class OP>
392  {
393  if (!stateStorageInitialized_) {
394 
395  // Check if there is any multivector to clone from.
396  Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
397  Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
398  if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
399  stateStorageInitialized_ = false;
400  return;
401  }
402  else {
403 
405  // blockSize*numBlocks dependent
406  //
407  int newsd = blockSize_*(numBlocks_+1);
408 
409  if (blockSize_==1) {
410  cs.resize( newsd );
411  sn.resize( newsd );
412  }
413  else {
414  beta.resize( newsd );
415  }
416 
417  // Initialize the state storage
418  TEUCHOS_TEST_FOR_EXCEPTION( blockSize_*static_cast<ptrdiff_t>(numBlocks_) > MVText::GetGlobalLength(*rhsMV),std::invalid_argument,
419  "Belos::BlockFGmresIter::setStateSize(): Cannot generate a Krylov basis with dimension larger the operator!");
420 
421  // If the subspace has not be initialized before, generate it using the LHS or RHS from lp_.
422  if (V_ == Teuchos::null) {
423  // Get the multivector that is not null.
424  Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
425  TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
426  "Belos::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
427  V_ = MVT::Clone( *tmp, newsd );
428  }
429  else {
430  // Generate V_ by cloning itself ONLY if more space is needed.
431  if (MVT::GetNumberVecs(*V_) < newsd) {
432  Teuchos::RCP<const MV> tmp = V_;
433  V_ = MVT::Clone( *tmp, newsd );
434  }
435  }
436 
437  if (Z_ == 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::BlockFGmresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
442  Z_ = MVT::Clone( *tmp, newsd );
443  }
444  else {
445  // Generate Z_ by cloning itself ONLY if more space is needed.
446  if (MVT::GetNumberVecs(*Z_) < newsd) {
447  Teuchos::RCP<const MV> tmp = Z_;
448  Z_ = MVT::Clone( *tmp, newsd );
449  }
450  }
451 
452  // Generate H_ only if it doesn't exist, otherwise resize it.
453  if (H_ == Teuchos::null)
454  H_ = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>( newsd, newsd-blockSize_ ) );
455  else
456  H_->shapeUninitialized( newsd, newsd-blockSize_ );
457 
458  // TODO: Insert logic so that Hessenberg matrix can be saved and reduced matrix is stored in R_
459  //R_ = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>( newsd, newsd-blockSize_ ) );
460  // Generate z_ only if it doesn't exist, otherwise resize it.
461  if (z_ == Teuchos::null)
462  z_ = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(newsd,blockSize_) );
463  else
464  z_->shapeUninitialized( newsd, blockSize_ );
465 
466  // State storage has now been initialized.
467  stateStorageInitialized_ = true;
468  }
469  }
470  }
471 
473  // Get the current update from this subspace.
474  template <class ScalarType, class MV, class OP>
476  {
477  //
478  // If this is the first iteration of the Arnoldi factorization,
479  // there is no update, so return Teuchos::null.
480  //
481  Teuchos::RCP<MV> currentUpdate = Teuchos::null;
482  if (curDim_==0) {
483  return currentUpdate;
484  } else {
485  const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
486  const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
487  Teuchos::BLAS<int,ScalarType> blas;
488  currentUpdate = MVT::Clone( *Z_, blockSize_ );
489  //
490  // Make a view and then copy the RHS of the least squares problem. DON'T OVERWRITE IT!
491  //
492  Teuchos::SerialDenseMatrix<int,ScalarType> y( Teuchos::Copy, *z_, curDim_, blockSize_ );
493  //
494  // Solve the least squares problem.
495  //
496  blas.TRSM( Teuchos::LEFT_SIDE, Teuchos::UPPER_TRI, Teuchos::NO_TRANS,
497  Teuchos::NON_UNIT_DIAG, curDim_, blockSize_, one,
498  H_->values(), H_->stride(), y.values(), y.stride() );
499  //
500  // Compute the current update.
501  //
502  std::vector<int> index(curDim_);
503  for ( int i=0; i<curDim_; i++ ) {
504  index[i] = i;
505  }
506  Teuchos::RCP<const MV> Zjp1 = MVT::CloneView( *Z_, index );
507  MVT::MvTimesMatAddMv( one, *Zjp1, y, zero, *currentUpdate );
508  }
509  return currentUpdate;
510  }
511 
512 
514  // Get the native residuals stored in this iteration.
515  // Note: No residual std::vector will be returned by FGmres.
516  template <class ScalarType, class MV, class OP>
517  Teuchos::RCP<const MV> BlockFGmresIter<ScalarType,MV,OP>::getNativeResiduals( std::vector<MagnitudeType> *norms ) const
518  {
519  //
520  // NOTE: Make sure the incoming std::vector is the correct size!
521  //
522  if ( norms && (int)norms->size() < blockSize_ )
523  norms->resize( blockSize_ );
524 
525  if (norms) {
526  Teuchos::BLAS<int,ScalarType> blas;
527  for (int j=0; j<blockSize_; j++) {
528  (*norms)[j] = blas.NRM2( blockSize_, &(*z_)(curDim_, j), 1);
529  }
530  }
531  return Teuchos::null;
532  }
533 
534 
535 
537  // Initialize this iteration object
538  template <class ScalarType, class MV, class OP>
540  {
541  // Initialize the state storage if it isn't already.
542  if (!stateStorageInitialized_)
543  setStateSize();
544 
545  TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
546  "Belos::BlockFGmresIter::initialize(): Cannot initialize state storage!");
547 
548  // NOTE: In BlockFGmresIter, V and Z are required!!!
549  // inconsitent multivectors widths and lengths will not be tolerated, and
550  // will be treated with exceptions.
551  //
552  std::string errstr("Belos::BlockFGmresIter::initialize(): Specified multivectors must have a consistent length and width.");
553 
554  if (newstate.V != Teuchos::null && newstate.z != Teuchos::null) {
555 
556  // initialize V_,z_, and curDim_
557 
558  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*newstate.V) != MVText::GetGlobalLength(*V_),
559  std::invalid_argument, errstr );
560  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.V) < blockSize_,
561  std::invalid_argument, errstr );
562  TEUCHOS_TEST_FOR_EXCEPTION( newstate.curDim > blockSize_*(numBlocks_+1),
563  std::invalid_argument, errstr );
564 
565  curDim_ = newstate.curDim;
566  int lclDim = MVT::GetNumberVecs(*newstate.V);
567 
568  // check size of Z
569  TEUCHOS_TEST_FOR_EXCEPTION(newstate.z->numRows() < curDim_ || newstate.z->numCols() < blockSize_, std::invalid_argument, errstr);
570 
571 
572  // copy basis vectors from newstate into V
573  if (newstate.V != V_) {
574  // only copy over the first block and print a warning.
575  if (curDim_ == 0 && lclDim > blockSize_) {
576  om_->stream(Warnings) << "Belos::BlockFGmresIter::initialize(): the solver was initialized with a kernel of " << lclDim << std::endl
577  << "The block size however is only " << blockSize_ << std::endl
578  << "The last " << lclDim - blockSize_ << " vectors will be discarded." << std::endl;
579  }
580  std::vector<int> nevind(curDim_+blockSize_);
581  for (int i=0; i<curDim_+blockSize_; i++) nevind[i] = i;
582  Teuchos::RCP<const MV> newV = MVT::CloneView( *newstate.V, nevind );
583  Teuchos::RCP<MV> lclV = MVT::CloneViewNonConst( *V_, nevind );
584  MVT::MvAddMv( 1.0, *newV, 0.0, *newV, *lclV );
585 
586  // done with local pointers
587  lclV = Teuchos::null;
588  }
589 
590  // put data into z_, make sure old information is not still hanging around.
591  if (newstate.z != z_) {
592  z_->putScalar();
593  Teuchos::SerialDenseMatrix<int,ScalarType> newZ(Teuchos::View,*newstate.z,curDim_+blockSize_,blockSize_);
594  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > lclz;
595  lclz = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View,*z_,curDim_+blockSize_,blockSize_) );
596  lclz->assign(newZ);
597 
598  // done with local pointers
599  lclz = Teuchos::null;
600  }
601 
602  }
603  else {
604 
605  TEUCHOS_TEST_FOR_EXCEPTION(newstate.V == Teuchos::null,std::invalid_argument,
606  "Belos::BlockFGmresIter::initialize(): BlockFGmresStateIterState does not have initial kernel V_0.");
607 
608  TEUCHOS_TEST_FOR_EXCEPTION(newstate.z == Teuchos::null,std::invalid_argument,
609  "Belos::BlockFGmresIter::initialize(): BlockFGmresStateIterState does not have initial norms z_0.");
610  }
611 
612  // the solver is initialized
613  initialized_ = true;
614 
615  /*
616  if (om_->isVerbosity( Debug ) ) {
617  // Check almost everything here
618  CheckList chk;
619  chk.checkV = true;
620  chk.checkArn = true;
621  chk.checkAux = true;
622  om_->print( Debug, accuracyCheck(chk, ": after initialize()") );
623  }
624  */
625 
626  }
627 
628 
630  // Iterate until the status test informs us we should stop.
631  template <class ScalarType, class MV, class OP>
633  {
634  //
635  // Allocate/initialize data structures
636  //
637  if (initialized_ == false) {
638  initialize();
639  }
640 
641  // Compute the current search dimension.
642  int searchDim = blockSize_*numBlocks_;
643 
645  // iterate until the status test tells us to stop.
646  //
647  // also break if our basis is full
648  //
649  while (stest_->checkStatus(this) != Passed && curDim_+blockSize_ <= searchDim) {
650 
651  iter_++;
652 
653  // F can be found at the curDim_ block, but the next block is at curDim_ + blockSize_.
654  int lclDim = curDim_ + blockSize_;
655 
656  // Get the current part of the basis.
657  std::vector<int> curind(blockSize_);
658  for (int i=0; i<blockSize_; i++) { curind[i] = lclDim + i; }
659  Teuchos::RCP<MV> Vnext = MVT::CloneViewNonConst(*V_,curind);
660 
661  // Get a view of the previous vectors.
662  // This is used for orthogonalization and for computing V^H K H
663  for (int i=0; i<blockSize_; i++) { curind[i] = curDim_ + i; }
664  Teuchos::RCP<const MV> Vprev = MVT::CloneView(*V_,curind);
665  Teuchos::RCP<MV> Znext = MVT::CloneViewNonConst(*Z_,curind);
666 
667  // Compute the next std::vector in the Krylov basis: Znext = M*Vprev
668  lp_->applyRightPrec(*Vprev,*Znext);
669  Vprev = Teuchos::null;
670 
671  // Compute the next std::vector in the Krylov basis: Vnext = A*Znext
672  lp_->applyOp(*Znext,*Vnext);
673  Znext = Teuchos::null;
674 
675  // Remove all previous Krylov basis vectors from Vnext
676  // Get a view of all the previous vectors
677  std::vector<int> prevind(lclDim);
678  for (int i=0; i<lclDim; i++) { prevind[i] = i; }
679  Vprev = MVT::CloneView(*V_,prevind);
680  Teuchos::Array<Teuchos::RCP<const MV> > AVprev(1, Vprev);
681 
682  // Get a view of the part of the Hessenberg matrix needed to hold the ortho coeffs.
683  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
684  subH = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>
685  ( Teuchos::View,*H_,lclDim,blockSize_,0,curDim_ ) );
686  Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > AsubH;
687  AsubH.append( subH );
688 
689  // Get a view of the part of the Hessenberg matrix needed to hold the norm coeffs.
690  Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> >
691  subR = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>
692  ( Teuchos::View,*H_,blockSize_,blockSize_,lclDim,curDim_ ) );
693  int rank = ortho_->projectAndNormalize(*Vnext,AsubH,subR,AVprev);
694  TEUCHOS_TEST_FOR_EXCEPTION(rank != blockSize_,GmresIterationOrthoFailure,
695  "Belos::BlockFGmresIter::iterate(): couldn't generate basis of full rank.");
696  //
697  // V has been extended, and H has been extended.
698  //
699  // Update the QR factorization of the upper Hessenberg matrix
700  //
701  updateLSQR();
702  //
703  // Update basis dim and release all pointers.
704  //
705  Vnext = Teuchos::null;
706  curDim_ += blockSize_;
707  //
708  /*
709  // When required, monitor some orthogonalities
710  if (om_->isVerbosity( Debug ) ) {
711  // Check almost everything here
712  CheckList chk;
713  chk.checkV = true;
714  chk.checkArn = true;
715  om_->print( Debug, accuracyCheck(chk, ": after local update") );
716  }
717  else if (om_->isVerbosity( OrthoDetails ) ) {
718  CheckList chk;
719  chk.checkV = true;
720  om_->print( OrthoDetails, accuracyCheck(chk, ": after local update") );
721  }
722  */
723 
724  } // end while (statusTest == false)
725 
726  }
727 
728 
729  template<class ScalarType, class MV, class OP>
731  {
732  int i, j, maxidx;
733  ScalarType sigma, mu, vscale, maxelem;
734  const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
735 
736  // Get correct dimension based on input "dim"
737  // Remember that ortho failures result in an exit before updateLSQR() is called.
738  // Therefore, it is possible that dim == curDim_.
739  int curDim = curDim_;
740  if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
741  curDim = dim;
742  }
743 
744  Teuchos::BLAS<int, ScalarType> blas;
745  //
746  // Apply previous transformations and compute new transformation to reduce upper-Hessenberg
747  // system to upper-triangular form.
748  //
749  if (blockSize_ == 1) {
750  //
751  // QR factorization of Least-Squares system with Givens rotations
752  //
753  for (i=0; i<curDim; i++) {
754  //
755  // Apply previous Givens rotations to new column of Hessenberg matrix
756  //
757  blas.ROT( 1, &(*H_)(i,curDim), 1, &(*H_)(i+1, curDim), 1, &cs[i], &sn[i] );
758  }
759  //
760  // Calculate new Givens rotation
761  //
762  blas.ROTG( &(*H_)(curDim,curDim), &(*H_)(curDim+1,curDim), &cs[curDim], &sn[curDim] );
763  (*H_)(curDim+1,curDim) = zero;
764  //
765  // Update RHS w/ new transformation
766  //
767  blas.ROT( 1, &(*z_)(curDim,0), 1, &(*z_)(curDim+1,0), 1, &cs[curDim], &sn[curDim] );
768  }
769  else {
770  //
771  // QR factorization of Least-Squares system with Householder reflectors
772  //
773  for (j=0; j<blockSize_; j++) {
774  //
775  // Apply previous Householder reflectors to new block of Hessenberg matrix
776  //
777  for (i=0; i<curDim+j; i++) {
778  sigma = blas.DOT( blockSize_, &(*H_)(i+1,i), 1, &(*H_)(i+1,curDim+j), 1);
779  sigma += (*H_)(i,curDim+j);
780  sigma *= beta[i];
781  blas.AXPY(blockSize_, -sigma, &(*H_)(i+1,i), 1, &(*H_)(i+1,curDim+j), 1);
782  (*H_)(i,curDim+j) -= sigma;
783  }
784  //
785  // Compute new Householder reflector
786  //
787  maxidx = blas.IAMAX( blockSize_+1, &(*H_)(curDim+j,curDim+j), 1 );
788  maxelem = (*H_)(curDim+j+maxidx-1,curDim+j);
789  for (i=0; i<blockSize_+1; i++)
790  (*H_)(curDim+j+i,curDim+j) /= maxelem;
791  sigma = blas.DOT( blockSize_, &(*H_)(curDim+j+1,curDim+j), 1,
792  &(*H_)(curDim+j+1,curDim+j), 1 );
793  if (sigma == zero) {
794  beta[curDim + j] = zero;
795  } else {
796  mu = Teuchos::ScalarTraits<ScalarType>::squareroot((*H_)(curDim+j,curDim+j)*(*H_)(curDim+j,curDim+j)+sigma);
797  if ( Teuchos::ScalarTraits<ScalarType>::real((*H_)(curDim+j,curDim+j))
798  < Teuchos::ScalarTraits<MagnitudeType>::zero() ) {
799  vscale = (*H_)(curDim+j,curDim+j) - mu;
800  } else {
801  vscale = -sigma / ((*H_)(curDim+j,curDim+j) + mu);
802  }
803  beta[curDim+j] = 2.0*vscale*vscale/(sigma + vscale*vscale);
804  (*H_)(curDim+j,curDim+j) = maxelem*mu;
805  for (i=0; i<blockSize_; i++)
806  (*H_)(curDim+j+1+i,curDim+j) /= vscale;
807  }
808  //
809  // Apply new Householder reflector to rhs
810  //
811  for (i=0; i<blockSize_; i++) {
812  sigma = blas.DOT( blockSize_, &(*H_)(curDim+j+1,curDim+j),
813  1, &(*z_)(curDim+j+1,i), 1);
814  sigma += (*z_)(curDim+j,i);
815  sigma *= beta[curDim+j];
816  blas.AXPY(blockSize_, -sigma, &(*H_)(curDim+j+1,curDim+j),
817  1, &(*z_)(curDim+j+1,i), 1);
818  (*z_)(curDim+j,i) -= sigma;
819  }
820  }
821  } // end if (blockSize_ == 1)
822 
823  // If the least-squares problem is updated wrt "dim" then update the curDim_.
824  if (dim >= curDim_ && dim < getMaxSubspaceDim()) {
825  curDim_ = dim + blockSize_;
826  }
827 
828  } // end updateLSQR()
829 
830 } // end Belos namespace
831 
832 #endif /* BELOS_BLOCK_FGMRES_ITER_HPP */
Collection of types and exceptions used within the Belos solvers.
void iterate()
This method performs block FGmres iterations until the status test indicates the need to stop or an e...
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
void setBlockSize(int blockSize)
Set the blocksize.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
Class which manages the output and verbosity of the Belos solvers.
BlockFGmresIter(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)
BlockFGmresIter constructor with linear problem, solver utilities, and parameter list of solver optio...
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< const MV > V
The current Krylov basis.
Pure virtual base class for defining the status testing capabilities of Belos.
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 getNumBlocks() const
Get the maximum number of blocks used by the iterative solver in solving this linear problem...
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.
int getCurSubspaceDim() const
Get the dimension of the search subspace used to generate the current solution to the linear problem...
Structure to contain pointers to GmresIteration state variables.
An extension of the MultiVecTraits class that adds a new vector length method.
A pure virtual class for defining the status tests for the Belos iterative solvers.
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...
void resetNumIters(int iter=0)
Reset the iteration count.
Class which defines basic traits for the operator type.
int getNumIters() const
Get the current iteration count.
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 .
Traits class which defines basic operations on multivectors.
GmresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
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.
Teuchos::RCP< const MV > Z
The current preconditioned Krylov basis (only used in flexible GMRES).
int curDim
The current dimension of the reduction.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
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.
This class implements the block flexible GMRES iteration, where a block Krylov subspace is constructe...
virtual ~BlockFGmresIter()
Destructor.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
bool isInitialized()
States whether the solver has been initialized or not.
MultiVecTraits< ScalarType, MV > MVT
int getMaxSubspaceDim() const
Get the maximum dimension allocated for the search subspace.
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
void initializeGmres(GmresIterationState< ScalarType, MV > newstate)
Initialize the solver to an iterate, providing a complete state.
Class which defines basic traits for the operator type.
SCT::magnitudeType MagnitudeType
void updateLSQR(int dim=-1)
Method for updating QR factorization of upper Hessenberg matrix.
void setNumBlocks(int numBlocks)
Set the maximum number of blocks used by the iterative solver.
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...
MultiVecTraitsExt< ScalarType, MV > MVText
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > H
The current Hessenberg matrix.