Ifpack2 Templated Preconditioning Package  Version 1.0
Ifpack2_Factory_decl.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Tempated Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2009) 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 Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK2_FACTORY_DECL_HPP
44 #define IFPACK2_FACTORY_DECL_HPP
45 
46 #include "Ifpack2_ConfigDefs.hpp"
48 #include "Ifpack2_Details_Factory.hpp"
49 
50 // FIXME (mfh 28 Jul 2015) I would very much not like to include ANY
51 // specific preconditioner header files here. However, these are
52 // unfortunately necessary for the largely useless clone() method.
53 #include "Ifpack2_Chebyshev.hpp"
54 #include "Ifpack2_RILUK.hpp"
55 #include "Ifpack2_Experimental_RBILUK.hpp"
56 
57 #include <type_traits>
58 
59 namespace Ifpack2 {
60 
62 bool supportsUnsymmetric (const std::string& prec_type);
63 
120 class Factory {
121 public:
132  template<class MatrixType>
133  static
134  Teuchos::RCP<Preconditioner<typename MatrixType::scalar_type,
135  typename MatrixType::local_ordinal_type,
136  typename MatrixType::global_ordinal_type,
137  typename MatrixType::node_type> >
138  create (const std::string& precType,
139  const Teuchos::RCP<const MatrixType>& matrix)
140  {
141  using Teuchos::RCP;
142  using Teuchos::rcp_implicit_cast;
143  typedef typename MatrixType::scalar_type SC;
144  typedef typename MatrixType::local_ordinal_type LO;
145  typedef typename MatrixType::global_ordinal_type GO;
146  typedef typename MatrixType::node_type NT;
147  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
148 
149  RCP<const row_matrix_type> A;
150  if (! matrix.is_null ()) {
151  A = rcp_implicit_cast<const row_matrix_type> (matrix);
152  }
153  Ifpack2::Details::Factory<SC, LO, GO, NT> factory;
154  return factory.create (precType, A);
155  }
156 
172  template<class MatrixType>
173  static
174  Teuchos::RCP<Preconditioner<typename MatrixType::scalar_type,
175  typename MatrixType::local_ordinal_type,
176  typename MatrixType::global_ordinal_type,
177  typename MatrixType::node_type> >
178  create (const std::string& precType,
179  const Teuchos::RCP<const MatrixType>& matrix,
180  const int overlap)
181  {
182  using Teuchos::RCP;
183  using Teuchos::rcp_implicit_cast;
184  typedef typename MatrixType::scalar_type SC;
185  typedef typename MatrixType::local_ordinal_type LO;
186  typedef typename MatrixType::global_ordinal_type GO;
187  typedef typename MatrixType::node_type NT;
188  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
189 
190  RCP<const row_matrix_type> A;
191  if (! matrix.is_null ()) {
192  A = rcp_implicit_cast<const row_matrix_type> (matrix);
193  }
194  Ifpack2::Details::Factory<SC, LO, GO, NT> factory;
195  return factory.create (precType, A, overlap);
196  }
197 
200  template<class InputMatrixType, class OutputMatrixType>
201  static
202  Teuchos::RCP<Preconditioner<typename OutputMatrixType::scalar_type,
203  typename OutputMatrixType::local_ordinal_type,
204  typename OutputMatrixType::global_ordinal_type,
205  typename OutputMatrixType::node_type> >
206  clone (const Teuchos::RCP<Preconditioner<typename InputMatrixType::scalar_type,
207  typename InputMatrixType::local_ordinal_type,
208  typename InputMatrixType::global_ordinal_type,
209  typename InputMatrixType::node_type> >& prec,
212  {
213  using Teuchos::null;
214  using Teuchos::RCP;
215  using Teuchos::rcp;
216  using Teuchos::rcp_dynamic_cast;
217 
218  // FIXME (mfh 09 Nov 2013) The code below assumes that the old and
219  // new scalar, local ordinal, and global ordinal types are the same.
220 
221  typedef typename InputMatrixType::scalar_type scalar_type;
222  typedef typename InputMatrixType::local_ordinal_type local_ordinal_type;
223  typedef typename InputMatrixType::global_ordinal_type global_ordinal_type;
224  typedef typename InputMatrixType::node_type old_node_type;
225  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
226  global_ordinal_type, old_node_type> input_row_matrix_type;
227 
228  static_assert (std::is_same<typename OutputMatrixType::scalar_type, scalar_type>::value,
229  "Input and output scalar_type must be the same.");
230  static_assert (std::is_same<typename OutputMatrixType::local_ordinal_type, local_ordinal_type>::value,
231  "Input and output local_ordinal_type must be the same.");
232  static_assert (std::is_same<typename OutputMatrixType::global_ordinal_type, global_ordinal_type>::value,
233  "Input and output global_ordinal_type must be the same.");
234  typedef typename OutputMatrixType::node_type new_node_type;
235  typedef Preconditioner<scalar_type, local_ordinal_type,
236  global_ordinal_type, new_node_type> output_prec_type;
237 
238  // FIXME (mfh 09 Nov 2013) The code below only knows how to clone
239  // three different kinds of preconditioners. This is probably because
240  // only two subclasses of Preconditioner implement a clone() method.
241 
242  RCP<output_prec_type> new_prec;
243  RCP<Chebyshev<input_row_matrix_type> > chebyPrec =
244  rcp_dynamic_cast<Chebyshev<input_row_matrix_type> > (prec);
245  if (! chebyPrec.is_null ()) {
246  new_prec = chebyPrec->clone (matrix, params);
247  return new_prec;
248  }
249  RCP<RILUK<input_row_matrix_type> > luPrec;
250  luPrec = rcp_dynamic_cast<RILUK<input_row_matrix_type> > (prec);
251  if (luPrec != null) {
252  new_prec = luPrec->clone (matrix);
253  return new_prec;
254  }
255  RCP<Experimental::RBILUK<input_row_matrix_type> > rbilukPrec;
256  rbilukPrec = rcp_dynamic_cast<Experimental::RBILUK<input_row_matrix_type> > (prec);
257  if (rbilukPrec != null) {
258  new_prec = rbilukPrec->clone (matrix);
259  return new_prec;
260  }
262  (true, std::logic_error, "Ifpack2::Factory::clone: Not implemented for the "
263  "current preconditioner type. The only supported types thus far are "
264  "Chebyshev, RILUK, and RBILUK.");
265  }
266 };
267 
268 } // namespace Ifpack2
269 
270 #endif // IFPACK2_FACTORY_DECL_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ILU(k) factorization of a given Tpetra::RowMatrix.
Definition: Ifpack2_RILUK_decl.hpp:254
Teuchos::RCP< Chebyshev< Tpetra::RowMatrix< typename NewMatrixType::scalar_type, typename NewMatrixType::local_ordinal_type, typename NewMatrixType::global_ordinal_type, typename NewMatrixType::node_type > > > clone(const Teuchos::RCP< const NewMatrixType > &A_newnode, const Teuchos::ParameterList &params) const
Clone this object to one with a different Node type.
Definition: Ifpack2_Chebyshev_decl.hpp:758
static Teuchos::RCP< Preconditioner< typename OutputMatrixType::scalar_type, typename OutputMatrixType::local_ordinal_type, typename OutputMatrixType::global_ordinal_type, typename OutputMatrixType::node_type > > clone(const Teuchos::RCP< Preconditioner< typename InputMatrixType::scalar_type, typename InputMatrixType::local_ordinal_type, typename InputMatrixType::global_ordinal_type, typename InputMatrixType::node_type > > &prec, const Teuchos::RCP< const OutputMatrixType > &matrix, const Teuchos::ParameterList &params=Teuchos::ParameterList())
Clones a preconditioner for a different node type from an Ifpack2 RILUK or Chebyshev preconditioner...
Definition: Ifpack2_Factory_decl.hpp:206
bool supportsUnsymmetric(const std::string &prec_type)
true if the specified preconditioner type supports nonsymmetric matrices, else false.
Definition: Ifpack2_Factory.cpp:55
bool is_null() const
Diagonally scaled Chebyshev iteration for Tpetra sparse matrices.
Definition: Ifpack2_Chebyshev_decl.hpp:199
Interface for all Ifpack2 preconditioners.
Definition: Ifpack2_Preconditioner.hpp:107
static Teuchos::RCP< Preconditioner< typename MatrixType::scalar_type, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type > > create(const std::string &precType, const Teuchos::RCP< const MatrixType > &matrix)
Create an instance of Ifpack2_Preconditioner given the string name of the preconditioner type...
Definition: Ifpack2_Factory_decl.hpp:138
Teuchos::RCP< RILUK< NewMatrixType > > clone(const Teuchos::RCP< const NewMatrixType > &A_newnode) const
Clone preconditioner to a new node type.
Definition: Ifpack2_RILUK_decl.hpp:677
"Factory" for creating Ifpack2 preconditioners.
Definition: Ifpack2_Factory_decl.hpp:120
static Teuchos::RCP< Preconditioner< typename MatrixType::scalar_type, typename MatrixType::local_ordinal_type, typename MatrixType::global_ordinal_type, typename MatrixType::node_type > > create(const std::string &precType, const Teuchos::RCP< const MatrixType > &matrix, const int overlap)
Create an instance of Ifpack2_Preconditioner given the string name of the preconditioner type...
Definition: Ifpack2_Factory_decl.hpp:178
Preconditioners and smoothers for Tpetra sparse matrices.
Definition: Ifpack2_AdditiveSchwarz_decl.hpp:72
ILU(k) factorization of a given Tpetra::Experimental::BlockCrsMatrix.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:128