Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Factory_decl.hpp
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack2: Templated 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 
117 class Factory {
118 public:
129  template<class MatrixType>
130  static
131  Teuchos::RCP<Preconditioner<typename MatrixType::scalar_type,
132  typename MatrixType::local_ordinal_type,
133  typename MatrixType::global_ordinal_type,
134  typename MatrixType::node_type> >
135  create (const std::string& precType,
136  const Teuchos::RCP<const MatrixType>& matrix)
137  {
138  using Teuchos::RCP;
139  using Teuchos::rcp_implicit_cast;
140  typedef typename MatrixType::scalar_type SC;
141  typedef typename MatrixType::local_ordinal_type LO;
142  typedef typename MatrixType::global_ordinal_type GO;
143  typedef typename MatrixType::node_type NT;
144  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
145 
146  RCP<const row_matrix_type> A;
147  if (! matrix.is_null ()) {
148  A = rcp_implicit_cast<const row_matrix_type> (matrix);
149  }
150  Ifpack2::Details::Factory<SC, LO, GO, NT> factory;
151  return factory.create (precType, A);
152  }
153 
169  template<class MatrixType>
170  static
171  Teuchos::RCP<Preconditioner<typename MatrixType::scalar_type,
172  typename MatrixType::local_ordinal_type,
173  typename MatrixType::global_ordinal_type,
174  typename MatrixType::node_type> >
175  create (const std::string& precType,
176  const Teuchos::RCP<const MatrixType>& matrix,
177  const int overlap)
178  {
179  using Teuchos::RCP;
180  using Teuchos::rcp_implicit_cast;
181  typedef typename MatrixType::scalar_type SC;
182  typedef typename MatrixType::local_ordinal_type LO;
183  typedef typename MatrixType::global_ordinal_type GO;
184  typedef typename MatrixType::node_type NT;
185  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
186 
187  RCP<const row_matrix_type> A;
188  if (! matrix.is_null ()) {
189  A = rcp_implicit_cast<const row_matrix_type> (matrix);
190  }
191  Ifpack2::Details::Factory<SC, LO, GO, NT> factory;
192  return factory.create (precType, A, overlap);
193  }
194 
197  template<class InputMatrixType, class OutputMatrixType>
198  static
199  Teuchos::RCP<Preconditioner<typename OutputMatrixType::scalar_type,
200  typename OutputMatrixType::local_ordinal_type,
201  typename OutputMatrixType::global_ordinal_type,
202  typename OutputMatrixType::node_type> >
203  clone (const Teuchos::RCP<Preconditioner<typename InputMatrixType::scalar_type,
204  typename InputMatrixType::local_ordinal_type,
205  typename InputMatrixType::global_ordinal_type,
206  typename InputMatrixType::node_type> >& prec,
209  {
210  using Teuchos::null;
211  using Teuchos::RCP;
212  using Teuchos::rcp;
213  using Teuchos::rcp_dynamic_cast;
214 
215  // FIXME (mfh 09 Nov 2013) The code below assumes that the old and
216  // new scalar, local ordinal, and global ordinal types are the same.
217 
218  typedef typename InputMatrixType::scalar_type scalar_type;
219  typedef typename InputMatrixType::local_ordinal_type local_ordinal_type;
220  typedef typename InputMatrixType::global_ordinal_type global_ordinal_type;
221  typedef typename InputMatrixType::node_type old_node_type;
222  typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type,
223  global_ordinal_type, old_node_type> input_row_matrix_type;
224 
225  static_assert (std::is_same<typename OutputMatrixType::scalar_type, scalar_type>::value,
226  "Input and output scalar_type must be the same.");
227  static_assert (std::is_same<typename OutputMatrixType::local_ordinal_type, local_ordinal_type>::value,
228  "Input and output local_ordinal_type must be the same.");
229  static_assert (std::is_same<typename OutputMatrixType::global_ordinal_type, global_ordinal_type>::value,
230  "Input and output global_ordinal_type must be the same.");
231  typedef typename OutputMatrixType::node_type new_node_type;
232  typedef Preconditioner<scalar_type, local_ordinal_type,
233  global_ordinal_type, new_node_type> output_prec_type;
234 
235  // FIXME (mfh 09 Nov 2013) The code below only knows how to clone
236  // three different kinds of preconditioners. This is probably because
237  // only two subclasses of Preconditioner implement a clone() method.
238 
239  RCP<output_prec_type> new_prec;
240  RCP<Chebyshev<input_row_matrix_type> > chebyPrec =
241  rcp_dynamic_cast<Chebyshev<input_row_matrix_type> > (prec);
242  if (! chebyPrec.is_null ()) {
243  new_prec = chebyPrec->clone (matrix, params);
244  return new_prec;
245  }
246  RCP<RILUK<input_row_matrix_type> > luPrec;
247  luPrec = rcp_dynamic_cast<RILUK<input_row_matrix_type> > (prec);
248  if (luPrec != null) {
249  new_prec = luPrec->clone (matrix);
250  return new_prec;
251  }
252  RCP<Experimental::RBILUK<input_row_matrix_type> > rbilukPrec;
253  rbilukPrec = rcp_dynamic_cast<Experimental::RBILUK<input_row_matrix_type> > (prec);
254  if (rbilukPrec != null) {
255  new_prec = rbilukPrec->clone (matrix);
256  return new_prec;
257  }
259  (true, std::logic_error, "Ifpack2::Factory::clone: Not implemented for the "
260  "current preconditioner type. The only supported types thus far are "
261  "Chebyshev, RILUK, and RBILUK.");
262  }
263 };
264 
265 } // namespace Ifpack2
266 
267 #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:243
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:203
bool supportsUnsymmetric(const std::string &prec_type)
true if the specified preconditioner type supports nonsymmetric matrices, else false.
Definition: Ifpack2_Factory.cpp:55
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:761
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Diagonally scaled Chebyshev iteration for Tpetra sparse matrices.
Definition: Ifpack2_Chebyshev_decl.hpp:199
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:624
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:135
&quot;Factory&quot; for creating Ifpack2 preconditioners.
Definition: Ifpack2_Factory_decl.hpp:117
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:175
ILU(k) factorization of a given Tpetra::BlockCrsMatrix.
Definition: Ifpack2_Experimental_RBILUK_decl.hpp:128
bool is_null() const