Thyra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_DiagonalScalarProd_def.hpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
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 Roscoe A. Bartlett (bartlettra@ornl.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 #ifndef THYRA_DIAGONAL_SCALAR_PROD_DEF_HPP
45 #define THYRA_DIAGONAL_SCALAR_PROD_DEF_HPP
46 
47 
48 #include "Thyra_DiagonalScalarProd_decl.hpp"
49 #include "Thyra_DetachedSpmdVectorView.hpp"
50 #include "Thyra_SpmdVectorSpaceBase.hpp"
51 #include "Thyra_AssertOp.hpp"
52 #include "Teuchos_CommHelpers.hpp"
53 
54 
55 namespace Thyra {
56 
57 
58 // Consturctors/Initializers/Accessors
59 
60 
61 template<class Scalar>
63 {}
64 
65 
66 template<class Scalar>
68  const RCP<const VectorBase<Scalar> > &s_diag )
69 {
70  s_diag_ = s_diag.assert_not_null();
71 }
72 
73 
74 // Overridden from ScalarProdBase
75 
76 
77 template<class Scalar>
79 {
80  return false;
81 }
82 
83 
84 template<class Scalar>
87  const ArrayView<Scalar> &scalarProds_out ) const
88 {
89 
90  using Teuchos::as;
92 
93  const Ordinal m = X.domain()->dim();
94 
95 #ifdef TEUCHOS_DEBUG
96  THYRA_ASSERT_VEC_SPACES( "DiagonalScalarProd<Scalar>::scalarProds(X,Y,sclarProds)",
97  *s_diag_->space(), *Y.range() );
98  THYRA_ASSERT_VEC_SPACES( "DiagonalScalarProd<Scalar>::scalarProds(X,Y,sclarProds)",
99  *X.range(), *Y.range() );
100  THYRA_ASSERT_VEC_SPACES( "DiagonalScalarProd<Scalar>::scalarProds(X,Y,sclarProds)",
101  *X.domain(), *Y.domain() );
102  TEUCHOS_ASSERT_EQUALITY( as<Ordinal>(scalarProds_out.size()), m );
103 #endif
104 
105  const ConstDetachedSpmdVectorView<Scalar> s_diag(s_diag_);
106 
107  const RCP<const Teuchos::Comm<Ordinal> > comm = s_diag.spmdSpace()->getComm();
108 
109  for (Ordinal j = 0; j < m; ++j) {
110 
113 
114  Scalar scalarProd_j = ST::zero();
115 
116  for (Ordinal i = 0; i < x.subDim(); ++i) {
117  scalarProd_j += ST::conjugate(x[i]) * s_diag[i] * y[i];
118  }
119 
120  if (!is_null(comm)) {
121  Scalar g_scalarProd_j = 0.0;
122  Teuchos::reduceAll<Ordinal,Scalar>(
123  *comm, Teuchos::REDUCE_SUM,
124  scalarProd_j,
125  Teuchos::outArg(g_scalarProd_j)
126  );
127  scalarProds_out[j] = g_scalarProd_j;
128  }
129  else {
130  scalarProds_out[j] = scalarProd_j;
131  }
132 
133  }
134 
135 }
136 
137 
138 template<class Scalar>
141 {
142  TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "ToDo: Implement when needed!")
143  TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
144 }
145 
146 
147 } // end namespace Thyra
148 
149 
150 #endif // THYRA_DIAGONAL_SCALAR_PROD_DEF_HPP
#define THYRA_ASSERT_VEC_SPACES(FUNC_NAME, VS1, VS2)
This is a very useful macro that should be used to validate that two vector spaces are compatible...
bool is_null(const boost::shared_ptr< T > &p)
virtual RCP< const VectorSpaceBase< Scalar > > range() const =0
Return a smart pointer for the range space for this operator.
virtual void scalarProdsImpl(const MultiVectorBase< Scalar > &X, const MultiVectorBase< Scalar > &Y, const ArrayView< Scalar > &scalarProds_out) const
virtual bool isEuclideanImpl() const
Returns false.
size_type size() const
RCP< const LinearOpBase< Scalar > > getLinearOpImpl() const
Teuchos::Ordinal Ordinal
Type for the dimension of a vector space. `*.
#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg)
Interface for a collection of column vectors called a multi-vector.
RCP< const VectorBase< Scalar > > col(Ordinal j) const
Calls colImpl().
Abstract interface for finite-dimensional dense vectors.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
const RCP< const SpmdVectorSpaceBase< Scalar > > spmdSpace() const
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
TypeTo as(const TypeFrom &t)
Create an explicit detached non-mutable (const) view of all of the local elements on this process of ...
void initialize(const RCP< const VectorBase< Scalar > > &s_diag)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)