Belos Version of the Day
Loading...
Searching...
No Matches
BelosTeuchosDenseAdapter.hpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Belos: Block Linear Solvers Package
4//
5// Copyright 2004-2016 NTESS and the Belos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9//
10#ifndef BELOS_TEUCHOS_DENSE_MAT_TRAITS_HPP
11#define BELOS_TEUCHOS_DENSE_MAT_TRAITS_HPP
12
18#include "Teuchos_RCP.hpp"
19#include "Teuchos_ScalarTraits.hpp"
20#include "Teuchos_SerialDenseMatrix.hpp"
21#include "Teuchos_SerialDenseSolver.hpp"
22#include "Teuchos_SerialSpdDenseSolver.hpp"
23
25#include "BelosDenseSolver.hpp"
26
27namespace Belos {
28
30 template<class ScalarType>
31 class TeuchosDenseSolver : public DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType> >
32 {
33 public:
34
36
37
39
43
45
46
47 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::setMatrix;
48
50 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::setVectors;
52
54
55
58 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::setSPD;
59
61
63 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::factorWithEquilibration;
64
66
68 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::solveWithTransposeFlag;
70
72
73
75
78 int factor()
79 {
80 //std::cout << "Calling DenseMatrix<Teuchos> factor!" << std::endl;
81 // Create solver object if one doesn't exist.
82 if ( (dSolver_==Teuchos::null) && (spdSolver_==Teuchos::null) )
83 {
84 if (spd_)
85 spdSolver_ = Teuchos::rcp( new Teuchos::SerialSpdDenseSolver<int,ScalarType>() );
86 else
87 dSolver_ = Teuchos::rcp( new Teuchos::SerialDenseSolver<int,ScalarType>() );
88 }
89
90 int info = 0;
91
92 // Set the matrix and factor
93 if (spd_)
94 {
95 if (newMatrix_)
96 {
97 spdMatrix_ = Teuchos::rcp( new Teuchos::SerialSymDenseMatrix<int,ScalarType>(Teuchos::View, true, A_->values(),
98 A_->numRows(), A_->numCols()) );
99 }
100 spdSolver_->setMatrix( spdMatrix_ );
101 spdSolver_->factorWithEquilibration( equilibrate_ );
102 info = spdSolver_->factor();
103 }
104 else
105 {
106 dSolver_->setMatrix( A_ );
107 dSolver_->solveWithTransposeFlag( TRANS_ );
108 dSolver_->factorWithEquilibration( equilibrate_ );
109 info = dSolver_->factor();
110 }
111
112 if (!info)
113 newMatrix_ = false;
114 return info;
115 }
116
118
121 int solve()
122 {
123 //std::cout << "Calling DenseMatrix<Teuchos> solve!" << std::endl;
124 // Check if this is a new matrix that has not been factored.
125 if (newMatrix_)
126 factor();
127
128 int info = 0;
129
130 if (spd_)
131 {
132 spdSolver_->setVectors( X_, B_ );
133 info = spdSolver_->solve();
134 }
135 else
136 {
137 dSolver_->setVectors( X_, B_ );
138 info = dSolver_->solve();
139 }
140
141 return info;
142 }
143
145
146 private:
147
148 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::newMatrix_;
149 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::equilibrate_;
150 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::TRANS_;
151 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::spd_;
152
153 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::A_;
154 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::X_;
155 using DenseSolver<ScalarType,Teuchos::SerialDenseMatrix<int,ScalarType>>::B_;
156
157 // Pointer to dense solver for general linear systems
158 Teuchos::RCP<Teuchos::SerialDenseSolver<int,ScalarType>> dSolver_;
159
160 // Pointer to dense solver for SPD linear systems
161 Teuchos::RCP<Teuchos::SerialSymDenseMatrix<int,ScalarType>> spdMatrix_;
162 Teuchos::RCP<Teuchos::SerialSpdDenseSolver<int,ScalarType>> spdSolver_;
163
164 };
165
166
168 template<class ScalarType>
169 class DenseMatTraits<ScalarType, Teuchos::SerialDenseMatrix<int,ScalarType>>{
170 public:
171
173
178 static Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> Create() {
179 return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>());
180 }
181
187 static Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> Create( const int numrows, const int numcols, bool initZero = true) {
188 return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>(numrows,numcols,initZero));
189 }
190
195 static Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> CreateCopy(const Teuchos::SerialDenseMatrix<int,ScalarType> & dm, bool transpose=false) {
196 if (transpose)
197 return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>(dm, Teuchos::CONJ_TRANS));
198 else
199 return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy, dm));
200 }
201
203 static ScalarType* GetRawHostPtr( Teuchos::SerialDenseMatrix<int,ScalarType> & dm )
204 { return dm.values(); }
205
207 static ScalarType const * GetConstRawHostPtr(const Teuchos::SerialDenseMatrix<int,ScalarType> & dm )
208 { return const_cast<ScalarType const *>(dm.values()); }
209
213 //static void RawPtrDataModified(Teuchos::SerialDenseMatrix<int,ScalarType> & dm ) { }
214
216 // Row and column indexing is zero-based.
217 // Source - Reference to another dense matrix from which values are to be copied.
218 // numRows - The number of rows in this matrix.
219 // numCols - The number of columns in this matrix.
220 // startRow - The row of Source from which the submatrix copy should start.
221 // startCol - The column of Source from which the submatrix copy should start.
222 //
223 // Should ints be const? Should they be ints or some other ordinal type?
224 static Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> Subview(Teuchos::SerialDenseMatrix<int,ScalarType> & source, int numRows, int numCols, int startRow=0, int startCol=0)
225 { return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View, source, numRows, numCols, startRow, startCol)); }
226
227 static Teuchos::RCP<const Teuchos::SerialDenseMatrix<int,ScalarType>> SubviewConst(
228 const Teuchos::SerialDenseMatrix<int,ScalarType> & source, int numRows, int numCols, int startRow=0, int startCol=0)
229 { return Teuchos::rcp(new const Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::View, source, numRows, numCols, startRow, startCol)); }
230
232 static Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType>> SubviewCopy( const Teuchos::SerialDenseMatrix<int,ScalarType>& source, int numRows, int numCols, int startRow=0, int startCol=0)
233 { return Teuchos::rcp(new Teuchos::SerialDenseMatrix<int,ScalarType>(Teuchos::Copy, source, numRows, numCols, startRow, startCol)); }
235
237
239 static int GetNumRows( const Teuchos::SerialDenseMatrix<int,ScalarType>& dm )
240 { return dm.numRows(); }
241
243 static int GetNumCols( const Teuchos::SerialDenseMatrix<int,ScalarType>& dm )
244 { return dm.numCols(); }
245
247 static int GetStride( const Teuchos::SerialDenseMatrix<int,ScalarType>& dm )
248 { return dm.stride(); }
249
251
253
254 /* \brief Reshaping method for changing the size of \c dm to have \c numrows rows and \c numcols columns.
255 * All values will be initialized to zero if the final argument is true.
256 * If the final argument is fale, the previous entries in
257 * the matrix will be maintained. For new entries that did not exist in the previous matrix, values will
258 * contain noise from memory.
259 */
260 static void Reshape( Teuchos::SerialDenseMatrix<int,ScalarType>& dm, const int numrows, const int numcols, bool initZero = false) {
261 if(initZero){
262 int err = dm.shape(numrows,numcols);
263 if(err != 0){throw std::runtime_error ("Error in DenseMatrixTraits::shape. Teuchos::SerialDenseMatrix.shape failed.");}
264 }
265 else{
266 int err = dm.reshape(numrows,numcols);
267 if(err != 0){throw std::runtime_error ("Error in DenseMatrixTraits::reshape. Teuchos::SerialDenseMatrix.reshape failed.");}
268 }
269 }
270
272
274
276 static ScalarType & Value( Teuchos::SerialDenseMatrix<int,ScalarType>& dm, const int i, const int j )
277 {
278 return dm(i,j);
279 }
280
282 static const ScalarType & ValueConst( const Teuchos::SerialDenseMatrix<int,ScalarType>& dm, const int i, const int j )
283 {
284 return dm(i,j);
285 }
286
287 static void SyncDeviceToHost(Teuchos::SerialDenseMatrix<int,ScalarType> &){ }
288
289 static void SyncHostToDevice(Teuchos::SerialDenseMatrix<int,ScalarType> &){ }
291
293
295 static void Add( Teuchos::SerialDenseMatrix<int,ScalarType>& thisDM, const Teuchos::SerialDenseMatrix<int,ScalarType>& sourceDM){
296 thisDM += sourceDM;
297 }
298
300 static void PutScalar( Teuchos::SerialDenseMatrix<int,ScalarType> & dm, ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero()){
301 dm.putScalar(value);
302 }
303
305 static void Scale( Teuchos::SerialDenseMatrix<int,ScalarType>& dm, ScalarType value){
306 dm.scale(value);
307 }
308
311 //TODO What to do here? Kinda needs random synced version??
312 static void Randomize( Teuchos::SerialDenseMatrix<int,ScalarType>& dm){
313 dm.random();
314 }
315
317 static void Assign( Teuchos::SerialDenseMatrix<int,ScalarType>& dest, const Teuchos::SerialDenseMatrix<int,ScalarType>& source){
318 dest.assign(source);
319 }
320
322 static typename Teuchos::ScalarTraits<ScalarType>::magnitudeType NormFrobenius(const Teuchos::SerialDenseMatrix<int,ScalarType>& dm) {
323 return dm.normFrobenius();
324 }
325
327 static typename Teuchos::ScalarTraits<ScalarType>::magnitudeType NormOne(const Teuchos::SerialDenseMatrix<int,ScalarType>& dm) {
328 return dm.normOne();
329 }
331
333
335 static Teuchos::RCP<DenseSolver<ScalarType, Teuchos::SerialDenseMatrix<int,ScalarType>>>
337
338 Teuchos::RCP<DenseSolver<ScalarType, Teuchos::SerialDenseMatrix<int,ScalarType>>> newSolver
339 = Teuchos::rcp( new TeuchosDenseSolver<ScalarType>() );
340 return newSolver;
341 }
343
344 };
345
346} // namespace Belos
347
348#endif // end file BELOS_TEUCHOS_DENSE_MAT_TRAITS_HPP
Virtual base class which defines solvers for dense matrix type.
static void PutScalar(Teuchos::SerialDenseMatrix< int, ScalarType > &dm, ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Fill all entries with value. Value is zero if not specified.
static void Reshape(Teuchos::SerialDenseMatrix< int, ScalarType > &dm, const int numrows, const int numcols, bool initZero=false)
static int GetNumRows(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Obtain the number of rows of dm.
static Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > SubviewCopy(const Teuchos::SerialDenseMatrix< int, ScalarType > &source, int numRows, int numCols, int startRow=0, int startCol=0)
Returns a deep copy of the requested subview.
static Teuchos::RCP< const Teuchos::SerialDenseMatrix< int, ScalarType > > SubviewConst(const Teuchos::SerialDenseMatrix< int, ScalarType > &source, int numRows, int numCols, int startRow=0, int startCol=0)
static ScalarType const * GetConstRawHostPtr(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Returns a raw pointer to const data on the host.
static void Assign(Teuchos::SerialDenseMatrix< int, ScalarType > &dest, const Teuchos::SerialDenseMatrix< int, ScalarType > &source)
Copies entries of source to dest (deep copy).
static void Scale(Teuchos::SerialDenseMatrix< int, ScalarType > &dm, ScalarType value)
Multiply all entries by a scalar. DM = value.*DM.
static Teuchos::RCP< DenseSolver< ScalarType, Teuchos::SerialDenseMatrix< int, ScalarType > > > createDenseSolver()
Returns a dense solver object for the dense matrix.
static ScalarType * GetRawHostPtr(Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Returns a raw pointer to the (non-const) data on the host.
static ScalarType & Value(Teuchos::SerialDenseMatrix< int, ScalarType > &dm, const int i, const int j)
Access a reference to the (i,j) entry of dm, e_i^T dm e_j.
static void Randomize(Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Fill the DM with random entries.
static Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > CreateCopy(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm, bool transpose=false)
Create a new copy Teuchos::SerialDenseMatrix<int,ScalarType>, possibly transposed.
static int GetStride(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Obtain the stride between the columns of dm.
static Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > Subview(Teuchos::SerialDenseMatrix< int, ScalarType > &source, int numRows, int numCols, int startRow=0, int startCol=0)
Marks host data modified to avoid device sync errors.
static void Add(Teuchos::SerialDenseMatrix< int, ScalarType > &thisDM, const Teuchos::SerialDenseMatrix< int, ScalarType > &sourceDM)
Adds sourceDM to thisDM and returns answer in thisDM.
static int GetNumCols(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Obtain the number of columns of dm.
static Teuchos::ScalarTraits< ScalarType >::magnitudeType NormOne(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Returns the one-norm of the dense matrix.
static Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > Create(const int numrows, const int numcols, bool initZero=true)
Creates a new empty Teuchos::SerialDenseMatrix<int,ScalarType> containing numvecs columns....
static void SyncDeviceToHost(Teuchos::SerialDenseMatrix< int, ScalarType > &)
static void SyncHostToDevice(Teuchos::SerialDenseMatrix< int, ScalarType > &)
static const ScalarType & ValueConst(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm, const int i, const int j)
Access a const reference to the (i,j) entry of dm, e_i^T dm e_j.
static Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > Create()
Creates a new empty Teuchos::SerialDenseMatrix<int,ScalarType> with no dimension.
static Teuchos::ScalarTraits< ScalarType >::magnitudeType NormFrobenius(const Teuchos::SerialDenseMatrix< int, ScalarType > &dm)
Returns the Frobenius norm of the dense matrix.
Virtual base class which defines basic traits for the multi-vector type.
void setSPD(bool flag)
Set if dense matrix is symmetric positive definite.
void solveWithTransposeFlag(Teuchos::ETransp trans)
All subsequent function calls will work with the transpose-type set by this method (Teuchos::NO_TRANS...
virtual int setMatrix(const Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > &A)
Sets the pointers for coefficient matrix.
Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B_
virtual int setVectors(const Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > &X, const Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > &B)
Sets the pointers for left and right hand side vector(s).
Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > X_
Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > A_
void factorWithEquilibration(bool flag)
Causes equilibration to be called just before the matrix factorization as part of the call to factor.
Alternative run-time polymorphic interface for operators.
Full specialization of Belos::DenseSolver for Teuchos::SerialDenseMatrix<int,ST>.
int solve()
Computes the solution X to AX = B for the this matrix and the B provided.
int factor()
Computes the in-place LU factorization of the matrix.
virtual ~TeuchosDenseSolver()
TeuchosDenseSolver destructor.
TeuchosDenseSolver()
Default constructor; matrix should be set using setMatrix(), LHS and RHS set with setVectors().

Generated for Belos by doxygen 1.9.8