Debian Codegraph @

packages/stokhos/src/sacado/kokkos/vector/Teuchos_SerialQRDenseSolver_MP_Vector.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_MP_VECTOR_HPP_
11 #define _TEUCHOS_SERIALQRDENSESOLVER_MP_VECTOR_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_MP_Vector.hpp"
20
21 /*! \class Teuchos::SerialQRDenseSolver
22 \brief Specialization for Sacado::MP::Vector< Storage<...> >
23 */
24
25 namespace Teuchos {
26
27 template<typename OrdinalType, typename Storage>
28 class SerialQRDenseSolver<OrdinalType, Sacado::MP::Vector<Storage> >
29 : public CompObject,
30 public Object
31 {
32
33 public:
34
35 typedef Sacado::MP::Vector<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::MP::Vector and its corresponding value type
266 template <typename Storage> struct MPVectorArrayHelper;
267
268 template <typename Ordinal, typename Value, typename Device>
269 struct MPVectorArrayHelper< Stokhos::DynamicStorage<Ordinal,Value,Device> >
270 {
271 typedef Stokhos::DynamicStorage<Ordinal,Value,Device> Storage;
272 typedef Sacado::MP::Vector<Storage> Vector;
273
274 static Value* getValueArray(Vector* v, Ordinal len) {
275 if (len == 0)
276 return 0;
277 return v[0].coeff(); // Assume data is contiguous
278 }
279
280 static Vector* getVectorArray(Value *v, Ordinal len, Ordinal vector_size){
281 if (len == 0)
282 return 0;
283 Vector* v_vec = static_cast<Vector*>(operator new(len*sizeof(Vector)));
284 for (Ordinal i=0; i<len; ++i)
285 new (v_vec+i) Vector(vector_size, v+i*vector_size, false);
286 return v_vec;
287 }
288
289 };
290
291 template <typename Ordinal, typename Value, typename Device, int Num>
292 struct MPVectorArrayHelper< Stokhos::StaticFixedStorage<Ordinal,Value,Num,Device> > {
293 typedef Stokhos::StaticFixedStorage<Ordinal,Value,Num,Device> Storage;
294 typedef Sacado::MP::Vector<Storage> Vector;
295
296 static Value* getValueArray(Vector* v, Ordinal len)
297 {
298 return reinterpret_cast<Value*>(v);
299 }
300
301 static Vector* getVectorArray(Value *v, Ordinal len, Ordinal vector_size)
302 {
303 return reinterpret_cast<Vector*>(v);
304 }
305 };
306
307 }
308
309 // Helper traits to distinguish work arrays for real and complex-valued datatypes.
310 using namespace details;
311
312 //=============================================================================
313
314 template<typename OrdinalType, typename Storage>
315 SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
316 SerialQRDenseSolver()
317 : CompObject(),
318 Object("Teuchos::SerialQRDenseSolver"),
319 M_(0),
320 N_(0)
321 {
322 resetMatrix();
323 }
324
325 //=============================================================================
326
327 template<typename OrdinalType, typename Storage>
328 void SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
329 resetVectors()
330 {
331 LHS_ = Teuchos::null;
332 RHS_ = Teuchos::null;
333 }
334 //=============================================================================
335
336 template<typename OrdinalType, typename Storage>
337 void SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
338 resetMatrix()
339 {
340 resetVectors();
341 M_ = 0;
342 N_ = 0;
343 }
344 //=============================================================================
345
346 template<typename OrdinalType, typename Storage>
347 RCP<SerialDenseMatrix<OrdinalType, typename Sacado::MP::Vector<Storage>::value_type> >
348 SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
349 createBaseMatrix(
350 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& mat) const
351 {
352 BaseScalarType* base_ptr =
353 MPVectorArrayHelper<Storage>::getValueArray(
354 mat->values(), mat->stride()*mat->numCols());
355 RCP<BaseMatrixType> base_mat =
356 rcp(new BaseMatrixType(Teuchos::View,
357 base_ptr,
358 mat->stride()*SacadoSize_,
359 mat->numRows()*SacadoSize_,
360 mat->numCols()));
361 return base_mat;
362 }
363 //=============================================================================
364
365 template<typename OrdinalType, typename Storage>
366 RCP<SerialDenseMatrix<OrdinalType, Sacado::MP::Vector<Storage> > >
367 SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
368 createMatrix(
369 const RCP<SerialDenseMatrix<OrdinalType,BaseScalarType> >& base_mat) const
370 {
371 ScalarType* ptr =
372 MPVectorArrayHelper<Storage>::getVectorArray(
373 base_mat->values(), base_mat->stride()*base_mat->numCols(), SacadoSize_);
374 RCP<MatrixType> mat =
375 rcp(new MatrixType(Teuchos::View,
376 ptr,
377 base_mat->stride()/SacadoSize_,
378 base_mat->numRows()/SacadoSize_,
379 base_mat->numCols()));
380 return mat;
381 }
382 //=============================================================================
383
384 template<typename OrdinalType, typename Storage>
385 int SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
386 setMatrix(const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& A)
387 {
388 TEUCHOS_TEST_FOR_EXCEPTION(
389 A->numRows() < A->numCols(), std::invalid_argument,
390 "SerialQRDenseSolver<T>::setMatrix: the matrix A must have A.numRows() >= A.numCols()!");
391
392 resetMatrix();
393 Matrix_ = A;
394 Factor_ = A;
395 FactorQ_ = A;
396 FactorR_ = A;
397 M_ = A->numRows();
398 N_ = A->numCols();
399 if (Storage::is_static)
400 SacadoSize_ = Storage::static_size;
401 else if (M_ > 0 && N_ > 0)
402 SacadoSize_ = (*A)(0,0).size();
403 else
404 SacadoSize_ = 0;
405
406 return base_QR_.setMatrix( createBaseMatrix(A) );
407 }
408 //=============================================================================
409
410 template<typename OrdinalType, typename Storage>
411 int SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
412 setVectors(
413 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& X,
414 const RCP<SerialDenseMatrix<OrdinalType,ScalarType> >& B)
415 {
416 // Check that these new vectors are consistent
417 TEUCHOS_TEST_FOR_EXCEPTION(
418 B->numCols() != X->numCols(), std::invalid_argument,
419 "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
420 TEUCHOS_TEST_FOR_EXCEPTION(
421 B->values()==0, std::invalid_argument,
422 "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
423 TEUCHOS_TEST_FOR_EXCEPTION(
424 X->values()==0, std::invalid_argument,
425 "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
426 TEUCHOS_TEST_FOR_EXCEPTION(
427 B->stride()<1, std::invalid_argument,
428 "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
429 TEUCHOS_TEST_FOR_EXCEPTION(
430 X->stride()<1, std::invalid_argument,
431 "SerialQRDenseSolver<T>::setVectors: X has an invalid stride!");
432
433 resetVectors();
434 LHS_ = X;
435 RHS_ = B;
436
437 return base_QR_.setVectors( createBaseMatrix(X), createBaseMatrix(B) );
438 }
439
440 //=============================================================================
441
442 template<typename OrdinalType, typename Storage>
443 int SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
444 formQ() {
445 int ret = base_QR_.formQ();
446 FactorQ_ = createMatrix( base_QR_.getQ() );
447 return(ret);
448 }
449
450 //=============================================================================
451
452 template<typename OrdinalType, typename Storage>
453 int SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
454 formR() {
455 int ret = base_QR_.formR();
456 FactorR_ = createMatrix( base_QR_.getR() );
457 Factor_ = createMatrix( base_QR_.getFactoredMatrix() );
458 return(ret);
459 }
460
461 //=============================================================================
462
463 template<typename OrdinalType, typename Storage>
464 int SerialQRDenseSolver<OrdinalType, Sacado::MP::Vector<Storage> >::
465 multiplyQ(ETransp transq, SerialDenseMatrix<OrdinalType, ScalarType> &C)
466 {
467 return base_QR_.multiplyQ(transq, createBaseMatrix(C));
468 }
469
470 //=============================================================================
471
472 template<typename OrdinalType, typename Storage>
473 int SerialQRDenseSolver<OrdinalType, Sacado::MP::Vector<Storage> >::
474 solveR(ETransp transr, SerialDenseMatrix<OrdinalType, ScalarType> &C)
475 {
476 return base_QR_.solveR(transr, createBaseMatrix(C));
477 }
478
479 //=============================================================================
480
481 template<typename OrdinalType, typename Storage>
482 void SerialQRDenseSolver<OrdinalType,Sacado::MP::Vector<Storage> >::
483 Print(std::ostream& os) const {
484
485 if (Matrix_!=Teuchos::null) os << "Solver Matrix" << std::endl << *Matrix_ << std::endl;
486 if (Factor_!=Teuchos::null) os << "Solver Factored Matrix" << std::endl << *Factor_ << std::endl;
487 if (LHS_ !=Teuchos::null) os << "Solver LHS" << std::endl << *LHS_ << std::endl;
488 if (RHS_ !=Teuchos::null) os << "Solver RHS" << std::endl << *RHS_ << std::endl;
489
490 }
491
492 } // namespace Teuchos
493
494 #endif /* _TEUCHOS_SERIALQRDENSESOLVER_MP_VECTOR_HPP_ */