Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosTsqrOrthoManager.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 
44 
45 #ifndef __BelosTsqrOrthoManager_hpp
46 #define __BelosTsqrOrthoManager_hpp
47 
49 // Belos doesn't have an SVQB orthomanager implemented yet, so we just
50 // use a default orthomanager for the case where the inner product
51 // matrix is nontrivial.
53 
54 
55 namespace Belos {
56 
78 template<class Scalar, class MV>
80 public:
81  typedef Scalar scalar_type;
85  typedef MV multivector_type;
86 
89 
98  virtual int
99  normalizeOutOfPlace (MV& X, MV& Q, mat_ptr B) const = 0;
100 
119  virtual int
121  MV& X_out,
123  mat_ptr B,
125 
128 };
129 
136 template<class Scalar, class MV>
138  public OrthoManager<Scalar, MV>,
139  public OutOfPlaceNormalizerMixin<Scalar, MV>
140 {
141 public:
142  typedef Scalar scalar_type;
145  typedef MV multivector_type;
146 
149 
151  impl_.setParameterList (params);
152  }
153 
155  return impl_.getNonconstParameterList ();
156  }
157 
159  return impl_.unsetParameterList ();
160  }
161 
170  return impl_.getValidParameters();
171  }
172 
183  return impl_.getFastParameters();
184  }
185 
202  const std::string& label = "Belos") :
203  impl_ (params, label)
204  {}
205 
210  TsqrOrthoManager (const std::string& label) :
211  impl_ (label)
212  {}
213 
215  virtual ~TsqrOrthoManager() {}
216 
238  void
240  {
241  impl_.setReorthogonalizationCallback (callback);
242  }
243 
244  void innerProd (const MV &X, const MV &Y, mat_type& Z) const {
245  return impl_.innerProd (X, Y, Z);
246  }
247 
248  void norm (const MV& X, std::vector<magnitude_type>& normVec) const {
249  return impl_.norm (X, normVec);
250  }
251 
252  void
253  project (MV &X,
256  {
257  return impl_.project (X, C, Q);
258  }
259 
260  int
261  normalize (MV &X, mat_ptr B) const
262  {
263  return impl_.normalize (X, B);
264  }
265 
266 protected:
267  virtual int
270  mat_ptr B,
272  {
273  return impl_.projectAndNormalize (X, C, B, Q);
274  }
275 
276 public:
293  int
294  normalizeOutOfPlace (MV& X, MV& Q, mat_ptr B) const
295  {
296  return impl_.normalizeOutOfPlace (X, Q, B);
297  }
298 
319  int
321  MV& X_out,
323  mat_ptr B,
325  {
326  return impl_.projectAndNormalizeOutOfPlace (X_in, X_out, C, B, Q);
327  }
328 
329  magnitude_type orthonormError (const MV &X) const {
330  return impl_.orthonormError (X);
331  }
332 
333  magnitude_type orthogError (const MV &X1, const MV &X2) const {
334  return impl_.orthogError (X1, X2);
335  }
336 
344  void setLabel (const std::string& label) {
345  impl_.setLabel (label);
346  }
347 
348  const std::string& getLabel() const { return impl_.getLabel(); }
349 
350 private:
357 
359  std::string label_;
360 };
361 
362 
376 template<class Scalar, class MV, class OP>
378  public MatOrthoManager<Scalar, MV, OP>,
379  public OutOfPlaceNormalizerMixin<Scalar, MV>
380 {
381 public:
382  typedef Scalar scalar_type;
385  typedef MV multivector_type;
387  typedef OP operator_type;
388 
391 
392 private:
402 
406 
410 
414 
415 public:
437  const std::string& label = "Belos",
439  MatOrthoManager<Scalar, MV, OP> (Op),
440  tsqr_ (params, label),
441  pDgks_ (Teuchos::null) // Initialized lazily
442  {}
443 
452  TsqrMatOrthoManager (const std::string& label = "Belos",
454  MatOrthoManager<Scalar, MV, OP> (Op),
455  tsqr_ (label),
456  pDgks_ (Teuchos::null) // Initialized lazily
457  {}
458 
460  virtual ~TsqrMatOrthoManager() {}
461 
470  return tsqr_.getValidParameters ();
471  }
472 
483  return tsqr_.getFastParameters ();
484  }
485 
487  tsqr_.setParameterList (params);
488  }
489 
490  const std::string& getLabel() const { return tsqr_.getLabel (); }
491 
492  void
494  {
495  // We override the base class' setOp() so that the
496  // DGKSOrthoManager instance gets the new op.
497  //
498  // The base_type notation helps C++ resolve the name for a
499  // member function of a templated base class.
500  base_type::setOp (Op); // base class gets a copy of the Op too
501 
502  if (! Op.is_null()) {
503  ensureDgksInit (); // Make sure the DGKS object has been initialized
504  pDgks_->setOp (Op);
505  }
506  }
507 
509  // The base_type notation helps C++ resolve the name for a
510  // member function of a templated base class.
511  return base_type::getOp();
512  }
513 
514  void
515  project (MV &X,
516  Teuchos::RCP<MV> MX,
519  {
520  if (getOp().is_null()) {
521  tsqr_.project (X, C, Q);
522  if (! MX.is_null()) {
523  // MX gets a copy of X; M is the identity operator.
524  MVT::Assign (X, *MX);
525  }
526  } else {
527  ensureDgksInit ();
528  pDgks_->project (X, MX, C, Q);
529  }
530  }
531 
532  void
533  project (MV &X,
536  {
537  project (X, Teuchos::null, C, Q);
538  }
539 
540  int
541  normalize (MV& X, Teuchos::RCP<MV> MX, mat_ptr B) const
542  {
543  if (getOp().is_null()) {
544  const int rank = tsqr_.normalize (X, B);
545  if (! MX.is_null()) {
546  // MX gets a copy of X; M is the identity operator.
547  MVT::Assign (X, *MX);
548  }
549  return rank;
550  } else {
551  ensureDgksInit ();
552  return pDgks_->normalize (X, MX, B);
553  }
554  }
555 
556  int normalize (MV& X, mat_ptr B) const {
557  return normalize (X, Teuchos::null, B);
558  }
559 
560  // Attempted fix for a warning about hiding
561  // OrthoManager::projectAndNormalize(). Unfortunately, this fix turns out
562  // to produce a compilation error with cray++, see bug #6129,
563  // <https://software.sandia.gov/bugzilla/show_bug.cgi?id=6129>.
564  //using Belos::OrthoManager<Scalar, MV>::projectAndNormalize;
565 
566 protected:
567  virtual int
569  Teuchos::RCP<MV> MX,
571  mat_ptr B,
573  {
574  if (getOp().is_null()) {
575  const int rank = tsqr_.projectAndNormalize (X, C, B, Q);
576  if (! MX.is_null()) {
577  // MX gets a copy of X; M is the identity operator.
578  MVT::Assign (X, *MX);
579  }
580  return rank;
581  } else {
582  ensureDgksInit ();
583  return pDgks_->projectAndNormalize (X, MX, C, B, Q);
584  }
585  }
586 
587 public:
588  int
589  normalizeOutOfPlace (MV& X, MV& Q, mat_ptr B) const
590  {
591  if (getOp().is_null()) {
592  return tsqr_.normalizeOutOfPlace (X, Q, B);
593  } else {
594  // DGKS normalizes in place, so we have to copy.
595  ensureDgksInit ();
596  const int rank = pDgks_->normalize (X, B);
597  MVT::Assign (X, Q);
598  return rank;
599  }
600  }
601 
602  int
604  MV& X_out,
606  mat_ptr B,
608  {
609  using Teuchos::null;
610 
611  if (getOp().is_null()) {
612  return tsqr_.projectAndNormalizeOutOfPlace (X_in, X_out, C, B, Q);
613  } else {
614  // DGKS normalizes in place, so we have to copy.
615  ensureDgksInit ();
616  const int rank = pDgks_->projectAndNormalize (X_in, null, C, B, Q);
617  MVT::Assign (X_in, X_out);
618  return rank;
619  }
620  }
621 
623  orthonormError (const MV &X, Teuchos::RCP<const MV> MX) const
624  {
625  if (getOp().is_null()) {
626  return tsqr_.orthonormError (X); // Ignore MX
627  } else {
628  ensureDgksInit ();
629  return pDgks_->orthonormError (X, MX);
630  }
631  }
632 
633  magnitude_type orthonormError (const MV &X) const {
634  return orthonormError (X, Teuchos::null);
635  }
636 
637  magnitude_type orthogError (const MV &X1, const MV &X2) const {
638  return orthogError (X1, Teuchos::null, X2);
639  }
640 
642  orthogError (const MV &X1,
644  const MV &X2) const
645  {
646  if (getOp ().is_null ()) {
647  // Ignore MX1, since we don't need to write to it.
648  return tsqr_.orthogError (X1, X2);
649  } else {
650  ensureDgksInit ();
651  return pDgks_->orthogError (X1, MX1, X2);
652  }
653  }
654 
655  void
656  setLabel (const std::string& label)
657  {
658  tsqr_.setLabel (label);
659 
660  // Make sure DGKS gets the new label, if it's initialized.
661  // Otherwise, it will get the new label on initialization.
662  if (! pDgks_.is_null ()) {
663  pDgks_->setLabel (label);
664  }
665  }
666 
667 private:
669  void
670  ensureDgksInit () const
671  {
672  // NOTE (mfh 11 Jan 2011) DGKS has a parameter that needs to be
673  // set. For now, we just use the default value of the parameter.
674  if (pDgks_.is_null ()) {
675  pDgks_ = Teuchos::rcp (new dgks_type (getLabel (), getOp ()));
676  }
677  }
678 
686  mutable tsqr_type tsqr_;
687 
694 };
695 
696 } // namespace Belos
697 
698 #endif // __BelosTsqrOrthoManager_hpp
Teuchos::RCP< mat_type > mat_ptr
virtual int projectAndNormalizeOutOfPlace(MV &X_in, MV &X_out, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const =0
Project and normalize X_in into X_out.
TsqrOrthoManagerImpl< Scalar, MV > tsqr_type
Implementation of TSQR-based orthogonalization.
void project(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
std::string label_
Label for timers (if timers are enabled at compile time).
int projectAndNormalize(MV &X, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Project X against Q and normalize X.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Interface of callback invoked by TsqrOrthoManager on reorthogonalization.
virtual ~TsqrMatOrthoManager()
Destructor (declared virtual for memory safety of derived classes).
bool is_null(const boost::shared_ptr< T > &p)
virtual int projectAndNormalizeWithMxImpl(MV &X, Teuchos::RCP< MV > MX, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
int projectAndNormalizeOutOfPlace(MV &X_in, MV &X_out, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Project and normalize X_in into X_out; overwrite X_in.
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Compute and .
magnitude_type orthogError(const MV &X1, const MV &X2) const
Return the Frobenius norm of the inner product of X1 with itself.
void norm(const MV &X, std::vector< magnitude_type > &normVec) const
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Mixin for out-of-place orthogonalization.
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get &quot;fast&quot; parameters for TsqrMatOrthoManager.
Teuchos::RCP< dgks_type > pDgks_
DGKS orthogonalization manager.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set parameters from the given parameter list.
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get &quot;fast&quot; parameters for TsqrOrthoManagerImpl.
TSQR-based OrthoManager subclass implementation.
virtual ~OutOfPlaceNormalizerMixin()
Trivial virtual destructor, to silence compiler warnings.
int normalize(MV &X, mat_ptr B)
Orthogonalize the columns of X in place.
int normalize(MV &X, mat_ptr B) const
magnitude_type orthonormError(const MV &X, Teuchos::RCP< const MV > MX) const
This method computes the error in orthonormality of a multivector. The method has the option of explo...
OP operator_type
Operator type with which this class was specialized.
void setLabel(const std::string &label)
Set the label for timers.
Teuchos::RCP< mat_type > mat_ptr
void setLabel(const std::string &label)
Set the label for (the timers for) this orthogonalization manager, and create new timers if the label...
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
static void Assign(const MV &A, MV &mv)
mv := A
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Default valid parameter list.
Traits class which defines basic operations on multivectors.
TsqrOrthoManager(const std::string &label)
Constructor (that sets default parameters).
int normalize(MV &X, Teuchos::RCP< MV > MX, mat_ptr B) const
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
int normalizeOutOfPlace(MV &X, MV &Q, mat_ptr B) const
Normalize X into Q*B, overwriting X with invalid values.
Classical Gram-Schmidt (with DGKS correction) implementation of the Belos::OrthoManager class...
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TsqrOrthoManagerImpl< Scalar, MV > impl_
The implementation of TSQR.
void innerProd(const MV &X, const MV &Y, mat_type &Z) const
Provides the inner product defining the orthogonality concepts.
void setOp(Teuchos::RCP< const OP > Op)
bool is_null(const RCP< T > &p)
void setReorthogonalizationCallback(const Teuchos::RCP< ReorthogonalizationCallback< Scalar > > &callback)
Set callback to be invoked on reorthogonalization.
virtual int normalizeOutOfPlace(MV &X, MV &Q, mat_ptr B) const =0
Normalize X into Q*B.
magnitude_type orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
const std::string & getLabel() const
Get the label for timers (if timers are enabled).
magnitude_type orthonormError(const MV &X) const
Return .
tsqr_type tsqr_
TSQR + BGS orthogonalization manager implementation.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get default parameters for TsqrMatOrthoManager.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
int projectAndNormalizeOutOfPlace(MV &X_in, MV &X_out, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Project and normalize X_in into X_out.
int normalizeOutOfPlace(MV &X, MV &Q, mat_ptr B) const
Normalize X into Q*B.
virtual ~TsqrOrthoManager()
Destructor, declared virtual for safe inheritance.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Default valid parameter list.
void ensureDgksInit() const
Ensure that the DGKSOrthoManager instance is initialized.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &params)
int normalize(MV &X, mat_ptr B) const
MV multivector_type
Multivector type with which this class was specialized.
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
virtual int projectAndNormalizeImpl(MV &X, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
MV multivector_type
Multivector type with which this class was specialized.
MatOrthoManager< Scalar, MV, OP > base_type
This will be used to help C++ resolve getOp().
void setLabel(const std::string &label)
This method sets the label used by the timers in the orthogonalization manager.
TsqrMatOrthoManager(const std::string &label="Belos", Teuchos::RCP< const OP > Op=Teuchos::null)
Constructor (that sets default parameters).
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
DGKSOrthoManager< Scalar, MV, OP > dgks_type
Implementation of DGKS-based orthogonalization.
Teuchos::RCP< const OP > getOp() const
Get operator.
TsqrMatOrthoManager(const Teuchos::RCP< Teuchos::ParameterList > &params, const std::string &label="Belos", Teuchos::RCP< const OP > Op=Teuchos::null)
Constructor (that sets user-specified parameters).
MultiVecTraits< Scalar, MV > MVT
Traits class for the multivector type.
An implementation of the Belos::MatOrthoManager that performs orthogonalization using (potentially) m...
Orthogonalization manager back end based on Tall Skinny QR (TSQR)
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &params)
magnitude_type orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors. This method. ...
Belos&#39;s templated virtual class for providing routines for orthogonalization and orthonormzalition of...
int projectAndNormalizeOutOfPlace(MV &X_in, MV &X_out, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Project and normalize X_in into X_out; overwrite X_in.
int normalizeOutOfPlace(MV &X, MV &Q, mat_ptr B)
Normalize X into Q*B, overwriting X.
MatOrthoManager subclass using TSQR or DGKS.
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters() const
Get &quot;fast&quot; parameters for TsqrOrthoManager.
Teuchos::RCP< const OP > getOp() const
void setOp(Teuchos::RCP< const OP > Op)
Set operator.
magnitude_type orthogError(const MV &X1, Teuchos::RCP< const MV > MX1, const MV &X2) const
This method computes the error in orthogonality of two multivectors. The method has the option of exp...
TsqrOrthoManager(const Teuchos::RCP< Teuchos::ParameterList > &params, const std::string &label="Belos")
Constructor (that sets user-specified parameters).
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
magnitude_type orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
TSQR-based OrthoManager subclass.
magnitude_type orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors.