MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_UnsmooshFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // MueLu: A package for multigrid based preconditioning
4 //
5 // Copyright 2012 NTESS and the MueLu contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef PACKAGES_MUELU_SRC_GRAPH_MUELU_UNSMOOSHFACTORY_DEF_HPP_
11 #define PACKAGES_MUELU_SRC_GRAPH_MUELU_UNSMOOSHFACTORY_DEF_HPP_
12 
13 #include "MueLu_Monitor.hpp"
14 
16 
17 namespace MueLu {
18 
19 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
21 
22 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
24  RCP<ParameterList> validParamList = rcp(new ParameterList());
25  validParamList->set<RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory for unamalgamated matrix. Row map of (unamalgamted) output prolongation operator should match row map of this A.");
26  validParamList->set<RCP<const FactoryBase> >("P", Teuchos::null, "Generating factory of the (amalgamated) prolongator P");
27  validParamList->set<RCP<const FactoryBase> >("DofStatus", Teuchos::null, "Generating factory for dofStatus array (usually the VariableDofLaplacdianFactory)");
28 
29  validParamList->set<int>("maxDofPerNode", 1, "Maximum number of DOFs per node");
30  validParamList->set<bool>("fineIsPadded", false, "true if finest level input matrix is padded");
31 
32  return validParamList;
33 }
34 
35 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
37  // const ParameterList& pL = GetParameterList();
38  Input(fineLevel, "A");
39  Input(coarseLevel, "P");
40 
41  // DofStatus only provided on the finest level (by user)
42  // On the coarser levels it is auto-generated using the DBC information from the unamalgamated matrix A
43  if (fineLevel.GetLevelID() == 0)
44  Input(fineLevel, "DofStatus");
45 }
46 
47 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
49  FactoryMonitor m(*this, "Build", coarseLevel);
50  typedef Teuchos::ScalarTraits<SC> STS;
51 
52  const ParameterList &pL = GetParameterList();
53 
54  // extract matrices (unamalgamated A and amalgamated P)
55  RCP<Matrix> unamalgA = Get<RCP<Matrix> >(fineLevel, "A");
56  RCP<Matrix> amalgP = Get<RCP<Matrix> >(coarseLevel, "P");
57 
58  // extract user parameters
59  int maxDofPerNode = pL.get<int>("maxDofPerNode");
60  bool fineIsPadded = pL.get<bool>("fineIsPadded");
61 
62  // get dofStatus information
63  // On the finest level it is provided by the user. On the coarser levels it is constructed
64  // using the DBC information of the matrix A
65  Teuchos::Array<char> dofStatus;
66  if (fineLevel.GetLevelID() == 0) {
67  dofStatus = Get<Teuchos::Array<char> >(fineLevel, "DofStatus");
68  } else {
69  // dof status is the dirichlet information of unsmooshed/unamalgamated A (fine level)
70  dofStatus = Teuchos::Array<char>(unamalgA->getRowMap()->getLocalNumElements() /*amalgP->getRowMap()->getLocalNumElements() * maxDofPerNode*/, 's');
71 
72  bool bHasZeroDiagonal = false;
74 
75  TEUCHOS_TEST_FOR_EXCEPTION(dirOrNot.size() != dofStatus.size(), MueLu::Exceptions::RuntimeError, "MueLu::UnsmooshFactory::Build: inconsistent number of coarse DBC array and dofStatus array. dirOrNot.size() = " << dirOrNot.size() << " dofStatus.size() = " << dofStatus.size());
76  for (decltype(dirOrNot.size()) i = 0; i < dirOrNot.size(); ++i) {
77  if (dirOrNot[i] == true) dofStatus[i] = 'p';
78  }
79  }
80 
81  // TODO: TAW the following check is invalid for SA-AMG based input prolongators
82  // TEUCHOS_TEST_FOR_EXCEPTION(amalgP->getDomainMap()->isSameAs(*amalgP->getColMap()) == false, MueLu::Exceptions::RuntimeError,"MueLu::UnsmooshFactory::Build: only support for non-overlapping aggregates. (column map of Ptent must be the same as domain map of Ptent)");
83 
84  // extract CRS information from amalgamated prolongation operator
85  Teuchos::ArrayRCP<const size_t> amalgRowPtr(amalgP->getLocalNumRows());
86  Teuchos::ArrayRCP<const LocalOrdinal> amalgCols(amalgP->getLocalNumEntries());
87  Teuchos::ArrayRCP<const Scalar> amalgVals(amalgP->getLocalNumEntries());
88  Teuchos::RCP<CrsMatrixWrap> amalgPwrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(amalgP);
89  Teuchos::RCP<CrsMatrix> amalgPcrs = amalgPwrap->getCrsMatrix();
90  amalgPcrs->getAllValues(amalgRowPtr, amalgCols, amalgVals);
91 
92  // calculate number of dof rows for new prolongator
93  size_t paddedNrows = amalgP->getRowMap()->getLocalNumElements() * Teuchos::as<size_t>(maxDofPerNode);
94 
95  // reserve CSR arrays for new prolongation operator
96  Teuchos::ArrayRCP<size_t> newPRowPtr(paddedNrows + 1);
97  Teuchos::ArrayRCP<LocalOrdinal> newPCols(amalgP->getLocalNumEntries() * maxDofPerNode);
98  Teuchos::ArrayRCP<Scalar> newPVals(amalgP->getLocalNumEntries() * maxDofPerNode);
99 
100  size_t rowCount = 0; // actual number of (local) in unamalgamated prolongator
101  if (fineIsPadded == true || fineLevel.GetLevelID() > 0) {
102  // build prolongation operator for padded fine level matrices.
103  // Note: padded fine level dofs are transferred by injection.
104  // That is, these interpolation stencils do not take averages of
105  // coarse level variables. Further, fine level Dirichlet points
106  // also use injection.
107 
108  size_t cnt = 0; // local id counter
109  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
110  // determine number of entries in amalgamated dof row i
111  size_t rowLength = amalgRowPtr[i + 1] - amalgRowPtr[i];
112 
113  // loop over dofs per node (unamalgamation)
114  for (int j = 0; j < maxDofPerNode; j++) {
115  newPRowPtr[i * maxDofPerNode + j] = cnt;
116  if (dofStatus[i * maxDofPerNode + j] == 's') { // add only "standard" dofs to unamalgamated prolongator
117  // loop over column entries in amalgamated P
118  for (size_t k = 0; k < rowLength; k++) {
119  newPCols[cnt] = amalgCols[k + amalgRowPtr[i]] * maxDofPerNode + j;
120  newPVals[cnt++] = amalgVals[k + amalgRowPtr[i]];
121  }
122  }
123  }
124  }
125 
126  newPRowPtr[paddedNrows] = cnt; // close row CSR array
127  rowCount = paddedNrows;
128  } else {
129  // Build prolongation operator for non-padded fine level matrices.
130  // Need to map from non-padded dofs to padded dofs. For this, look
131  // at the status array and skip padded dofs.
132 
133  size_t cnt = 0; // local id counter
134 
135  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
136  // determine number of entries in amalgamated dof row i
137  size_t rowLength = amalgRowPtr[i + 1] - amalgRowPtr[i];
138 
139  // loop over dofs per node (unamalgamation)
140  for (int j = 0; j < maxDofPerNode; j++) {
141  // no interpolation for padded fine dofs as they do not exist
142 
143  if (dofStatus[i * maxDofPerNode + j] == 's') { // add only "standard" dofs to unamalgamated prolongator
144  newPRowPtr[rowCount++] = cnt;
145  // loop over column entries in amalgamated P
146  for (size_t k = 0; k < rowLength; k++) {
147  newPCols[cnt] = amalgCols[k + amalgRowPtr[i]] * maxDofPerNode + j;
148  newPVals[cnt++] = amalgVals[k + amalgRowPtr[i]];
149  }
150  }
151  if (dofStatus[i * maxDofPerNode + j] == 'd') { // Dirichlet handling
152  newPRowPtr[rowCount++] = cnt;
153  }
154  }
155  }
156  newPRowPtr[rowCount] = cnt; // close row CSR array
157  } // fineIsPadded == false
158 
159  // generate coarse domain map
160  // So far no support for gid offset or strided maps. This information
161  // could be gathered easily from the unamalgamated fine level operator A.
162  std::vector<size_t> stridingInfo(1, maxDofPerNode);
163 
164  GlobalOrdinal nCoarseDofs = amalgP->getDomainMap()->getLocalNumElements() * maxDofPerNode;
165  GlobalOrdinal indexBase = amalgP->getDomainMap()->getIndexBase();
166  RCP<const Map> coarseDomainMap = StridedMapFactory::Build(amalgP->getDomainMap()->lib(),
168  nCoarseDofs,
169  indexBase,
170  stridingInfo,
171  amalgP->getDomainMap()->getComm(),
172  -1 /* stridedBlockId */,
173  0 /*domainGidOffset */);
174 
175  size_t nColCoarseDofs = Teuchos::as<size_t>(amalgP->getColMap()->getLocalNumElements() * maxDofPerNode);
176  Teuchos::Array<GlobalOrdinal> unsmooshColMapGIDs(nColCoarseDofs);
177  for (size_t c = 0; c < amalgP->getColMap()->getLocalNumElements(); ++c) {
178  GlobalOrdinal gid = (amalgP->getColMap()->getGlobalElement(c) - indexBase) * maxDofPerNode + indexBase;
179 
180  for (int i = 0; i < maxDofPerNode; ++i) {
181  unsmooshColMapGIDs[c * maxDofPerNode + i] = gid + i;
182  }
183  }
184  Teuchos::RCP<Map> coarseColMap = MapFactory::Build(amalgP->getDomainMap()->lib(),
186  unsmooshColMapGIDs(), // View,
187  indexBase,
188  amalgP->getDomainMap()->getComm());
189 
190  // Assemble unamalgamated P
191  Teuchos::RCP<CrsMatrix> unamalgPCrs = CrsMatrixFactory::Build(unamalgA->getRowMap(),
192  coarseColMap,
193  maxDofPerNode * amalgP->getLocalMaxNumRowEntries());
194  for (size_t i = 0; i < rowCount; i++) {
195  unamalgPCrs->insertLocalValues(i,
196  newPCols.view(newPRowPtr[i], newPRowPtr[i + 1] - newPRowPtr[i]),
197  newPVals.view(newPRowPtr[i], newPRowPtr[i + 1] - newPRowPtr[i]));
198  }
199  unamalgPCrs->fillComplete(coarseDomainMap, unamalgA->getRowMap());
200 
201  Teuchos::RCP<Matrix> unamalgP = Teuchos::rcp(new CrsMatrixWrap(unamalgPCrs));
202 
203  Set(coarseLevel, "P", unamalgP);
204 }
205 
206 } // namespace MueLu
207 
208 #endif /* PACKAGES_MUELU_SRC_GRAPH_MUELU_UNSMOOSHFACTORY_DEF_HPP_ */
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
size_type size() const
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:63
static Teuchos::ArrayRCP< const bool > DetectDirichletRowsExt(const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, bool &bHasZeroDiagonal, const Magnitude &tol=Teuchos::ScalarTraits< Scalar >::zero())
Detect Dirichlet rows (extended version)
int GetLevelID() const
Return level number.
Definition: MueLu_Level.cpp:51
Exception throws to report errors in the internal logical of the program.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.