Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosDGKSOrthoManager.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 
47 #ifndef BELOS_DGKS_ORTHOMANAGER_HPP
48 #define BELOS_DGKS_ORTHOMANAGER_HPP
49 
57 // #define ORTHO_DEBUG
58 
59 #include "BelosConfigDefs.hpp"
60 #include "BelosMultiVecTraits.hpp"
61 #include "BelosOperatorTraits.hpp"
62 #include "BelosMatOrthoManager.hpp"
63 
64 #include "Teuchos_as.hpp"
65 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
66 #ifdef BELOS_TEUCHOS_TIME_MONITOR
67 #include "Teuchos_TimeMonitor.hpp"
68 #endif // BELOS_TEUCHOS_TIME_MONITOR
69 
70 namespace Belos {
71 
76  template<class ScalarType>
79  {
80  using Teuchos::as;
81  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType magnitude_type;
83 
84  // Default parameter values for DGKS orthogonalization.
85  // Documentation will be embedded in the parameter list.
86  const int defaultMaxNumOrthogPasses = 2;
87  const magnitude_type one = STM::one();
88  const magnitude_type eps = STM::eps();
89  const magnitude_type squareRootTwo =
90  STM::squareroot (as<magnitude_type> (2));
91  const magnitude_type defaultBlkTol =
92  as<magnitude_type> (10) * STM::squareroot (eps);
93  const magnitude_type defaultDepTol = one / squareRootTwo;
94  const magnitude_type defaultSingTol = as<magnitude_type> (10) * eps;
95 
97  Teuchos::parameterList ("DGKS");
98  params->set ("maxNumOrthogPasses", defaultMaxNumOrthogPasses,
99  "Maximum number of orthogonalization passes "
100  "(includes the first). Default is 2, since "
101  "\"twice is enough\" for Krylov methods.");
102  params->set ("blkTol", defaultBlkTol,
103  "Block reorthogonalization threshhold.");
104  params->set ("depTol", defaultDepTol,
105  "(Non-block) reorthogonalization threshold.");
106  params->set ("singTol", defaultSingTol,
107  "Singular block detection threshold.");
108  return params;
109  }
110 
116  template<class ScalarType>
119  {
121  using Teuchos::RCP;
122  using Teuchos::rcp;
123  using Teuchos::ScalarTraits;
124  typedef typename ScalarTraits<ScalarType>::magnitudeType magnitude_type;
125  typedef ScalarTraits<magnitude_type> STM;
126 
127  RCP<const ParameterList> defaultParams = getDefaultDgksParameters<ScalarType> ();
128  // Start with a clone of the default parameters.
129  RCP<ParameterList> params = rcp (new ParameterList (*defaultParams));
130 
131  const int maxBlkOrtho = 1;
132  const magnitude_type blkTol = STM::zero();
133  const magnitude_type depTol = STM::zero();
134  const magnitude_type singTol = STM::zero();
135 
136  params->set ("maxNumOrthogPasses", maxBlkOrtho);
137  params->set ("blkTol", blkTol);
138  params->set ("depTol", depTol);
139  params->set ("singTol", singTol);
140 
141  return params;
142  }
143 
153  template<class ScalarType>
154  void TEUCHOS_DEPRECATED
156  int& maxNumOrthogPasses,
160  {
162  using Teuchos::RCP;
163  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType magnitude_type;
164  const magnitude_type zero = Teuchos::ScalarTraits<magnitude_type>::zero();
165  RCP<const ParameterList> defaultParams = getDefaultDgksParameters<ScalarType>();
166 
167  // Using temporary variables and fetching all values before
168  // setting the output arguments ensures the strong exception
169  // guarantee for this function: if an exception is thrown, no
170  // externally visible side effects (in this case, setting the
171  // output arguments) have taken place.
172  int _maxNumOrthogPasses;
173  magnitude_type _blkTol, _depTol, _singTol;
174  if (params.is_null())
175  {
176  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
177  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
178  _depTol = defaultParams->get<magnitude_type> ("depTol");
179  _singTol = defaultParams->get<magnitude_type> ("singTol");
180  }
181  else
182  {
183  try {
184  _maxNumOrthogPasses = params->get<int> ("maxNumOrthogPasses");
185  if (_maxNumOrthogPasses < 1)
186  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
188  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
189  }
190 
191  try {
192  _blkTol = params->get<magnitude_type> ("blkTol");
193  if (_blkTol < zero)
194  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
196  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
197  }
198 
199  try {
200  _depTol = params->get<magnitude_type> ("depTol");
201  if (_depTol < zero)
202  _depTol = defaultParams->get<magnitude_type> ("depTol");
204  _depTol = defaultParams->get<magnitude_type> ("depTol");
205  }
206 
207  try {
208  _singTol = params->get<magnitude_type> ("singTol");
209  if (_singTol < zero)
210  _singTol = defaultParams->get<magnitude_type> ("singTol");
212  _singTol = defaultParams->get<magnitude_type> ("singTol");
213  }
214  }
215  maxNumOrthogPasses = _maxNumOrthogPasses;
216  blkTol = _blkTol;
217  depTol = _depTol;
218  singTol = _singTol;
219  }
220 
221  template<class ScalarType, class MV, class OP>
223  public MatOrthoManager<ScalarType,MV,OP>,
225  {
226  private:
227  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
228  typedef typename Teuchos::ScalarTraits<MagnitudeType> MGT;
233 
234  public:
236 
237 
239  DGKSOrthoManager( const std::string& label = "Belos",
240  Teuchos::RCP<const OP> Op = Teuchos::null,
241  const int max_blk_ortho = 2,
242  const MagnitudeType blk_tol = 10*MGT::squareroot( MGT::eps() ),
243  const MagnitudeType dep_tol = MGT::one()/MGT::squareroot( 2*MGT::one() ),
244  const MagnitudeType sing_tol = 10*MGT::eps() )
245  : MatOrthoManager<ScalarType,MV,OP>(Op),
246  max_blk_ortho_( max_blk_ortho ),
247  blk_tol_( blk_tol ),
248  dep_tol_( dep_tol ),
249  sing_tol_( sing_tol ),
250  label_( label )
251  {
252 #ifdef BELOS_TEUCHOS_TIME_MONITOR
253  std::string orthoLabel = label_ + ": Orthogonalization";
254  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter( orthoLabel );
255 #endif
256  }
257 
260  const std::string& label = "Belos",
261  Teuchos::RCP<const OP> Op = Teuchos::null)
262  : MatOrthoManager<ScalarType,MV,OP>(Op),
263  max_blk_ortho_ (2),
264  blk_tol_ (Teuchos::as<MagnitudeType>(10) * MGT::squareroot(MGT::eps())),
265  dep_tol_ (MGT::one() / MGT::squareroot (Teuchos::as<MagnitudeType>(2))),
266  sing_tol_ (Teuchos::as<MagnitudeType>(10) * MGT::eps()),
267  label_( label )
268  {
269  setParameterList (plist);
270 
271 #ifdef BELOS_TEUCHOS_TIME_MONITOR
272  std::string orthoLabel = label_ + ": Orthogonalization";
273  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter( orthoLabel );
274 #endif
275  }
276 
280 
282 
283 
284  void
286  {
288  using Teuchos::parameterList;
289  using Teuchos::RCP;
290 
291  RCP<const ParameterList> defaultParams = getValidParameters();
292  RCP<ParameterList> params;
293  if (plist.is_null()) {
294  // No need to validate default parameters.
295  params = parameterList (*defaultParams);
296  } else {
297  params = plist;
298  params->validateParametersAndSetDefaults (*defaultParams);
299  }
300 
301  // Using temporary variables and fetching all values before
302  // setting the output arguments ensures the strong exception
303  // guarantee for this function: if an exception is thrown, no
304  // externally visible side effects (in this case, setting the
305  // output arguments) have taken place.
306  const int maxNumOrthogPasses = params->get<int> ("maxNumOrthogPasses");
307  const MagnitudeType blkTol = params->get<MagnitudeType> ("blkTol");
308  const MagnitudeType depTol = params->get<MagnitudeType> ("depTol");
309  const MagnitudeType singTol = params->get<MagnitudeType> ("singTol");
310 
311  max_blk_ortho_ = maxNumOrthogPasses;
312  blk_tol_ = blkTol;
313  dep_tol_ = depTol;
314  sing_tol_ = singTol;
315 
316  setMyParamList (params);
317  }
318 
321  {
322  using Teuchos::as;
324  using Teuchos::parameterList;
325  using Teuchos::RCP;
326 
327  if (defaultParams_.is_null()) {
328  RCP<ParameterList> params = parameterList ("DGKS");
329  const MagnitudeType eps = MGT::eps ();
330 
331  // Default parameter values for DGKS orthogonalization.
332  // Documentation will be embedded in the parameter list.
333  const int defaultMaxNumOrthogPasses = 2;
334  const MagnitudeType defaultBlkTol =
335  as<MagnitudeType> (10) * MGT::squareroot (eps);
336  const MagnitudeType defaultDepTol =
337  MGT::one() / MGT::squareroot (as<MagnitudeType> (2));
338 
339  const MagnitudeType defaultSingTol = as<MagnitudeType> (10) * eps;
340 
341  params->set ("maxNumOrthogPasses", defaultMaxNumOrthogPasses,
342  "Maximum number of orthogonalization passes (includes the "
343  "first). Default is 2, since \"twice is enough\" for Krylov "
344  "methods.");
345  params->set ("blkTol", defaultBlkTol, "Block reorthogonalization "
346  "threshhold.");
347  params->set ("depTol", defaultDepTol,
348  "(Non-block) reorthogonalization threshold.");
349  params->set ("singTol", defaultSingTol, "Singular block detection "
350  "threshold.");
351  defaultParams_ = params;
352  }
353  return defaultParams_;
354  }
355 
357 
360  {
362  using Teuchos::RCP;
363  using Teuchos::rcp;
364 
365  RCP<const ParameterList> defaultParams = getValidParameters ();
366  // Start with a clone of the default parameters.
367  RCP<ParameterList> params = rcp (new ParameterList (*defaultParams));
368 
369  const int maxBlkOrtho = 1;
370  const MagnitudeType blkTol = MGT::zero();
371  const MagnitudeType depTol = MGT::zero();
372  const MagnitudeType singTol = MGT::zero();
373 
374  params->set ("maxNumOrthogPasses", maxBlkOrtho);
375  params->set ("blkTol", blkTol);
376  params->set ("depTol", depTol);
377  params->set ("singTol", singTol);
378 
379  return params;
380  }
381 
383 
384 
386  void setBlkTol( const MagnitudeType blk_tol ) {
387  // Update the parameter list as well.
389  if (! params.is_null()) {
390  // If it's null, then we haven't called setParameterList()
391  // yet. It's entirely possible to construct the parameter
392  // list on demand, so we don't try to create the parameter
393  // list here.
394  params->set ("blkTol", blk_tol);
395  }
396  blk_tol_ = blk_tol;
397  }
398 
400  void setDepTol( const MagnitudeType dep_tol ) {
401  // Update the parameter list as well.
403  if (! params.is_null()) {
404  params->set ("depTol", dep_tol);
405  }
406  dep_tol_ = dep_tol;
407  }
408 
410  void setSingTol( const MagnitudeType sing_tol ) {
411  // Update the parameter list as well.
413  if (! params.is_null()) {
414  params->set ("singTol", sing_tol);
415  }
416  sing_tol_ = sing_tol;
417  }
418 
420  MagnitudeType getBlkTol() const { return blk_tol_; }
421 
423  MagnitudeType getDepTol() const { return dep_tol_; }
424 
426  MagnitudeType getSingTol() const { return sing_tol_; }
427 
429 
430 
432 
433 
461  void project ( MV &X, Teuchos::RCP<MV> MX,
464 
465 
468  void project ( MV &X,
471  project(X,Teuchos::null,C,Q);
472  }
473 
474 
475 
500  int normalize ( MV &X, Teuchos::RCP<MV> MX,
502 
503 
507  return normalize(X,Teuchos::null,B);
508  }
509 
510 
566  int projectAndNormalize ( MV &X, Teuchos::RCP<MV> MX,
570 
573  int projectAndNormalize ( MV &X,
577  return projectAndNormalize(X,Teuchos::null,C,B,Q);
578  }
579 
581 
583 
584 
591  orthonormError(const MV &X) const {
592  return orthonormError(X,Teuchos::null);
593  }
594 
602  orthonormError(const MV &X, Teuchos::RCP<const MV> MX) const;
603 
608  orthogError(const MV &X1, const MV &X2) const {
609  return orthogError(X1,Teuchos::null,X2);
610  }
611 
617  orthogError(const MV &X1, Teuchos::RCP<const MV> MX1, const MV &X2) const;
618 
620 
622 
623 
626  void setLabel(const std::string& label);
627 
630  const std::string& getLabel() const { return label_; }
631 
633 
634  private:
635 
637  int max_blk_ortho_;
639  MagnitudeType blk_tol_;
641  MagnitudeType dep_tol_;
643  MagnitudeType sing_tol_;
644 
646  std::string label_;
647 #ifdef BELOS_TEUCHOS_TIME_MONITOR
648  Teuchos::RCP<Teuchos::Time> timerOrtho_;
649 #endif // BELOS_TEUCHOS_TIME_MONITOR
650 
652  mutable Teuchos::RCP<Teuchos::ParameterList> defaultParams_;
653 
655  int findBasis(MV &X, Teuchos::RCP<MV> MX,
657  bool completeBasis, int howMany = -1 ) const;
658 
660  bool blkOrtho ( MV &X, Teuchos::RCP<MV> MX,
663 
677  int blkOrthoSing ( MV &X, Teuchos::RCP<MV> MX,
681  };
682 
684  // Set the label for this orthogonalization manager and create new timers if it's changed
685  template<class ScalarType, class MV, class OP>
686  void DGKSOrthoManager<ScalarType,MV,OP>::setLabel(const std::string& label)
687  {
688  if (label != label_) {
689  label_ = label;
690  std::string orthoLabel = label_ + ": Orthogonalization";
691 #ifdef BELOS_TEUCHOS_TIME_MONITOR
692  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter(orthoLabel);
693 #endif
694  }
695  }
696 
698  // Compute the distance from orthonormality
699  template<class ScalarType, class MV, class OP>
702  const ScalarType ONE = SCT::one();
703  int rank = MVT::GetNumberVecs(X);
706  for (int i=0; i<rank; i++) {
707  xTx(i,i) -= ONE;
708  }
709  return xTx.normFrobenius();
710  }
711 
713  // Compute the distance from orthogonality
714  template<class ScalarType, class MV, class OP>
717  int r1 = MVT::GetNumberVecs(X1);
718  int r2 = MVT::GetNumberVecs(X2);
721  return xTx.normFrobenius();
722  }
723 
725  // Find an Op-orthonormal basis for span(X) - span(W)
726  template<class ScalarType, class MV, class OP>
728  MV &X, Teuchos::RCP<MV> MX,
732  {
733  using Teuchos::Array;
734  using Teuchos::null;
735  using Teuchos::is_null;
736  using Teuchos::RCP;
737  using Teuchos::rcp;
739  typedef SerialDenseMatrix< int, ScalarType > serial_dense_matrix_type;
740  typedef typename Array< RCP< const MV > >::size_type size_type;
741 
742 #ifdef BELOS_TEUCHOS_TIME_MONITOR
743  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
744 #endif
745 
746  ScalarType ONE = SCT::one();
747  ScalarType ZERO = SCT::zero();
748 
749  int nq = Q.size();
750  int xc = MVT::GetNumberVecs( X );
751  ptrdiff_t xr = MVText::GetGlobalLength( X );
752  int rank = xc;
753 
754  // If the user doesn't want to store the normalization
755  // coefficients, allocate some local memory for them. This will
756  // go away at the end of this method.
757  if (is_null (B)) {
758  B = rcp (new serial_dense_matrix_type (xc, xc));
759  }
760  // Likewise, if the user doesn't want to store the projection
761  // coefficients, allocate some local memory for them. Also make
762  // sure that all the entries of C are the right size. We're going
763  // to overwrite them anyway, so we don't have to worry about the
764  // contents (other than to resize them if they are the wrong
765  // size).
766  if (C.size() < nq)
767  C.resize (nq);
768  for (size_type k = 0; k < nq; ++k)
769  {
770  const int numRows = MVT::GetNumberVecs (*Q[k]);
771  const int numCols = xc; // Number of vectors in X
772 
773  if (is_null (C[k]))
774  C[k] = rcp (new serial_dense_matrix_type (numRows, numCols));
775  else if (C[k]->numRows() != numRows || C[k]->numCols() != numCols)
776  {
777  int err = C[k]->reshape (numRows, numCols);
778  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error,
779  "DGKS orthogonalization: failed to reshape "
780  "C[" << k << "] (the array of block "
781  "coefficients resulting from projecting X "
782  "against Q[1:" << nq << "]).");
783  }
784  }
785 
786  /****** DO NO MODIFY *MX IF _hasOp == false ******/
787  if (this->_hasOp) {
788  if (MX == Teuchos::null) {
789  // we need to allocate space for MX
790  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
791  OPT::Apply(*(this->_Op),X,*MX);
792  }
793  }
794  else {
795  // Op == I --> MX = X (ignore it if the user passed it in)
796  MX = Teuchos::rcp( &X, false );
797  }
798 
799  int mxc = MVT::GetNumberVecs( *MX );
800  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
801 
802  // short-circuit
803  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument, "Belos::DGKSOrthoManager::projectAndNormalize(): X must be non-empty" );
804 
805  int numbas = 0;
806  for (int i=0; i<nq; i++) {
807  numbas += MVT::GetNumberVecs( *Q[i] );
808  }
809 
810  // check size of B
811  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
812  "Belos::DGKSOrthoManager::projectAndNormalize(): Size of X must be consistant with size of B" );
813  // check size of X and MX
814  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
815  "Belos::DGKSOrthoManager::projectAndNormalize(): MVT returned negative dimensions for X,MX" );
816  // check size of X w.r.t. MX
817  TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr, std::invalid_argument,
818  "Belos::DGKSOrthoManager::projectAndNormalize(): Size of X must be consistant with size of MX" );
819  // check feasibility
820  //TEUCHOS_TEST_FOR_EXCEPTION( numbas+xc > xr, std::invalid_argument,
821  // "Belos::DGKSOrthoManager::projectAndNormalize(): Orthogonality constraints not feasible" );
822 
823  // Some flags for checking dependency returns from the internal orthogonalization methods
824  bool dep_flg = false;
825 
826  // Make a temporary copy of X and MX, just in case a block dependency is detected.
827  Teuchos::RCP<MV> tmpX, tmpMX;
828  tmpX = MVT::CloneCopy(X);
829  if (this->_hasOp) {
830  tmpMX = MVT::CloneCopy(*MX);
831  }
832 
833  // Use the cheaper block orthogonalization.
834  dep_flg = blkOrtho( X, MX, C, Q );
835 
836  // If a dependency has been detected in this block, then perform
837  // the more expensive single-vector orthogonalization.
838  if (dep_flg) {
839  rank = blkOrthoSing( *tmpX, tmpMX, C, B, Q );
840 
841  // Copy tmpX back into X.
842  MVT::MvAddMv( ONE, *tmpX, ZERO, *tmpX, X );
843  if (this->_hasOp) {
844  MVT::MvAddMv( ONE, *tmpMX, ZERO, *tmpMX, *MX );
845  }
846  }
847  else {
848  // There is no dependency, so orthonormalize new block X
849  rank = findBasis( X, MX, B, false );
850  if (rank < xc) {
851  // A dependency was found during orthonormalization of X,
852  // rerun orthogonalization using more expensive single-vector orthogonalization.
853  rank = blkOrthoSing( *tmpX, tmpMX, C, B, Q );
854 
855  // Copy tmpX back into X.
856  MVT::MvAddMv( ONE, *tmpX, ZERO, *tmpX, X );
857  if (this->_hasOp) {
858  MVT::MvAddMv( ONE, *tmpMX, ZERO, *tmpMX, *MX );
859  }
860  }
861  }
862 
863  // this should not raise an std::exception; but our post-conditions oblige us to check
864  TEUCHOS_TEST_FOR_EXCEPTION( rank > xc || rank < 0, std::logic_error,
865  "Belos::DGKSOrthoManager::projectAndNormalize(): Debug error in rank variable." );
866 
867  // Return the rank of X.
868  return rank;
869  }
870 
871 
872 
874  // Find an Op-orthonormal basis for span(X), with rank numvectors(X)
875  template<class ScalarType, class MV, class OP>
877  MV &X, Teuchos::RCP<MV> MX,
879 
880 #ifdef BELOS_TEUCHOS_TIME_MONITOR
881  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
882 #endif
883 
884  // call findBasis, with the instruction to try to generate a basis of rank numvecs(X)
885  return findBasis(X, MX, B, true);
886  }
887 
888 
889 
891  template<class ScalarType, class MV, class OP>
893  MV &X, Teuchos::RCP<MV> MX,
896  // For the inner product defined by the operator Op or the identity (Op == 0)
897  // -> Orthogonalize X against each Q[i]
898  // Modify MX accordingly
899  //
900  // Note that when Op is 0, MX is not referenced
901  //
902  // Parameter variables
903  //
904  // X : Vectors to be transformed
905  //
906  // MX : Image of the block vector X by the mass matrix
907  //
908  // Q : Bases to orthogonalize against. These are assumed orthonormal, mutually and independently.
909  //
910 
911 #ifdef BELOS_TEUCHOS_TIME_MONITOR
912  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
913 #endif
914 
915  int xc = MVT::GetNumberVecs( X );
916  ptrdiff_t xr = MVText::GetGlobalLength( X );
917  int nq = Q.size();
918  std::vector<int> qcs(nq);
919  // short-circuit
920  if (nq == 0 || xc == 0 || xr == 0) {
921  return;
922  }
923  ptrdiff_t qr = MVText::GetGlobalLength ( *Q[0] );
924  // if we don't have enough C, expand it with null references
925  // if we have too many, resize to throw away the latter ones
926  // if we have exactly as many as we have Q, this call has no effect
927  C.resize(nq);
928 
929 
930  /****** DO NO MODIFY *MX IF _hasOp == false ******/
931  if (this->_hasOp) {
932  if (MX == Teuchos::null) {
933  // we need to allocate space for MX
934  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
935  OPT::Apply(*(this->_Op),X,*MX);
936  }
937  }
938  else {
939  // Op == I --> MX = X (ignore it if the user passed it in)
940  MX = Teuchos::rcp( &X, false );
941  }
942  int mxc = MVT::GetNumberVecs( *MX );
943  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
944 
945  // check size of X and Q w.r.t. common sense
946  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
947  "Belos::DGKSOrthoManager::project(): MVT returned negative dimensions for X,MX" );
948  // check size of X w.r.t. MX and Q
949  TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr || xr!=qr, std::invalid_argument,
950  "Belos::DGKSOrthoManager::project(): Size of X not consistant with MX,Q" );
951 
952  // tally up size of all Q and check/allocate C
953  int baslen = 0;
954  for (int i=0; i<nq; i++) {
955  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength( *Q[i] ) != qr, std::invalid_argument,
956  "Belos::DGKSOrthoManager::project(): Q lengths not mutually consistant" );
957  qcs[i] = MVT::GetNumberVecs( *Q[i] );
958  TEUCHOS_TEST_FOR_EXCEPTION( qr < qcs[i], std::invalid_argument,
959  "Belos::DGKSOrthoManager::project(): Q has less rows than columns" );
960  baslen += qcs[i];
961 
962  // check size of C[i]
963  if ( C[i] == Teuchos::null ) {
965  }
966  else {
967  TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != qcs[i] || C[i]->numCols() != xc , std::invalid_argument,
968  "Belos::DGKSOrthoManager::project(): Size of Q not consistant with size of C" );
969  }
970  }
971 
972  // Use the cheaper block orthogonalization, don't check for rank deficiency.
973  blkOrtho( X, MX, C, Q );
974 
975  }
976 
978  // Find an Op-orthonormal basis for span(X), with the option of extending the subspace so that
979  // the rank is numvectors(X)
980  template<class ScalarType, class MV, class OP>
982  MV &X, Teuchos::RCP<MV> MX,
984  bool completeBasis, int howMany ) const {
985  // For the inner product defined by the operator Op or the identity (Op == 0)
986  // -> Orthonormalize X
987  // Modify MX accordingly
988  //
989  // Note that when Op is 0, MX is not referenced
990  //
991  // Parameter variables
992  //
993  // X : Vectors to be orthonormalized
994  //
995  // MX : Image of the multivector X under the operator Op
996  //
997  // Op : Pointer to the operator for the inner product
998  //
999  //
1000 
1001  const ScalarType ONE = SCT::one();
1002  const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
1003 
1004  int xc = MVT::GetNumberVecs( X );
1005  ptrdiff_t xr = MVText::GetGlobalLength( X );
1006 
1007  if (howMany == -1) {
1008  howMany = xc;
1009  }
1010 
1011  /*******************************************************
1012  * If _hasOp == false, we will not reference MX below *
1013  *******************************************************/
1014 
1015  // if Op==null, MX == X (via pointer)
1016  // Otherwise, either the user passed in MX or we will allocated and compute it
1017  if (this->_hasOp) {
1018  if (MX == Teuchos::null) {
1019  // we need to allocate space for MX
1020  MX = MVT::Clone(X,xc);
1021  OPT::Apply(*(this->_Op),X,*MX);
1022  }
1023  }
1024 
1025  /* if the user doesn't want to store the coefficienets,
1026  * allocate some local memory for them
1027  */
1028  if ( B == Teuchos::null ) {
1030  }
1031 
1032  int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
1033  ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
1034 
1035  // check size of C, B
1036  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
1037  "Belos::DGKSOrthoManager::findBasis(): X must be non-empty" );
1038  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
1039  "Belos::DGKSOrthoManager::findBasis(): Size of X not consistant with size of B" );
1040  TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
1041  "Belos::DGKSOrthoManager::findBasis(): Size of X not consistant with size of MX" );
1042  TEUCHOS_TEST_FOR_EXCEPTION( static_cast<ptrdiff_t>(xc) > xr, std::invalid_argument,
1043  "Belos::DGKSOrthoManager::findBasis(): Size of X not feasible for normalization" );
1044  TEUCHOS_TEST_FOR_EXCEPTION( howMany < 0 || howMany > xc, std::invalid_argument,
1045  "Belos::DGKSOrthoManager::findBasis(): Invalid howMany parameter" );
1046 
1047  /* xstart is which column we are starting the process with, based on howMany
1048  * columns before xstart are assumed to be Op-orthonormal already
1049  */
1050  int xstart = xc - howMany;
1051 
1052  for (int j = xstart; j < xc; j++) {
1053 
1054  // numX is
1055  // * number of currently orthonormal columns of X
1056  // * the index of the current column of X
1057  int numX = j;
1058  bool addVec = false;
1059 
1060  // Get a view of the vector currently being worked on.
1061  std::vector<int> index(1);
1062  index[0] = numX;
1063  Teuchos::RCP<MV> Xj = MVT::CloneViewNonConst( X, index );
1064  Teuchos::RCP<MV> MXj;
1065  if ((this->_hasOp)) {
1066  // MXj is a view of the current vector in MX
1067  MXj = MVT::CloneViewNonConst( *MX, index );
1068  }
1069  else {
1070  // MXj is a pointer to Xj, and MUST NOT be modified
1071  MXj = Xj;
1072  }
1073 
1074  // Get a view of the previous vectors.
1075  std::vector<int> prev_idx( numX );
1076  Teuchos::RCP<const MV> prevX, prevMX;
1077 
1078  if (numX > 0) {
1079  for (int i=0; i<numX; i++) {
1080  prev_idx[i] = i;
1081  }
1082  prevX = MVT::CloneView( X, prev_idx );
1083  if (this->_hasOp) {
1084  prevMX = MVT::CloneView( *MX, prev_idx );
1085  }
1086  }
1087 
1088  // Make storage for these Gram-Schmidt iterations.
1090  std::vector<ScalarType> oldDot( 1 ), newDot( 1 );
1091  //
1092  // Save old MXj vector and compute Op-norm
1093  //
1094  Teuchos::RCP<MV> oldMXj = MVT::CloneCopy( *MXj );
1095  MVT::MvDot( *Xj, *MXj, oldDot );
1096  // Xj^H Op Xj should be real and positive, by the hermitian positive definiteness of Op
1097  TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(oldDot[0]) < ZERO, OrthoError,
1098  "Belos::DGKSOrthoManager::findBasis(): Negative definiteness discovered in inner product" );
1099 
1100  if (numX > 0) {
1101  // Apply the first step of Gram-Schmidt
1102 
1103  // product <- prevX^T MXj
1104  MatOrthoManager<ScalarType,MV,OP>::innerProd(*prevX,*Xj,MXj,product);
1105 
1106  // Xj <- Xj - prevX prevX^T MXj
1107  // = Xj - prevX product
1108  MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
1109 
1110  // Update MXj
1111  if (this->_hasOp) {
1112  // MXj <- Op*Xj_new
1113  // = Op*(Xj_old - prevX prevX^T MXj)
1114  // = MXj - prevMX product
1115  MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
1116  }
1117 
1118  // Compute new Op-norm
1119  MVT::MvDot( *Xj, *MXj, newDot );
1120 
1121  // Check if a correction is needed.
1122  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(dep_tol_*oldDot[0]) ) {
1123  // Apply the second step of Gram-Schmidt
1124  // This is the same as above
1126 
1128  product += P2;
1129  MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
1130  if ((this->_hasOp)) {
1131  MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
1132  }
1133  } // if (newDot[0] < dep_tol_*oldDot[0])
1134 
1135  } // if (numX > 0)
1136 
1137  // Compute Op-norm with old MXj
1138  MVT::MvDot( *Xj, *oldMXj, newDot );
1139 
1140  // Check to see if the new vector is dependent.
1141  if (completeBasis) {
1142  //
1143  // We need a complete basis, so add random vectors if necessary
1144  //
1145  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(sing_tol_*oldDot[0]) ) {
1146 
1147  // Add a random vector and orthogonalize it against previous vectors in block.
1148  addVec = true;
1149 #ifdef ORTHO_DEBUG
1150  std::cout << "Belos::DGKSOrthoManager::findBasis() --> Random for column " << numX << std::endl;
1151 #endif
1152  //
1153  Teuchos::RCP<MV> tempXj = MVT::Clone( X, 1 );
1154  Teuchos::RCP<MV> tempMXj;
1155  MVT::MvRandom( *tempXj );
1156  if (this->_hasOp) {
1157  tempMXj = MVT::Clone( X, 1 );
1158  OPT::Apply( *(this->_Op), *tempXj, *tempMXj );
1159  }
1160  else {
1161  tempMXj = tempXj;
1162  }
1163  MVT::MvDot( *tempXj, *tempMXj, oldDot );
1164  //
1165  for (int num_orth=0; num_orth<max_blk_ortho_; num_orth++){
1166  MatOrthoManager<ScalarType,MV,OP>::innerProd(*prevX,*tempXj,tempMXj,product);
1167  MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *tempXj );
1168  if (this->_hasOp) {
1169  MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *tempMXj );
1170  }
1171  }
1172  // Compute new Op-norm
1173  MVT::MvDot( *tempXj, *tempMXj, newDot );
1174  //
1175  if ( SCT::magnitude(newDot[0]) >= SCT::magnitude(oldDot[0]*sing_tol_) ) {
1176  // Copy vector into current column of _basisvecs
1177  MVT::MvAddMv( ONE, *tempXj, ZERO, *tempXj, *Xj );
1178  if (this->_hasOp) {
1179  MVT::MvAddMv( ONE, *tempMXj, ZERO, *tempMXj, *MXj );
1180  }
1181  }
1182  else {
1183  return numX;
1184  }
1185  }
1186  }
1187  else {
1188  //
1189  // We only need to detect dependencies.
1190  //
1191  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(oldDot[0]*blk_tol_) ) {
1192  return numX;
1193  }
1194  }
1195 
1196  // If we haven't left this method yet, then we can normalize the new vector Xj.
1197  // Normalize Xj.
1198  // Xj <- Xj / std::sqrt(newDot)
1199  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1200 
1201  if (SCT::magnitude(diag) > ZERO) {
1202  MVT::MvAddMv( ONE/diag, *Xj, ZERO, *Xj, *Xj );
1203  if (this->_hasOp) {
1204  // Update MXj.
1205  MVT::MvAddMv( ONE/diag, *MXj, ZERO, *MXj, *MXj );
1206  }
1207  }
1208 
1209  // If we've added a random vector, enter a zero in the j'th diagonal element.
1210  if (addVec) {
1211  (*B)(j,j) = ZERO;
1212  }
1213  else {
1214  (*B)(j,j) = diag;
1215  }
1216 
1217  // Save the coefficients, if we are working on the original vector and not a randomly generated one
1218  if (!addVec) {
1219  for (int i=0; i<numX; i++) {
1220  (*B)(i,j) = product(i,0);
1221  }
1222  }
1223 
1224  } // for (j = 0; j < xc; ++j)
1225 
1226  return xc;
1227  }
1228 
1230  // Routine to compute the block orthogonalization
1231  template<class ScalarType, class MV, class OP>
1232  bool
1233  DGKSOrthoManager<ScalarType, MV, OP>::blkOrtho ( MV &X, Teuchos::RCP<MV> MX,
1236  {
1237  int nq = Q.size();
1238  int xc = MVT::GetNumberVecs( X );
1239  bool dep_flg = false;
1240  const ScalarType ONE = SCT::one();
1241 
1242  std::vector<int> qcs( nq );
1243  for (int i=0; i<nq; i++) {
1244  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1245  }
1246 
1247  // Perform the Gram-Schmidt transformation for a block of vectors
1248 
1249  // Compute the initial Op-norms
1250  std::vector<ScalarType> oldDot( xc );
1251  MVT::MvDot( X, *MX, oldDot );
1252 
1254  // Define the product Q^T * (Op*X)
1255  for (int i=0; i<nq; i++) {
1256  // Multiply Q' with MX
1258  // Multiply by Q and subtract the result in X
1259  MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
1260 
1261  // Update MX, with the least number of applications of Op as possible
1262  if (this->_hasOp) {
1263  if (xc <= qcs[i]) {
1264  OPT::Apply( *(this->_Op), X, *MX);
1265  }
1266  else {
1267  // this will possibly be used again below; don't delete it
1268  MQ[i] = MVT::Clone( *Q[i], qcs[i] );
1269  OPT::Apply( *(this->_Op), *Q[i], *MQ[i] );
1270  MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
1271  }
1272  }
1273  }
1274 
1275  // Do as many steps of classical Gram-Schmidt as required by max_blk_ortho_
1276  for (int j = 1; j < max_blk_ortho_; ++j) {
1277 
1278  for (int i=0; i<nq; i++) {
1280 
1281  // Apply another step of classical Gram-Schmidt
1283  *C[i] += C2;
1284  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
1285 
1286  // Update MX, with the least number of applications of Op as possible
1287  if (this->_hasOp) {
1288  if (MQ[i].get()) {
1289  // MQ was allocated and computed above; use it
1290  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
1291  }
1292  else if (xc <= qcs[i]) {
1293  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1294  OPT::Apply( *(this->_Op), X, *MX);
1295  }
1296  }
1297  } // for (int i=0; i<nq; i++)
1298  } // for (int j = 0; j < max_blk_ortho; ++j)
1299 
1300  // Compute new Op-norms
1301  std::vector<ScalarType> newDot(xc);
1302  MVT::MvDot( X, *MX, newDot );
1303 
1304  // Check to make sure the new block of vectors are not dependent on previous vectors
1305  for (int i=0; i<xc; i++){
1306  if (SCT::magnitude(newDot[i]) < SCT::magnitude(oldDot[i] * blk_tol_)) {
1307  dep_flg = true;
1308  break;
1309  }
1310  } // end for (i=0;...)
1311 
1312  return dep_flg;
1313  }
1314 
1315 
1316  template<class ScalarType, class MV, class OP>
1317  int
1318  DGKSOrthoManager<ScalarType, MV, OP>::blkOrthoSing ( MV &X, Teuchos::RCP<MV> MX,
1322  {
1324 
1325  const ScalarType ONE = SCT::one();
1326  const ScalarType ZERO = SCT::zero();
1327 
1328  int nq = Q.size();
1329  int xc = MVT::GetNumberVecs( X );
1330  std::vector<int> indX( 1 );
1331  std::vector<ScalarType> oldDot( 1 ), newDot( 1 );
1332 
1333  std::vector<int> qcs( nq );
1334  for (int i=0; i<nq; i++) {
1335  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1336  }
1337 
1338  // Create pointers for the previous vectors of X that have already been orthonormalized.
1339  Teuchos::RCP<const MV> lastQ;
1340  Teuchos::RCP<MV> Xj, MXj;
1342 
1343  // Perform the Gram-Schmidt transformation for each vector in the block of vectors.
1344  for (int j=0; j<xc; j++) {
1345 
1346  bool dep_flg = false;
1347 
1348  // Get a view of the previously orthogonalized vectors and B, add it to the arrays.
1349  if (j > 0) {
1350  std::vector<int> index( j );
1351  for (int ind=0; ind<j; ind++) {
1352  index[ind] = ind;
1353  }
1354  lastQ = MVT::CloneView( X, index );
1355 
1356  // Add these views to the Q and C arrays.
1357  Q.push_back( lastQ );
1358  C.push_back( B );
1359  qcs.push_back( MVT::GetNumberVecs( *lastQ ) );
1360  }
1361 
1362  // Get a view of the current vector in X to orthogonalize.
1363  indX[0] = j;
1364  Xj = MVT::CloneViewNonConst( X, indX );
1365  if (this->_hasOp) {
1366  MXj = MVT::CloneViewNonConst( *MX, indX );
1367  }
1368  else {
1369  MXj = Xj;
1370  }
1371 
1372  // Compute the initial Op-norms
1373  MVT::MvDot( *Xj, *MXj, oldDot );
1374 
1375  Teuchos::Array<Teuchos::RCP<MV> > MQ(Q.size());
1376  // Define the product Q^T * (Op*X)
1377  for (int i=0; i<Q.size(); i++) {
1378 
1379  // Get a view of the current serial dense matrix
1380  Teuchos::SerialDenseMatrix<int,ScalarType> tempC( Teuchos::View, *C[i], qcs[i], 1, 0, j );
1381 
1382  // Multiply Q' with MX
1383  MatOrthoManager<ScalarType,MV,OP>::innerProd(*Q[i],*Xj,MXj,tempC);
1384  // Multiply by Q and subtract the result in Xj
1385  MVT::MvTimesMatAddMv( -ONE, *Q[i], tempC, ONE, *Xj );
1386 
1387  // Update MXj, with the least number of applications of Op as possible
1388  if (this->_hasOp) {
1389  if (xc <= qcs[i]) {
1390  OPT::Apply( *(this->_Op), *Xj, *MXj);
1391  }
1392  else {
1393  // this will possibly be used again below; don't delete it
1394  MQ[i] = MVT::Clone( *Q[i], qcs[i] );
1395  OPT::Apply( *(this->_Op), *Q[i], *MQ[i] );
1396  MVT::MvTimesMatAddMv( -ONE, *MQ[i], tempC, ONE, *MXj );
1397  }
1398  }
1399  }
1400 
1401  // Compute the Op-norms
1402  MVT::MvDot( *Xj, *MXj, newDot );
1403 
1404  // Do one step of classical Gram-Schmidt orthogonalization
1405  // with a second correction step if needed.
1406 
1407  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(oldDot[0]*dep_tol_) ) {
1408 
1409  for (int i=0; i<Q.size(); i++) {
1410  Teuchos::SerialDenseMatrix<int,ScalarType> tempC( Teuchos::View, *C[i], qcs[i], 1, 0, j );
1412 
1413  // Apply another step of classical Gram-Schmidt
1415  tempC += C2;
1416  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, *Xj );
1417 
1418  // Update MXj, with the least number of applications of Op as possible
1419  if (this->_hasOp) {
1420  if (MQ[i].get()) {
1421  // MQ was allocated and computed above; use it
1422  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MXj );
1423  }
1424  else if (xc <= qcs[i]) {
1425  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1426  OPT::Apply( *(this->_Op), *Xj, *MXj);
1427  }
1428  }
1429  } // for (int i=0; i<Q.size(); i++)
1430 
1431  // Compute the Op-norms after the correction step.
1432  MVT::MvDot( *Xj, *MXj, newDot );
1433 
1434  } // if ()
1435 
1436  // Check for linear dependence.
1437  if (SCT::magnitude(newDot[0]) < SCT::magnitude(oldDot[0]*sing_tol_)) {
1438  dep_flg = true;
1439  }
1440 
1441  // Normalize the new vector if it's not dependent
1442  if (!dep_flg) {
1443  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1444 
1445  MVT::MvAddMv( ONE/diag, *Xj, ZERO, *Xj, *Xj );
1446  if (this->_hasOp) {
1447  // Update MXj.
1448  MVT::MvAddMv( ONE/diag, *MXj, ZERO, *MXj, *MXj );
1449  }
1450 
1451  // Enter value on diagonal of B.
1452  (*B)(j,j) = diag;
1453  }
1454  else {
1455  // Create a random vector and orthogonalize it against all previous columns of Q.
1456  Teuchos::RCP<MV> tempXj = MVT::Clone( X, 1 );
1457  Teuchos::RCP<MV> tempMXj;
1458  MVT::MvRandom( *tempXj );
1459  if (this->_hasOp) {
1460  tempMXj = MVT::Clone( X, 1 );
1461  OPT::Apply( *(this->_Op), *tempXj, *tempMXj );
1462  }
1463  else {
1464  tempMXj = tempXj;
1465  }
1466  MVT::MvDot( *tempXj, *tempMXj, oldDot );
1467  //
1468  for (int num_orth=0; num_orth<max_blk_ortho_; num_orth++) {
1469 
1470  for (int i=0; i<Q.size(); i++) {
1471  Teuchos::SerialDenseMatrix<int,ScalarType> product( qcs[i], 1 );
1472 
1473  // Apply another step of classical Gram-Schmidt
1474  MatOrthoManager<ScalarType,MV,OP>::innerProd(*Q[i],*tempXj,tempMXj,product);
1475  MVT::MvTimesMatAddMv( -ONE, *Q[i], product, ONE, *tempXj );
1476 
1477  // Update MXj, with the least number of applications of Op as possible
1478  if (this->_hasOp) {
1479  if (MQ[i].get()) {
1480  // MQ was allocated and computed above; use it
1481  MVT::MvTimesMatAddMv( -ONE, *MQ[i], product, ONE, *tempMXj );
1482  }
1483  else if (xc <= qcs[i]) {
1484  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1485  OPT::Apply( *(this->_Op), *tempXj, *tempMXj);
1486  }
1487  }
1488  } // for (int i=0; i<nq; i++)
1489 
1490  }
1491 
1492  // Compute the Op-norms after the correction step.
1493  MVT::MvDot( *tempXj, *tempMXj, newDot );
1494 
1495  // Copy vector into current column of Xj
1496  if ( SCT::magnitude(newDot[0]) >= SCT::magnitude(oldDot[0]*sing_tol_) ) {
1497  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1498 
1499  // Enter value on diagonal of B.
1500  (*B)(j,j) = ZERO;
1501 
1502  // Copy vector into current column of _basisvecs
1503  MVT::MvAddMv( ONE/diag, *tempXj, ZERO, *tempXj, *Xj );
1504  if (this->_hasOp) {
1505  MVT::MvAddMv( ONE/diag, *tempMXj, ZERO, *tempMXj, *MXj );
1506  }
1507  }
1508  else {
1509  return j;
1510  }
1511  } // if (!dep_flg)
1512 
1513  // Remove the vectors from array
1514  if (j > 0) {
1515  Q.resize( nq );
1516  C.resize( nq );
1517  qcs.resize( nq );
1518  }
1519 
1520  } // for (int j=0; j<xc; j++)
1521 
1522  return xc;
1523  }
1524 
1525 } // namespace Belos
1526 
1527 #endif // BELOS_DGKS_ORTHOMANAGER_HPP
1528 
void setSingTol(const MagnitudeType sing_tol)
Set parameter for singular block detection.
bool is_null(const boost::shared_ptr< T > &p)
int normalize(MV &X, Teuchos::RCP< MV > MX, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B) const
This method takes a multivector X and attempts to compute an orthonormal basis for ...
static magnitudeType eps()
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.
void setBlkTol(const MagnitudeType blk_tol)
Set parameter for block re-orthogonalization threshhold.
T & get(const std::string &name, T def_value)
void setDepTol(const MagnitudeType dep_tol)
Set parameter for re-orthogonalization threshhold.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static RCP< Time > getNewCounter(const std::string &name)
static T squareroot(T x)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Declaration of basic traits for the multivector type.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
DGKSOrthoManager(const Teuchos::RCP< Teuchos::ParameterList > &plist, const std::string &label="Belos", Teuchos::RCP< const OP > Op=Teuchos::null)
Constructor that takes a list of parameters.
An extension of the MultiVecTraits class that adds a new vector length method.
Class which defines basic traits for the operator type.
void setLabel(const std::string &label)
This method sets the label used by the timers in the orthogonalization manager.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Traits class which defines basic operations on multivectors.
int projectAndNormalize(MV &X, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
This method calls projectAndNormalize(X,Teuchos::null,C,B,Q); see documentation for that function...
void innerProd(const MV &X, const MV &Y, Teuchos::SerialDenseMatrix< int, ScalarType > &Z) const
Provides the inner product defining the orthogonality concepts, using the provided operator...
MagnitudeType getDepTol() const
Return parameter for re-orthogonalization threshhold.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const Teuchos::ParameterList > TEUCHOS_DEPRECATED getDefaultDgksParameters()
Default parameters for DGKSOrthoManager.
void setMyParamList(const RCP< ParameterList > &paramList)
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
void project(MV &X, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
This method calls project(X,Teuchos::null,C,Q); see documentation for that function.
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors, measured as the Frobenius norm o...
RCP< ParameterList > getNonconstParameterList()
DGKSOrthoManager(const std::string &label="Belos", Teuchos::RCP< const OP > Op=Teuchos::null, const int max_blk_ortho=2, const MagnitudeType blk_tol=10 *MGT::squareroot(MGT::eps()), const MagnitudeType dep_tol=MGT::one()/MGT::squareroot(2 *MGT::one()), const MagnitudeType sing_tol=10 *MGT::eps())
Constructor specifying re-orthogonalization tolerance.
void project(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Given a list of (mutually and internally) orthonormal bases Q, this method takes a multivector X and ...
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
Teuchos::RCP< const Teuchos::ParameterList > TEUCHOS_DEPRECATED getFastDgksParameters()
&quot;Fast&quot; parameters for DGKSOrthoManager
TypeTo as(const TypeFrom &t)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
An implementation of the Belos::MatOrthoManager that performs orthogonalization using (potentially) m...
Class which defines basic traits for the operator type.
int normalize(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B) const
This method calls normalize(X,Teuchos::null,B); see documentation for that function.
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
MagnitudeType getBlkTol() const
Return parameter for block re-orthogonalization threshhold.
Belos header file which uses auto-configuration information to include necessary C++ headers...
int projectAndNormalize(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Given a set of bases Q[i] and a multivector X, this method computes an orthonormal basis for ...
MagnitudeType getSingTol() const
Return parameter for singular block detection.
void TEUCHOS_DEPRECATED readDgksParameters(const Teuchos::RCP< const Teuchos::ParameterList > &params, int &maxNumOrthogPasses, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType &blkTol, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType &depTol, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType &singTol)
Read DGKS options from the given parameter list.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters() const
bool is_null() const