40 #ifndef TPETRA_TSQRADAPTOR_HPP
41 #define TPETRA_TSQRADAPTOR_HPP
47 #include "Tpetra_ConfigDefs.hpp"
49 #ifdef HAVE_TPETRA_TSQR
50 # include "Tsqr_NodeTsqrFactory.hpp"
52 # include "Tsqr_DistTsqr.hpp"
55 # include "Tsqr_TeuchosMessenger.hpp"
56 # include "Tpetra_MultiVector.hpp"
57 # include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
84 class TsqrAdaptor :
public Teuchos::ParameterListAcceptorDefaultBase {
86 using scalar_type =
typename MV::scalar_type;
87 using ordinal_type =
typename MV::local_ordinal_type;
88 using dense_matrix_type =
89 Teuchos::SerialDenseMatrix<ordinal_type, scalar_type>;
90 using magnitude_type =
91 typename Teuchos::ScalarTraits<scalar_type>::magnitudeType;
94 using node_tsqr_factory_type =
95 TSQR::NodeTsqrFactory<scalar_type, ordinal_type,
96 typename MV::device_type>;
97 using node_tsqr_type = TSQR::NodeTsqr<ordinal_type, scalar_type>;
98 using dist_tsqr_type = TSQR::DistTsqr<ordinal_type, scalar_type>;
99 using tsqr_type = TSQR::Tsqr<ordinal_type, scalar_type>;
101 TSQR::MatView<ordinal_type, scalar_type>
104 TEUCHOS_ASSERT( ! tsqr_.is_null() );
109 const ordinal_type lclNumRows(X.getLocalLength());
110 const ordinal_type numCols(X.getNumVectors());
111 scalar_type* X_ptr =
nullptr;
114 ordinal_type X_stride = 1;
115 if(tsqr_->wants_device_memory()) {
118 auto X_view = X.getLocalViewDevice();
119 X_ptr =
reinterpret_cast<scalar_type*
>(X_view.data());
120 X_stride =
static_cast<ordinal_type
>(X_view.stride(1));
122 X_stride = ordinal_type(1);
128 auto X_view = X.getLocalViewHost();
129 X_ptr =
reinterpret_cast<scalar_type*
>(X_view.data());
130 X_stride =
static_cast<ordinal_type
>(X_view.stride(1));
132 X_stride = ordinal_type(1);
135 using mat_view_type = TSQR::MatView<ordinal_type, scalar_type>;
136 return mat_view_type(lclNumRows, numCols, X_ptr, X_stride);
146 TsqrAdaptor(
const Teuchos::RCP<Teuchos::ParameterList>& plist) :
147 nodeTsqr_(node_tsqr_factory_type::getNodeTsqr()),
148 distTsqr_(new dist_tsqr_type),
149 tsqr_(new tsqr_type(nodeTsqr_, distTsqr_))
151 setParameterList(plist);
156 nodeTsqr_(node_tsqr_factory_type::getNodeTsqr()),
157 distTsqr_(new dist_tsqr_type),
158 tsqr_(new tsqr_type(nodeTsqr_, distTsqr_))
160 setParameterList(Teuchos::null);
164 Teuchos::RCP<const Teuchos::ParameterList>
165 getValidParameters()
const
167 if(defaultParams_.is_null()) {
168 auto params = Teuchos::parameterList(
"TSQR implementation");
169 params->set(
"NodeTsqr", *(nodeTsqr_->getValidParameters()));
170 params->set(
"DistTsqr", *(distTsqr_->getValidParameters()));
171 defaultParams_ = params;
173 return defaultParams_;
202 setParameterList(
const Teuchos::RCP<Teuchos::ParameterList>& plist)
204 auto params = plist.is_null() ?
205 Teuchos::parameterList(*getValidParameters()) : plist;
206 using Teuchos::sublist;
207 nodeTsqr_->setParameterList(sublist(params,
"NodeTsqr"));
208 distTsqr_->setParameterList(sublist(params,
"DistTsqr"));
210 this->setMyParamList(params);
235 factorExplicit(MV& A,
237 dense_matrix_type& R,
238 const bool forceNonnegativeDiagonal=
false)
240 TEUCHOS_TEST_FOR_EXCEPTION
241 (! A.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
242 "factorExplicit: Input MultiVector A must have constant stride.");
243 TEUCHOS_TEST_FOR_EXCEPTION
244 (! Q.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
245 "factorExplicit: Input MultiVector Q must have constant stride.");
247 TEUCHOS_ASSERT( ! tsqr_.is_null() );
249 auto A_view = get_mat_view(A);
250 auto Q_view = get_mat_view(Q);
251 constexpr
bool contiguousCacheBlocks =
false;
252 tsqr_->factorExplicitRaw(A_view.extent(0),
254 A_view.data(), A_view.stride(1),
255 Q_view.data(), Q_view.stride(1),
256 R.values(), R.stride(),
257 contiguousCacheBlocks,
258 forceNonnegativeDiagonal);
293 dense_matrix_type& R,
294 const magnitude_type& tol)
296 TEUCHOS_TEST_FOR_EXCEPTION
297 (! Q.isConstantStride(), std::invalid_argument,
"TsqrAdaptor::"
298 "revealRank: Input MultiVector Q must have constant stride.");
301 auto Q_view = get_mat_view(Q);
302 constexpr
bool contiguousCacheBlocks =
false;
303 return tsqr_->revealRankRaw(Q_view.extent(0),
305 Q_view.data(), Q_view.stride(1),
306 R.values(), R.stride(),
307 tol, contiguousCacheBlocks);
312 Teuchos::RCP<node_tsqr_type> nodeTsqr_;
315 Teuchos::RCP<dist_tsqr_type> distTsqr_;
318 Teuchos::RCP<tsqr_type> tsqr_;
321 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
347 prepareTsqr(
const MV& mv)
362 prepareDistTsqr(
const MV& mv)
365 using Teuchos::rcp_implicit_cast;
366 using mess_type = TSQR::TeuchosMessenger<scalar_type>;
367 using base_mess_type = TSQR::MessengerBase<scalar_type>;
369 auto comm = mv.getMap()->getComm();
370 RCP<mess_type> mess(
new mess_type(comm));
371 auto messBase = rcp_implicit_cast<base_mess_type>(mess);
372 distTsqr_->init(messBase);
378 #endif // HAVE_TPETRA_TSQR
380 #endif // TPETRA_TSQRADAPTOR_HPP