13#ifndef __Belos_SimpleOrthoManager_hpp
14#define __Belos_SimpleOrthoManager_hpp
21#include <Teuchos_ParameterList.hpp>
22#include <Teuchos_StandardCatchMacros.hpp>
23#include <Teuchos_TimeMonitor.hpp>
35 template<
class Scalar,
class MV,
class DM>
41 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType
magnitude_type;
46 typedef Teuchos::ScalarTraits<Scalar> STS;
47 typedef Teuchos::ScalarTraits<magnitude_type> STM;
52 Teuchos::RCP<OutputManager<Scalar> > outMan_;
54 bool reorthogonalize_;
58 mutable Teuchos::RCP<Teuchos::ParameterList> defaultParams_;
60#ifdef BELOS_TEUCHOS_TIME_MONITOR
62 Teuchos::RCP<Teuchos::Time> timerOrtho_;
64 Teuchos::RCP<Teuchos::Time> timerProject_;
66 Teuchos::RCP<Teuchos::Time> timerNormalize_;
76 static Teuchos::RCP<Teuchos::Time>
77 makeTimer (
const std::string& prefix,
78 const std::string& timerName)
80 const std::string timerLabel =
81 prefix.empty() ? timerName : (prefix +
": " + timerName);
82 return Teuchos::TimeMonitor::getNewCounter (timerLabel);
94 Teuchos::RCP<const Teuchos::ParameterList>
97 using Teuchos::ParameterList;
98 using Teuchos::parameterList;
101 const std::string defaultNormalizationMethod (
"MGS");
102 const bool defaultReorthogonalization =
false;
104 if (defaultParams_.is_null()) {
105 RCP<ParameterList> params = parameterList (
"Simple");
106 params->set (
"Normalization", defaultNormalizationMethod,
107 "Which normalization method to use. Valid values are \"MGS\""
108 " (for Modified Gram-Schmidt) and \"CGS\" (for Classical "
110 params->set (
"Reorthogonalization", defaultReorthogonalization,
111 "Whether to perform one (unconditional) reorthogonalization "
113 defaultParams_ = params;
115 return defaultParams_;
123 Teuchos::RCP<const Teuchos::ParameterList>
126 using Teuchos::ParameterList;
127 using Teuchos::parameterList;
131 const std::string fastNormalizationMethod (
"CGS");
132 const bool fastReorthogonalization =
false;
136 fastParams->set (
"Normalization", fastNormalizationMethod);
137 fastParams->set (
"Reorthogonalization", fastReorthogonalization);
145 using Teuchos::ParameterList;
146 using Teuchos::parameterList;
148 using Teuchos::Exceptions::InvalidParameter;
151 RCP<ParameterList> params;
152 if (plist.is_null ()) {
153 params = parameterList (*defaultParams);
156 params->validateParametersAndSetDefaults (*defaultParams);
158 const std::string normalizeImpl = params->get<std::string>(
"Normalization");
159 const bool reorthogonalize = params->get<
bool>(
"Reorthogonalization");
161 if (normalizeImpl ==
"MGS" ||
162 normalizeImpl ==
"Mgs" ||
163 normalizeImpl ==
"mgs") {
165 params->set (
"Normalization", std::string (
"MGS"));
168 params->set (
"Normalization", std::string (
"CGS"));
170 reorthogonalize_ = reorthogonalize;
172 this->setMyParamList (params);
186 const std::string& label,
187 const Teuchos::RCP<Teuchos::ParameterList>& params) :
191#ifdef BELOS_TEUCHOS_TIME_MONITOR
192 timerOrtho_ = makeTimer (label,
"All orthogonalization");
193 timerProject_ = makeTimer (label,
"Projection");
194 timerNormalize_ = makeTimer (label,
"Normalization");
198 if (! outMan_.is_null ()) {
200 std::ostream& dbg = outMan_->stream(
Debug);
201 dbg <<
"Belos::SimpleOrthoManager constructor:" << endl
202 <<
"-- Normalization method: "
203 << (useMgs_ ?
"MGS" :
"CGS") << endl
204 <<
"-- Reorthogonalize (unconditionally)? "
205 << (reorthogonalize_ ?
"Yes" :
"No") << endl;
215#ifdef BELOS_TEUCHOS_TIME_MONITOR
216 timerOrtho_ = makeTimer (label,
"All orthogonalization");
217 timerProject_ = makeTimer (label,
"Projection");
218 timerNormalize_ = makeTimer (label,
"Normalization");
227 void innerProd (
const MV &X,
const MV &Y, DM & Z)
const {
231 void norm (
const MV& X, std::vector<magnitude_type>& normVec)
const {
235 if (normVec.size () <
static_cast<size_t> (numCols)) {
236 normVec.resize (numCols);
243 Teuchos::Array<Teuchos::RCP<DM>> C,
244 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const
246#ifdef BELOS_TEUCHOS_TIME_MONITOR
247 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
248 Teuchos::TimeMonitor timerMonitorProject(*timerProject_);
251 allocateProjectionCoefficients (C, Q, X,
true);
252 rawProject (X, Q, C);
253 if (reorthogonalize_) {
254 Teuchos::Array<Teuchos::RCP<DM> > C2;
255 allocateProjectionCoefficients (C2, Q, X,
false);
256 for (
int k = 0; k < Q.size(); ++k)
264#ifdef BELOS_TEUCHOS_TIME_MONITOR
265 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
266 Teuchos::TimeMonitor timerMonitorProject(*timerNormalize_);
270 return normalizeMgs (X, B);
272 return normalizeCgs (X, B);
279 Teuchos::Array<Teuchos::RCP<DM>> C,
281 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const
294 const Scalar ONE = STS::one();
299 for (
int k = 0; k < ncols; ++k) {
311 Teuchos::RCP<DM> X1_T_X2 =
DMT::Create(ncols_X1, ncols_X2);
316 void setLabel (
const std::string& label) { label_ = label; }
317 const std::string&
getLabel()
const {
return label_; }
322 normalizeMgs (MV &X, Teuchos::RCP<DM> &B)
const
324 using Teuchos::Range1D;
343 std::vector<magnitude_type> normVec (1);
344 for (
int j = 0; j < numCols; ++j) {
347 for (
int i = 0; i < j; ++i) {
349 const MV& X_i = *X_prv;
353 if (reorthogonalize_) {
365 if (normVec[0] != STM::zero()) {
376 normalizeCgs (MV &X, Teuchos::RCP<DM> &B)
const
378 using Teuchos::Range1D;
395 std::vector<magnitude_type> normVec (1);
404 norm (*X_cur, normVec);
408 if (theNorm != STM::zero ()) {
409 const Scalar invNorm = STS::one () / theNorm;
418 for (
int j = 1; j < numCols; ++j) {
428 if (reorthogonalize_) {
435 norm (*X_cur, normVec);
439 if (theNorm != STM::zero ()) {
440 const Scalar invNorm = STS::one () / theNorm;
452 allocateProjectionCoefficients (Teuchos::Array<Teuchos::RCP<DM>>& C,
453 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
455 const bool attemptToRecycle =
true)
const
459 const int num_Q_blocks = Q.size();
461 C.resize (num_Q_blocks);
464 int numAllocated = 0;
465 if (attemptToRecycle) {
466 for (
int i = 0; i < num_Q_blocks; ++i) {
470 if (C[i].is_null ()) {
487 for (
int i = 0; i < num_Q_blocks; ++i) {
493 if (! outMan_.is_null()) {
495 std::ostream& dbg = outMan_->stream(
Debug);
496 dbg <<
"SimpleOrthoManager::allocateProjectionCoefficients: "
497 <<
"Allocated " << numAllocated <<
" blocks out of "
498 << num_Q_blocks << endl;
504 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
505 Teuchos::ArrayView<Teuchos::RCP<DM> > C)
const
508 const int num_Q_blocks = Q.size();
509 for (
int i = 0; i < num_Q_blocks; ++i) {
511 const MV& Qi = *Q[i];
Belos header file which uses auto-configuration information to include necessary C++ headers.
Declaration of basic traits for the multivector type.
Templated virtual class for providing orthogonalization/orthonormalization methods.
Class which manages the output and verbosity of the Belos solvers.
Virtual base class which defines basic traits for the multi-vector type.
static Teuchos::RCP< DM > Subview(DM &source, int numRows, int numCols, int startRow=0, int startCol=0)
static void SyncHostToDevice(DM &dm)
static void PutScalar(DM &dm, Scalar value=Teuchos::ScalarTraits< Scalar >::zero())
static int GetNumRows(const DM &dm)
static void SyncDeviceToHost(DM &dm)
static void Add(DM &thisDM, const DM &sourceDM)
static void Reshape(DM &dm, const int numrows, const int numcols, bool initZero=false)
static Scalar & Value(DM &dm, const int i, const int j)
static Teuchos::ScalarTraits< Scalar >::magnitudeType NormFrobenius(const DM &dm)
static Teuchos::RCP< DM > Create()
static int GetNumCols(const DM &dm)
Traits class which defines basic operations on multivectors.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &normvec, NormType type=TwoNorm)
static void MvTimesMatAddMv(const Scalar alpha, const MV &A, const DM &B, const Scalar beta, MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static void MvTransMv(const Scalar alpha, const MV &A, const MV &mv, DM &B)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static void MvScale(MV &mv, const Scalar alpha)
static int GetNumberVecs(const MV &mv)
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
void norm(const MV &X, std::vector< magnitude_type > &normVec) const
magnitude_type orthonormError(const MV &X) const
This method computes the error in orthonormality of a multivector.
void project(MV &X, Teuchos::Array< Teuchos::RCP< DM > > 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, DM &Z) const
Provides the inner product defining the orthogonality concepts.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
const std::string & getLabel() const
This method returns the label being used by the timers in the orthogonalization manager.
void setLabel(const std::string &label)
This method sets the label used by the timers in the orthogonalization manager.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a default list of parameters.
virtual ~SimpleOrthoManager()
Virtual destructor for memory safety of derived classes.
SimpleOrthoManager(const std::string &label="Belos")
Constructor.
virtual int projectAndNormalizeImpl(MV &X, Teuchos::Array< Teuchos::RCP< DM > > C, Teuchos::RCP< DM > B, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q) const
int normalize(MV &X, Teuchos::RCP< DM > B) const
This method takes a multivector X and attempts to compute an orthonormal basis for ,...
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get a "fast" list of parameters.
SimpleOrthoManager(const Teuchos::RCP< OutputManager< Scalar > > &outMan, const std::string &label, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor.
magnitude_type orthogError(const MV &X1, const MV &X2) const
This method computes the error in orthogonality of two multivectors.