Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosICGSOrthoManager.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_ICGS_ORTHOMANAGER_HPP
48 #define BELOS_ICGS_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 ICGS orthogonalization.
85  // Documentation will be embedded in the parameter list.
86  const int defaultMaxNumOrthogPasses = 2;
87  const magnitude_type eps = STM::eps();
88  const magnitude_type defaultBlkTol =
89  as<magnitude_type> (10) * STM::squareroot (eps);
90  const magnitude_type defaultSingTol = as<magnitude_type> (10) * eps;
91 
93  Teuchos::parameterList ("ICGS");
94  params->set ("maxNumOrthogPasses", defaultMaxNumOrthogPasses,
95  "Maximum number of orthogonalization passes "
96  "(includes the first). Default is 2, since "
97  "\"twice is enough\" for Krylov methods.");
98  params->set ("blkTol", defaultBlkTol,
99  "Block reorthogonalization threshhold.");
100  params->set ("singTol", defaultSingTol,
101  "Singular block detection threshold.");
102  return params;
103  }
104 
109  template<class ScalarType>
112  {
114  using Teuchos::RCP;
115  using Teuchos::rcp;
116  using Teuchos::ScalarTraits;
117  typedef typename ScalarTraits<ScalarType>::magnitudeType magnitude_type;
118  typedef ScalarTraits<magnitude_type> STM;
119 
120  RCP<const ParameterList> defaultParams = getDefaultIcgsParameters<ScalarType>();
121  // Start with a clone of the default parameters.
122  RCP<ParameterList> params = rcp (new ParameterList (*defaultParams));
123 
124  const int maxBlkOrtho = 1;
125  const magnitude_type blkTol = STM::zero();
126  const magnitude_type singTol = STM::zero();
127 
128  params->set ("maxNumOrthogPasses", maxBlkOrtho);
129  params->set ("blkTol", blkTol);
130  params->set ("singTol", singTol);
131 
132  return params;
133  }
134 
144  template<class ScalarType>
145  void TEUCHOS_DEPRECATED
147  int& maxNumOrthogPasses,
150  {
152  using Teuchos::RCP;
153  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType magnitude_type;
154  const magnitude_type zero = Teuchos::ScalarTraits<magnitude_type>::zero();
155  RCP<const ParameterList> defaultParams = getDefaultIcgsParameters<ScalarType>();
156 
157  // Using temporary variables and fetching all values before
158  // setting the output arguments ensures the strong exception
159  // guarantee for this function: if an exception is thrown, no
160  // externally visible side effects (in this case, setting the
161  // output arguments) have taken place.
162  int _maxNumOrthogPasses;
163  magnitude_type _blkTol, _singTol;
164  if (params.is_null()) {
165  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
166  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
167  _singTol = defaultParams->get<magnitude_type> ("singTol");
168  } else {
169  try {
170  _maxNumOrthogPasses = params->get<int> ("maxNumOrthogPasses");
171  if (_maxNumOrthogPasses < 1)
172  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
174  _maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
175  }
176 
177  try {
178  _blkTol = params->get<magnitude_type> ("blkTol");
179  if (_blkTol < zero)
180  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
182  try {
183  // People may have used depTol instead of blkTol for this
184  // parameter's name, by analogy with DGKS.
185  _blkTol = params->get<magnitude_type> ("depTol");
187  _blkTol = defaultParams->get<magnitude_type> ("blkTol");
188  }
189  }
190 
191  try {
192  _singTol = params->get<magnitude_type> ("singTol");
193  if (_singTol < zero)
194  _singTol = defaultParams->get<magnitude_type> ("singTol");
196  _singTol = defaultParams->get<magnitude_type> ("singTol");
197  }
198  }
199  maxNumOrthogPasses = _maxNumOrthogPasses;
200  blkTol = _blkTol;
201  singTol = _singTol;
202  }
203 
204 
205  template<class ScalarType, class MV, class OP>
207  public MatOrthoManager<ScalarType,MV,OP>,
209  {
210  private:
211  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
212  typedef typename Teuchos::ScalarTraits<MagnitudeType> MGT;
217 
218  public:
220 
221 
223  ICGSOrthoManager( const std::string& label = "Belos",
224  Teuchos::RCP<const OP> Op = Teuchos::null,
225  const int max_ortho_steps = 2,
226  const MagnitudeType blk_tol = 10*MGT::squareroot( MGT::eps() ),
227  const MagnitudeType sing_tol = 10*MGT::eps() )
228  : MatOrthoManager<ScalarType,MV,OP>(Op),
229  max_ortho_steps_( max_ortho_steps ),
230  blk_tol_( blk_tol ),
231  sing_tol_( sing_tol ),
232  label_( label )
233  {
234 #ifdef BELOS_TEUCHOS_TIME_MONITOR
235  std::string orthoLabel = label_ + ": Orthogonalization";
236  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter(orthoLabel);
237 
238  std::string updateLabel = label_ + ": Ortho (Update)";
239  timerUpdate_ = Teuchos::TimeMonitor::getNewCounter(updateLabel);
240 
241  std::string normLabel = label_ + ": Ortho (Norm)";
242  timerNorm_ = Teuchos::TimeMonitor::getNewCounter(normLabel);
243 
244  std::string ipLabel = label_ + ": Ortho (Inner Product)";
245  timerInnerProd_ = Teuchos::TimeMonitor::getNewCounter(ipLabel);
246 #endif
247  }
248 
251  const std::string& label = "Belos",
252  Teuchos::RCP<const OP> Op = Teuchos::null) :
253  MatOrthoManager<ScalarType,MV,OP>(Op),
254  max_ortho_steps_ (2),
255  blk_tol_ (10 * MGT::squareroot (MGT::eps())),
256  sing_tol_ (10 * MGT::eps()),
257  label_ (label)
258  {
259  setParameterList (plist);
260 
261 #ifdef BELOS_TEUCHOS_TIME_MONITOR
262  std::string orthoLabel = label_ + ": Orthogonalization";
263  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter(orthoLabel);
264 
265  std::string updateLabel = label_ + ": Ortho (Update)";
266  timerUpdate_ = Teuchos::TimeMonitor::getNewCounter(updateLabel);
267 
268  std::string normLabel = label_ + ": Ortho (Norm)";
269  timerNorm_ = Teuchos::TimeMonitor::getNewCounter(normLabel);
270 
271  std::string ipLabel = label_ + ": Ortho (Inner Product)";
272  timerInnerProd_ = Teuchos::TimeMonitor::getNewCounter(ipLabel);
273 #endif
274  }
275 
279 
281 
282 
283  void
285  {
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  params = parameterList (*defaultParams);
295  } else {
296  params = plist;
297  // Some users might want to specify "blkTol" as "depTol". Due
298  // to this case, we don't invoke
299  // validateParametersAndSetDefaults on params. Instead, we go
300  // through the parameter list one parameter at a time and look
301  // for alternatives.
302  }
303 
304  // Using temporary variables and fetching all values before
305  // setting the output arguments ensures the strong exception
306  // guarantee for this function: if an exception is thrown, no
307  // externally visible side effects (in this case, setting the
308  // output arguments) have taken place.
309  int maxNumOrthogPasses;
310  MagnitudeType blkTol;
311  MagnitudeType singTol;
312 
313  try {
314  maxNumOrthogPasses = params->get<int> ("maxNumOrthogPasses");
315  } catch (InvalidParameterName&) {
316  maxNumOrthogPasses = defaultParams->get<int> ("maxNumOrthogPasses");
317  params->set ("maxNumOrthogPasses", maxNumOrthogPasses);
318  }
319 
320  // Handling of the "blkTol" parameter is a special case. This
321  // is because some users may prefer to call this parameter
322  // "depTol" for consistency with DGKS. However, our default
323  // parameter list calls this "blkTol", and we don't want the
324  // default list's value to override the user's value. Thus, we
325  // first check the user's parameter list for both names, and
326  // only then access the default parameter list.
327  try {
328  blkTol = params->get<MagnitudeType> ("blkTol");
329  } catch (InvalidParameterName&) {
330  try {
331  blkTol = params->get<MagnitudeType> ("depTol");
332  // "depTol" is the wrong name, so remove it and replace with
333  // "blkTol". We'll set "blkTol" below.
334  params->remove ("depTol");
335  } catch (InvalidParameterName&) {
336  blkTol = defaultParams->get<MagnitudeType> ("blkTol");
337  }
338  params->set ("blkTol", blkTol);
339  }
340 
341  try {
342  singTol = params->get<MagnitudeType> ("singTol");
343  } catch (InvalidParameterName&) {
344  singTol = defaultParams->get<MagnitudeType> ("singTol");
345  params->set ("singTol", singTol);
346  }
347 
348  max_ortho_steps_ = maxNumOrthogPasses;
349  blk_tol_ = blkTol;
350  sing_tol_ = singTol;
351 
352  setMyParamList (params);
353  }
354 
357  {
358  using Teuchos::as;
360  using Teuchos::parameterList;
361  using Teuchos::RCP;
362 
363  if (defaultParams_.is_null()) {
364  RCP<ParameterList> params = parameterList ("ICGS");
365 
366  // Default parameter values for ICGS orthogonalization.
367  // Documentation will be embedded in the parameter list.
368  const int defaultMaxNumOrthogPasses = 2;
369  const MagnitudeType eps = MGT::eps();
370  const MagnitudeType defaultBlkTol =
371  as<MagnitudeType> (10) * MGT::squareroot (eps);
372  const MagnitudeType defaultSingTol = as<MagnitudeType> (10) * eps;
373 
374  params->set ("maxNumOrthogPasses", defaultMaxNumOrthogPasses,
375  "Maximum number of orthogonalization passes (includes the "
376  "first). Default is 2, since \"twice is enough\" for Krylov "
377  "methods.");
378  params->set ("blkTol", defaultBlkTol, "Block reorthogonalization "
379  "threshhold.");
380  params->set ("singTol", defaultSingTol, "Singular block detection "
381  "threshold.");
382  defaultParams_ = params;
383  }
384  return defaultParams_;
385  }
387 
394  {
395  using Teuchos::as;
397  using Teuchos::parameterList;
398  using Teuchos::RCP;
399 
400  RCP<const ParameterList> defaultParams = getValidParameters ();
401  // Start with a clone of the default parameters.
402  RCP<ParameterList> params = parameterList (*defaultParams);
403 
404  const int maxBlkOrtho = 1; // No block reorthogonalization
405  const MagnitudeType blkTol = MGT::zero();
406  const MagnitudeType singTol = MGT::zero();
407 
408  params->set ("maxNumOrthogPasses", maxBlkOrtho);
409  params->set ("blkTol", blkTol);
410  params->set ("singTol", singTol);
411 
412  return params;
413  }
414 
416 
417 
419  void setBlkTol( const MagnitudeType blk_tol ) {
420  // Update the parameter list as well.
422  if (! params.is_null()) {
423  // If it's null, then we haven't called setParameterList()
424  // yet. It's entirely possible to construct the parameter
425  // list on demand, so we don't try to create the parameter
426  // list here.
427  params->set ("blkTol", blk_tol);
428  }
429  blk_tol_ = blk_tol;
430  }
431 
433  void setSingTol( const MagnitudeType sing_tol ) {
434  // Update the parameter list as well.
436  if (! params.is_null()) {
437  // If it's null, then we haven't called setParameterList()
438  // yet. It's entirely possible to construct the parameter
439  // list on demand, so we don't try to create the parameter
440  // list here.
441  params->set ("singTol", sing_tol);
442  }
443  sing_tol_ = sing_tol;
444  }
445 
447  MagnitudeType getBlkTol() const { return blk_tol_; }
448 
450  MagnitudeType getSingTol() const { return sing_tol_; }
451 
453 
454 
456 
457 
485  void project ( MV &X, Teuchos::RCP<MV> MX,
488 
489 
492  void project ( MV &X,
495  project(X,Teuchos::null,C,Q);
496  }
497 
498 
499 
524  int normalize ( MV &X, Teuchos::RCP<MV> MX,
526 
527 
531  return normalize(X,Teuchos::null,B);
532  }
533 
534 
576  int projectAndNormalize ( MV &X, Teuchos::RCP<MV> MX,
580 
583  int projectAndNormalize ( MV &X,
587  return projectAndNormalize(X,Teuchos::null,C,B,Q);
588  }
589 
591 
593 
594 
599  orthonormError(const MV &X) const {
600  return orthonormError(X,Teuchos::null);
601  }
602 
608  orthonormError(const MV &X, Teuchos::RCP<const MV> MX) const;
609 
614  orthogError(const MV &X1, const MV &X2) const {
615  return orthogError(X1,Teuchos::null,X2);
616  }
617 
623  orthogError(const MV &X1, Teuchos::RCP<const MV> MX1, const MV &X2) const;
624 
626 
628 
629 
632  void setLabel(const std::string& label);
633 
636  const std::string& getLabel() const { return label_; }
637 
639 
640  private:
642  int max_ortho_steps_;
644  MagnitudeType blk_tol_;
646  MagnitudeType sing_tol_;
647 
649  std::string label_;
650 #ifdef BELOS_TEUCHOS_TIME_MONITOR
651  Teuchos::RCP<Teuchos::Time> timerOrtho_, timerUpdate_,
652  timerNorm_, timerScale_, timerInnerProd_;
653 #endif // BELOS_TEUCHOS_TIME_MONITOR
654 
656  mutable Teuchos::RCP<Teuchos::ParameterList> defaultParams_;
657 
659  int findBasis(MV &X, Teuchos::RCP<MV> MX,
661  bool completeBasis, int howMany = -1 ) const;
662 
664  bool blkOrtho1 ( MV &X, Teuchos::RCP<MV> MX,
667 
669  bool blkOrtho ( MV &X, Teuchos::RCP<MV> MX,
672 
686  int blkOrthoSing ( MV &X, Teuchos::RCP<MV> MX,
690  };
691 
693  // Set the label for this orthogonalization manager and create new timers if it's changed
694  template<class ScalarType, class MV, class OP>
695  void ICGSOrthoManager<ScalarType,MV,OP>::setLabel(const std::string& label)
696  {
697  if (label != label_) {
698  label_ = label;
699  std::string orthoLabel = label_ + ": Orthogonalization";
700 #ifdef BELOS_TEUCHOS_TIME_MONITOR
701  timerOrtho_ = Teuchos::TimeMonitor::getNewCounter(orthoLabel);
702 #endif
703 
704  std::string updateLabel = label_ + ": Ortho (Update)";
705 #ifdef BELOS_TEUCHOS_TIME_MONITOR
706  timerUpdate_ = Teuchos::TimeMonitor::getNewCounter(updateLabel);
707 #endif
708 
709  std::string normLabel = label_ + ": Ortho (Norm)";
710 #ifdef BELOS_TEUCHOS_TIME_MONITOR
711  timerNorm_ = Teuchos::TimeMonitor::getNewCounter(normLabel);
712 #endif
713 
714  std::string ipLabel = label_ + ": Ortho (Inner Product)";
715 #ifdef BELOS_TEUCHOS_TIME_MONITOR
716  timerInnerProd_ = Teuchos::TimeMonitor::getNewCounter(ipLabel);
717 #endif
718  }
719  }
720 
722  // Compute the distance from orthonormality
723  template<class ScalarType, class MV, class OP>
726  const ScalarType ONE = SCT::one();
727  int rank = MVT::GetNumberVecs(X);
730  for (int i=0; i<rank; i++) {
731  xTx(i,i) -= ONE;
732  }
733  return xTx.normFrobenius();
734  }
735 
737  // Compute the distance from orthogonality
738  template<class ScalarType, class MV, class OP>
741  int r1 = MVT::GetNumberVecs(X1);
742  int r2 = MVT::GetNumberVecs(X2);
745  return xTx.normFrobenius();
746  }
747 
749  // Find an Op-orthonormal basis for span(X) - span(W)
750  template<class ScalarType, class MV, class OP>
752  MV &X, Teuchos::RCP<MV> MX,
756  {
757  using Teuchos::Array;
758  using Teuchos::null;
759  using Teuchos::is_null;
760  using Teuchos::RCP;
761  using Teuchos::rcp;
763  typedef SerialDenseMatrix< int, ScalarType > serial_dense_matrix_type;
764  typedef typename Array< RCP< const MV > >::size_type size_type;
765 
766 #ifdef BELOS_TEUCHOS_TIME_MONITOR
767  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
768 #endif
769 
770  ScalarType ONE = SCT::one();
771  //ScalarType ZERO = SCT::zero();
772 
773  int nq = Q.size();
774  int xc = MVT::GetNumberVecs( X );
775  ptrdiff_t xr = MVText::GetGlobalLength( X );
776  int rank = xc;
777 
778  // If the user doesn't want to store the normalization
779  // coefficients, allocate some local memory for them. This will
780  // go away at the end of this method.
781  if (is_null (B)) {
782  B = rcp (new serial_dense_matrix_type (xc, xc));
783  }
784  // Likewise, if the user doesn't want to store the projection
785  // coefficients, allocate some local memory for them. Also make
786  // sure that all the entries of C are the right size. We're going
787  // to overwrite them anyway, so we don't have to worry about the
788  // contents (other than to resize them if they are the wrong
789  // size).
790  if (C.size() < nq)
791  C.resize (nq);
792  for (size_type k = 0; k < nq; ++k)
793  {
794  const int numRows = MVT::GetNumberVecs (*Q[k]);
795  const int numCols = xc; // Number of vectors in X
796 
797  if (is_null (C[k]))
798  C[k] = rcp (new serial_dense_matrix_type (numRows, numCols));
799  else if (C[k]->numRows() != numRows || C[k]->numCols() != numCols)
800  {
801  int err = C[k]->reshape (numRows, numCols);
802  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error,
803  "IMGS orthogonalization: failed to reshape "
804  "C[" << k << "] (the array of block "
805  "coefficients resulting from projecting X "
806  "against Q[1:" << nq << "]).");
807  }
808  }
809 
810  /****** DO NOT MODIFY *MX IF _hasOp == false ******/
811  if (this->_hasOp) {
812  if (MX == Teuchos::null) {
813  // we need to allocate space for MX
814  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
815  OPT::Apply(*(this->_Op),X,*MX);
816  }
817  }
818  else {
819  // Op == I --> MX = X (ignore it if the user passed it in)
820  MX = Teuchos::rcp( &X, false );
821  }
822 
823  int mxc = MVT::GetNumberVecs( *MX );
824  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
825 
826  // short-circuit
827  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument, "Belos::ICGSOrthoManager::projectAndNormalize(): X must be non-empty" );
828 
829  int numbas = 0;
830  for (int i=0; i<nq; i++) {
831  numbas += MVT::GetNumberVecs( *Q[i] );
832  }
833 
834  // check size of B
835  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
836  "Belos::ICGSOrthoManager::projectAndNormalize(): Size of X must be consistant with size of B" );
837  // check size of X and MX
838  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
839  "Belos::ICGSOrthoManager::projectAndNormalize(): MVT returned negative dimensions for X,MX" );
840  // check size of X w.r.t. MX
841  TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr, std::invalid_argument,
842  "Belos::ICGSOrthoManager::projectAndNormalize(): Size of X must be consistant with size of MX" );
843  // check feasibility
844  //TEUCHOS_TEST_FOR_EXCEPTION( numbas+xc > xr, std::invalid_argument,
845  // "Belos::ICGSOrthoManager::projectAndNormalize(): Orthogonality constraints not feasible" );
846 
847  // Some flags for checking dependency returns from the internal orthogonalization methods
848  bool dep_flg = false;
849 
850  // Make a temporary copy of X and MX, just in case a block dependency is detected.
851  Teuchos::RCP<MV> tmpX, tmpMX;
852  tmpX = MVT::CloneCopy(X);
853  if (this->_hasOp) {
854  tmpMX = MVT::CloneCopy(*MX);
855  }
856 
857  if (xc == 1) {
858 
859  // Use the cheaper block orthogonalization.
860  // NOTE: Don't check for dependencies because the update has one vector.
861  dep_flg = blkOrtho1( X, MX, C, Q );
862 
863  // Normalize the new block X
864  {
865 #ifdef BELOS_TEUCHOS_TIME_MONITOR
866  Teuchos::TimeMonitor normTimer( *timerNorm_ );
867 #endif
868  if ( B == Teuchos::null ) {
870  }
871  std::vector<ScalarType> diag(xc);
872  MVT::MvDot( X, *MX, diag );
873  (*B)(0,0) = SCT::squareroot(SCT::magnitude(diag[0]));
874  rank = 1;
875  MVT::MvScale( X, ONE/(*B)(0,0) );
876  if (this->_hasOp) {
877  // Update MXj.
878  MVT::MvScale( *MX, ONE/(*B)(0,0) );
879  }
880  }
881 
882  }
883  else {
884 
885  // Use the cheaper block orthogonalization.
886  dep_flg = blkOrtho( X, MX, C, Q );
887 
888  // If a dependency has been detected in this block, then perform
889  // the more expensive nonblock (single vector at a time)
890  // orthogonalization.
891  if (dep_flg) {
892  rank = blkOrthoSing( *tmpX, tmpMX, C, B, Q );
893 
894  // Copy tmpX back into X.
895  MVT::Assign( *tmpX, X );
896  if (this->_hasOp) {
897  MVT::Assign( *tmpMX, *MX );
898  }
899  }
900  else {
901  // There is no dependency, so orthonormalize new block X
902  rank = findBasis( X, MX, B, false );
903  if (rank < xc) {
904  // A dependency was found during orthonormalization of X,
905  // rerun orthogonalization using more expensive nonblock
906  // orthogonalization.
907  rank = blkOrthoSing( *tmpX, tmpMX, C, B, Q );
908 
909  // Copy tmpX back into X.
910  MVT::Assign( *tmpX, X );
911  if (this->_hasOp) {
912  MVT::Assign( *tmpMX, *MX );
913  }
914  }
915  }
916  } // if (xc == 1) {
917 
918  // this should not raise an std::exception; but our post-conditions oblige us to check
919  TEUCHOS_TEST_FOR_EXCEPTION( rank > xc || rank < 0, std::logic_error,
920  "Belos::ICGSOrthoManager::projectAndNormalize(): Debug error in rank variable." );
921 
922  // Return the rank of X.
923  return rank;
924  }
925 
926 
927 
929  // Find an Op-orthonormal basis for span(X), with rank numvectors(X)
930  template<class ScalarType, class MV, class OP>
932  MV &X, Teuchos::RCP<MV> MX,
934 
935 #ifdef BELOS_TEUCHOS_TIME_MONITOR
936  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
937 #endif
938 
939  // call findBasis, with the instruction to try to generate a basis of rank numvecs(X)
940  return findBasis(X, MX, B, true);
941  }
942 
943 
944 
946  template<class ScalarType, class MV, class OP>
948  MV &X, Teuchos::RCP<MV> MX,
951  // For the inner product defined by the operator Op or the identity (Op == 0)
952  // -> Orthogonalize X against each Q[i]
953  // Modify MX accordingly
954  //
955  // Note that when Op is 0, MX is not referenced
956  //
957  // Parameter variables
958  //
959  // X : Vectors to be transformed
960  //
961  // MX : Image of the block of vectors X by the mass matrix
962  //
963  // Q : Bases to orthogonalize against. These are assumed orthonormal, mutually and independently.
964  //
965 
966 #ifdef BELOS_TEUCHOS_TIME_MONITOR
967  Teuchos::TimeMonitor orthotimer(*timerOrtho_);
968 #endif
969 
970  int xc = MVT::GetNumberVecs( X );
971  ptrdiff_t xr = MVText::GetGlobalLength( X );
972  int nq = Q.size();
973  std::vector<int> qcs(nq);
974  // short-circuit
975  if (nq == 0 || xc == 0 || xr == 0) {
976  return;
977  }
978  ptrdiff_t qr = MVText::GetGlobalLength ( *Q[0] );
979  // if we don't have enough C, expand it with null references
980  // if we have too many, resize to throw away the latter ones
981  // if we have exactly as many as we have Q, this call has no effect
982  C.resize(nq);
983 
984 
985  /****** DO NOT MODIFY *MX IF _hasOp == false ******/
986  if (this->_hasOp) {
987  if (MX == Teuchos::null) {
988  // we need to allocate space for MX
989  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
990  OPT::Apply(*(this->_Op),X,*MX);
991  }
992  }
993  else {
994  // Op == I --> MX = X (ignore it if the user passed it in)
995  MX = Teuchos::rcp( &X, false );
996  }
997  int mxc = MVT::GetNumberVecs( *MX );
998  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
999 
1000  // check size of X and Q w.r.t. common sense
1001  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
1002  "Belos::ICGSOrthoManager::project(): MVT returned negative dimensions for X,MX" );
1003  // check size of X w.r.t. MX and Q
1004  TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr || xr!=qr, std::invalid_argument,
1005  "Belos::ICGSOrthoManager::project(): Size of X not consistant with MX,Q" );
1006 
1007  // tally up size of all Q and check/allocate C
1008  int baslen = 0;
1009  for (int i=0; i<nq; i++) {
1010  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength( *Q[i] ) != qr, std::invalid_argument,
1011  "Belos::ICGSOrthoManager::project(): Q lengths not mutually consistant" );
1012  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1013  TEUCHOS_TEST_FOR_EXCEPTION( qr < qcs[i], std::invalid_argument,
1014  "Belos::ICGSOrthoManager::project(): Q has less rows than columns" );
1015  baslen += qcs[i];
1016 
1017  // check size of C[i]
1018  if ( C[i] == Teuchos::null ) {
1020  }
1021  else {
1022  TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != qcs[i] || C[i]->numCols() != xc , std::invalid_argument,
1023  "Belos::ICGSOrthoManager::project(): Size of Q not consistant with size of C" );
1024  }
1025  }
1026 
1027  // Use the cheaper block orthogonalization, don't check for rank deficiency.
1028  blkOrtho( X, MX, C, Q );
1029 
1030  }
1031 
1033  // Find an Op-orthonormal basis for span(X), with the option of extending the subspace so that
1034  // the rank is numvectors(X)
1035  template<class ScalarType, class MV, class OP>
1036  int
1038  findBasis (MV &X,
1039  Teuchos::RCP<MV> MX,
1041  bool completeBasis,
1042  int howMany) const
1043  {
1044  // For the inner product defined by the operator Op or the identity (Op == 0)
1045  // -> Orthonormalize X
1046  // Modify MX accordingly
1047  //
1048  // Note that when Op is 0, MX is not referenced
1049  //
1050  // Parameter variables
1051  //
1052  // X : Vectors to be orthonormalized
1053  // MX : Image of the multivector X under the operator Op
1054  // Op : Pointer to the operator for the inner product
1055  //
1056  using Teuchos::as;
1057 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1058  Teuchos::TimeMonitor normTimer (*timerNorm_);
1059 #endif // BELOS_TEUCHOS_TIME_MONITOR
1060 
1061  const ScalarType ONE = SCT::one ();
1062  const MagnitudeType ZERO = SCT::magnitude (SCT::zero ());
1063 
1064  const int xc = MVT::GetNumberVecs (X);
1065  const ptrdiff_t xr = MVText::GetGlobalLength (X);
1066 
1067  if (howMany == -1) {
1068  howMany = xc;
1069  }
1070 
1071  /*******************************************************
1072  * If _hasOp == false, we will not reference MX below *
1073  *******************************************************/
1074 
1075  // if Op==null, MX == X (via pointer)
1076  // Otherwise, either the user passed in MX or we will allocated and compute it
1077  if (this->_hasOp) {
1078  if (MX == Teuchos::null) {
1079  // we need to allocate space for MX
1080  MX = MVT::Clone(X,xc);
1081  OPT::Apply(*(this->_Op),X,*MX);
1082  }
1083  }
1084 
1085  /* if the user doesn't want to store the coefficienets,
1086  * allocate some local memory for them
1087  */
1088  if ( B == Teuchos::null ) {
1090  }
1091 
1092  const int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
1093  const ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
1094 
1095  // check size of C, B
1096  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
1097  "Belos::ICGSOrthoManager::findBasis(): X must be non-empty" );
1098  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
1099  "Belos::ICGSOrthoManager::findBasis(): Size of X not consistant with size of B" );
1100  TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
1101  "Belos::ICGSOrthoManager::findBasis(): Size of X not consistant with size of MX" );
1102  TEUCHOS_TEST_FOR_EXCEPTION( as<ptrdiff_t> (xc) > xr, std::invalid_argument,
1103  "Belos::ICGSOrthoManager::findBasis(): Size of X not feasible for normalization" );
1104  TEUCHOS_TEST_FOR_EXCEPTION( howMany < 0 || howMany > xc, std::invalid_argument,
1105  "Belos::ICGSOrthoManager::findBasis(): Invalid howMany parameter" );
1106 
1107  /* xstart is which column we are starting the process with, based on howMany
1108  * columns before xstart are assumed to be Op-orthonormal already
1109  */
1110  int xstart = xc - howMany;
1111 
1112  for (int j = xstart; j < xc; j++) {
1113 
1114  // numX is
1115  // * number of currently orthonormal columns of X
1116  // * the index of the current column of X
1117  int numX = j;
1118  bool addVec = false;
1119 
1120  // Get a view of the vector currently being worked on.
1121  std::vector<int> index(1);
1122  index[0] = numX;
1123  Teuchos::RCP<MV> Xj = MVT::CloneViewNonConst( X, index );
1124  Teuchos::RCP<MV> MXj;
1125  if (this->_hasOp) {
1126  // MXj is a view of the current vector in MX
1127  MXj = MVT::CloneViewNonConst( *MX, index );
1128  }
1129  else {
1130  // MXj is a pointer to Xj, and MUST NOT be modified
1131  MXj = Xj;
1132  }
1133 
1134  // Get a view of the previous vectors.
1135  std::vector<int> prev_idx( numX );
1136  Teuchos::RCP<const MV> prevX, prevMX;
1137 
1138  if (numX > 0) {
1139  for (int i=0; i<numX; i++) {
1140  prev_idx[i] = i;
1141  }
1142  prevX = MVT::CloneView( X, prev_idx );
1143  if (this->_hasOp) {
1144  prevMX = MVT::CloneView( *MX, prev_idx );
1145  }
1146  }
1147 
1148  // Make storage for these Gram-Schmidt iterations.
1150  std::vector<ScalarType> oldDot( 1 ), newDot( 1 );
1151  //
1152  // Save old MXj vector and compute Op-norm
1153  //
1154  Teuchos::RCP<MV> oldMXj = MVT::CloneCopy( *MXj );
1155  MVT::MvDot( *Xj, *MXj, oldDot );
1156  // Xj^H Op Xj should be real and positive, by the hermitian positive definiteness of Op
1157  TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(oldDot[0]) < ZERO, OrthoError,
1158  "Belos::ICGSOrthoManager::findBasis(): Negative definiteness discovered in inner product" );
1159 
1160  if (numX > 0) {
1161 
1163 
1164  for (int i=0; i<max_ortho_steps_; ++i) {
1165 
1166  // product <- prevX^T MXj
1167  {
1168 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1169  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1170 #endif
1172  }
1173 
1174  // Xj <- Xj - prevX prevX^T MXj
1175  // = Xj - prevX product
1176  {
1177 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1178  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1179 #endif
1180  MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
1181  }
1182 
1183  // Update MXj
1184  if (this->_hasOp) {
1185  // MXj <- Op*Xj_new
1186  // = Op*(Xj_old - prevX prevX^T MXj)
1187  // = MXj - prevMX product
1188 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1189  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1190 #endif
1191  MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
1192  }
1193 
1194  // Set coefficients
1195  if ( i==0 )
1196  product = P2;
1197  else
1198  product += P2;
1199  }
1200 
1201  } // if (numX > 0)
1202 
1203  // Compute Op-norm with old MXj
1204  MVT::MvDot( *Xj, *oldMXj, newDot );
1205 
1206  // Check to see if the new vector is dependent.
1207  if (completeBasis) {
1208  //
1209  // We need a complete basis, so add random vectors if necessary
1210  //
1211  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(sing_tol_*oldDot[0]) ) {
1212 
1213  // Add a random vector and orthogonalize it against previous vectors in block.
1214  addVec = true;
1215 #ifdef ORTHO_DEBUG
1216  std::cout << "Belos::ICGSOrthoManager::findBasis() --> Random for column " << numX << std::endl;
1217 #endif
1218  //
1219  Teuchos::RCP<MV> tempXj = MVT::Clone( X, 1 );
1220  Teuchos::RCP<MV> tempMXj;
1221  MVT::MvRandom( *tempXj );
1222  if (this->_hasOp) {
1223  tempMXj = MVT::Clone( X, 1 );
1224  OPT::Apply( *(this->_Op), *tempXj, *tempMXj );
1225  }
1226  else {
1227  tempMXj = tempXj;
1228  }
1229  MVT::MvDot( *tempXj, *tempMXj, oldDot );
1230  //
1231  for (int num_orth=0; num_orth<max_ortho_steps_; num_orth++){
1232  {
1233 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1234  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1235 #endif
1236  MatOrthoManager<ScalarType,MV,OP>::innerProd(*prevX,*tempXj,tempMXj,product);
1237  }
1238  {
1239 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1240  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1241 #endif
1242  MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *tempXj );
1243  }
1244  if (this->_hasOp) {
1245 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1246  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1247 #endif
1248  MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *tempMXj );
1249  }
1250  }
1251  // Compute new Op-norm
1252  MVT::MvDot( *tempXj, *tempMXj, newDot );
1253  //
1254  if ( SCT::magnitude(newDot[0]) >= SCT::magnitude(oldDot[0]*sing_tol_) ) {
1255  // Copy vector into current column of _basisvecs
1256  MVT::Assign( *tempXj, *Xj );
1257  if (this->_hasOp) {
1258  MVT::Assign( *tempMXj, *MXj );
1259  }
1260  }
1261  else {
1262  return numX;
1263  }
1264  }
1265  }
1266  else {
1267  //
1268  // We only need to detect dependencies.
1269  //
1270  if ( SCT::magnitude(newDot[0]) < SCT::magnitude(oldDot[0]*blk_tol_) ) {
1271  return numX;
1272  }
1273  }
1274 
1275  // If we haven't left this method yet, then we can normalize the new vector Xj.
1276  // Normalize Xj.
1277  // Xj <- Xj / std::sqrt(newDot)
1278  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1279  {
1280  MVT::MvScale( *Xj, ONE/diag );
1281  if (this->_hasOp) {
1282  // Update MXj.
1283  MVT::MvScale( *MXj, ONE/diag );
1284  }
1285  }
1286 
1287  // If we've added a random vector, enter a zero in the j'th diagonal element.
1288  if (addVec) {
1289  (*B)(j,j) = ZERO;
1290  }
1291  else {
1292  (*B)(j,j) = diag;
1293  }
1294 
1295  // Save the coefficients, if we are working on the original vector and not a randomly generated one
1296  if (!addVec) {
1297  for (int i=0; i<numX; i++) {
1298  (*B)(i,j) = product(i,0);
1299  }
1300  }
1301 
1302  } // for (j = 0; j < xc; ++j)
1303 
1304  return xc;
1305  }
1306 
1308  // Routine to compute the block orthogonalization
1309  template<class ScalarType, class MV, class OP>
1310  bool
1311  ICGSOrthoManager<ScalarType, MV, OP>::blkOrtho1 ( MV &X, Teuchos::RCP<MV> MX,
1314  {
1315  int nq = Q.size();
1316  int xc = MVT::GetNumberVecs( X );
1317  const ScalarType ONE = SCT::one();
1318 
1319  std::vector<int> qcs( nq );
1320  for (int i=0; i<nq; i++) {
1321  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1322  }
1323 
1324  // Perform the Gram-Schmidt transformation for a block of vectors
1325 
1327  // Define the product Q^T * (Op*X)
1328  for (int i=0; i<nq; i++) {
1329  // Multiply Q' with MX
1330  {
1331 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1332  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1333 #endif
1335  }
1336  // Multiply by Q and subtract the result in X
1337  {
1338 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1339  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1340 #endif
1341  MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
1342  }
1343 
1344  // Update MX, with the least number of applications of Op as possible
1345  if (this->_hasOp) {
1346  if (xc <= qcs[i]) {
1347  OPT::Apply( *(this->_Op), X, *MX);
1348  }
1349  else {
1350  // this will possibly be used again below; don't delete it
1351  MQ[i] = MVT::Clone( *Q[i], qcs[i] );
1352  OPT::Apply( *(this->_Op), *Q[i], *MQ[i] );
1353  MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
1354  }
1355  }
1356  }
1357 
1358  // Do as many steps of classical Gram-Schmidt as required by max_ortho_steps_
1359  for (int j = 1; j < max_ortho_steps_; ++j) {
1360 
1361  for (int i=0; i<nq; i++) {
1363 
1364  // Apply another step of classical Gram-Schmidt
1365  {
1366 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1367  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1368 #endif
1370  }
1371  *C[i] += C2;
1372  {
1373 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1374  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1375 #endif
1376  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
1377  }
1378 
1379  // Update MX, with the least number of applications of Op as possible
1380  if (this->_hasOp) {
1381  if (MQ[i].get()) {
1382  // MQ was allocated and computed above; use it
1383  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
1384  }
1385  else if (xc <= qcs[i]) {
1386  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1387  OPT::Apply( *(this->_Op), X, *MX);
1388  }
1389  }
1390  } // for (int i=0; i<nq; i++)
1391  } // for (int j = 0; j < max_ortho_steps; ++j)
1392 
1393  return false;
1394  }
1395 
1397  // Routine to compute the block orthogonalization
1398  template<class ScalarType, class MV, class OP>
1399  bool
1400  ICGSOrthoManager<ScalarType, MV, OP>::blkOrtho ( MV &X, Teuchos::RCP<MV> MX,
1403  {
1404  int nq = Q.size();
1405  int xc = MVT::GetNumberVecs( X );
1406  bool dep_flg = false;
1407  const ScalarType ONE = SCT::one();
1408 
1409  std::vector<int> qcs( nq );
1410  for (int i=0; i<nq; i++) {
1411  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1412  }
1413 
1414  // Perform the Gram-Schmidt transformation for a block of vectors
1415 
1416  // Compute the initial Op-norms
1417  std::vector<ScalarType> oldDot( xc );
1418  MVT::MvDot( X, *MX, oldDot );
1419 
1421  // Define the product Q^T * (Op*X)
1422  for (int i=0; i<nq; i++) {
1423  // Multiply Q' with MX
1424  {
1425 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1426  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1427 #endif
1429  }
1430  // Multiply by Q and subtract the result in X
1431  {
1432 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1433  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1434 #endif
1435  MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
1436  }
1437  // Update MX, with the least number of applications of Op as possible
1438  if (this->_hasOp) {
1439  if (xc <= qcs[i]) {
1440  OPT::Apply( *(this->_Op), X, *MX);
1441  }
1442  else {
1443  // this will possibly be used again below; don't delete it
1444  MQ[i] = MVT::Clone( *Q[i], qcs[i] );
1445  OPT::Apply( *(this->_Op), *Q[i], *MQ[i] );
1446  MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
1447  }
1448  }
1449  }
1450 
1451  // Do as many steps of classical Gram-Schmidt as required by max_ortho_steps_
1452  for (int j = 1; j < max_ortho_steps_; ++j) {
1453 
1454  for (int i=0; i<nq; i++) {
1456 
1457  // Apply another step of classical Gram-Schmidt
1458  {
1459 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1460  Teuchos::TimeMonitor innerProdTimer( *timerInnerProd_ );
1461 #endif
1463  }
1464  *C[i] += C2;
1465  {
1466 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1467  Teuchos::TimeMonitor updateTimer( *timerUpdate_ );
1468 #endif
1469  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
1470  }
1471 
1472  // Update MX, with the least number of applications of Op as possible
1473  if (this->_hasOp) {
1474  if (MQ[i].get()) {
1475  // MQ was allocated and computed above; use it
1476  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
1477  }
1478  else if (xc <= qcs[i]) {
1479  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1480  OPT::Apply( *(this->_Op), X, *MX);
1481  }
1482  }
1483  } // for (int i=0; i<nq; i++)
1484  } // for (int j = 0; j < max_ortho_steps; ++j)
1485 
1486  // Compute new Op-norms
1487  std::vector<ScalarType> newDot(xc);
1488  MVT::MvDot( X, *MX, newDot );
1489 
1490  // Check to make sure the new block of vectors are not dependent on previous vectors
1491  for (int i=0; i<xc; i++){
1492  if (SCT::magnitude(newDot[i]) < SCT::magnitude(oldDot[i] * blk_tol_)) {
1493  dep_flg = true;
1494  break;
1495  }
1496  } // end for (i=0;...)
1497 
1498  return dep_flg;
1499  }
1500 
1501  template<class ScalarType, class MV, class OP>
1502  int
1503  ICGSOrthoManager<ScalarType, MV, OP>::blkOrthoSing ( MV &X, Teuchos::RCP<MV> MX,
1507  {
1509 
1510  const ScalarType ONE = SCT::one();
1511  const ScalarType ZERO = SCT::zero();
1512 
1513  int nq = Q.size();
1514  int xc = MVT::GetNumberVecs( X );
1515  std::vector<int> indX( 1 );
1516  std::vector<ScalarType> oldDot( 1 ), newDot( 1 );
1517 
1518  std::vector<int> qcs( nq );
1519  for (int i=0; i<nq; i++) {
1520  qcs[i] = MVT::GetNumberVecs( *Q[i] );
1521  }
1522 
1523  // Create pointers for the previous vectors of X that have already been orthonormalized.
1524  Teuchos::RCP<const MV> lastQ;
1525  Teuchos::RCP<MV> Xj, MXj;
1527 
1528  // Perform the Gram-Schmidt transformation for each vector in the block of vectors.
1529  for (int j=0; j<xc; j++) {
1530 
1531  bool dep_flg = false;
1532 
1533  // Get a view of the previously orthogonalized vectors and B, add it to the arrays.
1534  if (j > 0) {
1535  std::vector<int> index( j );
1536  for (int ind=0; ind<j; ind++) {
1537  index[ind] = ind;
1538  }
1539  lastQ = MVT::CloneView( X, index );
1540 
1541  // Add these views to the Q and C arrays.
1542  Q.push_back( lastQ );
1543  C.push_back( B );
1544  qcs.push_back( MVT::GetNumberVecs( *lastQ ) );
1545  }
1546 
1547  // Get a view of the current vector in X to orthogonalize.
1548  indX[0] = j;
1549  Xj = MVT::CloneViewNonConst( X, indX );
1550  if (this->_hasOp) {
1551  MXj = MVT::CloneViewNonConst( *MX, indX );
1552  }
1553  else {
1554  MXj = Xj;
1555  }
1556 
1557  // Compute the initial Op-norms
1558  MVT::MvDot( *Xj, *MXj, oldDot );
1559 
1560  Teuchos::Array<Teuchos::RCP<MV> > MQ(Q.size());
1561  // Define the product Q^T * (Op*X)
1562  for (int i=0; i<Q.size(); i++) {
1563 
1564  // Get a view of the current serial dense matrix
1565  Teuchos::SerialDenseMatrix<int,ScalarType> tempC( Teuchos::View, *C[i], qcs[i], 1, 0, j );
1566 
1567  // Multiply Q' with MX
1568  MatOrthoManager<ScalarType,MV,OP>::innerProd(*Q[i],*Xj,MXj,tempC);
1569  // Multiply by Q and subtract the result in Xj
1570  MVT::MvTimesMatAddMv( -ONE, *Q[i], tempC, ONE, *Xj );
1571 
1572  // Update MXj, with the least number of applications of Op as possible
1573  if (this->_hasOp) {
1574  if (xc <= qcs[i]) {
1575  OPT::Apply( *(this->_Op), *Xj, *MXj);
1576  }
1577  else {
1578  // this will possibly be used again below; don't delete it
1579  MQ[i] = MVT::Clone( *Q[i], qcs[i] );
1580  OPT::Apply( *(this->_Op), *Q[i], *MQ[i] );
1581  MVT::MvTimesMatAddMv( -ONE, *MQ[i], tempC, ONE, *MXj );
1582  }
1583  }
1584  }
1585 
1586  // Do any additional steps of classical Gram-Schmidt orthogonalization
1587  for (int num_ortho_steps=1; num_ortho_steps < max_ortho_steps_; ++num_ortho_steps) {
1588 
1589  for (int i=0; i<Q.size(); i++) {
1590  Teuchos::SerialDenseMatrix<int,ScalarType> tempC( Teuchos::View, *C[i], qcs[i], 1, 0, j );
1592 
1593  // Apply another step of classical Gram-Schmidt
1595  tempC += C2;
1596  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, *Xj );
1597 
1598  // Update MXj, with the least number of applications of Op as possible
1599  if (this->_hasOp) {
1600  if (MQ[i].get()) {
1601  // MQ was allocated and computed above; use it
1602  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MXj );
1603  }
1604  else if (xc <= qcs[i]) {
1605  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1606  OPT::Apply( *(this->_Op), *Xj, *MXj);
1607  }
1608  }
1609  } // for (int i=0; i<Q.size(); i++)
1610 
1611  } // for (int num_ortho_steps=1; num_ortho_steps < max_ortho_steps_; ++num_ortho_steps)
1612 
1613  // Check for linear dependence.
1614  if (SCT::magnitude(newDot[0]) < SCT::magnitude(oldDot[0]*sing_tol_)) {
1615  dep_flg = true;
1616  }
1617 
1618  // Normalize the new vector if it's not dependent
1619  if (!dep_flg) {
1620  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1621 
1622  MVT::MvScale( *Xj, ONE/diag );
1623  if (this->_hasOp) {
1624  // Update MXj.
1625  MVT::MvScale( *MXj, ONE/diag );
1626  }
1627 
1628  // Enter value on diagonal of B.
1629  (*B)(j,j) = diag;
1630  }
1631  else {
1632  // Create a random vector and orthogonalize it against all previous columns of Q.
1633  Teuchos::RCP<MV> tempXj = MVT::Clone( X, 1 );
1634  Teuchos::RCP<MV> tempMXj;
1635  MVT::MvRandom( *tempXj );
1636  if (this->_hasOp) {
1637  tempMXj = MVT::Clone( X, 1 );
1638  OPT::Apply( *(this->_Op), *tempXj, *tempMXj );
1639  }
1640  else {
1641  tempMXj = tempXj;
1642  }
1643  MVT::MvDot( *tempXj, *tempMXj, oldDot );
1644  //
1645  for (int num_orth=0; num_orth<max_ortho_steps_; num_orth++) {
1646 
1647  for (int i=0; i<Q.size(); i++) {
1648  Teuchos::SerialDenseMatrix<int,ScalarType> product( qcs[i], 1 );
1649 
1650  // Apply another step of classical Gram-Schmidt
1651  MatOrthoManager<ScalarType,MV,OP>::innerProd(*Q[i],*tempXj,tempMXj,product);
1652  MVT::MvTimesMatAddMv( -ONE, *Q[i], product, ONE, *tempXj );
1653 
1654  // Update MXj, with the least number of applications of Op as possible
1655  if (this->_hasOp) {
1656  if (MQ[i].get()) {
1657  // MQ was allocated and computed above; use it
1658  MVT::MvTimesMatAddMv( -ONE, *MQ[i], product, ONE, *tempMXj );
1659  }
1660  else if (xc <= qcs[i]) {
1661  // MQ was not allocated and computed above; it was cheaper to use X before and it still is
1662  OPT::Apply( *(this->_Op), *tempXj, *tempMXj);
1663  }
1664  }
1665  } // for (int i=0; i<nq; i++)
1666 
1667  }
1668 
1669  // Compute the Op-norms after the correction step.
1670  MVT::MvDot( *tempXj, *tempMXj, newDot );
1671 
1672  // Copy vector into current column of Xj
1673  if ( SCT::magnitude(newDot[0]) >= SCT::magnitude(oldDot[0]*sing_tol_) ) {
1674  ScalarType diag = SCT::squareroot(SCT::magnitude(newDot[0]));
1675 
1676  // Enter value on diagonal of B.
1677  (*B)(j,j) = ZERO;
1678 
1679  // Copy vector into current column of _basisvecs
1680  MVT::MvAddMv( ONE/diag, *tempXj, ZERO, *tempXj, *Xj );
1681  if (this->_hasOp) {
1682  MVT::MvAddMv( ONE/diag, *tempMXj, ZERO, *tempMXj, *MXj );
1683  }
1684  }
1685  else {
1686  return j;
1687  }
1688  } // if (!dep_flg)
1689 
1690  // Remove the vectors from array
1691  if (j > 0) {
1692  Q.resize( nq );
1693  C.resize( nq );
1694  qcs.resize( nq );
1695  }
1696 
1697  } // for (int j=0; j<xc; j++)
1698 
1699  return xc;
1700  }
1701 
1702 } // namespace Belos
1703 
1704 #endif // BELOS_ICGS_ORTHOMANAGER_HPP
1705 
void setBlkTol(const MagnitudeType blk_tol)
Set parameter for block re-orthogonalization threshhold.
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
bool is_null(const boost::shared_ptr< T > &p)
static magnitudeType eps()
T & get(const std::string &name, T def_value)
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)
ICGSOrthoManager(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.
static T squareroot(T x)
MagnitudeType getBlkTol() const
Return parameter for block re-orthogonalization threshhold.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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...
Declaration of basic traits for the multivector type.
void setLabel(const std::string &label)
This method sets the label used by the timers in the orthogonalization manager.
Teuchos::RCP< const Teuchos::ParameterList > TEUCHOS_DEPRECATED getFastIcgsParameters()
&quot;Fast&quot; parameters for ICGSOrthoManager.
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...
An extension of the MultiVecTraits class that adds a new vector length method.
Class which defines basic traits for the operator type.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Traits class which defines basic operations on multivectors.
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...
void TEUCHOS_DEPRECATED readIcgsParameters(const Teuchos::RCP< const Teuchos::ParameterList > &params, int &maxNumOrthogPasses, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType &blkTol, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType &singTol)
Read ICGS options from the given parameter list.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector, measured as the Frobenius norm of ...
void setMyParamList(const RCP< ParameterList > &paramList)
RCP< ParameterList > getNonconstParameterList()
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.
void setSingTol(const MagnitudeType sing_tol)
Set parameter for singular block detection.
MagnitudeType getSingTol() const
Return parameter for singular block detection.
An implementation of the Belos::MatOrthoManager that performs orthogonalization using multiple steps ...
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 ...
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.
TypeTo as(const TypeFrom &t)
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters() const
&quot;Fast&quot; but possibly unsafe or less accurate parameters.
Class which defines basic traits for the operator type.
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Belos header file which uses auto-configuration information to include necessary C++ headers...
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 ...
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 ...
ICGSOrthoManager(const std::string &label="Belos", Teuchos::RCP< const OP > Op=Teuchos::null, const int max_ortho_steps=2, const MagnitudeType blk_tol=10 *MGT::squareroot(MGT::eps()), const MagnitudeType sing_tol=10 *MGT::eps())
Constructor specifying re-orthogonalization tolerance.
Teuchos::RCP< const Teuchos::ParameterList > TEUCHOS_DEPRECATED getDefaultIcgsParameters()
Default parameters for ICGSOrthoManager.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
bool is_null() const