MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_ReplicatePFactory_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_REPLICATEPFACTORY_DEF_HPP
47 #define MUELU_REPLICATEPFACTORY_DEF_HPP
48 
49 #include <stdlib.h>
50 #include <iomanip>
51 
52 // #include <Teuchos_LAPACK.hpp>
56 
57 #include <Xpetra_CrsMatrixWrap.hpp>
58 #include <Xpetra_ImportFactory.hpp>
59 #include <Xpetra_Matrix.hpp>
60 #include <Xpetra_MapFactory.hpp>
61 #include <Xpetra_MultiVectorFactory.hpp>
62 #include <Xpetra_VectorFactory.hpp>
63 
64 #include <Xpetra_IO.hpp>
65 
67 
68 #include "MueLu_Monitor.hpp"
69 
70 namespace MueLu {
71 
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  RCP<ParameterList> validParamList = rcp(new ParameterList());
75  validParamList->setEntry("replicate: npdes", ParameterEntry(1));
76 
77  return validParamList;
78 }
79 
80 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
82  DeclareInput(Level& fineLevel, Level& /* coarseLevel */) const {
83  // Input(fineLevel, "Psubblock");
84 }
85 
86 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88  Level& coarseLevel) const {
89  return BuildP(fineLevel, coarseLevel);
90 }
91 
92 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
94  Level& coarseLevel) const {
95  FactoryMonitor m(*this, "Build", coarseLevel);
96 
97  RCP<Matrix> Psubblock = coarseLevel.Get<RCP<Matrix> >("Psubblock", NoFactory::get());
98  const ParameterList& pL = GetParameterList();
99  const LO dofPerNode = as<LO>(pL.get<int>("replicate: npdes"));
100 
101  Teuchos::ArrayRCP<const size_t> amalgRowPtr(Psubblock->getLocalNumRows());
102  Teuchos::ArrayRCP<const LocalOrdinal> amalgCols(Psubblock->getLocalNumEntries());
103  Teuchos::ArrayRCP<const Scalar> amalgVals(Psubblock->getLocalNumEntries());
104  Teuchos::RCP<CrsMatrixWrap> Psubblockwrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(Psubblock);
105  Teuchos::RCP<CrsMatrix> Psubblockcrs = Psubblockwrap->getCrsMatrix();
106  Psubblockcrs->getAllValues(amalgRowPtr, amalgCols, amalgVals);
107 
108  size_t paddedNrows = Psubblock->getRowMap()->getLocalNumElements() * Teuchos::as<size_t>(dofPerNode);
109  Teuchos::ArrayRCP<size_t> newPRowPtr(paddedNrows + 1);
110  Teuchos::ArrayRCP<LocalOrdinal> newPCols(Psubblock->getLocalNumEntries() * dofPerNode);
111  Teuchos::ArrayRCP<Scalar> newPVals(Psubblock->getLocalNumEntries() * dofPerNode);
112  size_t cnt = 0; // local id counter
113  for (decltype(amalgRowPtr.size()) i = 0; i < amalgRowPtr.size() - 1; i++) {
114  size_t rowLength = amalgRowPtr[i + 1] - amalgRowPtr[i];
115  for (int j = 0; j < dofPerNode; j++) {
116  newPRowPtr[i * dofPerNode + j] = cnt;
117  for (size_t k = 0; k < rowLength; k++) {
118  newPCols[cnt] = amalgCols[k + amalgRowPtr[i]] * dofPerNode + j;
119  newPVals[cnt++] = amalgVals[k + amalgRowPtr[i]];
120  }
121  }
122  }
123 
124  newPRowPtr[paddedNrows] = cnt; // close row CSR array
125  std::vector<size_t> stridingInfo(1, dofPerNode);
126 
127  GlobalOrdinal nCoarseDofs = Psubblock->getDomainMap()->getLocalNumElements() * dofPerNode;
128  GlobalOrdinal indexBase = Psubblock->getDomainMap()->getIndexBase();
129  RCP<const Map> coarseDomainMap = StridedMapFactory::Build(Psubblock->getDomainMap()->lib(),
131  nCoarseDofs,
132  indexBase,
133  stridingInfo,
134  Psubblock->getDomainMap()->getComm(),
135  -1 /* stridedBlockId */,
136  0 /*domainGidOffset */);
137 
138  size_t nColCoarseDofs = Teuchos::as<size_t>(Psubblock->getColMap()->getLocalNumElements() * dofPerNode);
139  Teuchos::Array<GlobalOrdinal> unsmooshColMapGIDs(nColCoarseDofs);
140  for (size_t c = 0; c < Psubblock->getColMap()->getLocalNumElements(); ++c) {
141  GlobalOrdinal gid = (Psubblock->getColMap()->getGlobalElement(c) - indexBase) * dofPerNode + indexBase;
142 
143  for (int i = 0; i < dofPerNode; ++i) {
144  unsmooshColMapGIDs[c * dofPerNode + i] = gid + i;
145  }
146  }
147  Teuchos::RCP<Map> coarseColMap = MapFactory::Build(Psubblock->getDomainMap()->lib(),
149  unsmooshColMapGIDs(), // View,
150  indexBase,
151  Psubblock->getDomainMap()->getComm());
152 
153  Teuchos::Array<GlobalOrdinal> unsmooshRowMapGIDs(paddedNrows);
154  for (size_t c = 0; c < Psubblock->getRowMap()->getLocalNumElements(); ++c) {
155  GlobalOrdinal gid = (Psubblock->getRowMap()->getGlobalElement(c) - indexBase) * dofPerNode + indexBase;
156 
157  for (int i = 0; i < dofPerNode; ++i) {
158  unsmooshRowMapGIDs[c * dofPerNode + i] = gid + i;
159  }
160  }
161  Teuchos::RCP<Map> fineRowMap = MapFactory::Build(Psubblock->getDomainMap()->lib(),
163  unsmooshRowMapGIDs(), // View,
164  indexBase,
165  Psubblock->getDomainMap()->getComm());
166 
167  Teuchos::RCP<CrsMatrix> bigPCrs = CrsMatrixFactory::Build(fineRowMap, coarseColMap,
168  dofPerNode * Psubblock->getLocalMaxNumRowEntries());
169  for (size_t i = 0; i < paddedNrows; i++) {
170  bigPCrs->insertLocalValues(i,
171  newPCols.view(newPRowPtr[i], newPRowPtr[i + 1] - newPRowPtr[i]),
172  newPVals.view(newPRowPtr[i], newPRowPtr[i + 1] - newPRowPtr[i]));
173  }
174  bigPCrs->fillComplete(coarseDomainMap, fineRowMap);
175 
176  Teuchos::RCP<Matrix> bigP = Teuchos::rcp(new CrsMatrixWrap(bigPCrs));
177 
178  Set(coarseLevel, "P", bigP);
179 }
180 
181 } // namespace MueLu
182 
183 #define MUELU_REPLICATEPFACTORY_SHORT
184 #endif // MUELU_REPLICATEPFACTORY_DEF_HPP
ParameterList & setEntry(const std::string &name, U &&entry)
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access). Usage: Level-&gt;Get&lt; RCP&lt;Matrix&gt; &gt;(&quot;A&quot;, factory) if factory == NULL =&gt; use default factory.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
Timer to be used in factories. Similar to Monitor but with additional timers.
LocalOrdinal LO
T * get() const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
static const NoFactory * get()
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Definition: MueLu_Level.hpp:99
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.