Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/tpetra/Tpetra_TsqrAdaptor_UQ_PCE.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef TPETRA_TSQR_ADAPTOR_UQ_PCE_HPP
11 #define TPETRA_TSQR_ADAPTOR_UQ_PCE_HPP
12
13 #include <Tpetra_ConfigDefs.hpp> // HAVE_TPETRA_TSQR, etc.
14
15 #ifdef HAVE_TPETRA_TSQR
16
17 #include "Stokhos_Sacado_Kokkos_UQ_PCE.hpp"
18
19 # include "Tsqr_NodeTsqrFactory.hpp" // create intranode TSQR object
20 # include "Tsqr.hpp" // full (internode + intranode) TSQR
21 # include "Tsqr_DistTsqr.hpp" // internode TSQR
22 // Subclass of TSQR::MessengerBase, implemented using Teuchos
23 // communicator template helper functions
24 # include "Tsqr_TeuchosMessenger.hpp"
25 # include "Tpetra_MultiVector.hpp"
26 # include "Teuchos_ParameterListAcceptorDefaultBase.hpp"
27 # include <stdexcept>
28
29 // Base TsqrAdator template we will specialize
30 # include "Tpetra_TsqrAdaptor.hpp"
31
32 namespace Tpetra {
33
34 /// \class TsqrAdaptor
35 /// \brief Adaptor from Tpetra::MultiVector to TSQR for UQ::PCE scalar type
36 /// \author Eric Phipps
37 ///
38 /// This specialization works be extracting the underlying array within the
39 /// multivector and converting to a standard scalar type.
40 template <class Storage, class LO, class GO, class Node>
41 class TsqrAdaptor< Tpetra::MultiVector< Sacado::UQ::PCE<Storage>,
42 LO, GO, Node > > :
43 public Teuchos::ParameterListAcceptorDefaultBase {
44 public:
45 typedef Tpetra::MultiVector< Sacado::UQ::PCE<Storage>, LO, GO, Node > MV;
46 typedef typename MV::scalar_type mp_scalar_type;
47
48 // For Sacado::UQ::PCE< Storage<Ordinal,Scalar,Device> > this is Scalar
49 typedef typename mp_scalar_type::scalar_type scalar_type;
50 typedef typename mp_scalar_type::ordinal_type mp_ordinal_type;
51 typedef typename MV::local_ordinal_type ordinal_type;
52 typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
53 typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
54
55 private:
56 using node_tsqr_factory_type =
57 TSQR::NodeTsqrFactory<scalar_type, ordinal_type,
58 typename MV::device_type>;
59 using node_tsqr_type = TSQR::NodeTsqr<ordinal_type, scalar_type>;
60 using dist_tsqr_type = TSQR::DistTsqr<ordinal_type, scalar_type>;
61 using tsqr_type = TSQR::Tsqr<ordinal_type, scalar_type>;
62
63 public:
64 /// \brief Constructor (that accepts a parameter list).
65 ///
66 /// \param plist [in/out] List of parameters for configuring TSQR.
67 /// The specific parameter keys that are read depend on the TSQR
68 /// implementation. For details, call \c getValidParameters()
69 /// and examine the documentation embedded therein.
70 TsqrAdaptor (const Teuchos::RCP<Teuchos::ParameterList>& plist) :
71 nodeTsqr_ (node_tsqr_factory_type::getNodeTsqr ()),
72 distTsqr_ (new dist_tsqr_type),
73 tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
74 ready_ (false)
75 {
76 setParameterList (plist);
77 }
78
79 //! Constructor (that uses default parameters).
80 TsqrAdaptor () :
81 nodeTsqr_ (new node_tsqr_factory_type::getNodeTsqr ()),
82 distTsqr_ (new dist_tsqr_type),
83 tsqr_ (new tsqr_type (nodeTsqr_, distTsqr_)),
84 ready_ (false)
85 {
86 setParameterList (Teuchos::null);
87 }
88
89 Teuchos::RCP<const Teuchos::ParameterList>
90 getValidParameters () const
91 {
92 using Teuchos::RCP;
93 using Teuchos::rcp;
94 using Teuchos::ParameterList;
95 using Teuchos::parameterList;
96
97 if (defaultParams_.is_null()) {
98 RCP<ParameterList> params = parameterList ("TSQR implementation");
99 params->set ("NodeTsqr", *(nodeTsqr_->getValidParameters ()));
100 params->set ("DistTsqr", *(distTsqr_->getValidParameters ()));
101 defaultParams_ = params;
102 }
103 return defaultParams_;
104 }
105
106 void
107 setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
108 {
109 using Teuchos::ParameterList;
110 using Teuchos::parameterList;
111 using Teuchos::RCP;
112 using Teuchos::sublist;
113
114 RCP<ParameterList> params = plist.is_null() ?
115 parameterList (*getValidParameters ()) : plist;
116 nodeTsqr_->setParameterList (sublist (params, "NodeTsqr"));
117 distTsqr_->setParameterList (sublist (params, "DistTsqr"));
118
119 this->setMyParamList (params);
120 }
121
122 /// \brief Compute QR factorization [Q,R] = qr(A,0).
123 ///
124 /// \param A [in/out] On input: the multivector to factor.
125 /// Overwritten with garbage on output.
126 ///
127 /// \param Q [out] On output: the (explicitly stored) Q factor in
128 /// the QR factorization of the (input) multivector A.
129 ///
130 /// \param R [out] On output: the R factor in the QR factorization
131 /// of the (input) multivector A.
132 ///
133 /// \param forceNonnegativeDiagonal [in] If true, then (if
134 /// necessary) do extra work (modifying both the Q and R
135 /// factors) in order to force the R factor to have a
136 /// nonnegative diagonal.
137 ///
138 /// \warning Currently, this method only works if A and Q have the
139 /// same communicator and row distribution ("Map," in Petra
140 /// terms) as those of the multivector given to this adapter
141 /// instance's constructor. Otherwise, the result of this
142 /// method is undefined.
143 void
144 factorExplicit (MV& A,
145 MV& Q,
146 dense_matrix_type& R,
147 const bool forceNonnegativeDiagonal=false)
148 {
149 prepareTsqr (Q); // Finish initializing TSQR.
150
151 ordinal_type numRows;
152 ordinal_type numCols;
153 ordinal_type LDA;
154 ordinal_type LDQ;
155 scalar_type* A_ptr;
156 scalar_type* Q_ptr;
157
158 getNonConstView (numRows, numCols, A_ptr, LDA, A);
159 getNonConstView (numRows, numCols, Q_ptr, LDQ, Q);
160 const bool contiguousCacheBlocks = false;
161 tsqr_->factorExplicitRaw (numRows, numCols, A_ptr, LDA,
162 Q_ptr, LDQ, R.values (), R.stride (),
163 contiguousCacheBlocks,
164 forceNonnegativeDiagonal);
165 }
166
167 /// \brief Rank-revealing decomposition
168 ///
169 /// Using the R factor and explicit Q factor from
170 /// factorExplicit(), compute the singular value decomposition
171 /// (SVD) of R: \f$R = U \Sigma V^*\f$. If R is full rank (with
172 /// respect to the given relative tolerance \c tol), do not modify
173 /// Q or R. Otherwise, compute \f$Q := Q \cdot U\f$ and \f$R :=
174 /// \Sigma V^*\f$ in place. If R was modified, then it may not
175 /// necessarily be upper triangular on output.
176 ///
177 /// \param Q [in/out] On input: explicit Q factor computed by
178 /// factorExplicit(). (Must be an orthogonal resp. unitary
179 /// matrix.) On output: If R is of full numerical rank with
180 /// respect to the tolerance tol, Q is unmodified. Otherwise, Q
181 /// is updated so that the first \c rank columns of Q are a
182 /// basis for the column space of A (the original matrix whose
183 /// QR factorization was computed by factorExplicit()). The
184 /// remaining columns of Q are a basis for the null space of A.
185 ///
186 /// \param R [in/out] On input: N by N upper triangular matrix
187 /// with leading dimension LDR >= N. On output: if input is
188 /// full rank, R is unchanged on output. Otherwise, if \f$R = U
189 /// \Sigma V^*\f$ is the SVD of R, on output R is overwritten
190 /// with \f$\Sigma \cdot V^*\f$. This is also an N by N matrix,
191 /// but it may not necessarily be upper triangular.
192 ///
193 /// \param tol [in] Relative tolerance for computing the numerical
194 /// rank of the matrix R.
195 ///
196 /// \return Rank \f$r\f$ of R: \f$ 0 \leq r \leq N\f$.
197 int
198 revealRank (MV& Q,
199 dense_matrix_type& R,
200 const magnitude_type& tol)
201 {
202 prepareTsqr (Q); // Finish initializing TSQR.
203
204 // FIXME (mfh 18 Oct 2010) Check Teuchos::Comm<int> object in Q
205 // to make sure it is the same communicator as the one we are
206 // using in our dist_tsqr_type implementation.
207
208 ordinal_type numRows;
209 ordinal_type numCols;
210 scalar_type* Q_ptr;
211 ordinal_type LDQ;
212 getNonConstView (numRows, numCols, Q_ptr, LDQ, Q);
213 const bool contiguousCacheBlocks = false;
214 return tsqr_->revealRankRaw (numRows, numCols, Q_ptr, LDQ,
215 R.values (), R.stride (), tol,
216 contiguousCacheBlocks);
217 }
218
219 private:
220 //! The intranode TSQR implementation instance.
221 Teuchos::RCP<node_tsqr_type> nodeTsqr_;
222
223 //! The internode TSQR implementation instance.
224 Teuchos::RCP<dist_tsqr_type> distTsqr_;
225
226 //! The (full) TSQR implementation instance.
227 Teuchos::RCP<tsqr_type> tsqr_;
228
229 //! Default parameter list. Initialized by getValidParameters().
230 mutable Teuchos::RCP<const Teuchos::ParameterList> defaultParams_;
231
232 //! Whether TSQR has been fully initialized.
233 bool ready_;
234
235 /// \brief Finish TSQR initialization.
236 ///
237 /// The intranode and internode TSQR implementations both have a
238 /// two-stage initialization procedure: first, setting parameters
239 /// (which may happen at construction), and second, getting
240 /// information they need from the multivector input in order to
241 /// finish initialization. For intranode TSQR, this includes the
242 /// Kokkos Node instance; for internode TSQR, this includes the
243 /// communicator. The second stage of initialization happens in
244 /// this class' computational routines; all of those routines
245 /// accept one or more multivector inputs, which this method can
246 /// use for finishing initialization. Thus, users of this class
247 /// never need to see the two-stage initialization.
248 ///
249 /// \param mv [in] Multivector object, used only to access the
250 /// underlying communicator object (in this case,
251 /// Teuchos::Comm<int>, accessed via the Tpetra::Map belonging
252 /// to the multivector) and Kokkos Node instance. All
253 /// multivector objects used with this Adaptor instance must
254 /// have the same map, communicator, and Kokkos Node instance.
255 void
256 prepareTsqr (const MV& mv)
257 {
258 if (! ready_) {
259 prepareDistTsqr (mv);
260 ready_ = true;
261 }
262 }
263
264 /// \brief Finish interprocess TSQR initialization.
265 ///
266 /// \param mv [in] A valid Tpetra::MultiVector instance whose
267 /// communicator wrapper we will use to prepare TSQR.
268 ///
269 /// \note It's OK to call this method more than once; it is idempotent.
270 void
271 prepareDistTsqr (const MV& mv)
272 {
273 using Teuchos::RCP;
274 using Teuchos::rcp_implicit_cast;
275 typedef TSQR::TeuchosMessenger<scalar_type> mess_type;
276 typedef TSQR::MessengerBase<scalar_type> base_mess_type;
277
278 RCP<const Teuchos::Comm<int> > comm = mv.getMap()->getComm();
279 RCP<mess_type> mess (new mess_type (comm));
280 RCP<base_mess_type> messBase = rcp_implicit_cast<base_mess_type> (mess);
281 distTsqr_->init (messBase);
282 }
283
284 /// \brief Extract a nonpersistent view of A's data as a
285 /// scalar_type matrix, stored as a flat column-major array.
286 ///
287 /// \warning TSQR does not currently support multivectors with
288 /// nonconstant stride. If A has nonconstant stride, this
289 /// method will throw an exception.
290 static void
291 getNonConstView (ordinal_type& numRows,
292 ordinal_type& numCols,
293 scalar_type*& A_ptr,
294 ordinal_type& LDA,
295 const MV& A)
296 {
297 // FIXME (mfh 25 Oct 2010) We should be able to run TSQR even if
298 // storage of A uses nonconstant stride internally. We would
299 // have to copy and pack into a matrix with constant stride, and
300 // then unpack on exit. For now we choose just to raise an
301 // exception.
302 TEUCHOS_TEST_FOR_EXCEPTION
303 (! A.isConstantStride(), std::invalid_argument,
304 "TSQR does not currently support Tpetra::MultiVector "
305 "inputs that do not have constant stride.");
306
307 // FIXME (mfh 16 Jan 2016) When I got here, I found strides[0]
308 // instead of strides[1] for the stride. I don't think this is
309 // right. However, I don't know about these Stokhos scalar
310 // types so I'll just do what was here.
311 //
312 // STOKHOS' TYPES ARE NOT TESTED WITH TSQR REGULARLY SO IT IS
313 // POSSIBLE THAT THE ORIGINAL CODE WAS WRONG.
314
315 typedef typename MV::dual_view_type view_type;
316 typedef typename view_type::t_dev::array_type flat_array_type;
317
318 // Reinterpret the data as a longer array of the base scalar
319 // type. TSQR currently forbids MultiVector input with
320 // nonconstant stride, so we need not worry about that here.
321
322 flat_array_type flat_mv = A.getLocalViewDevice();
323
324 numRows = static_cast<ordinal_type> (flat_mv.extent(0));
325 numCols = static_cast<ordinal_type> (flat_mv.extent(1));
326 A_ptr = flat_mv.data ();
327
328 ordinal_type strides[2];
329 flat_mv.stride (strides);
330 LDA = strides[0];
331 }
332 };
333 } // namespace Tpetra
334
335 #endif // HAVE_TPETRA_TSQR
336
337 #endif // TPETRA_TSQR_ADAPTOR_UQ_PCE_HPP