45 #ifndef __BelosTsqrOrthoManagerImpl_hpp 46 #define __BelosTsqrOrthoManagerImpl_hpp 52 #include "Teuchos_as.hpp" 53 #include "Teuchos_LAPACK.hpp" 54 #include "Teuchos_ParameterList.hpp" 55 #include "Teuchos_ParameterListAcceptorDefaultBase.hpp" 56 #ifdef BELOS_TEUCHOS_TIME_MONITOR 57 # include "Teuchos_TimeMonitor.hpp" 58 #endif // BELOS_TEUCHOS_TIME_MONITOR 130 template<
class Scalar>
132 public std::binary_function<Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
133 Teuchos::ArrayView<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
153 operator() (Teuchos::ArrayView<magnitude_type> normsBeforeFirstPass,
154 Teuchos::ArrayView<magnitude_type> normsAfterFirstPass) = 0;
157 template<
class Scalar>
191 template<
class Scalar,
class MV>
193 public Teuchos::ParameterListAcceptorDefaultBase {
199 typedef Teuchos::SerialDenseMatrix<int, Scalar>
mat_type;
203 typedef Teuchos::ScalarTraits<Scalar> SCT;
204 typedef Teuchos::ScalarTraits<magnitude_type> SCTM;
207 typedef typename MVT::tsqr_adaptor_type tsqr_adaptor_type;
217 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters ()
const;
220 void setParameterList (
const Teuchos::RCP<Teuchos::ParameterList>& params);
232 Teuchos::RCP<const Teuchos::ParameterList> getFastParameters ();
251 const std::string& label);
281 reorthogCallback_ = callback;
292 if (label != label_) {
295 #ifdef BELOS_TEUCHOS_TIME_MONITOR 296 clearTimer (label,
"All orthogonalization");
297 clearTimer (label,
"Projection");
298 clearTimer (label,
"Normalization");
300 timerOrtho_ = makeTimer (label,
"All orthogonalization");
301 timerProject_ = makeTimer (label,
"Projection");
302 timerNormalize_ = makeTimer (label,
"Normalization");
303 #endif // BELOS_TEUCHOS_TIME_MONITOR 308 const std::string&
getLabel ()
const {
return label_; }
321 MVT::MvTransMv (SCT::one(), X, Y, Z);
342 norm (
const MV& X, std::vector<magnitude_type>& normVec)
const;
355 Teuchos::Array<mat_ptr> C,
356 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q);
371 int normalize (MV& X, mat_ptr B);
392 normalizeOutOfPlace (MV& X, MV& Q, mat_ptr B);
409 Teuchos::Array<mat_ptr> C,
411 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
415 return projectAndNormalizeImpl (X, X,
false, C, B, Q);
440 Teuchos::Array<mat_ptr> C,
442 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
446 return projectAndNormalizeImpl (X_in, X_out,
true, C, B, Q);
456 const Scalar ONE = SCT::one();
457 const int ncols = MVT::GetNumberVecs(X);
458 mat_type XTX (ncols, ncols);
459 innerProd (X, X, XTX);
460 for (
int k = 0; k < ncols; ++k) {
463 return XTX.normFrobenius();
471 const int ncols_X1 = MVT::GetNumberVecs (X1);
472 const int ncols_X2 = MVT::GetNumberVecs (X2);
473 mat_type X1_T_X2 (ncols_X1, ncols_X2);
474 innerProd (X1, X2, X1_T_X2);
475 return X1_T_X2.normFrobenius();
489 Teuchos::RCP<Teuchos::ParameterList> params_;
492 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
498 tsqr_adaptor_type tsqrAdaptor_;
517 bool randomizeNullSpace_;
524 bool reorthogonalizeBlocks_;
529 bool throwOnReorthogFault_;
532 magnitude_type blockReorthogThreshold_;
535 magnitude_type relativeRankTolerance_;
543 bool forceNonnegativeDiagonal_;
545 #ifdef BELOS_TEUCHOS_TIME_MONITOR 546 Teuchos::RCP<Teuchos::Time> timerOrtho_;
550 Teuchos::RCP<Teuchos::Time> timerProject_;
553 Teuchos::RCP<Teuchos::Time> timerNormalize_;
554 #endif // BELOS_TEUCHOS_TIME_MONITOR 557 Teuchos::RCP<ReorthogonalizationCallback<Scalar> > reorthogCallback_;
559 #ifdef BELOS_TEUCHOS_TIME_MONITOR 560 static Teuchos::RCP<Teuchos::Time>
569 makeTimer (
const std::string& prefix,
570 const std::string& timerName)
572 const std::string timerLabel =
573 prefix.empty() ? timerName : (prefix +
": " + timerName);
574 return Teuchos::TimeMonitor::getNewCounter (timerLabel);
583 clearTimer (
const std::string& prefix,
584 const std::string& timerName)
586 const std::string timerLabel =
587 prefix.empty() ? timerName : (prefix +
": " + timerName);
588 Teuchos::TimeMonitor::clearCounter (timerLabel);
590 #endif // BELOS_TEUCHOS_TIME_MONITOR 594 raiseReorthogFault (
const std::vector<magnitude_type>& normsAfterFirstPass,
595 const std::vector<magnitude_type>& normsAfterSecondPass,
596 const std::vector<int>& faultIndices);
608 checkProjectionDims (
int& ncols_X,
612 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const;
625 allocateProjectionCoefficients (Teuchos::Array<mat_ptr>& C,
626 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
628 const bool attemptToRecycle =
true)
const;
639 projectAndNormalizeImpl (MV& X_in,
641 const bool outOfPlace,
642 Teuchos::Array<mat_ptr> C,
644 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q);
653 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
654 Teuchos::ArrayView<mat_ptr> C)
const;
659 const Teuchos::RCP<const MV>& Q,
660 const mat_ptr& C)
const;
688 int rawNormalize (MV& X, MV& Q, mat_type& B);
707 int normalizeOne (MV& X, mat_ptr B)
const;
736 int normalizeImpl (MV& X, MV& Q, mat_ptr B,
const bool outOfPlace);
739 template<
class Scalar,
class MV>
744 using Teuchos::ParameterList;
745 using Teuchos::parameterList;
747 using Teuchos::sublist;
750 RCP<const ParameterList> defaultParams = getValidParameters ();
752 RCP<ParameterList> tsqrParams;
754 RCP<ParameterList> theParams;
755 if (params.is_null()) {
756 theParams = parameterList (*defaultParams);
764 randomizeNullSpace_ =
765 theParams->get<
bool> (
"randomizeNullSpace",
766 defaultParams->get<
bool> (
"randomizeNullSpace"));
767 reorthogonalizeBlocks_ =
768 theParams->get<
bool> (
"reorthogonalizeBlocks",
769 defaultParams->get<
bool> (
"reorthogonalizeBlocks"));
770 throwOnReorthogFault_ =
771 theParams->get<
bool> (
"throwOnReorthogFault",
772 defaultParams->get<
bool> (
"throwOnReorthogFault"));
773 blockReorthogThreshold_ =
774 theParams->get<M> (
"blockReorthogThreshold",
775 defaultParams->get<M> (
"blockReorthogThreshold"));
776 relativeRankTolerance_ =
777 theParams->get<M> (
"relativeRankTolerance",
778 defaultParams->get<M> (
"relativeRankTolerance"));
779 forceNonnegativeDiagonal_ =
780 theParams->get<
bool> (
"forceNonnegativeDiagonal",
781 defaultParams->get<
bool> (
"forceNonnegativeDiagonal"));
785 if (! theParams->isSublist (
"TSQR implementation")) {
786 theParams->set (
"TSQR implementation",
787 defaultParams->sublist (
"TSQR implementation"));
789 tsqrParams = sublist (theParams,
"TSQR implementation",
true);
793 tsqrAdaptor_.setParameterList (tsqrParams);
796 setMyParamList (theParams);
799 template<
class Scalar,
class MV>
802 const std::string& label) :
806 randomizeNullSpace_ (true),
807 reorthogonalizeBlocks_ (true),
808 throwOnReorthogFault_ (false),
809 blockReorthogThreshold_ (0),
810 relativeRankTolerance_ (0),
811 forceNonnegativeDiagonal_ (false)
815 #ifdef BELOS_TEUCHOS_TIME_MONITOR 816 timerOrtho_ = makeTimer (label,
"All orthogonalization");
817 timerProject_ = makeTimer (label,
"Projection");
818 timerNormalize_ = makeTimer (label,
"Normalization");
819 #endif // BELOS_TEUCHOS_TIME_MONITOR 822 template<
class Scalar,
class MV>
828 randomizeNullSpace_ (true),
829 reorthogonalizeBlocks_ (true),
830 throwOnReorthogFault_ (false),
831 blockReorthogThreshold_ (0),
832 relativeRankTolerance_ (0),
833 forceNonnegativeDiagonal_ (false)
837 #ifdef BELOS_TEUCHOS_TIME_MONITOR 838 timerOrtho_ = makeTimer (label,
"All orthogonalization");
839 timerProject_ = makeTimer (label,
"Projection");
840 timerNormalize_ = makeTimer (label,
"Normalization");
841 #endif // BELOS_TEUCHOS_TIME_MONITOR 844 template<
class Scalar,
class MV>
847 norm (
const MV& X, std::vector<magnitude_type>& normVec)
const 852 if (normVec.size() <
static_cast<size_t>(numCols))
853 normVec.resize (numCols);
857 template<
class Scalar,
class MV>
860 Teuchos::Array<mat_ptr> C,
861 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
863 #ifdef BELOS_TEUCHOS_TIME_MONITOR 871 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
872 #endif // BELOS_TEUCHOS_TIME_MONITOR 874 int ncols_X, num_Q_blocks, ncols_Q_total;
875 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X, Q);
879 if (ncols_X == 0 || num_Q_blocks == 0 || ncols_Q_total == 0)
883 allocateProjectionCoefficients (C, Q, X,
true);
887 std::vector<magnitude_type> columnNormsBefore (ncols_X,
magnitude_type(0));
888 if (reorthogonalizeBlocks_)
893 rawProject (X, Q, C);
897 if (reorthogonalizeBlocks_) {
898 std::vector<magnitude_type> columnNormsAfter (ncols_X,
magnitude_type(0));
907 bool reorthogonalize =
false;
908 for (
int j = 0; j < ncols_X; ++j) {
909 if (columnNormsAfter[j] < relThres * columnNormsBefore[j]) {
910 reorthogonalize =
true;
914 if (reorthogonalize) {
917 if (! reorthogCallback_.is_null()) {
918 reorthogCallback_->operator() (Teuchos::arrayViewFromVector (columnNormsBefore),
919 Teuchos::arrayViewFromVector (columnNormsAfter));
922 Teuchos::Array<mat_ptr> C2;
923 allocateProjectionCoefficients (C2, Q, X,
false);
927 rawProject (X, Q, C2);
929 for (
int k = 0; k < num_Q_blocks; ++k)
936 template<
class Scalar,
class MV>
940 using Teuchos::Range1D;
943 #ifdef BELOS_TEUCHOS_TIME_MONITOR 944 Teuchos::TimeMonitor timerMonitorNormalize(*timerNormalize_);
949 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
950 #endif // BELOS_TEUCHOS_TIME_MONITOR 964 return normalizeOne (X, B);
1006 return normalizeImpl (X, *Q_, B,
false);
1009 return normalizeImpl (X, *Q_view, B,
false);
1013 template<
class Scalar,
class MV>
1017 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
1019 const bool attemptToRecycle)
const 1021 const int num_Q_blocks = Q.size();
1023 C.resize (num_Q_blocks);
1024 if (attemptToRecycle)
1026 for (
int i = 0; i < num_Q_blocks; ++i)
1032 C[i] = rcp (
new mat_type (ncols_Qi, ncols_X));
1036 if (Ci.numRows() != ncols_Qi || Ci.numCols() != ncols_X)
1037 Ci.shape (ncols_Qi, ncols_X);
1039 Ci.putScalar (SCT::zero());
1045 for (
int i = 0; i < num_Q_blocks; ++i)
1048 C[i] = rcp (
new mat_type (ncols_Qi, ncols_X));
1053 template<
class Scalar,
class MV>
1058 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1059 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
1060 Teuchos::TimeMonitor timerMonitorNormalize(*timerNormalize_);
1061 #endif // BELOS_TEUCHOS_TIME_MONITOR 1066 }
else if (numVecs == 1) {
1068 using Teuchos::Range1D;
1073 const int rank = normalizeOne (X, B);
1082 return normalizeImpl (X, Q, B,
true);
1086 template<
class Scalar,
class MV>
1091 const bool outOfPlace,
1092 Teuchos::Array<mat_ptr> C,
1094 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
1096 using Teuchos::Range1D;
1100 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1103 Teuchos::TimeMonitor timerMonitorOrtho(*timerOrtho_);
1104 #endif // BELOS_TEUCHOS_TIME_MONITOR 1109 std::invalid_argument,
1110 "Belos::TsqrOrthoManagerImpl::" 1111 "projectAndNormalizeImpl(..., outOfPlace=true, ...):" 1113 <<
" columns, but X_in has " 1118 int ncols_X, num_Q_blocks, ncols_Q_total;
1119 checkProjectionDims (ncols_X, num_Q_blocks, ncols_Q_total, X_in, Q);
1126 if (num_Q_blocks == 0 || ncols_Q_total == 0) {
1137 allocateProjectionCoefficients (C, Q, X_in,
true);
1142 std::vector<magnitude_type> normsBeforeFirstPass (ncols_X, SCTM::zero());
1143 if (reorthogonalizeBlocks_) {
1148 rawProject (X_in, Q, C);
1161 B_out = rcp (
new mat_type (ncols_X, ncols_X));
1164 TEUCHOS_TEST_FOR_EXCEPTION(B->numRows() < ncols_X || B->numCols() < ncols_X,
1165 std::invalid_argument,
1166 "normalizeOne: Input matrix B must be at " 1167 "least " << ncols_X <<
" x " << ncols_X
1168 <<
", but is instead " << B->numRows()
1169 <<
" x " << B->numCols() <<
".");
1173 B_out = rcp (
new mat_type (Teuchos::View, *B, ncols_X, ncols_X));
1179 const int firstPassRank = outOfPlace ?
1190 int rank = firstPassRank;
1206 if (firstPassRank < ncols_X && randomizeNullSpace_) {
1207 const int numNullSpaceCols = ncols_X - firstPassRank;
1208 const Range1D nullSpaceIndices (firstPassRank, ncols_X - 1);
1211 Teuchos::Array<mat_ptr> C_null (num_Q_blocks);
1212 for (
int k = 0; k < num_Q_blocks; ++k) {
1214 C_null[k] = rcp (
new mat_type (numColsQk, numNullSpaceCols));
1217 RCP<mat_type> B_null (
new mat_type (numNullSpaceCols, numNullSpaceCols));
1219 int randomVectorsRank;
1232 rawProject (*X_in_null, Q, C_null);
1240 rawProject (*X_null, Q, C_null);
1241 randomVectorsRank =
normalize (*X_null, B_null);
1247 TEUCHOS_TEST_FOR_EXCEPTION(randomVectorsRank != numNullSpaceCols,
1249 "Belos::TsqrOrthoManagerImpl::projectAndNormalize" 1250 "OutOfPlace(): After projecting and normalizing the " 1251 "random vectors (used to replace the null space " 1252 "basis vectors from normalizing X), they have rank " 1253 << randomVectorsRank <<
", but should have full " 1254 "rank " << numNullSpaceCols <<
".");
1259 if (reorthogonalizeBlocks_) {
1262 std::vector<magnitude_type>
1263 normsAfterFirstPass (firstPassRank, SCTM::zero());
1264 std::vector<magnitude_type>
1265 normsAfterSecondPass (firstPassRank, SCTM::zero());
1279 Teuchos::BLAS<int, Scalar> blas;
1280 for (
int j = 0; j < firstPassRank; ++j) {
1281 const Scalar*
const B_j = &(*B_out)(0,j);
1284 normsAfterFirstPass[j] = blas.NRM2 (firstPassRank, B_j, 1);
1288 bool reorthogonalize =
false;
1289 for (
int j = 0; j < firstPassRank; ++j) {
1298 if (normsAfterFirstPass[j] < curThreshold) {
1299 reorthogonalize =
true;
1306 if (! reorthogCallback_.is_null()) {
1307 using Teuchos::arrayViewFromVector;
1308 (*reorthogCallback_) (arrayViewFromVector (normsBeforeFirstPass),
1309 arrayViewFromVector (normsAfterFirstPass));
1323 bool reorthogFault =
false;
1325 std::vector<int> faultIndices;
1326 if (reorthogonalize) {
1327 using Teuchos::Copy;
1328 using Teuchos::NO_TRANS;
1340 Teuchos::Array<mat_ptr> C2;
1341 allocateProjectionCoefficients (C2, Q, X_in,
false);
1346 rawProject (X_in, Q, C2);
1349 RCP<mat_type> B2 (
new mat_type (ncols_X, ncols_X));
1352 const int secondPassRank = outOfPlace ?
1355 rank = secondPassRank;
1360 mat_type B_copy (Copy, *B_out, B_out->numRows(), B_out->numCols());
1362 const int err = B_out->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1363 *B2, B_copy, SCT::zero());
1364 TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::logic_error,
1365 "Teuchos::SerialDenseMatrix::multiply " 1366 "returned err = " << err <<
" != 0");
1370 for (
int k = 0; k < num_Q_blocks; ++k) {
1371 mat_type C_k_copy (Copy, *C[k], C[k]->numRows(), C[k]->numCols());
1374 const int err1 = C[k]->multiply (NO_TRANS, NO_TRANS, SCT::one(),
1375 *C2[k], B_copy, SCT::one());
1376 TEUCHOS_TEST_FOR_EXCEPTION(err1 != 0, std::logic_error,
1377 "Teuchos::SerialDenseMatrix::multiply " 1378 "returned err = " << err1 <<
" != 0");
1383 for (
int j = 0; j < rank; ++j) {
1384 const Scalar*
const B2_j = &(*B2)(0,j);
1385 normsAfterSecondPass[j] = blas.NRM2 (rank, B2_j, 1);
1390 reorthogFault =
false;
1391 for (
int j = 0; j < rank; ++j) {
1394 if (normsAfterSecondPass[j] < relativeLowerBound) {
1395 reorthogFault =
true;
1396 faultIndices.push_back (j);
1401 if (reorthogFault) {
1402 if (throwOnReorthogFault_) {
1403 raiseReorthogFault (normsAfterFirstPass,
1404 normsAfterSecondPass,
1412 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
1413 "TsqrOrthoManagerImpl has not yet implemented" 1414 " recovery from an orthogonalization fault.");
1422 template<
class Scalar,
class MV>
1426 const std::vector<magnitude_type>& normsAfterSecondPass,
1427 const std::vector<int>& faultIndices)
1430 typedef std::vector<int>::size_type size_type;
1431 std::ostringstream os;
1433 os <<
"Orthogonalization fault at the following column(s) of X:" << endl;
1434 os <<
"Column\tNorm decrease factor" << endl;
1435 for (size_type k = 0; k < faultIndices.size(); ++k) {
1436 const int index = faultIndices[k];
1438 normsAfterSecondPass[index] / normsAfterFirstPass[index];
1439 os << index <<
"\t" << decreaseFactor << endl;
1444 template<
class Scalar,
class MV>
1445 Teuchos::RCP<const Teuchos::ParameterList>
1448 using Teuchos::ParameterList;
1449 using Teuchos::parameterList;
1452 if (defaultParams_.is_null()) {
1453 RCP<ParameterList> params = parameterList (
"TsqrOrthoManagerImpl");
1457 params->set (
"TSQR implementation", *(tsqrAdaptor_.getValidParameters()),
1458 "TSQR implementation parameters.");
1462 const bool defaultRandomizeNullSpace =
true;
1463 params->set (
"randomizeNullSpace", defaultRandomizeNullSpace,
1464 "Whether to fill in null space vectors with random data.");
1466 const bool defaultReorthogonalizeBlocks =
true;
1467 params->set (
"reorthogonalizeBlocks", defaultReorthogonalizeBlocks,
1468 "Whether to do block reorthogonalization as necessary.");
1474 params->set (
"blockReorthogThreshold", defaultBlockReorthogThreshold,
1475 "If reorthogonalizeBlocks==true, and if the norm of " 1476 "any column within a block decreases by this much or " 1477 "more after orthogonalization, we reorthogonalize.");
1482 Teuchos::as<magnitude_type>(10) * SCTM::eps();
1487 params->set (
"relativeRankTolerance", defaultRelativeRankTolerance,
1488 "Relative tolerance to determine the numerical rank of a " 1489 "block when normalizing.");
1493 const bool defaultThrowOnReorthogFault =
true;
1494 params->set (
"throwOnReorthogFault", defaultThrowOnReorthogFault,
1495 "Whether to throw an exception if an orthogonalization " 1496 "fault occurs. This only matters if reorthogonalization " 1497 "is enabled (reorthogonalizeBlocks==true).");
1499 const bool defaultForceNonnegativeDiagonal =
false;
1500 params->set (
"forceNonnegativeDiagonal", defaultForceNonnegativeDiagonal,
1501 "Whether to force the R factor produced by the normalization " 1502 "step to have a nonnegative diagonal.");
1504 defaultParams_ = params;
1506 return defaultParams_;
1509 template<
class Scalar,
class MV>
1510 Teuchos::RCP<const Teuchos::ParameterList>
1513 using Teuchos::ParameterList;
1519 RCP<ParameterList> params = rcp (
new ParameterList (*defaultParams));
1528 const bool randomizeNullSpace =
false;
1529 params->set (
"randomizeNullSpace", randomizeNullSpace);
1530 const bool reorthogonalizeBlocks =
false;
1531 params->set (
"reorthogonalizeBlocks", reorthogonalizeBlocks);
1536 template<
class Scalar,
class MV>
1541 Teuchos::SerialDenseMatrix<int, Scalar>& B)
1548 tsqrAdaptor_.factorExplicit (X, Q, B, forceNonnegativeDiagonal_);
1551 rank = tsqrAdaptor_.revealRank (Q, B, relativeRankTolerance_);
1552 }
catch (std::exception& e) {
1558 template<
class Scalar,
class MV>
1562 Teuchos::RCP<Teuchos::SerialDenseMatrix<int, Scalar> > B)
const 1571 const int numRows = B->numRows();
1572 const int numCols = B->numCols();
1573 TEUCHOS_TEST_FOR_EXCEPTION(numRows < 1 || numCols < 1,
1574 std::invalid_argument,
1575 "normalizeOne: Input matrix B must be at " 1576 "least 1 x 1, but is instead " << numRows
1577 <<
" x " << numCols <<
".");
1579 B_out = rcp (
new mat_type (Teuchos::View, *B, 1, 1));
1583 std::vector<magnitude_type> theNorm (1, SCTM::zero());
1585 (*B_out)(0,0) = theNorm[0];
1599 if (theNorm[0] == SCTM::zero()) {
1602 if (randomizeNullSpace_) {
1605 if (theNorm[0] == SCTM::zero()) {
1611 "vector has norm zero!");
1616 const Scalar alpha = SCT::one() / theNorm[0];
1624 const Scalar alpha = SCT::one() / theNorm[0];
1631 template<
class Scalar,
class MV>
1635 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q,
1636 Teuchos::ArrayView<Teuchos::RCP<Teuchos::SerialDenseMatrix<int, Scalar> > > C)
const 1638 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1639 Teuchos::TimeMonitor timerMonitorNormalize(*timerProject_);
1640 #endif // BELOS_TEUCHOS_TIME_MONITOR 1643 const int num_Q_blocks = Q.size();
1644 for (
int i = 0; i < num_Q_blocks; ++i)
1653 const MV& Qi = *Q[i];
1660 template<
class Scalar,
class MV>
1664 const Teuchos::RCP<const MV>& Q,
1665 const Teuchos::RCP<Teuchos::SerialDenseMatrix<int, Scalar> >& C)
const 1667 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1668 Teuchos::TimeMonitor timerMonitorNormalize(*timerProject_);
1669 #endif // BELOS_TEUCHOS_TIME_MONITOR 1676 template<
class Scalar,
class MV>
1681 Teuchos::RCP<Teuchos::SerialDenseMatrix<int, Scalar> > B,
1682 const bool outOfPlace)
1684 using Teuchos::Range1D;
1687 using Teuchos::ScalarTraits;
1688 using Teuchos::tuple;
1697 TEUCHOS_TEST_FOR_EXCEPTION(
1699 "TsqrOrthoManagerImpl::normalizeImpl: Q has " 1701 "few, since X has " << numCols <<
" columns.");
1713 B_out = rcp (
new mat_type (numCols, numCols));
1716 TEUCHOS_TEST_FOR_EXCEPTION(
1717 B->numRows() < numCols || B->numCols() < numCols, std::invalid_argument,
1718 "TsqrOrthoManagerImpl::normalizeImpl: Input matrix B must be at least " 1719 << numCols <<
" x " << numCols <<
", but is instead " << B->numRows ()
1720 <<
" x " << B->numCols() <<
".");
1723 B_out = rcp (
new mat_type (Teuchos::View, *B, numCols, numCols));
1735 const int rank = rawNormalize (X, *Q_view, *B_out);
1745 TEUCHOS_TEST_FOR_EXCEPTION(
1746 rank < 0 || rank > numCols, std::logic_error,
1747 "Belos::TsqrOrthoManagerImpl::normalizeImpl: rawNormalize returned rank " 1748 " = " << rank <<
" for a matrix X with " << numCols <<
" columns. " 1749 "Please report this bug to the Belos developers.");
1753 if (rank == numCols || ! randomizeNullSpace_) {
1762 if (randomizeNullSpace_ && rank < numCols) {
1769 const int nullSpaceNumCols = numCols - rank;
1772 Range1D nullSpaceIndices (rank, numCols-1);
1790 bool anyZero =
false;
1791 typedef typename std::vector<magnitude_type>::const_iterator iter_type;
1792 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1793 if (*it == SCTM::zero()) {
1798 std::ostringstream os;
1799 os <<
"TsqrOrthoManagerImpl::normalizeImpl: " 1800 "We are being asked to randomize the null space, for a matrix " 1801 "with " << numCols <<
" columns and reported column rank " 1802 << rank <<
". The inclusive range of columns to fill with " 1803 "random data is [" << nullSpaceIndices.lbound() <<
"," 1804 << nullSpaceIndices.ubound() <<
"]. After filling the null " 1805 "space vectors with random numbers, at least one of the vectors" 1806 " has norm zero. Here are the norms of all the null space " 1808 for (iter_type it = norms.begin(); it != norms.end(); ++it) {
1810 if (it+1 != norms.end())
1813 os <<
"].) There is a tiny probability that this could happen " 1814 "randomly, but it is likely a bug. Please report it to the " 1815 "Belos developers, especially if you are able to reproduce the " 1828 RCP<const MV> Q_col =
MVT::CloneView (*Q_view, Range1D (0, rank-1));
1834 rawProject (*Q_null, Q_col, C_null);
1846 mat_type B_null (nullSpaceNumCols, nullSpaceNumCols);
1848 const int nullSpaceBasisRank = rawNormalize (*Q_null, *X_null, B_null);
1862 if (nullSpaceBasisRank < nullSpaceNumCols) {
1865 std::ostringstream os;
1866 os <<
"TsqrOrthoManagerImpl::normalizeImpl: " 1867 <<
"We are being asked to randomize the null space, " 1868 <<
"for a matrix with " << numCols <<
" columns and " 1869 <<
"column rank " << rank <<
". After projecting and " 1870 <<
"normalizing the generated random vectors, they " 1871 <<
"only have rank " << nullSpaceBasisRank <<
". They" 1872 <<
" should have full rank " << nullSpaceNumCols
1873 <<
". (The inclusive range of columns to fill with " 1874 <<
"random data is [" << nullSpaceIndices.lbound()
1875 <<
"," << nullSpaceIndices.ubound() <<
"]. The " 1876 <<
"column norms of the resulting Q factor are: [";
1877 for (
typename std::vector<magnitude_type>::size_type k = 0;
1878 k < norms.size(); ++k) {
1880 if (k != norms.size()-1) {
1884 os <<
"].) There is a tiny probability that this could " 1885 <<
"happen randomly, but it is likely a bug. Please " 1886 <<
"report it to the Belos developers, especially if " 1887 <<
"you are able to reproduce the behavior.";
1889 TEUCHOS_TEST_FOR_EXCEPTION(nullSpaceBasisRank < nullSpaceNumCols,
1901 }
else if (rank > 0) {
1903 RCP<const MV> Q_col =
MVT::CloneView (*Q_view, Range1D (0, rank-1));
1912 template<
class Scalar,
class MV>
1919 Teuchos::ArrayView<Teuchos::RCP<const MV> > Q)
const 1927 int the_ncols_X, the_num_Q_blocks, the_ncols_Q_total;
1928 the_num_Q_blocks = Q.size();
1932 the_ncols_Q_total = 0;
1935 using Teuchos::ArrayView;
1937 typedef typename ArrayView<RCP<const MV> >::const_iterator iter_type;
1938 for (iter_type it = Q.begin(); it != Q.end(); ++it) {
1939 const MV& Qi = **it;
1944 ncols_X = the_ncols_X;
1945 num_Q_blocks = the_num_Q_blocks;
1946 ncols_Q_total = the_ncols_Q_total;
1951 #endif // __BelosTsqrOrthoManagerImpl_hpp 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.
Interface of callback invoked by TsqrOrthoManager on reorthogonalization.
void project(MV &X, Teuchos::Array< mat_ptr > C, Teuchos::ArrayView< Teuchos::RCP< const MV > > Q)
Compute and .
void norm(const MV &X, std::vector< magnitude_type > &normVec) const
Compute the 2-norm of each column j of X.
TsqrOrthoManagerImpl(const Teuchos::RCP< Teuchos::ParameterList > ¶ms, const std::string &label)
Constructor (that sets user-specified parameters).
magnitude_type orthogError(const MV &X1, const MV &X2) const
Return the Frobenius norm of the inner product of X1 with itself.
Teuchos::SerialDenseMatrix< int, Scalar > mat_type
Type of the projection and normalization coefficients.
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set parameters from the given parameter list.
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).
Teuchos::RCP< const Teuchos::ParameterList > getFastParameters()
Get "fast" parameters for TsqrOrthoManagerImpl.
TSQR-based OrthoManager subclass implementation.
Declaration of basic traits for the multivector type.
void setReorthogonalizationCallback(const Teuchos::RCP< ReorthogonalizationCallback< Scalar > > &callback)
Set callback to be invoked on reorthogonalization.
Error in TsqrOrthoManager or TsqrOrthoManagerImpl.
int normalize(MV &X, mat_ptr B)
Orthogonalize the columns of X in place.
void setLabel(const std::string &label)
Set the label for timers.
An extension of the MultiVecTraits class that adds a new vector length method.
Teuchos::RCP< mat_type > mat_ptr
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void Assign(const MV &A, MV &mv)
mv := A
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
The type of a norm result.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Default valid parameter list.
Traits class which defines basic operations on multivectors.
Scalar scalar_type
The template parameter of this class; the type of an inner product result.
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).
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
const std::string & getLabel() const
Get the label for timers (if timers are enabled).
magnitude_type orthonormError(const MV &X) const
Return .
Exception thrown to signal error in an orthogonalization manager method.
Teuchos::ScalarTraits< Scalar >::magnitudeType magnitude_type
magnitude_type relativeRankTolerance() const
Relative tolerance for determining (via the SVD) whether a block is of full numerical rank...
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
TsqrOrthoError(const std::string &what_arg)
virtual ~ReorthogonalizationCallback()
Destructor (virtual for memory safety of derived classes)
Templated virtual class for providing orthogonalization/orthonormalization methods.
TsqrOrthoFault(const std::string &what_arg)
static ptrdiff_t GetGlobalLength(const MV &mv)
Obtain the vector length of mv.
magnitude_type blockReorthogThreshold() const
Relative tolerance for triggering a block reorthogonalization.
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.
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.
void innerProd(const MV &X, const MV &Y, mat_type &Z) const
Euclidean inner product.
Belos header file which uses auto-configuration information to include necessary C++ headers...