10 #ifndef TPETRA_TSQRADAPTOR_HPP
11 #define TPETRA_TSQRADAPTOR_HPP
17 #include "Tpetra_ConfigDefs.hpp"
19 #ifdef HAVE_TPETRA_TSQR
20 # include "Tsqr_NodeTsqrFactory.hpp"
22 # include "Tsqr_DistTsqr.hpp"
25 # include "Tsqr_TeuchosMessenger.hpp"
26 # include "Tpetra_MultiVector.hpp"
27 # include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
54 class TsqrAdaptor :
public Teuchos::ParameterListAcceptorDefaultBase {
56 using scalar_type =
typename MV::scalar_type;
57 using ordinal_type =
typename MV::local_ordinal_type;
58 using dense_matrix_type =
59 Teuchos::SerialDenseMatrix<ordinal_type, scalar_type>;
60 using magnitude_type =
61 typename Teuchos::ScalarTraits<scalar_type>::magnitudeType;
64 using node_tsqr_factory_type =
65 TSQR::NodeTsqrFactory<scalar_type, ordinal_type,
66 typename MV::device_type>;
67 using node_tsqr_type = TSQR::NodeTsqr<ordinal_type, scalar_type>;
68 using dist_tsqr_type = TSQR::DistTsqr<ordinal_type, scalar_type>;
69 using tsqr_type = TSQR::Tsqr<ordinal_type, scalar_type>;
71 TSQR::MatView<ordinal_type, scalar_type>
74 TEUCHOS_ASSERT( ! tsqr_.is_null() );
79 const ordinal_type lclNumRows(X.getLocalLength());
80 const ordinal_type numCols(X.getNumVectors());
81 scalar_type* X_ptr =
nullptr;
84 ordinal_type X_stride = 1;
85 if(tsqr_->wants_device_memory()) {
86 auto X_view = X.getLocalViewDevice(Access::ReadWrite);
87 X_ptr =
reinterpret_cast<scalar_type*
>(X_view.data());
88 X_stride =
static_cast<ordinal_type
>(X_view.stride(1));
90 X_stride = ordinal_type(1);
94 auto X_view = X.getLocalViewHost(Access::ReadWrite);
95 X_ptr =
reinterpret_cast<scalar_type*
>(X_view.data());
96 X_stride =
static_cast<ordinal_type
>(X_view.stride(1));
98 X_stride = ordinal_type(1);
101 using mat_view_type = TSQR::MatView<ordinal_type, scalar_type>;
102 return mat_view_type(lclNumRows, numCols, X_ptr, X_stride);
112 TsqrAdaptor(
const Teuchos::RCP<Teuchos::ParameterList>& plist) :
113 nodeTsqr_(node_tsqr_factory_type::getNodeTsqr()),
114 distTsqr_(new dist_tsqr_type),
115 tsqr_(new tsqr_type(nodeTsqr_, distTsqr_))
117 setParameterList(plist);
122 nodeTsqr_(node_tsqr_factory_type::getNodeTsqr()),
123 distTsqr_(new dist_tsqr_type),
124 tsqr_(new tsqr_type(nodeTsqr_, distTsqr_))
126 setParameterList(Teuchos::null);
130 Teuchos::RCP<const Teuchos::ParameterList>
131 getValidParameters()
const
133 if(defaultParams_.is_null()) {
134 auto params = Teuchos::parameterList(
"TSQR implementation");
135 params->set(
"NodeTsqr", *(nodeTsqr_->getValidParameters()));
136 params->set(
"DistTsqr", *(distTsqr_->getValidParameters()));
137 defaultParams_ = params;
139 return defaultParams_;
168 setParameterList(
const Teuchos::RCP<Teuchos::ParameterList>& plist)
170 auto params = plist.is_null() ?
171 Teuchos::parameterList(*getValidParameters()) : plist;
172 using Teuchos::sublist;
173 nodeTsqr_->setParameterList(sublist(params,
"NodeTsqr"));
174 distTsqr_->setParameterList(sublist(params,
"DistTsqr"));
176 this->setMyParamList(params);
201 factorExplicit(MV& A,
203 dense_matrix_type& R,
204 const bool forceNonnegativeDiagonal=
false)
206 TEUCHOS_TEST_FOR_EXCEPTION
207 (! A.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
208 "factorExplicit: Input MultiVector A must have constant stride.");
209 TEUCHOS_TEST_FOR_EXCEPTION
210 (! Q.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
211 "factorExplicit: Input MultiVector Q must have constant stride.");
213 TEUCHOS_ASSERT( ! tsqr_.is_null() );
215 auto A_view = get_mat_view(A);
216 auto Q_view = get_mat_view(Q);
217 constexpr
bool contiguousCacheBlocks =
false;
218 tsqr_->factorExplicitRaw(A_view.extent(0),
220 A_view.data(), A_view.stride(1),
221 Q_view.data(), Q_view.stride(1),
222 R.values(), R.stride(),
223 contiguousCacheBlocks,
224 forceNonnegativeDiagonal);
259 dense_matrix_type& R,
260 const magnitude_type& tol)
262 TEUCHOS_TEST_FOR_EXCEPTION
263 (! Q.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
264 "revealRank: Input MultiVector Q must have constant stride.");
267 auto Q_view = get_mat_view(Q);
268 constexpr
bool contiguousCacheBlocks =
false;
269 return tsqr_->revealRankRaw(Q_view.extent(0),
271 Q_view.data(), Q_view.stride(1),
272 R.values(), R.stride(),
273 tol, contiguousCacheBlocks);
278 Teuchos::RCP<node_tsqr_type> nodeTsqr_;
281 Teuchos::RCP<dist_tsqr_type> distTsqr_;
284 Teuchos::RCP<tsqr_type> tsqr_;
287 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
313 prepareTsqr(
const MV& mv)
328 prepareDistTsqr(
const MV& mv)
331 using Teuchos::rcp_implicit_cast;
332 using mess_type = TSQR::TeuchosMessenger<scalar_type>;
333 using base_mess_type = TSQR::MessengerBase<scalar_type>;
335 auto comm = mv.getMap()->getComm();
336 RCP<mess_type> mess(
new mess_type(comm));
337 auto messBase = rcp_implicit_cast<base_mess_type>(mess);
338 distTsqr_->init(messBase);
344 #endif // HAVE_TPETRA_TSQR
346 #endif // TPETRA_TSQRADAPTOR_HPP