Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_ContainerFactory_def.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_CONTAINERFACTORY_DEF_H
44 #define IFPACK2_CONTAINERFACTORY_DEF_H
45 
47 #include "Ifpack2_TriDiContainer.hpp"
48 #include "Ifpack2_DenseContainer.hpp"
49 #include "Ifpack2_SparseContainer.hpp"
50 #include "Ifpack2_BandedContainer.hpp"
51 #include "Ifpack2_BlockTriDiContainer.hpp"
52 #include "Ifpack2_ILUT.hpp"
53 #include "Teuchos_ArrayView.hpp"
54 
55 #include <sstream>
56 
57 namespace Ifpack2 {
58 
59 template<typename MatrixType>
60 void ContainerFactory<MatrixType>::
61 registerDefaults()
62 {
63  registerContainer<Ifpack2::TriDiContainer<MatrixType, scalar_type>>("TriDi");
64  registerContainer<Ifpack2::DenseContainer<MatrixType, scalar_type>>("Dense");
65  registerContainer<Ifpack2::BandedContainer<MatrixType, scalar_type>>("Banded");
66  registerContainer<SparseContainer<MatrixType, ILUT<MatrixType>>>("SparseILUT");
67 #ifdef HAVE_IFPACK2_AMESOS2
68  registerContainer<SparseContainer<MatrixType, Details::Amesos2Wrapper<MatrixType>>>("SparseAmesos");
69  registerContainer<SparseContainer<MatrixType, Details::Amesos2Wrapper<MatrixType>>>("SparseAmesos2");
70 #endif
71 #ifdef HAVE_IFPACK2_EXPERIMENTAL_KOKKOSKERNELS_FEATURES
72  registerContainer<Ifpack2::BlockTriDiContainer<MatrixType>>("BlockTriDi");
73 #endif
74  registeredDefaults = true;
75 }
76 
77 template<typename MatrixType>
78 template<typename ContainerType>
80 registerContainer(std::string containerType)
81 {
82  //overwrite any existing registration with the same name
83  table[containerType] = Teuchos::rcp(new Details::ContainerFactoryEntry<MatrixType, ContainerType>());
84 }
85 
86 template<typename MatrixType>
89 build(std::string containerType, const Teuchos::RCP<const MatrixType>& A,
91  int OverlapLevel, scalar_type DampingFactor)
92 {
93  if(!registeredDefaults)
94  {
95  registerDefaults();
96  }
97  //In the case that Amesos2 isn't enabled, provide a better error message than the generic one
98  #ifndef HAVE_IFPACK2_AMESOS2
99  if(containerType == "SparseAmesos" || containerType == "SparseAmesos2")
100  {
101  throw std::invalid_argument("Container type SparseAmesos (aka SparseAmesos2) was requested but Amesos2 isn't enabled.\n"
102  "Add the CMake option \"-D Trilinos_ENABLE_Amesos2=ON\" to enable it.");
103  }
104  #endif
105  auto it = table.find(containerType);
106  if(it == table.end())
107  {
108  std::ostringstream oss;
109  oss << "Container type \"" << containerType << "\" not registered.\n";
110  oss << "Call ContainerFactory<MatrixType>::registerContainer<ContainerType>(containerName) first.\n";
111  oss << "Currently registered Container types: ";
112  for(auto r : table)
113  {
114  oss << '\"' << r.first << "\" ";
115  }
116  //remove the single trailing space from final message
117  auto str = oss.str();
118  str = str.substr(0, str.length() - 1);
119  throw std::invalid_argument(str);
120  }
121  return it->second->build(A, localRows, importer, OverlapLevel, DampingFactor);
122 }
123 
124 template<typename MatrixType>
126 deregisterContainer(std::string containerType)
127 {
128  auto it = table.find(containerType);
129  if(it != table.end())
130  {
131  table.erase(it);
132  }
133 }
134 
135 // Definitions of static data
136 
137 template<typename MatrixType>
138 std::map<std::string, Teuchos::RCP<Details::ContainerFactoryEntryBase<MatrixType>>> ContainerFactory<MatrixType>::table;
139 
140 template<typename MatrixType>
141 bool ContainerFactory<MatrixType>::registeredDefaults; //this will initially be false
142 
143 
144 } // namespace Ifpack2
145 
146 #define IFPACK2_CONTAINERFACTORY_INSTANT(S,LO,GO,N) \
147 template struct Ifpack2::ContainerFactory<Tpetra::RowMatrix<S, LO, GO, N>>;
148 
149 #endif // IFPACK2_DETAILS_CONTAINERFACTORY_H
static Teuchos::RCP< BaseContainer > build(std::string containerType, const Teuchos::RCP< const MatrixType > &A, const Teuchos::Array< Teuchos::Array< local_ordinal_type >> &localRows, const Teuchos::RCP< const import_type > importer, int OverlapLevel, scalar_type DampingFactor)
Build a specialization of Ifpack2::Container given a key that has been registered.
Definition: Ifpack2_ContainerFactory_def.hpp:89
static void registerContainer(std::string containerType)
Registers a specialization of Ifpack2::Container by binding a key (string) to it. ...
Definition: Ifpack2_ContainerFactory_def.hpp:80
A static &quot;factory&quot; that provides a way to register and construct arbitrary Ifpack2::Container subclas...
Definition: Ifpack2_ContainerFactory_decl.hpp:113
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void deregisterContainer(std::string containerType)
Registers a specialization of Ifpack2::Container by binding a key (string) to it. ...
Definition: Ifpack2_ContainerFactory_def.hpp:126
MatrixType::scalar_type scalar_type
The type of the entries of the input MatrixType.
Definition: Ifpack2_ContainerFactory_decl.hpp:119
Ifpack2::ContainerFactory class declaration.