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 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef IFPACK2_CONTAINERFACTORY_DEF_H
11 #define IFPACK2_CONTAINERFACTORY_DEF_H
12 
14 #include "Ifpack2_TriDiContainer.hpp"
15 #include "Ifpack2_DenseContainer.hpp"
16 #include "Ifpack2_SparseContainer.hpp"
17 #include "Ifpack2_BandedContainer.hpp"
18 #include "Ifpack2_BlockTriDiContainer.hpp"
19 #include "Ifpack2_ILUT.hpp"
20 #include "Teuchos_ArrayView.hpp"
21 
22 #include <sstream>
23 
24 namespace Ifpack2 {
25 
26 template<typename MatrixType>
27 void ContainerFactory<MatrixType>::
28 registerDefaults()
29 {
30  registerContainer<Ifpack2::TriDiContainer<MatrixType, scalar_type>>("TriDi");
31  registerContainer<Ifpack2::DenseContainer<MatrixType, scalar_type>>("Dense");
32  registerContainer<Ifpack2::BandedContainer<MatrixType, scalar_type>>("Banded");
33  registerContainer<SparseContainer<MatrixType, ILUT<MatrixType>>>("SparseILUT");
34 #ifdef HAVE_IFPACK2_AMESOS2
35  registerContainer<SparseContainer<MatrixType, Details::Amesos2Wrapper<MatrixType>>>("SparseAmesos");
36  registerContainer<SparseContainer<MatrixType, Details::Amesos2Wrapper<MatrixType>>>("SparseAmesos2");
37 #endif
38 #ifdef HAVE_IFPACK2_EXPERIMENTAL_KOKKOSKERNELS_FEATURES
39  registerContainer<Ifpack2::BlockTriDiContainer<MatrixType>>("BlockTriDi");
40 #endif
41  registeredDefaults = true;
42 }
43 
44 template<typename MatrixType>
45 template<typename ContainerType>
47 registerContainer(std::string containerType)
48 {
49  //overwrite any existing registration with the same name
50  table[containerType] = Teuchos::rcp(new Details::ContainerFactoryEntry<MatrixType, ContainerType>());
51 }
52 
53 template<typename MatrixType>
56 build(std::string containerType,
59  const Teuchos::RCP<const import_type> importer,
60  bool pointIndexed)
61 {
62  if(!registeredDefaults)
63  {
64  registerDefaults();
65  }
66  //In the case that Amesos2 isn't enabled, provide a better error message than the generic one
67  #ifndef HAVE_IFPACK2_AMESOS2
68  if(containerType == "SparseAmesos" || containerType == "SparseAmesos2")
69  {
70  throw std::invalid_argument("Container type SparseAmesos (aka SparseAmesos2) was requested but Amesos2 isn't enabled.\n"
71  "Add the CMake option \"-D Trilinos_ENABLE_Amesos2=ON\" to enable it.");
72  }
73  #endif
74  if(containerType == "BlockTriDi" && pointIndexed)
75  {
76  throw std::runtime_error("Ifpack2::BlockTriDi does not support decoupled blocks or split rows.\n");
77  }
78  auto it = table.find(containerType);
79  if(it == table.end())
80  {
81  std::ostringstream oss;
82  oss << "Container type \"" << containerType << "\" not registered.\n";
83  oss << "Call ContainerFactory<MatrixType>::registerContainer<ContainerType>(containerName) first.\n";
84  oss << "Currently registered Container types: ";
85  for(auto r : table)
86  {
87  oss << '\"' << r.first << "\" ";
88  }
89  //remove the single trailing space from final message
90  auto str = oss.str();
91  str = str.substr(0, str.length() - 1);
92  throw std::invalid_argument(str);
93  }
94  return it->second->build(A, localRows, importer, pointIndexed);
95 }
96 
97 template<typename MatrixType>
99 deregisterContainer(std::string containerType)
100 {
101  auto it = table.find(containerType);
102  if(it != table.end())
103  {
104  table.erase(it);
105  }
106 }
107 
108 // Definitions of static data
109 
110 template<typename MatrixType>
111 std::map<std::string, Teuchos::RCP<Details::ContainerFactoryEntryBase<MatrixType>>> ContainerFactory<MatrixType>::table;
112 
113 template<typename MatrixType>
114 bool ContainerFactory<MatrixType>::registeredDefaults; //this will initially be false
115 
116 
117 } // namespace Ifpack2
118 
119 #define IFPACK2_CONTAINERFACTORY_INSTANT(S,LO,GO,N) \
120 template struct Ifpack2::ContainerFactory<Tpetra::RowMatrix<S, LO, GO, N>>;
121 
122 #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:47
A static &quot;factory&quot; that provides a way to register and construct arbitrary Ifpack2::Container subclas...
Definition: Ifpack2_ContainerFactory_decl.hpp:78
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:99
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:56
Ifpack2::ContainerFactory class declaration.