Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SerialQRDenseSolver_MP_Vector.hpp
Go to the documentation of this file.
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 
16 #include <new>
18 
19 #include "Sacado_MP_Vector.hpp"
20 
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 
37 
39 
42 
44  virtual ~SerialQRDenseSolver() {}
46 
48 
49 
51 
54 
56 
62 
64 
65 
67 
69  void factorWithEquilibration(bool flag) {
70  base_QR_.factorWithEquilibration(flag);
71  }
72 
74  void solveWithTranspose(bool flag) {
75  base_QR_.solveWithTranspose(flag);
76  }
77 
80  base_QR_.solveWithTranspose(trans);
81  }
82 
84 
86 
87 
89 
92  int factor() { return base_QR_.factor(); }
93 
95 
98  int solve() { return base_QR_.solve(); }
99 
101 
105  return base_QR_.computeEquilibrateScaling();
106  }
107 
109 
113  int equilibrateMatrix() { return base_QR_.equilibrateMatrix(); }
114 
116 
120  int equilibrateRHS() { return base_QR_.equilibrateRHS(); }
121 
123 
127  int unequilibrateLHS() { return base_QR_.unequilibrateLHS(); }
128 
130 
133  int formQ();
134 
136 
139  int formR();
140 
142 
146 
148 
153 
155 
156 
158  bool transpose() { return base_QR_.transpose(); }
159 
161  bool factored() { return base_QR_.factored(); }
162 
164  bool equilibratedA() { return base_QR_.equilibratedA(); }
165 
167  bool equilibratedB() { return base_QR_.equilibratedB(); }
168 
170  bool shouldEquilibrate() { return base_QR_.shouldEquilibrate(); }
171 
173  bool solved() { return base_QR_.solved(); }
174 
176  bool formedQ() { return base_QR_.formedQ(); }
177 
179  bool formedR() { return base_QR_.formedR();}
180 
182 
184 
185 
188 
191 
194 
197 
200 
203 
205  OrdinalType numRows() const {return(M_);}
206 
208  OrdinalType numCols() const {return(N_);}
209 
211  std::vector<ScalarType> tau() const { return base_QR_.tau(); }
212 
214  MagnitudeType ANORM() const { return base_QR_.ANORM(); }
215 
217 
219 
220  void Print(std::ostream& os) const;
223  protected:
224 
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;
235 
236  OrdinalType M_;
237  OrdinalType N_;
238  OrdinalType SacadoSize_;
239 
246 
253 
254  private:
255 
256  // SerialQRDenseSolver copy constructor (put here because we don't want user access)
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  {
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> > {
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>
329 resetVectors()
330 {
331  LHS_ = Teuchos::null;
332  RHS_ = Teuchos::null;
333 }
334 //=============================================================================
335 
336 template<typename OrdinalType, typename Storage>
338 resetMatrix()
339 {
340  resetVectors();
341  M_ = 0;
342  N_ = 0;
343 }
344 //=============================================================================
345 
346 template<typename OrdinalType, typename Storage>
349 createBaseMatrix(
351 {
352  BaseScalarType* base_ptr =
354  mat->values(), mat->stride()*mat->numCols());
355  RCP<BaseMatrixType> base_mat =
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>
368 createMatrix(
369  const RCP<SerialDenseMatrix<OrdinalType,BaseScalarType> >& base_mat) const
370 {
371  ScalarType* ptr =
373  base_mat->values(), base_mat->stride()*base_mat->numCols(), SacadoSize_);
374  RCP<MatrixType> mat =
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>
387 {
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>
412 setVectors(
415 {
416  // Check that these new vectors are consistent
418  B->numCols() != X->numCols(), std::invalid_argument,
419  "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
421  B->values()==0, std::invalid_argument,
422  "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
424  X->values()==0, std::invalid_argument,
425  "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
427  B->stride()<1, std::invalid_argument,
428  "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
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>
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>
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>
466 {
467  return base_QR_.multiplyQ(transq, createBaseMatrix(C));
468 }
469 
470 //=============================================================================
471 
472 template<typename OrdinalType, typename Storage>
475 {
476  return base_QR_.solveR(transr, createBaseMatrix(C));
477 }
478 
479 //=============================================================================
480 
481 template<typename OrdinalType, typename 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_ */
void factorWithEquilibration(bool flag)
Causes equilibration to be called just before the matrix factorization as part of the call to factor...
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getFactoredMatrix() const
Returns pointer to factored matrix (assuming factorization has been performed).
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorR_
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getMatrix() const
Returns pointer to current matrix.
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)
bool factored()
Returns true if matrix is factored (factor available via getFactoredMatrix()).
int multiplyQ(ETransp transq, SerialDenseMatrix< OrdinalType, ScalarType > &C)
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getRHS() const
Returns pointer to current RHS.
int solveR(ETransp transr, SerialDenseMatrix< OrdinalType, ScalarType > &C)
Statically allocated storage class.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
bool equilibratedB()
Returns true if RHS is equilibrated (RHS available via getRHS()).
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > LHS_
SerialQRDenseSolver & operator=(const SerialQRDenseSolver< OrdinalType, ScalarType > &Source)
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getLHS() const
Returns pointer to current LHS.
bool shouldEquilibrate()
Returns true if the LAPACK general rules for equilibration suggest you should equilibrate the system...
void solveWithTranspose(bool flag)
If flag is true, causes all subsequent function calls to work with the adjoint of this matrix...
Templated class for solving dense linear problems.
Specialization for Sacado::UQ::PCE&lt; Storage&lt;...&gt; &gt;
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Matrix_
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorQ_
MagnitudeType ANORM() const
Returns the absolute value of the largest element of this matrix (returns -1 if not yet computed)...
bool solved()
Returns true if the current set of vectors has been solved.
void Print(std::ostream &os) const
int unequilibrateLHS()
Unscales the solution vectors if equilibration was used to solve the system.
bool equilibratedA()
Returns true if factor is equilibrated (factor available via getFactoredMatrix()).
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Factor_
std::vector< ScalarType > tau() const
Returns pointer to pivot vector (if factorization has been computed), zero otherwise.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > RHS_
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getR() const
Returns pointer to R (assuming factorization has been performed).
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getQ() const
Returns pointer to Q (assuming factorization has been performed).
int solve()
Computes the solution X to AX = B for the this matrix and the B provided to SetVectors()..
int factor()
Computes the in-place QR factorization of the matrix using the LAPACK routine _GETRF or the Eigen cla...
void solveWithTransposeFlag(Teuchos::ETransp trans)
All subsequent function calls will work with the transpose-type set by this method (Teuchos::NO_TRANS...
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
bool transpose()
Returns true if adjoint of this matrix has and will be used.