Belos Package Browser (Single Doxygen Collection)  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyBetterOperator.hpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // *****************************************************************************
4 // Belos: Block Linear Solvers Package
5 //
6 // Copyright 2004-2016 NTESS and the Belos contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10 */
11 
12 #ifndef MY_BETTER_OPERATOR_HPP
13 #define MY_BETTER_OPERATOR_HPP
14 
15 #include "BelosConfigDefs.hpp"
16 #include "BelosOperator.hpp"
17 #include "MyMultiVec.hpp"
18 #include "Teuchos_BLAS.hpp"
19 
21 
33 template <class ScalarType>
34 class MyBetterOperator : public Belos::Operator<ScalarType>
35 {
36 
37 public:
38 
40  MyBetterOperator(const int nrows, const int *colptr,
41  const int nnz, const int *rowin, const ScalarType *vals)
42  : _nr(nrows), _nnz(nnz), _cptr(nrows+1), _rind(nnz), _vals(nnz)
43  {
44  std::copy<const int*,IntIter>(colptr,colptr+nrows+1,_cptr.begin());
45  std::copy<const int*,IntIter>(rowin,rowin+nnz,_rind.begin());
46  std::copy<const ScalarType*,STIter>(vals,vals+nnz,_vals.begin());
47  }
48 
51  { }
52 
56  Belos::ETrans trans = Belos::NOTRANS) const
57  {
58  const MyMultiVec<ScalarType>* MyX;
59  MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X);
60  assert (MyX != 0);
61 
63  MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y);
64  assert (MyY != 0);
65 
66  // Initialize output std::vector to zero.
68 
69  assert (X.GetNumberVecs() == Y.GetNumberVecs());
70  assert (X.GetGlobalLength() == Y.GetGlobalLength());
71 
72  int nv = X.GetNumberVecs();
73 
74  // Apply operator
75  int IA1, IA2, ri;
76  ScalarType aval;
77  int i,j,v;
78  for (j=0; j<_nr; j++) {
79  IA1 = _cptr[j]-1;
80  IA2 = _cptr[j+1]-1;
81  for (i=IA1; i<IA2; i++) {
82  ri = _rind[i]-1;
83  aval = _vals[i];
84  for (v=0; v<nv; v++) {
85  (*MyY)[v][ri] += aval*(*MyX)[v][j];
86  }
87  }
88  }
89  }
90 
91  void Print( std::ostream& os ) {
92  for (int j=0; j<_nr; j++) {
93  int IA1 = _cptr[j]-1;
94  int IA2 = _cptr[j+1]-1;
95  for (int i=IA1; i<IA2; i++) {
96  os << "("<<_rind[i]-1<<","<<j<<")\t"<<_vals[i]<< std::endl;
97  }
98  }
99  }
100 
101  private:
102  typedef typename std::vector<ScalarType>::iterator STIter;
103  typedef std::vector<int>::iterator IntIter;
105  int _nr, _nnz;
107  std::vector<int> _cptr;
109  std::vector<int> _rind;
111  std::vector<ScalarType> _vals;
112 };
113 
114 #endif //MY_BETTER_OPERATOR_HPP
std::vector< int >::iterator IntIter
std::vector< ScalarType >::iterator STIter
void Apply(const Belos::MultiVec< ScalarType > &X, Belos::MultiVec< ScalarType > &Y, Belos::ETrans trans=Belos::NOTRANS) const
Applies the matrix to a multivector.
MyBetterOperator(const int nrows, const int *colptr, const int nnz, const int *rowin, const ScalarType *vals)
Constructor.
~MyBetterOperator()
Deconstructor.
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
std::vector< ScalarType > _vals
Values.
void Print(std::ostream &os)
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
ETrans
Whether to apply the (conjugate) transpose of an operator.
Definition: BelosTypes.hpp:49
Simple example of a user&#39;s defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:33
Alternative run-time polymorphic interface for operators.
std::vector< int > _rind
Row indices.
std::vector< int > _cptr
Column pointers.
Alternative run-time polymorphic interface for operators.
Interface for multivectors used by Belos&#39; linear solvers.
Belos header file which uses auto-configuration information to include necessary C++ headers...
void MvInit(const ScalarType alpha)
Replace each element of the vectors in *this with alpha.
Definition: MyMultiVec.hpp:366
Simple example of a user&#39;s defined Belos::Operator class.
int _nr
Number of rows and columns.