MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_CombinePFactory_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_COMBINEPFACTORY_DEF_HPP
47 #define MUELU_COMBINEPFACTORY_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 
68 
69 #include "MueLu_MasterList.hpp"
70 #include "MueLu_Monitor.hpp"
71 
72 namespace MueLu {
73 
74 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
76  RCP<ParameterList> validParamList = rcp(new ParameterList());
77  validParamList->setEntry("combine: numBlks", ParameterEntry(1));
78  validParamList->set<RCP<const FactoryBase>>("A", Teuchos::null, "Generating factory of the matrix A");
79 
80  return validParamList;
81 }
82 
83 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
85  DeclareInput(Level& fineLevel, Level& /* coarseLevel */) const {
86  // Input(fineLevel, "subblock");
87 }
88 
89 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
91  Level& coarseLevel) const {
92  return BuildP(fineLevel, coarseLevel);
93 }
94 
95 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
97  Level& coarseLevel) const {
98  FactoryMonitor m(*this, "Build", coarseLevel);
99 
100  const ParameterList& pL = GetParameterList();
101  const LO nBlks = as<LO>(pL.get<int>("combine: numBlks"));
102 
103  RCP<Matrix> A = Get<RCP<Matrix>>(fineLevel, "A");
104 
105  // Record all matrices that each define a block in block diagonal comboP
106  // matrix used for PDE/multiblock interpolation. Additionally, count
107  // total number of local rows, nonzeros, coarseDofs, and colDofs.
108 
109  Teuchos::ArrayRCP<RCP<Matrix>> arrayOfMatrices(nBlks);
110  size_t nComboRowMap = 0, nnzCombo = 0, nComboColMap = 0, nComboDomMap = 0;
111 
112  LO nTotalNumberLocalColMapEntries = 0;
113  Teuchos::ArrayRCP<size_t> DomMapSizePerBlk(nBlks);
114  Teuchos::ArrayRCP<size_t> ColMapSizePerBlk(nBlks);
115  Teuchos::ArrayRCP<size_t> ColMapLocalSizePerBlk(nBlks);
116  Teuchos::ArrayRCP<size_t> ColMapRemoteSizePerBlk(nBlks);
117  Teuchos::ArrayRCP<size_t> ColMapLocalCumulativePerBlk(nBlks + 1); // hardwire 0th entry so that it has the value of 0
118  Teuchos::ArrayRCP<size_t> ColMapRemoteCumulativePerBlk(nBlks + 1); // hardwire 0th entry so that it has the value of 0
119  for (int j = 0; j < nBlks; j++) {
120  std::string blockName = "Psubblock" + Teuchos::toString(j);
121  if (coarseLevel.IsAvailable(blockName, NoFactory::get())) {
122  arrayOfMatrices[j] = coarseLevel.Get<RCP<Matrix>>(blockName, NoFactory::get());
123  nComboRowMap += Teuchos::as<size_t>((arrayOfMatrices[j])->getRowMap()->getLocalNumElements());
124  DomMapSizePerBlk[j] = Teuchos::as<size_t>((arrayOfMatrices[j])->getDomainMap()->getLocalNumElements());
125  ColMapSizePerBlk[j] = Teuchos::as<size_t>((arrayOfMatrices[j])->getColMap()->getLocalNumElements());
126  nComboDomMap += DomMapSizePerBlk[j];
127  nComboColMap += ColMapSizePerBlk[j];
128  nnzCombo += Teuchos::as<size_t>((arrayOfMatrices[j])->getLocalNumEntries());
129  TEUCHOS_TEST_FOR_EXCEPTION((arrayOfMatrices[j])->getDomainMap()->getIndexBase() != 0, Exceptions::RuntimeError, "interpolation subblocks must use 0 indexbase");
130 
131  // figure out how many empty entries in each column map
132  int tempii = 0;
133  for (int i = 0; i < (int)DomMapSizePerBlk[j]; i++) {
134  // if ( (arrayOfMatrices[j])->getDomainMap()->getGlobalElement(i) == (arrayOfMatrices[j])->getColMap()->getGlobalElement(tempii) ) nTotalNumberLocalColMapEntries++;
135  if ((arrayOfMatrices[j])->getDomainMap()->getGlobalElement(i) == (arrayOfMatrices[j])->getColMap()->getGlobalElement(tempii)) tempii++;
136  }
137  nTotalNumberLocalColMapEntries += tempii;
138  ColMapLocalSizePerBlk[j] = tempii;
139  ColMapRemoteSizePerBlk[j] = ColMapSizePerBlk[j] - ColMapLocalSizePerBlk[j];
140  } else {
141  arrayOfMatrices[j] = Teuchos::null;
142  ColMapLocalSizePerBlk[j] = 0;
143  ColMapRemoteSizePerBlk[j] = 0;
144  }
145  ColMapLocalCumulativePerBlk[j + 1] = ColMapLocalSizePerBlk[j] + ColMapLocalCumulativePerBlk[j];
146  ColMapRemoteCumulativePerBlk[j + 1] = ColMapRemoteSizePerBlk[j] + ColMapRemoteCumulativePerBlk[j];
147  }
148  TEUCHOS_TEST_FOR_EXCEPTION(nComboRowMap != A->getRowMap()->getLocalNumElements(), Exceptions::RuntimeError, "sum of subblock rows != #row's Afine");
149 
150  // build up csr arrays for combo block diagonal P
151  Teuchos::ArrayRCP<size_t> comboPRowPtr(nComboRowMap + 1);
152  Teuchos::ArrayRCP<LocalOrdinal> comboPCols(nnzCombo);
153  Teuchos::ArrayRCP<Scalar> comboPVals(nnzCombo);
154 
155  size_t nnzCnt = 0, nrowCntFromPrevBlks = 0, ncolCntFromPrevBlks = 0;
156  LO maxNzPerRow = 0;
157  for (int j = 0; j < nBlks; j++) {
158  // grab csr pointers for individual blocks of P
159  if (arrayOfMatrices[j] != Teuchos::null) {
160  Teuchos::ArrayRCP<const size_t> subblockRowPtr((arrayOfMatrices[j])->getLocalNumRows());
161  Teuchos::ArrayRCP<const LocalOrdinal> subblockCols((arrayOfMatrices[j])->getLocalNumEntries());
162  Teuchos::ArrayRCP<const Scalar> subblockVals((arrayOfMatrices[j])->getLocalNumEntries());
163  if ((int)(arrayOfMatrices[j])->getLocalMaxNumRowEntries() > maxNzPerRow) maxNzPerRow = (int)(arrayOfMatrices[j])->getLocalMaxNumRowEntries();
164  Teuchos::RCP<CrsMatrixWrap> subblockwrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>((arrayOfMatrices[j]));
165  Teuchos::RCP<CrsMatrix> subblockcrs = subblockwrap->getCrsMatrix();
166  subblockcrs->getAllValues(subblockRowPtr, subblockCols, subblockVals);
167 
168  // copy jth block into csr arrays of comboP
169 
170  for (decltype(subblockRowPtr.size()) i = 0; i < subblockRowPtr.size() - 1; i++) {
171  size_t rowLength = subblockRowPtr[i + 1] - subblockRowPtr[i];
172  comboPRowPtr[nrowCntFromPrevBlks + i] = nnzCnt;
173  for (size_t k = 0; k < rowLength; k++) {
174  if ((int)subblockCols[k + subblockRowPtr[i]] < (int)ColMapLocalSizePerBlk[j]) {
175  comboPCols[nnzCnt] = subblockCols[k + subblockRowPtr[i]] + ColMapLocalCumulativePerBlk[j];
176  if ((int)comboPCols[nnzCnt] >= (int)ColMapLocalCumulativePerBlk[nBlks]) {
177  printf("ERROR1\n");
178  exit(1);
179  }
180  } else {
181  // Here we subtract off the number of local colmap guys ... so this tell us where we are among ghost unknowns. We then want to stick this ghost after
182  // all the Local guys ... so we add ColMapLocalCumulativePerBlk[nBlks] .... finally we need to account for the fact that other ghost blocks may have already
183  // been handled ... so we then add + ColMapRemoteCumulativePerBlk[j];
184  comboPCols[nnzCnt] = subblockCols[k + subblockRowPtr[i]] - ColMapLocalSizePerBlk[j] + ColMapLocalCumulativePerBlk[nBlks] + ColMapRemoteCumulativePerBlk[j];
185  if ((int)comboPCols[nnzCnt] >= (int)(ColMapLocalCumulativePerBlk[nBlks] + ColMapRemoteCumulativePerBlk[nBlks])) {
186  printf("ERROR2\n");
187  exit(1);
188  }
189  }
190  comboPVals[nnzCnt++] = subblockVals[k + subblockRowPtr[i]];
191  }
192  }
193 
194  nrowCntFromPrevBlks += Teuchos::as<size_t>((arrayOfMatrices[j])->getRowMap()->getLocalNumElements());
195  ncolCntFromPrevBlks += DomMapSizePerBlk[j]; // rst: check this
196  }
197  }
198  comboPRowPtr[nComboRowMap] = nnzCnt;
199 
200  // Come up with global IDs for the coarse grid maps. We assume that each xxx
201  // block has a minimum GID of 0. Since MueLu is generally creating these
202  // GIDS, this assumption is probably correct, but we'll check it.
203 
204  Teuchos::Array<GlobalOrdinal> comboDomainMapGIDs(nComboDomMap);
205  Teuchos::Array<GlobalOrdinal> comboColMapGIDs(nComboColMap);
206 
207  LO nTotalNumberRemoteColMapEntries = 0;
208  GlobalOrdinal offset = 0;
209  size_t domainMapIndex = 0;
210  int nComboColIndex = 0;
211  for (int j = 0; j < nBlks; j++) {
212  int nThisBlkColIndex = 0;
213 
214  GlobalOrdinal tempMax = 0, maxGID = 0;
215  if (arrayOfMatrices[j] != Teuchos::null) tempMax = (arrayOfMatrices[j])->getDomainMap()->getMaxGlobalIndex();
216  Teuchos::reduceAll(*(A->getDomainMap()->getComm()), Teuchos::REDUCE_MAX, tempMax, Teuchos::ptr(&maxGID));
217 
218  if (arrayOfMatrices[j] != Teuchos::null) {
219  TEUCHOS_TEST_FOR_EXCEPTION(arrayOfMatrices[j]->getDomainMap()->getMinAllGlobalIndex() < 0, Exceptions::RuntimeError, "Global ID assumption for domainMap not met within subblock");
220 
221  GO priorDomGID = 0;
222  for (size_t c = 0; c < DomMapSizePerBlk[j]; ++c) { // check this
223  // The global ids of jth block are assumed to go between 0 and maxGID_j. So the 1th blocks DomainGIDs should start at maxGID_0+1. The 2nd
224  // block DomainDIGS starts at maxGID_0+1 + maxGID_1 + 1. We use offset to keep track of these starting points.
225  priorDomGID = (arrayOfMatrices[j])->getDomainMap()->getGlobalElement(c);
226  comboDomainMapGIDs[domainMapIndex++] = offset + priorDomGID;
227  if (priorDomGID == (arrayOfMatrices[j])->getColMap()->getGlobalElement(nThisBlkColIndex)) {
228  comboColMapGIDs[nComboColIndex++] = offset + priorDomGID;
229  nThisBlkColIndex++;
230  }
231  }
232 
233  for (size_t cc = nThisBlkColIndex; cc < ColMapSizePerBlk[j]; ++cc) {
234  comboColMapGIDs[nTotalNumberLocalColMapEntries + nTotalNumberRemoteColMapEntries++] = offset + (arrayOfMatrices[j])->getColMap()->getGlobalElement(cc);
235  }
236  }
237  offset += maxGID + 1;
238  }
239 #ifdef out
240  RCP<const Map> coarseDomainMap = Xpetra::MapFactory<LO, GO, NO>::Build(A->getDomainMap()->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), comboDomainMapGIDs, 0, A->getDomainMap()->getComm());
241  RCP<const Map> coarseColMap = Xpetra::MapFactory<LO, GO, NO>::Build(A->getDomainMap()->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), comboColMapGIDs, 0, A->getDomainMap()->getComm());
242 #endif
243 
244  RCP<const Map> coarseDomainMap = Xpetra::MapFactory<LO, GO, NO>::Build(A->getDomainMap()->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), comboDomainMapGIDs, 0, A->getRowMap()->getComm());
245  RCP<const Map> coarseColMap = Xpetra::MapFactory<LO, GO, NO>::Build(A->getDomainMap()->lib(), Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), comboColMapGIDs, 0, A->getRowMap()->getComm());
246 
247  Teuchos::RCP<CrsMatrix> comboPCrs = CrsMatrixFactory::Build(A->getRowMap(), coarseColMap, maxNzPerRow);
248  // comboPCrs->getCrsGraph(); //.getRowInfo(6122);
249  // comboPCrs->getRowInfo(6122);
250 
251  // Teuchos::RCP<CrsMatrix> comboPCrs = CrsMatrixFactory::Build(A->getRowMap(), coarseColMap,nnzCombo+1000);
252 
253  // for (size_t i = 0; i < nComboRowMap; i++) {
254  // printf("FIXME\n"); if (nComboRowMap > 6142) nComboRowMap = 6142;
255  for (size_t i = 0; i < nComboRowMap; i++) {
256  comboPCrs->insertLocalValues(i, comboPCols.view(comboPRowPtr[i], comboPRowPtr[i + 1] - comboPRowPtr[i]),
257  comboPVals.view(comboPRowPtr[i], comboPRowPtr[i + 1] - comboPRowPtr[i]));
258  }
259  comboPCrs->fillComplete(coarseDomainMap, A->getRowMap());
260 
261  Teuchos::RCP<Matrix> comboP = Teuchos::rcp(new CrsMatrixWrap(comboPCrs));
262 
263  Set(coarseLevel, "P", comboP);
264 }
265 
266 } // namespace MueLu
267 
268 #define MUELU_COMBINEPFACTORY_SHORT
269 #endif // MUELU_COMBINEPFACTORY_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.
GlobalOrdinal GO
T & get(const std::string &name, T def_value)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
LocalOrdinal LO
size_type size() const
static const NoFactory * get()
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
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
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int >> &comm, LocalGlobal lg=Xpetra::GloballyDistributed)
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Exception throws to report errors in the internal logical of the program.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
std::string toString(const T &t)
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need&#39;s value has been saved.
ArrayView< T > view(size_type lowerOffset, size_type size) const