MueLu  Version of the Day
BelosMueLuAdapter.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_MUELU_ADAPTER_HPP
47 #define BELOS_MUELU_ADAPTER_HPP
48 
49 #ifdef HAVE_MUELU_EPETRA
50 #include <BelosOperator.hpp>
51 #endif
52 
53 #ifdef HAVE_MUELU_AMGX
54 #include "MueLu_AMGXOperator.hpp"
55 #endif
56 
57 #include <BelosOperatorT.hpp>
58 
59 #include "MueLu_ConfigDefs.hpp"
60 #include "MueLu_Hierarchy.hpp"
61 
62 namespace Belos {
63  using Teuchos::RCP;
64  using Teuchos::rcpFromRef;
65 
66  //
68 
69 
73  class MueLuOpFailure : public BelosError {public:
74  MueLuOpFailure(const std::string& what_arg) : BelosError(what_arg)
75  {}};
76 
77  //TODO: doc
78  template <class Scalar,
79  class LocalOrdinal = int,
80  class GlobalOrdinal = LocalOrdinal,
81  class Node = KokkosClassic::DefaultNode::DefaultNodeType>
82  class MueLuOp :
83  public OperatorT<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
84 #ifdef HAVE_MUELU_TPETRA
85  , public OperatorT<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
86 #endif
87  {
88  public:
89 
91 
92 
95 #ifdef HAVE_MUELU_AMGX
97 #endif
98  virtual ~MueLuOp() {}
101 
103 
104 
110  void Apply(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans = NOTRANS ) const {
111 
112  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, MueLuOpFailure,
113  "Belos::MueLuOp::Apply, transpose mode != NOTRANS not supported by MueLu preconditionners.");
114 
115  // This does not matter for Hierarchy, but matters for AMGX
116  y.putScalar(0.0);
117 
118 #ifdef HAVE_MUELU_AMGX
119  if (!AMGX_.is_null()) {
120  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX = Xpetra::toTpetra(x);
121  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY = Xpetra::toTpetra(y);
122 
123  AMGX_->apply(tX, tY);
124 
125  }
126 #endif
127  if (!Hierarchy_.is_null())
128  Hierarchy_->Iterate(x, y, 1, true);
129  }
130 
131 #ifdef HAVE_MUELU_TPETRA
132  // TO SKIP THE TRAIT IMPLEMENTATION OF XPETRA::MULTIVECTOR
138  void Apply(const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans = NOTRANS ) const {
139 
140  TEUCHOS_TEST_FOR_EXCEPTION(trans!=NOTRANS, MueLuOpFailure,
141  "Belos::MueLuOp::Apply, transpose mode != NOTRANS not supported by MueLu preconditionners.");
142 
143  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate(), but it matters for AMGX
144  y.putScalar(0.0);
145 
146 #ifdef HAVE_MUELU_AMGX
147  if (!AMGX_.is_null())
148  AMGX_->apply(x, y);
149 #endif
150 
151  if (!Hierarchy_.is_null()) {
152  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & temp_x = const_cast<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &>(x);
153 
154  const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX(rcpFromRef(temp_x));
155  Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY(rcpFromRef(y));
156  Hierarchy_->Iterate(tX, tY, 1, true);
157  }
158  }
159 #endif
160 
161  private:
162  RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Hierarchy_;
163 #ifdef HAVE_MUELU_AMGX
164  RCP<MueLu::AMGXOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > AMGX_;
165 #endif
166  };
167 
168  template <>
169  class MueLuOp<double, int, int> :
170  public OperatorT<Xpetra::MultiVector<double, int, int> >
171 #ifdef HAVE_MUELU_TPETRA
172  , public OperatorT<Tpetra::MultiVector<double, int, int> >
173 #endif
174 #ifdef HAVE_MUELU_EPETRA
175  , public OperatorT<Epetra_MultiVector>
176  , public Belos::Operator<double>
177 #endif
178  {
179  typedef double Scalar;
180  typedef int LocalOrdinal;
181  typedef int GlobalOrdinal;
182  typedef Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal>::node_type Node;
183 
184  public:
185 
187 #ifdef HAVE_MUELU_AMGX
189 #endif
190  virtual ~MueLuOp() {}
191 
192  void Apply(const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans = NOTRANS ) const {
193 
194  TEUCHOS_TEST_FOR_EXCEPTION(trans != NOTRANS, MueLuOpFailure,
195  "Belos::MueLuOp::Apply, transpose mode != NOTRANS not supported by MueLu preconditionners.");
196 
197  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate(), but it matters for AMGX
198  y.putScalar(0.0);
199 
200 #ifdef HAVE_MUELU_AMGX
201  if (!AMGX_.is_null()) {
202  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX = Xpetra::toTpetra(x);
203  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY = Xpetra::toTpetra(y);
204 
205  AMGX_->apply(tX, tY);
206  }
207 #endif
208  if (!Hierarchy_.is_null())
209  Hierarchy_->Iterate(x, y, 1, true);
210  }
211 
212 #ifdef HAVE_MUELU_TPETRA
213  void Apply ( const Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& x, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& y, ETrans trans=NOTRANS ) const {
214  TEUCHOS_TEST_FOR_EXCEPTION(trans != NOTRANS, MueLuOpFailure,
215  "Belos::MueLuOp::Apply, transpose mode != NOTRANS not supported by MueLu preconditionners.");
216 
217  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate(), but it matters for AMGX
218  y.putScalar(0.0);
219 
220 #ifdef HAVE_MUELU_AMGX
221  if (!AMGX_.is_null())
222  AMGX_->apply(x, y);
223 #endif
224 
225  if (!Hierarchy_.is_null()) {
226  Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> & temp_x = const_cast<Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> &>(x);
227 
228  const Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tX(rcpFromRef(temp_x));
229  Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tY(rcpFromRef(y));
230 
231  tY.putScalar(0.0);
232 
233  Hierarchy_->Iterate(tX, tY, 1, true);
234  }
235  }
236 #endif
237 
238 #ifdef HAVE_MUELU_EPETRA
239  // TO SKIP THE TRAIT IMPLEMENTATION OF XPETRA::MULTIVECTOR
245  void Apply(const Epetra_MultiVector& x, Epetra_MultiVector& y, ETrans trans = NOTRANS) const {
246  TEUCHOS_TEST_FOR_EXCEPTION(trans != NOTRANS, MueLuOpFailure,
247  "Belos::MueLuOp::Apply, transpose mode != NOTRANS not supported by MueLu preconditionners.");
248 
249  Epetra_MultiVector& temp_x = const_cast<Epetra_MultiVector&>(x);
250 
251  const Xpetra::EpetraMultiVector tX(rcpFromRef(temp_x));
252  Xpetra::EpetraMultiVector tY(rcpFromRef(y));
253 
254  //FIXME InitialGuessIsZero currently does nothing in MueLu::Hierarchy.Iterate().
255  tY.putScalar(0.0);
256 
257  Hierarchy_->Iterate(tX, tY, 1, true);
258  }
259 
265  void Apply(const Belos::MultiVec<double>& x, Belos::MultiVec<double>& y, ETrans trans = NOTRANS ) const {
266  const Epetra_MultiVector* vec_x = dynamic_cast<const Epetra_MultiVector*>(&x);
267  Epetra_MultiVector* vec_y = dynamic_cast<Epetra_MultiVector*>(&y);
268 
269  TEUCHOS_TEST_FOR_EXCEPTION(vec_x==NULL || vec_y==NULL, MueLuOpFailure,
270  "Belos::MueLuOp::Apply, x and/or y cannot be dynamic cast to an Epetra_MultiVector.");
271 
272  Apply(*vec_x, *vec_y, trans);
273  }
274 #endif
275 
276  private:
277  RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node> > Hierarchy_;
278 #ifdef HAVE_MUELU_AMGX
279  RCP<MueLu::AMGXOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> > AMGX_;
280 #endif
281  };
282 
283 } // namespace Belos
284 
285 #endif // BELOS_MUELU_ADAPTER_HPP
void Apply(const Belos::MultiVec< double > &x, Belos::MultiVec< double > &y, ETrans trans=NOTRANS) const
This routine takes the Belos::MultiVec x and applies the operator to it resulting in the Belos::Multi...
void Apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y, ETrans trans=NOTRANS) const
This routine takes the Tpetra::MultiVector x and applies the operator to it resulting in the Tpetra::...
MueLuOpFailure is thrown when a return value from an MueLu call on an Xpetra::Operator or MueLu::Hier...
Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal >::node_type Node
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::...
RCP< MueLu::AMGXOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > AMGX_
MueLuOpFailure(const std::string &what_arg)
RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Hierarchy_
void Apply(const Epetra_MultiVector &x, Epetra_MultiVector &y, ETrans trans=NOTRANS) const
This routine takes the Tpetra::MultiVector x and applies the operator to it resulting in the Tpetra::...
MueLuOp(const RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &H)
Default constructor.
RCP< MueLu::AMGXOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > AMGX_
void Apply(const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x, Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y, ETrans trans=NOTRANS) const
void Apply(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &x, Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y, ETrans trans=NOTRANS) const
MueLuOp(const RCP< MueLu::AMGXOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A)
RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Hierarchy_
MueLuOp(const RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &H)
MueLuOp(const RCP< MueLu::AMGXOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &A)
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
Adapter for AmgX library from Nvidia.