45 #ifndef __Belos_SimpleOrthoManager_hpp 46 #define __Belos_SimpleOrthoManager_hpp 52 #include <Teuchos_ParameterList.hpp> 53 #include <Teuchos_ParameterListAcceptorDefaultBase.hpp> 54 #include <Teuchos_StandardCatchMacros.hpp> 55 #include <Teuchos_TimeMonitor.hpp> 67 template<
class Scalar,
class MV>
70 public Teuchos::ParameterListAcceptorDefaultBase
74 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType
magnitude_type;
75 typedef Teuchos::SerialDenseMatrix<int, Scalar>
mat_type;
76 typedef Teuchos::RCP<Teuchos::SerialDenseMatrix<int, Scalar> >
mat_ptr;
80 typedef Teuchos::ScalarTraits<Scalar> STS;
81 typedef Teuchos::ScalarTraits<magnitude_type> STM;
86 Teuchos::RCP<OutputManager<Scalar> > outMan_;
88 bool reorthogonalize_;
92 mutable Teuchos::RCP<Teuchos::ParameterList> defaultParams_;
94 #ifdef BELOS_TEUCHOS_TIME_MONITOR 95 Teuchos::RCP<Teuchos::Time> timerOrtho_;
98 Teuchos::RCP<Teuchos::Time> timerProject_;
100 Teuchos::RCP<Teuchos::Time> timerNormalize_;
110 static Teuchos::RCP<Teuchos::Time>
111 makeTimer (
const std::string& prefix,
112 const std::string& timerName)
114 const std::string timerLabel =
115 prefix.empty() ? timerName : (prefix +
": " + timerName);
116 return Teuchos::TimeMonitor::getNewCounter (timerLabel);
118 #endif // BELOS_TEUCHOS_TIME_MONITOR 128 Teuchos::RCP<const Teuchos::ParameterList>
131 using Teuchos::ParameterList;
132 using Teuchos::parameterList;
135 const std::string defaultNormalizationMethod (
"MGS");
136 const bool defaultReorthogonalization =
false;
138 if (defaultParams_.is_null()) {
139 RCP<ParameterList> params = parameterList (
"Simple");
140 params->set (
"Normalization", defaultNormalizationMethod,
141 "Which normalization method to use. Valid values are \"MGS\"" 142 " (for Modified Gram-Schmidt) and \"CGS\" (for Classical " 144 params->set (
"Reorthogonalization", defaultReorthogonalization,
145 "Whether to perform one (unconditional) reorthogonalization " 147 defaultParams_ = params;
149 return defaultParams_;
161 static TEUCHOS_DEPRECATED Teuchos::RCP<const Teuchos::ParameterList>
164 using Teuchos::ParameterList;
165 using Teuchos::parameterList;
168 const std::string defaultNormalizationMethod (
"MGS");
169 const bool defaultReorthogonalization =
false;
171 RCP<ParameterList> params = parameterList (
"Simple");
172 params->set (
"Normalization", defaultNormalizationMethod,
173 "Which normalization method to use. Valid values are \"MGS\"" 174 " (for Modified Gram-Schmidt) and \"CGS\" (for Classical " 176 params->set (
"Reorthogonalization", defaultReorthogonalization,
177 "Whether to perform one (unconditional) reorthogonalization " 187 Teuchos::RCP<const Teuchos::ParameterList>
190 using Teuchos::ParameterList;
191 using Teuchos::parameterList;
195 const std::string fastNormalizationMethod (
"CGS");
196 const bool fastReorthogonalization =
false;
200 fastParams->set (
"Normalization", fastNormalizationMethod);
201 fastParams->set (
"Reorthogonalization", fastReorthogonalization);
209 using Teuchos::ParameterList;
210 using Teuchos::parameterList;
212 using Teuchos::Exceptions::InvalidParameter;
215 RCP<ParameterList> params;
216 if (plist.is_null ()) {
217 params = parameterList (*defaultParams);
220 params->validateParametersAndSetDefaults (*defaultParams);
222 const std::string normalizeImpl = params->get<std::string>(
"Normalization");
223 const bool reorthogonalize = params->get<
bool>(
"Reorthogonalization");
225 if (normalizeImpl ==
"MGS" ||
226 normalizeImpl ==
"Mgs" ||
227 normalizeImpl ==
"mgs") {
229 params->set (
"Normalization", std::string (
"MGS"));
232 params->set (
"Normalization", std::string (
"CGS"));
234 reorthogonalize_ = reorthogonalize;
236 setMyParamList (params);
250 const std::string& label,
251 const Teuchos::RCP<Teuchos::ParameterList>& params) :
255 #ifdef BELOS_TEUCHOS_TIME_MONITOR 256 timerOrtho_ = makeTimer (label,
"All orthogonalization");
257 timerProject_ = makeTimer (label,
"Projection");
258 timerNormalize_ = makeTimer (label,
"Normalization");
259 #endif // BELOS_TEUCHOS_TIME_MONITOR 262 if (! outMan_.is_null ()) {
264 std::ostream& dbg = outMan_->stream(
Debug);
265 dbg <<
"Belos::SimpleOrthoManager constructor:" << endl
266 <<
"-- Normalization method: " 267 << (useMgs_ ?
"MGS" :
"CGS") << endl
268 <<
"-- Reorthogonalize (unconditionally)? " 269 << (reorthogonalize_ ?
"Yes" :
"No") << endl;
279 #ifdef BELOS_TEUCHOS_TIME_MONITOR 280 timerOrtho_ = makeTimer (label,
"All orthogonalization");
281 timerProject_ = makeTimer (label,
"Projection");
282 timerNormalize_ = makeTimer (label,
"Normalization");
283 #endif // BELOS_TEUCHOS_TIME_MONITOR 291 void innerProd (
const MV &X,
const MV &Y, mat_type& Z)
const {
295 void norm (
const MV& X, std::vector<magnitude_type>& normVec)
const {
299 if (normVec.size () <
static_cast<size_t> (numCols)) {
300 normVec.resize (numCols);
307 Teuchos::Array<mat_ptr> C,
308 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const 310 #ifdef BELOS_TEUCHOS_TIME_MONITOR 311 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
312 Teuchos::TimeMonitor timerMonitorProject(*timerProject_);
313 #endif // BELOS_TEUCHOS_TIME_MONITOR 315 allocateProjectionCoefficients (C, Q, X,
true);
316 rawProject (X, Q, C);
317 if (reorthogonalize_) {
318 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,Scalar> > > C2;
319 allocateProjectionCoefficients (C2, Q, X,
false);
320 for (
int k = 0; k < Q.size(); ++k)
328 #ifdef BELOS_TEUCHOS_TIME_MONITOR 329 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
330 Teuchos::TimeMonitor timerMonitorProject(*timerNormalize_);
331 #endif // BELOS_TEUCHOS_TIME_MONITOR 334 return normalizeMgs (X, B);
336 return normalizeCgs (X, B);
342 Teuchos::Array<mat_ptr> C,
344 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const 355 const Scalar ONE = STS::one();
357 mat_type XTX (ncols, ncols);
359 for (
int k = 0; k < ncols; ++k) {
362 return XTX.normFrobenius();
370 mat_type X1_T_X2 (ncols_X1, ncols_X2);
372 return X1_T_X2.normFrobenius();
375 void setLabel (
const std::string& label) { label_ = label; }
376 const std::string&
getLabel()
const {
return label_; }
382 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,Scalar> > B)
const 384 using Teuchos::Range1D;
395 B = rcp (
new mat_type (numCols, numCols));
396 }
else if (B->numRows () != numCols || B->numCols () != numCols) {
397 B->shape (numCols, numCols);
401 std::vector<magnitude_type> normVec (1);
402 for (
int j = 0; j < numCols; ++j) {
405 for (
int i = 0; i < j; ++i) {
407 const MV& X_i = *X_prv;
408 mat_type B_ij (View, *B, 1, 1, i, j);
411 if (reorthogonalize_) {
415 const Scalar B_ij_first = (*B)(i, j);
418 (*B)(i, j) += B_ij_first;
423 const magnitude_type theNorm = normVec[0];
424 (*B)(j, j) = theNorm;
425 if (normVec[0] != STM::zero()) {
436 normalizeCgs (MV &X, mat_ptr B)
const 438 using Teuchos::Range1D;
449 B = rcp (
new mat_type (numCols, numCols));
450 }
else if (B->numRows() != numCols || B->numCols() != numCols) {
451 B->shape (numCols, numCols);
453 mat_type& B_ref = *B;
456 std::vector<magnitude_type> normVec (1);
459 mat_type B2 (numCols, numCols);
465 norm (*X_cur, normVec);
466 const magnitude_type theNorm = normVec[0];
467 B_ref(0,0) = theNorm;
468 if (theNorm != STM::zero ()) {
469 const Scalar invNorm = STS::one () / theNorm;
478 for (
int j = 1; j < numCols; ++j) {
481 mat_type B_prvcur (View, B_ref, j, 1, 0, j);
488 if (reorthogonalize_) {
489 mat_type B2_prvcur (View, B2, j, 1, 0, j);
492 B_prvcur += B2_prvcur;
495 norm (*X_cur, normVec);
496 const magnitude_type theNorm = normVec[0];
497 B_ref(j,j) = theNorm;
498 if (theNorm != STM::zero ()) {
499 const Scalar invNorm = STS::one () / theNorm;
511 allocateProjectionCoefficients (Teuchos::Array<mat_ptr>& C,
512 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
514 const bool attemptToRecycle =
true)
const 518 const int num_Q_blocks = Q.size();
520 C.resize (num_Q_blocks);
523 int numAllocated = 0;
524 if (attemptToRecycle) {
525 for (
int i = 0; i < num_Q_blocks; ++i) {
529 if (C[i].is_null ()) {
530 C[i] = rcp (
new mat_type (ncols_Qi, ncols_X));
534 mat_type& Ci = *C[i];
535 if (Ci.numRows() != ncols_Qi || Ci.numCols() != ncols_X) {
536 Ci.shape (ncols_Qi, ncols_X);
540 Ci.putScalar (STS::zero());
546 for (
int i = 0; i < num_Q_blocks; ++i) {
548 C[i] = rcp (
new mat_type (ncols_Qi, ncols_X));
552 if (! outMan_.is_null()) {
554 std::ostream& dbg = outMan_->stream(
Debug);
555 dbg <<
"SimpleOrthoManager::allocateProjectionCoefficients: " 556 <<
"Allocated " << numAllocated <<
" blocks out of " 557 << num_Q_blocks << endl;
563 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
564 Teuchos::ArrayView<mat_ptr> C)
const 567 const int num_Q_blocks = Q.size();
568 for (
int i = 0; i < num_Q_blocks; ++i) {
569 mat_type& Ci = *C[i];
570 const MV& Qi = *Q[i];
579 #endif // __Belos_SimpleOrthoManager_hpp void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Project X against the (orthogonal) entries of Q.
void innerProd(const MV &X, const MV &Y, mat_type &Z) const
Provides the inner product defining the orthogonality concepts.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
magnitude_type orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
virtual ~SimpleOrthoManager()
Virtual destructor for memory safety of derived classes.
Class which manages the output and verbosity of the Belos solvers.
int normalize(MV &X, mat_ptr B) const
This method takes a multivector X and attempts to compute an orthonormal basis for ...
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
Teuchos::RCP< Teuchos::SerialDenseMatrix< int, Scalar > > mat_ptr
SimpleOrthoManager(const std::string &label)
Constructor.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
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.
SimpleOrthoManager(const Teuchos::RCP< OutputManager< Scalar > > &outMan, const std::string &label, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor.
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Traits class which defines basic operations on multivectors.
Belos's templated virtual class for providing routines for orthogonalization and orthonormzalition of...
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get a "fast" list of parameters.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
Simple OrthoManager implementation for benchmarks.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a default list of parameters.
int projectAndNormalize(MV &X, Teuchos::Array< mat_ptr > C, mat_ptr B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
Project X against the Q[i] and normalize X.
void norm(const MV &X, std::vector< magnitude_type > &normVec) const
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
Templated virtual class for providing orthogonalization/orthonormalization methods.
static TEUCHOS_DEPRECATED Teuchos::RCP< const Teuchos::ParameterList > getDefaultParameters()
Get a default list of parameters.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
static void MvScale(MV &mv, const ScalarType alpha)
Scale each element of the vectors in mv with alpha.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
magnitude_type orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors.
Belos header file which uses auto-configuration information to include necessary C++ headers...
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.