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