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,
92  const Teuchos::RCP<const import_type> importer,
93  bool pointIndexed)
94 {
95  if(!registeredDefaults)
96  {
97  registerDefaults();
98  }
99  //In the case that Amesos2 isn't enabled, provide a better error message than the generic one
100  #ifndef HAVE_IFPACK2_AMESOS2
101  if(containerType == "SparseAmesos" || containerType == "SparseAmesos2")
102  {
103  throw std::invalid_argument("Container type SparseAmesos (aka SparseAmesos2) was requested but Amesos2 isn't enabled.\n"
104  "Add the CMake option \"-D Trilinos_ENABLE_Amesos2=ON\" to enable it.");
105  }
106  #endif
107  if(containerType == "BlockTriDi" && pointIndexed)
108  {
109  throw std::runtime_error("Ifpack2::BlockTriDi does not support decoupled blocks or split rows.\n");
110  }
111  auto it = table.find(containerType);
112  if(it == table.end())
113  {
114  std::ostringstream oss;
115  oss << "Container type \"" << containerType << "\" not registered.\n";
116  oss << "Call ContainerFactory<MatrixType>::registerContainer<ContainerType>(containerName) first.\n";
117  oss << "Currently registered Container types: ";
118  for(auto r : table)
119  {
120  oss << '\"' << r.first << "\" ";
121  }
122  //remove the single trailing space from final message
123  auto str = oss.str();
124  str = str.substr(0, str.length() - 1);
125  throw std::invalid_argument(str);
126  }
127  return it->second->build(A, localRows, importer, pointIndexed);
128 }
129 
130 template<typename MatrixType>
132 deregisterContainer(std::string containerType)
133 {
134  auto it = table.find(containerType);
135  if(it != table.end())
136  {
137  table.erase(it);
138  }
139 }
140 
141 // Definitions of static data
142 
143 template<typename MatrixType>
144 std::map<std::string, Teuchos::RCP<Details::ContainerFactoryEntryBase<MatrixType>>> ContainerFactory<MatrixType>::table;
145 
146 template<typename MatrixType>
147 bool ContainerFactory<MatrixType>::registeredDefaults; //this will initially be false
148 
149 
150 } // namespace Ifpack2
151 
152 #define IFPACK2_CONTAINERFACTORY_INSTANT(S,LO,GO,N) \
153 template struct Ifpack2::ContainerFactory<Tpetra::RowMatrix<S, LO, GO, N>>;
154 
155 #endif // IFPACK2_DETAILS_CONTAINERFACTORY_H
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:111
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:132
static Teuchos::RCP< BaseContainer > build(std::string containerType, const Teuchos::RCP< const MatrixType > &A, const Teuchos::Array< Teuchos::Array< local_ordinal_type >> &partitions, const Teuchos::RCP< const import_type > importer, bool pointIndexed)
Build a specialization of Ifpack2::Container given a key that has been registered.
Definition: Ifpack2_ContainerFactory_def.hpp:89
Ifpack2::ContainerFactory class declaration.