Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/Teuchos_SerialQRDenseSolver_UQ_PCE.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_
11 #define _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_
12 /*! \file Teuchos_SerialQRDenseSolver.hpp
13 \brief Templated class for solving dense linear problems.
14 */
15
16 #include <new>
17 #include "Teuchos_SerialQRDenseSolver.hpp"
18
19 #include "Sacado_UQ_PCE.hpp"
20
21 /*! \class Teuchos::SerialQRDenseSolver
22 \brief Specialization for Sacado::UQ::PCE< Storage<...> >
23 */
24
25 namespace Teuchos {
26
27 template<typename OrdinalType, typename Storage>
28 class SerialQRDenseSolver<OrdinalType, Sacado::UQ::PCE<Storage> >
29 : public CompObject,
30 public Object
31 {
32
33 public:
34
35 typedef Sacado::UQ::PCE<Storage> ScalarType;
36 typedef typename ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
37
38 //! @name Constructor/Destructor Methods
39 //@{
40 //! Default constructor; matrix should be set using setMatrix(), LHS and RHS set with setVectors().
41 SerialQRDenseSolver();
42
43 //! SerialQRDenseSolver destructor.
44 virtual ~SerialQRDenseSolver() {}
45 //@}
46
47 //! @name Set Methods
48 //@{
49
50 //! Sets the pointers for coefficient matrix
51 /*! Row dimension of A must be greater than or equal to the column dimension of A.
52 */
53 int setMatrix(const RCP<SerialDenseMatrix<OrdinalType, ScalarType> >& A);
54
55 //! Sets the pointers for left and right hand side vector(s).
56 /*! Row dimension of X must match column dimension of matrix A, row dimension of B
57 must match row dimension of A.
58 */
59 int setVectors(const RCP<SerialDenseMatrix<OrdinalType, ScalarType> >& X,
60 const RCP<SerialDenseMatrix<OrdinalType, ScalarType> >& B);
61 //@}
62
63 //! @name Strategy Modifying Methods
64 //@{
65
66 //! Causes equilibration to be called just before the matrix factorization as part of the call to \c factor.
67 /*! \note This method must be called before the factorization is performed, otherwise it will have no effect.
68 */
69 void factorWithEquilibration(bool flag) {
70 base_QR_.factorWithEquilibration(flag);
71 }
72
73 //! If \c flag is true, causes all subsequent function calls to work with the adjoint of \e this matrix, otherwise not.
74 void solveWithTranspose(bool flag) {
75 base_QR_.solveWithTranspose(flag);
76 }
77
78 //! All subsequent function calls will work with the transpose-type set by this method (\c Teuchos::NO_TRANS or Teuchos::CONJ_TRANS).
79 void solveWithTransposeFlag(Teuchos::ETransp trans) {
80 base_QR_.solveWithTranspose(trans);
81 }
82
83 //@}
84
85 //! @name Factor/Solve/Invert Methods
86 //@{
87
88 //! Computes the in-place QR factorization of the matrix using the LAPACK routine \e _GETRF or the Eigen class \e HouseholderQR
89 /*!
90 \return Integer error code, set to 0 if successful.
91 */
92 int factor() { return base_QR_.factor(); }
93
94 //! Computes the solution X to AX = B for the \e this matrix and the B provided to SetVectors()..
95 /*!
96 \return Integer error code, set to 0 if successful.
97 */
98 int solve() { return base_QR_.solve(); }
99
100 //! Determines if \e this matrix should be scaled.
101 /*!
102 \return Integer error code, set to 0 if successful.
103 */
104 int computeEquilibrateScaling() {
105 return base_QR_.computeEquilibrateScaling();
106 }
107
108 //! Equilibrates the \e this matrix.
109 /*!
110 \note This method will be called automatically in solve() method if factorWithEquilibration( true ) is called.
111 \return Integer error code, set to 0 if successful.
112 */
113 int equilibrateMatrix() { return base_QR_.equilibrateMatrix(); }
114
115 //! Equilibrates the current RHS.
116 /*!
117 \note This method will be called automatically in solve() method if factorWithEquilibration( true ) is called.
118 \return Integer error code, set to 0 if successful.
119 */
120 int equilibrateRHS() { return base_QR_.equilibrateRHS(); }
121
122 //! Unscales the solution vectors if equilibration was used to solve the system.
123 /*!
124 \note This method will be called automatically in solve() method if factorWithEquilibration( true ) is called.
125 \return Integer error code, set to 0 if successful.
126 */
127 int unequilibrateLHS() { return base_QR_.unequilibrateLHS(); }
128
129 //! Explicitly forms the unitary matrix Q.
130 /*!
131 \return Integer error code, set to 0 if successful.
132 */
133 int formQ();
134
135 //! Explicitly forms the upper triangular matrix R.
136 /*!
137 \return Integer error code, set to 0 if successful.
138 */
139 int formR();
140
141 //! Left multiply the input matrix by the unitary matrix Q or its adjoint.
142 /*!
143 \return Integer error code, set to 0 if successful.
144 */
145 int multiplyQ (ETransp transq, SerialDenseMatrix<OrdinalType, ScalarType> &C);
146
147 //! Solve input matrix on the left with the upper triangular matrix R or its adjoint.
148 /*!
149 \return Integer error code, set to 0 if successful.
150 */
151 int solveR (ETransp transr, SerialDenseMatrix<OrdinalType, ScalarType> &C);
152 //@}
153
154 //! @name Query methods
155 //@{
156
157 //! Returns true if adjoint of \e this matrix has and will be used.
158 bool transpose() { return base_QR_.transpose(); }
159
160 //! Returns true if matrix is factored (factor available via getFactoredMatrix()).
161 bool factored() { return base_QR_.factored(); }
162
163 //! Returns true if factor is equilibrated (factor available via getFactoredMatrix()).
164 bool equilibratedA() { return base_QR_.equilibratedA(); }
165
166 //! Returns true if RHS is equilibrated (RHS available via getRHS()).
167 bool equilibratedB() { return base_QR_.equilibratedB(); }
168
169 //! Returns true if the LAPACK general rules for equilibration suggest you should equilibrate the system.
170 bool shouldEquilibrate() { return base_QR_.shouldEquilibrate(); }
171
172 //! Returns true if the current set of vectors has been solved.
173 bool solved() { return base_QR_.solved(); }
174
175 //! Returns true if Q has been formed explicitly.
176 bool formedQ() { return base_QR_.formedQ(); }
177
178 //! Returns true if R has been formed explicitly.
179 bool formedR() { return base_QR_.formedR();}
180
181 //@}
182
183 //! @name Data Accessor methods
184 //@{
185
186 //! Returns pointer to current matrix.
187 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getMatrix() const {return(Matrix_);}
188
189 //! Returns pointer to factored matrix (assuming factorization has been performed).
190 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getFactoredMatrix() const {return(Factor_);}
191
192 //! Returns pointer to Q (assuming factorization has been performed).
193 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getQ() const {return(FactorQ_);}
194
195 //! Returns pointer to R (assuming factorization has been performed).
196 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getR() const {return(FactorR_);}
197
198 //! Returns pointer to current LHS.
199 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getLHS() const {return(LHS_);}
200
201 //! Returns pointer to current RHS.
202 RCP<SerialDenseMatrix<OrdinalType, ScalarType> > getRHS() const {return(RHS_);}
203
204 //! Returns row dimension of system.
205 OrdinalType numRows() const {return(M_);}
206
207 //! Returns column dimension of system.
208 OrdinalType numCols() const {return(N_);}
209
210 //! Returns pointer to pivot vector (if factorization has been computed), zero otherwise.
211 std::vector<ScalarType> tau() const { return base_QR_.tau(); }
212
213 //! Returns the absolute value of the largest element of \e this matrix (returns -1 if not yet computed).
214 MagnitudeType ANORM() const { return base_QR_.ANORM(); }
215
216 //@}
217
218 //! @name I/O methods
219 //@{
220 //! Print service methods; defines behavior of ostream << operator.
221 void Print(std::ostream& os) const;
222 //@}
223 protected:
224
225 typedef typename ScalarType::value_type BaseScalarType;
226 typedef SerialQRDenseSolver<OrdinalType,BaseScalarType> BaseQRType;
227 typedef SerialDenseMatrix<OrdinalType, BaseScalarType> BaseMatrixType;
228 typedef SerialDenseMatrix<OrdinalType, ScalarType> MatrixType;
229
230 void resetMatrix();
231 void resetVectors();
232 RCP<BaseMatrixType> createBaseMatrix(const RCP<MatrixType>& mat) const;
233 RCP<MatrixType> createMatrix(const RCP<BaseMatrixType>& base_mat) const;
234 BaseQRType base_QR_;
235
236 OrdinalType M_;
237 OrdinalType N_;
238 OrdinalType SacadoSize_;
239
240 RCP<MatrixType> Matrix_;
241 RCP<MatrixType> LHS_;
242 RCP<MatrixType> RHS_;
243 RCP<MatrixType> Factor_;
244 RCP<MatrixType> FactorQ_;
245 RCP<MatrixType> FactorR_;
246
247 RCP<BaseMatrixType> Base_Matrix_;
248 RCP<BaseMatrixType> Base_LHS_;
249 RCP<BaseMatrixType> Base_RHS_;
250 RCP<BaseMatrixType> Base_Factor_;
251 RCP<BaseMatrixType> Base_FactorQ_;
252 RCP<BaseMatrixType> Base_FactorR_;
253
254 private:
255
256 // SerialQRDenseSolver copy constructor (put here because we don't want user access)
257 SerialQRDenseSolver(const SerialQRDenseSolver& Source);
258 SerialQRDenseSolver & operator=(const SerialQRDenseSolver& Source);
259
260 };
261
262 namespace details {
263
264 // Helper traits class for converting between arrays of
265 // Sacado::UQ::PCE and its corresponding value type
266 template <typename Storage> struct PCEArrayHelper;
267
268 template <typename Ordinal, typename Value, typename Device>
269 struct PCEArrayHelper< Stokhos::DynamicStorage<Ordinal,Value,Device> >
270 {
271 typedef Stokhos::DynamicStorage<Ordinal,Value,Device> Storage;
272 typedef Sacado::UQ::PCE<Storage> pce_type;
273 typedef typename pce_type::cijk_type cijk_type;
274
275 static Value* getValueArray(pce_type* v, Ordinal len) {
276 if (len == 0)
277 return 0;
278 return v[0].coeff(); // Assume data is contiguous
279 }
280
281 static pce_type* getPCEArray(Value *v, Ordinal len, Ordinal vector_size){
282 if (len == 0)
283 return 0;
284 pce_type* v_pce =
285 static_cast<pce_type*>(operator new(len*sizeof(pce_type)));
286 cijk_type cijk = Kokkos::getGlobalCijkTensor<cijk_type>();
287 for (Ordinal i=0; i<len; ++i)
288 new (v_pce+i) pce_type(cijk, vector_size, v+i*vector_size, false);
289 return v_pce;
290 }
291
292 };
293
294 }
295
296 // Helper traits to distinguish work arrays for real and complex-valued datatypes.
297 using namespace details;
298
299 //=============================================================================
300
301 template<typename OrdinalType, typename Storage>
302 SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
303 SerialQRDenseSolver()
304 : CompObject(),
305 Object("Teuchos::SerialQRDenseSolver"),
306 M_(0),
307 N_(0)
308 {
309 resetMatrix();
310 }
311
312 //=============================================================================
313
314 template<typename OrdinalType, typename Storage>
315 void SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
316 resetVectors()
317 {
318 LHS_ = Teuchos::null;
319 RHS_ = Teuchos::null;
320 }
321 //=============================================================================
322
323 template<typename OrdinalType, typename Storage>
324 void SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
325 resetMatrix()
326 {
327 resetVectors();
328 M_ = 0;
329 N_ = 0;
330 }
331 //=============================================================================
332
333 template<typename OrdinalType, typename Storage>
334 RCP<SerialDenseMatrix<OrdinalType, typename Sacado::UQ::PCE<Storage>::value_type> >
335 SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
336 createBaseMatrix(
337 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& mat) const
338 {
339 BaseScalarType* base_ptr =
340 PCEArrayHelper<Storage>::getValueArray(
341 mat->values(), mat->stride()*mat->numCols());
342 RCP<BaseMatrixType> base_mat =
343 rcp(new BaseMatrixType(Teuchos::View,
344 base_ptr,
345 mat->stride()*SacadoSize_,
346 mat->numRows()*SacadoSize_,
347 mat->numCols()));
348 return base_mat;
349 }
350 //=============================================================================
351
352 template<typename OrdinalType, typename Storage>
353 RCP<SerialDenseMatrix<OrdinalType, Sacado::UQ::PCE<Storage> > >
354 SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
355 createMatrix(
356 const RCP<SerialDenseMatrix<OrdinalType,BaseScalarType> >& base_mat) const
357 {
358 ScalarType* ptr =
359 PCEArrayHelper<Storage>::getPCEArray(
360 base_mat->values(), base_mat->stride()*base_mat->numCols(), SacadoSize_);
361 RCP<MatrixType> mat =
362 rcp(new MatrixType(Teuchos::View,
363 ptr,
364 base_mat->stride()/SacadoSize_,
365 base_mat->numRows()/SacadoSize_,
366 base_mat->numCols()));
367 return mat;
368 }
369 //=============================================================================
370
371 template<typename OrdinalType, typename Storage>
372 int SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
373 setMatrix(const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& A)
374 {
375 TEUCHOS_TEST_FOR_EXCEPTION(
376 A->numRows() < A->numCols(), std::invalid_argument,
377 "SerialQRDenseSolver<T>::setMatrix: the matrix A must have A.numRows() >= A.numCols()!");
378
379 resetMatrix();
380 Matrix_ = A;
381 Factor_ = A;
382 FactorQ_ = A;
383 FactorR_ = A;
384 M_ = A->numRows();
385 N_ = A->numCols();
386 if (Storage::is_static)
387 SacadoSize_ = Storage::static_size;
388 else if (M_ > 0 && N_ > 0)
389 SacadoSize_ = (*A)(0,0).size();
390 else
391 SacadoSize_ = 0;
392
393 return base_QR_.setMatrix( createBaseMatrix(A) );
394 }
395 //=============================================================================
396
397 template<typename OrdinalType, typename Storage>
398 int SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
399 setVectors(
400 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& X,
401 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& B)
402 {
403 // Check that these new vectors are consistent
404 TEUCHOS_TEST_FOR_EXCEPTION(
405 B->numCols() != X->numCols(), std::invalid_argument,
406 "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
407 TEUCHOS_TEST_FOR_EXCEPTION(
408 B->values()==0, std::invalid_argument,
409 "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
410 TEUCHOS_TEST_FOR_EXCEPTION(
411 X->values()==0, std::invalid_argument,
412 "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
413 TEUCHOS_TEST_FOR_EXCEPTION(
414 B->stride()<1, std::invalid_argument,
415 "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
416 TEUCHOS_TEST_FOR_EXCEPTION(
417 X->stride()<1, std::invalid_argument,
418 "SerialQRDenseSolver<T>::setVectors: X has an invalid stride!");
419
420 resetVectors();
421 LHS_ = X;
422 RHS_ = B;
423
424 return base_QR_.setVectors( createBaseMatrix(X), createBaseMatrix(B) );
425 }
426
427 //=============================================================================
428
429 template<typename OrdinalType, typename Storage>
430 int SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
431 formQ() {
432 int ret = base_QR_.formQ();
433 FactorQ_ = createMatrix( base_QR_.getQ() );
434 return(ret);
435 }
436
437 //=============================================================================
438
439 template<typename OrdinalType, typename Storage>
440 int SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
441 formR() {
442 int ret = base_QR_.formR();
443 FactorR_ = createMatrix( base_QR_.getR() );
444 Factor_ = createMatrix( base_QR_.getFactoredMatrix() );
445 return(ret);
446 }
447
448 //=============================================================================
449
450 template<typename OrdinalType, typename Storage>
451 int SerialQRDenseSolver<OrdinalType, Sacado::UQ::PCE<Storage> >::
452 multiplyQ(ETransp transq, SerialDenseMatrix<OrdinalType, ScalarType> &C)
453 {
454 return base_QR_.multiplyQ(transq, createBaseMatrix(C));
455 }
456
457 //=============================================================================
458
459 template<typename OrdinalType, typename Storage>
460 int SerialQRDenseSolver<OrdinalType, Sacado::UQ::PCE<Storage> >::
461 solveR(ETransp transr, SerialDenseMatrix<OrdinalType, ScalarType> &C)
462 {
463 return base_QR_.solveR(transr, createBaseMatrix(C));
464 }
465
466 //=============================================================================
467
468 template<typename OrdinalType, typename Storage>
469 void SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
470 Print(std::ostream& os) const {
471
472 if (Matrix_!=Teuchos::null) os << "Solver Matrix" << std::endl << *Matrix_ << std::endl;
473 if (Factor_!=Teuchos::null) os << "Solver Factored Matrix" << std::endl << *Factor_ << std::endl;
474 if (LHS_ !=Teuchos::null) os << "Solver LHS" << std::endl << *LHS_ << std::endl;
475 if (RHS_ !=Teuchos::null) os << "Solver RHS" << std::endl << *RHS_ << std::endl;
476
477 }
478
479 } // namespace Teuchos
480
481 #endif /* _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_ */