Thyra  Version of the Day
Thyra_MultiVectorBase_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (bartlettra@ornl.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef THYRA_MULTI_VECTOR_BASE_HPP
43 #define THYRA_MULTI_VECTOR_BASE_HPP
44 
45 #include "Thyra_MultiVectorBase_decl.hpp"
46 #include "Thyra_LinearOpBase.hpp"
47 #include "Thyra_VectorSpaceBase.hpp"
48 
49 #include "Thyra_VectorBase.hpp"
50 #include "Thyra_VectorStdOps_decl.hpp"
51 
52 #include "RTOpPack_TOpAbs.hpp"
53 #include "RTOpPack_ROpNorm1.hpp"
54 
55 namespace Thyra {
56 
57 
58 // Provide access to the columns as VectorBase objects
59 
60 
61 template<class Scalar>
62 RCP<const VectorBase<Scalar> >
64 {
65  return const_cast<MultiVectorBase*>(this)->nonconstColImpl(j);
66 }
67 
68 
69 // Overridden methods from LinearOpBase
70 
71 
72 template<class Scalar>
73 RCP<const LinearOpBase<Scalar> >
75 {
76  return this->clone_mv();
77 }
78 
79 // Overridden methods from RowStatLinearOpBase
80 
81 template<class Scalar>
83 rowStatIsSupportedImpl(const RowStatLinearOpBaseUtils::ERowStat rowStat) const
84 {
85  switch (rowStat) {
86  case RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM:
87  case RowStatLinearOpBaseUtils::ROW_STAT_ROW_SUM:
88  case RowStatLinearOpBaseUtils::ROW_STAT_INV_COL_SUM:
89  case RowStatLinearOpBaseUtils::ROW_STAT_COL_SUM:
90  return true;
91  break;
92  default:
93  TEUCHOS_TEST_FOR_EXCEPT(true);
94  }
95 
96  return false; // will never be called
97 }
98 
99 template<class Scalar>
101 getRowStatImpl(const RowStatLinearOpBaseUtils::ERowStat rowStat,
102  const Ptr<VectorBase<Scalar> > &rowStatVec) const
103 {
104  switch (rowStat) {
105  case RowStatLinearOpBaseUtils::ROW_STAT_INV_ROW_SUM:
106  absRowSum(rowStatVec);
107  ::Thyra::reciprocal<Scalar>(*rowStatVec,rowStatVec.ptr());
108  break;
109  case RowStatLinearOpBaseUtils::ROW_STAT_ROW_SUM:
110  // compute absolute row sum
111  absRowSum(rowStatVec);
112  break;
113  case RowStatLinearOpBaseUtils::ROW_STAT_INV_COL_SUM:
114  absColSum(rowStatVec);
115  ::Thyra::reciprocal<Scalar>(*rowStatVec,rowStatVec.ptr());
116  break;
117  case RowStatLinearOpBaseUtils::ROW_STAT_COL_SUM:
118  // compute absolute row sum
119  absColSum(rowStatVec);
120  break;
121  default:
122  TEUCHOS_TEST_FOR_EXCEPT(true);
123  }
124 }
125 
126 // Overridden methods from ScaledLinearOpBase
127 
128 template<class Scalar>
131 {
132  return true;
133 }
134 
135 template<class Scalar>
138 {
139  return true;
140 }
141 
142 template<class Scalar>
145 {
146  // loop over each column applying the row scaling
147  for(Ordinal i=0;i<this->domain()->dim();i++)
148  ::Thyra::ele_wise_scale<Scalar>(row_scaling,this->col(i).ptr());
149 }
150 
151 template<class Scalar>
154 {
155  // this is probably incorrect if the domain is distrbuted
156  // but if it is on every processor its probably fine...
157 
159  col_scaling.acquireDetachedView(Thyra::Range1D(),&view);
160 
161  Teuchos::ArrayRCP<const Scalar> col_scaling_vec = view.values();
162 
163  // check to make sure things match up
164  TEUCHOS_ASSERT(this->domain()->dim()==col_scaling_vec.size());
165 
166  for(Ordinal i=0;i<this->domain()->dim();i++)
167  ::Thyra::scale<Scalar>(col_scaling_vec[i],this->col(i).ptr());
168 }
169 
170 // helper methods
171 
172 template<class Scalar>
174 absRowSum(const Teuchos::Ptr<Thyra::VectorBase<Scalar> > & output) const
175 {
176  using Teuchos::RCP;
177  using Teuchos::ptrFromRef;
178  using Teuchos::tuple;
179 
180  // compute absolute value of multi-vector
181  RTOpPack::TOpAbs<Scalar> abs_op;
182  RCP<MultiVectorBase<Scalar> > abs_mv = createMembers(this->range(),this->domain());
183  ::Thyra::applyOp<Scalar>( abs_op, tuple(ptrFromRef(*this)), tuple(abs_mv.ptr()), Teuchos::null );
184 
185  // compute sum over all rows
186  RCP<VectorBase<Scalar> > ones = Thyra::createMember(this->domain());
187  ::Thyra::put_scalar<Scalar>(Teuchos::ScalarTraits<Scalar>::one(),ones.ptr());
188  ::Thyra::apply<Scalar>(*abs_mv,Thyra::NOTRANS,*ones,output);
189 }
190 
191 template<class Scalar>
193 absColSum(const Teuchos::Ptr<Thyra::VectorBase<Scalar> > & output) const
194 {
195  using Teuchos::tuple;
196  using Teuchos::ptrInArg;
197  using Teuchos::null;
198  using Teuchos::Array;
199  using Teuchos::ArrayView;
200 
202  output->acquireDetachedView(Thyra::Range1D(),&view);
203 
204  // Thyra::norms_1<Scalar>(*this,view.values()());
205 
206  ArrayView<Scalar> norms = view.values()();
207  RTOpPack::ROpNorm1<Scalar> op;
208 
209  const int m = this->domain()->dim();
210  Array<RCP<RTOpPack::ReductTarget> > rcp_op_targs(m);
211  Array<Ptr<RTOpPack::ReductTarget> > op_targs(m);
212  for( int kc = 0; kc < m; ++kc ) {
213  rcp_op_targs[kc] = op.reduct_obj_create();
214  op_targs[kc] = rcp_op_targs[kc].ptr();
215  }
216  ::Thyra::applyOp<Scalar>(op, tuple(ptrInArg(*this)),
217  ArrayView<Ptr<MultiVectorBase<Scalar> > >(null),
218  op_targs );
219  for( int kc = 0; kc < m; ++kc ) {
220  norms[kc] = op(*op_targs[kc]);
221  }
222 
223  output->commitDetachedView(&view);
224 }
225 
226 
227 } // end namespace Thyra
228 
229 
230 #endif // THYRA_MULTI_VECTOR_BASE_HPP
void absColSum(const Teuchos::Ptr< Thyra::VectorBase< Scalar > > &output) const
void acquireDetachedView(const Range1D &rng, RTOpPack::ConstSubVectorView< Scalar > *sub_vec) const
Calls acquireDetachedVectorViewImpl().
void absRowSum(const Teuchos::Ptr< Thyra::VectorBase< Scalar > > &output) const
virtual void getRowStatImpl(const RowStatLinearOpBaseUtils::ERowStat rowStat, const Ptr< VectorBase< Scalar > > &rowStatVec) const
Use the non-transposed operator.
virtual void scaleLeftImpl(const VectorBase< Scalar > &row_scaling)
virtual bool rowStatIsSupportedImpl(const RowStatLinearOpBaseUtils::ERowStat rowStat) const
virtual bool supportsScaleRightImpl() const
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
Interface for a collection of column vectors called a multi-vector.
Abstract interface for finite-dimensional dense vectors.
virtual bool supportsScaleLeftImpl() const
virtual void scaleRightImpl(const VectorBase< Scalar > &col_scaling)
virtual RCP< const VectorBase< Scalar > > colImpl(Ordinal j) const
Return a non-changeable view of a constituent column vector.
const ArrayRCP< Scalar > values() const
RCP< const LinearOpBase< Scalar > > clone() const
This function is simply overridden to return this->clone_mv().
Teuchos::Range1D Range1D