Belos Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
MyBetterOperator.hpp
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// Belos: Block Linear Solvers Package
6// Copyright 2004 Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef MY_BETTER_OPERATOR_HPP
45#define MY_BETTER_OPERATOR_HPP
46
47#include "BelosConfigDefs.hpp"
48#include "BelosOperator.hpp"
49#include "MyMultiVec.hpp"
50#include "Teuchos_BLAS.hpp"
51
53
65template <class ScalarType>
66class MyBetterOperator : public Belos::Operator<ScalarType>
67{
68
69public:
70
72 MyBetterOperator(const int nrows, const int *colptr,
73 const int nnz, const int *rowin, const ScalarType *vals)
74 : _nr(nrows), _nnz(nnz), _cptr(nrows+1), _rind(nnz), _vals(nnz)
75 {
76 std::copy<const int*,IntIter>(colptr,colptr+nrows+1,_cptr.begin());
77 std::copy<const int*,IntIter>(rowin,rowin+nnz,_rind.begin());
78 std::copy<const ScalarType*,STIter>(vals,vals+nnz,_vals.begin());
79 }
80
83 { }
84
88 Belos::ETrans trans = Belos::NOTRANS) const
89 {
90 const MyMultiVec<ScalarType>* MyX;
91 MyX = dynamic_cast<const MyMultiVec<ScalarType>*>(&X);
92 assert (MyX != 0);
93
95 MyY = dynamic_cast<MyMultiVec<ScalarType>*>(&Y);
96 assert (MyY != 0);
97
98 // Initialize output std::vector to zero.
99 MyY->MvInit( Teuchos::ScalarTraits<ScalarType>::zero() );
100
101 assert (X.GetNumberVecs() == Y.GetNumberVecs());
102 assert (X.GetGlobalLength() == Y.GetGlobalLength());
103
104 int nv = X.GetNumberVecs();
105
106 // Apply operator
107 int IA1, IA2, ri;
108 ScalarType aval;
109 int i,j,v;
110 for (j=0; j<_nr; j++) {
111 IA1 = _cptr[j]-1;
112 IA2 = _cptr[j+1]-1;
113 for (i=IA1; i<IA2; i++) {
114 ri = _rind[i]-1;
115 aval = _vals[i];
116 for (v=0; v<nv; v++) {
117 (*MyY)[v][ri] += aval*(*MyX)[v][j];
118 }
119 }
120 }
121 }
122
123 void Print( std::ostream& os ) {
124 for (int j=0; j<_nr; j++) {
125 int IA1 = _cptr[j]-1;
126 int IA2 = _cptr[j+1]-1;
127 for (int i=IA1; i<IA2; i++) {
128 os << "("<<_rind[i]-1<<","<<j<<")\t"<<_vals[i]<< std::endl;
129 }
130 }
131 }
132
133 private:
134 typedef typename std::vector<ScalarType>::iterator STIter;
135 typedef std::vector<int>::iterator IntIter;
137 int _nr, _nnz;
139 std::vector<int> _cptr;
141 std::vector<int> _rind;
143 std::vector<ScalarType> _vals;
144};
145
146#endif //MY_BETTER_OPERATOR_HPP
Belos header file which uses auto-configuration information to include necessary C++ headers.
Alternative run-time polymorphic interface for operators.
Interface for multivectors used by Belos' linear solvers.
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
Alternative run-time polymorphic interface for operators.
Simple example of a user's defined Belos::Operator class.
std::vector< int > _rind
Row indices.
void Print(std::ostream &os)
MyBetterOperator(const int nrows, const int *colptr, const int nnz, const int *rowin, const ScalarType *vals)
Constructor.
std::vector< int > _cptr
Column pointers.
int _nr
Number of rows and columns.
std::vector< ScalarType > _vals
Values.
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()
Deconstructor.
Simple example of a user's defined Belos::MultiVec class.
Definition: MyMultiVec.hpp:66
void MvInit(const ScalarType alpha)
Replace each element of the vectors in *this with alpha.
Definition: MyMultiVec.hpp:398
ETrans
Whether to apply the (conjugate) transpose of an operator.
Definition: BelosTypes.hpp:81
@ NOTRANS
Definition: BelosTypes.hpp:81