MueLu  Version of the Day
BelosXpetraAdapterOperator.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef BELOS_XPETRA_ADAPTER_OPERATOR_HPP
47 #define BELOS_XPETRA_ADAPTER_OPERATOR_HPP
48 
49 //Note: using MACRO HAVE_XPETRA_ instead of HAVE_MUELU_ because this file will eventually be moved to Xpetra
50 
51 #include "Xpetra_ConfigDefs.hpp"
52 
53 #ifdef HAVE_XPETRA_EPETRA
54 #include <BelosOperator.hpp>
55 #endif
56 
57 #include <BelosOperatorT.hpp>
58 
59 namespace Belos {
60  using Teuchos::RCP;
61  using Teuchos::rcpFromRef;
62 
63  //
65 
66 
70  class XpetraOpFailure : public BelosError {public:
71  XpetraOpFailure(const std::string& what_arg) : BelosError(what_arg)
72  {}};
73 
74  // TODO: doc
75  // TODO: should be it named XpetraOp (if Xpetra used by other packages) ?
76  template <class Scalar,
77  class LocalOrdinal = int,
78  class GlobalOrdinal = LocalOrdinal,
79  class Node = KokkosClassic::DefaultNode::DefaultNodeType>
80  class XpetraOp :
81  public OperatorT<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
82 #ifdef HAVE_XPETRA_TPETRA
83  , public OperatorT<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
84 #endif
85  {
86 
87  public:
88 
90 
91 
93  XpetraOp (const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > & Op) : Op_(Op) {}
94 
96  virtual ~XpetraOp() {};
98 
100 
101 
107  void Apply ( const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans=NOTRANS ) const {
108  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, XpetraOpFailure,
109  "Belos::XpetraOp::Apply, transpose mode != NOTRANS not supported.");
110 
111  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
112  y.putScalar(0.0);
113 
114  Op_->apply(x,y);
115  }
116 
117 #ifdef HAVE_XPETRA_TPETRA
118  // TO SKIP THE TRAIT IMPLEMENTATION OF XPETRA::MULTIVECTOR
124  void Apply ( const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans=NOTRANS ) const {
125  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, XpetraOpFailure,
126  "Belos::MueLuTpetraOp::Apply, transpose mode != NOTRANS not supported.");
127 
128 
129  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & temp_x = const_cast<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &>(x);
130 
131  const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX(rcpFromRef(temp_x));
132  Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY(rcpFromRef(y));
133 
134  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
135  tY.putScalar(0.0);
136 
137  Op_->apply(tX,tY);
138  }
139 #endif
140 
141  private:
142 
143  RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Op_;
144  };
145 
146  template <>
147  class XpetraOp<double, int, int>
148  :
149  public OperatorT<Xpetra::MultiVector<double, int, int> >
150 #ifdef HAVE_XPETRA_TPETRA
151  , public OperatorT<Tpetra::MultiVector<double, int, int> >
152 #endif
153 #ifdef HAVE_XPETRA_EPETRA
154  , public OperatorT<Epetra_MultiVector>
155 #endif
156  {
157  typedef double Scalar;
158  typedef int LocalOrdinal;
159  typedef int GlobalOrdinal;
160  typedef Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type Node;
161 
162  public:
163 
164  XpetraOp(const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > & Op) : Op_(Op) {}
165 
166  virtual ~XpetraOp() {};
167 
168  void Apply ( const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans=NOTRANS ) const {
169  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, XpetraOpFailure,
170  "Belos::XpetraOp::Apply, transpose mode != NOTRANS not supported.");
171 
172  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
173  y.putScalar(0.0);
174 
175  Op_->apply(x,y);
176  }
177 
178 #ifdef HAVE_XPETRA_TPETRA
179  void Apply ( const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans=NOTRANS ) const {
180  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, XpetraOpFailure,
181  "Belos::MueLuTpetraOp::Apply, transpose mode != NOTRANS not supported.");
182 
183  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & temp_x = const_cast<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &>(x);
184 
185  const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX(rcpFromRef(temp_x));
186  Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY(rcpFromRef(y));
187 
188  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
189  tY.putScalar(0.0);
190 
191  Op_->apply(tX,tY);
192  }
193 #endif
194 
195 #ifdef HAVE_XPETRA_EPETRA
196  // TO SKIP THE TRAIT IMPLEMENTATION OF XPETRA::MULTIVECTOR
202  void Apply ( const Epetra_MultiVector& x, Epetra_MultiVector& y, ETrans trans=NOTRANS ) const {
203  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, XpetraOpFailure,
204  "Belos::MueLuTpetraOp::Apply, transpose mode != NOTRANS not supported.");
205 
206  Epetra_MultiVector & temp_x = const_cast<Epetra_MultiVector &>(x);
207 
208  const Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> tX(rcpFromRef(temp_x));
209  Xpetra::EpetraMultiVectorT<GlobalOrdinal,Node> tY(rcpFromRef(y));
210 
211  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
212  tY.putScalar(0.0);
213 
214  Op_->apply(tX,tY);
215  }
216 #endif
217 
218  private:
219 
220  RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Op_;
221  };
222 
223 } // namespace Belos
224 
225 #endif // BELOS_XPETRA_ADAPTER_OPERATOR_HPP
void Apply(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y, ETrans trans=NOTRANS) const
virtual ~XpetraOp()
Destructor.
XpetraOp(const RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Op)
Default constructor.
XpetraOp(const RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &Op)
Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal >::node_type Node
XpetraOpFailure(const std::string &what_arg)
XpetraOpFailure is thrown when a return value from an MueLu call on an Xpetra::Operator or MueLu::Hie...
RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op_
RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op_
void Apply(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y, ETrans trans=NOTRANS) const
This routine takes the Xpetra::MultiVector x and applies the operator to it resulting in the Xpetra::...