MueLu Version of the Day
Loading...
Searching...
No Matches
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
53// #include <Teuchos_LAPACK.hpp>
54#include <Teuchos_SerialDenseMatrix.hpp>
55#include <Teuchos_SerialDenseVector.hpp>
56#include <Teuchos_SerialDenseSolver.hpp>
57
58#include <Xpetra_CrsMatrixWrap.hpp>
59#include <Xpetra_ImportFactory.hpp>
60#include <Xpetra_Matrix.hpp>
61#include <Xpetra_MapFactory.hpp>
62#include <Xpetra_MultiVectorFactory.hpp>
63#include <Xpetra_VectorFactory.hpp>
64
65#include <Xpetra_IO.hpp>
66
69
70#include "MueLu_MasterList.hpp"
71#include "MueLu_Monitor.hpp"
72
73namespace MueLu {
74
75 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
77 RCP<ParameterList> validParamList = rcp(new ParameterList());
78 validParamList->setEntry("combine: numBlks",ParameterEntry(1));
79 validParamList->set< RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A");
80
81 return validParamList;
82 }
83
84 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
86 DeclareInput(Level& fineLevel, Level& /* coarseLevel */) const {
87// Input(fineLevel, "subblock");
88 }
89
90 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
92 Level& coarseLevel) const {
93 return BuildP(fineLevel, coarseLevel);
94 }
95
96 template <class Scalar,class LocalOrdinal, class GlobalOrdinal, class Node>
98 Level& coarseLevel) const {
99 FactoryMonitor m(*this, "Build", coarseLevel);
100
101 const ParameterList& pL = GetParameterList();
102 const LO nBlks = as<LO>(pL.get<int> ("combine: numBlks"));
103
104 RCP<Matrix> A = Get< RCP<Matrix> > (fineLevel, "A");
105
106 // Record all matrices that each define a block in block diagonal comboP
107 // matrix used for PDE/multiblock interpolation. Additionally, count
108 // total number of local rows, nonzeros, coarseDofs, and colDofs.
109
110 Teuchos::ArrayRCP<RCP<Matrix>> arrayOfMatrices(nBlks);
111 size_t nComboRowMap = 0, nnzCombo = 0, nComboColMap = 0, nComboDomMap = 0;
112
113 LO nTotalNumberLocalColMapEntries = 0;
114 Teuchos::ArrayRCP<size_t> DomMapSizePerBlk(nBlks);
115 Teuchos::ArrayRCP<size_t> ColMapSizePerBlk(nBlks);
116 Teuchos::ArrayRCP<size_t> ColMapLocalSizePerBlk(nBlks);
117 Teuchos::ArrayRCP<size_t> ColMapRemoteSizePerBlk(nBlks);
118 Teuchos::ArrayRCP<size_t> ColMapLocalCumulativePerBlk(nBlks+1); // hardwire 0th entry so that it has the value of 0
119 Teuchos::ArrayRCP<size_t> ColMapRemoteCumulativePerBlk(nBlks+1); // hardwire 0th entry so that it has the value of 0
120 for(int j = 0; j < nBlks; j++) {
121 std::string blockName = "Psubblock" + Teuchos::toString(j);
122 if (coarseLevel.IsAvailable(blockName, NoFactory::get())) {
123 arrayOfMatrices[j] = coarseLevel.Get< RCP<Matrix> >(blockName, NoFactory::get());
124 nComboRowMap += Teuchos::as<size_t>((arrayOfMatrices[j])->getRowMap()->getLocalNumElements());
125 DomMapSizePerBlk[j] = Teuchos::as<size_t>((arrayOfMatrices[j])->getDomainMap()->getLocalNumElements());
126 ColMapSizePerBlk[j] = Teuchos::as<size_t>((arrayOfMatrices[j])->getColMap()->getLocalNumElements());
127 nComboDomMap += DomMapSizePerBlk[j];
128 nComboColMap += ColMapSizePerBlk[j];
129 nnzCombo += Teuchos::as<size_t>((arrayOfMatrices[j])->getLocalNumEntries());
130 TEUCHOS_TEST_FOR_EXCEPTION((arrayOfMatrices[j])->getDomainMap()->getIndexBase() != 0, Exceptions::RuntimeError, "interpolation subblocks must use 0 indexbase");
131
132
133 // figure out how many empty entries in each column map
134 int tempii = 0;
135 for (int i = 0; i < (int) DomMapSizePerBlk[j]; i++ ) {
136// if ( (arrayOfMatrices[j])->getDomainMap()->getGlobalElement(i) == (arrayOfMatrices[j])->getColMap()->getGlobalElement(tempii) ) nTotalNumberLocalColMapEntries++;
137 if ( (arrayOfMatrices[j])->getDomainMap()->getGlobalElement(i) == (arrayOfMatrices[j])->getColMap()->getGlobalElement(tempii) ) tempii++;
138 }
139 nTotalNumberLocalColMapEntries += tempii;
140 ColMapLocalSizePerBlk[j] = tempii;
141 ColMapRemoteSizePerBlk[j] = ColMapSizePerBlk[j] - ColMapLocalSizePerBlk[j];
142 }
143 else {
144 arrayOfMatrices[j] = Teuchos::null;
145 ColMapLocalSizePerBlk[j] = 0;
146 ColMapRemoteSizePerBlk[j] = 0;
147 }
148 ColMapLocalCumulativePerBlk[j+1] = ColMapLocalSizePerBlk[j] + ColMapLocalCumulativePerBlk[j];
149 ColMapRemoteCumulativePerBlk[j+1]= ColMapRemoteSizePerBlk[j] + ColMapRemoteCumulativePerBlk[j];
150 }
151 TEUCHOS_TEST_FOR_EXCEPTION(nComboRowMap != A->getRowMap()->getLocalNumElements(), Exceptions::RuntimeError, "sum of subblock rows != #row's Afine");
152
153 // build up csr arrays for combo block diagonal P
154 Teuchos::ArrayRCP<size_t> comboPRowPtr(nComboRowMap+1);
155 Teuchos::ArrayRCP<LocalOrdinal> comboPCols(nnzCombo);
156 Teuchos::ArrayRCP<Scalar> comboPVals(nnzCombo);
157
158 size_t nnzCnt = 0, nrowCntFromPrevBlks = 0,ncolCntFromPrevBlks = 0;
159 LO maxNzPerRow = 0;
160 for(int j = 0; j < nBlks; j++) {
161 // grab csr pointers for individual blocks of P
162 if (arrayOfMatrices[j] != Teuchos::null) {
163 Teuchos::ArrayRCP<const size_t> subblockRowPtr((arrayOfMatrices[j])->getLocalNumRows());
164 Teuchos::ArrayRCP<const LocalOrdinal> subblockCols((arrayOfMatrices[j])->getLocalNumEntries());
165 Teuchos::ArrayRCP<const Scalar> subblockVals((arrayOfMatrices[j])->getLocalNumEntries());
166 if ( (int) (arrayOfMatrices[j])->getLocalMaxNumRowEntries() > maxNzPerRow) maxNzPerRow = (int) (arrayOfMatrices[j])->getLocalMaxNumRowEntries();
167 Teuchos::RCP<CrsMatrixWrap> subblockwrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>((arrayOfMatrices[j]));
168 Teuchos::RCP<CrsMatrix> subblockcrs = subblockwrap->getCrsMatrix();
169 subblockcrs->getAllValues(subblockRowPtr, subblockCols, subblockVals);
170
171 // copy jth block into csr arrays of comboP
172
173 for (decltype(subblockRowPtr.size()) i = 0; i < subblockRowPtr.size() - 1; i++) {
174 size_t rowLength = subblockRowPtr[i+1] - subblockRowPtr[i];
175 comboPRowPtr[ nrowCntFromPrevBlks+i ] = nnzCnt;
176 for (size_t k = 0; k < rowLength; k++) {
177 if ( (int) subblockCols[k+subblockRowPtr[i]] < (int) ColMapLocalSizePerBlk[j]) {
178 comboPCols[nnzCnt ] = subblockCols[k+subblockRowPtr[i]] + ColMapLocalCumulativePerBlk[j];
179 if ( (int) comboPCols[nnzCnt] >= (int) ColMapLocalCumulativePerBlk[nBlks]) { printf("ERROR1\n"); exit(1); }
180 }
181 else {
182 // 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
183 // all the Local guys ... so we add ColMapLocalCumulativePerBlk[nBlks] .... finally we need to account for the fact that other ghost blocks may have already
184 // been handled ... so we then add + ColMapRemoteCumulativePerBlk[j];
185 comboPCols[nnzCnt ] = subblockCols[k+subblockRowPtr[i]] - ColMapLocalSizePerBlk[j] + ColMapLocalCumulativePerBlk[nBlks] + ColMapRemoteCumulativePerBlk[j];
186 if ( (int) comboPCols[nnzCnt] >= (int) (ColMapLocalCumulativePerBlk[nBlks]+ ColMapRemoteCumulativePerBlk[nBlks])) { printf("ERROR2\n"); exit(1); }
187 }
188 comboPVals[nnzCnt++] = subblockVals[k+subblockRowPtr[i]];
189 }
190 }
191
192 nrowCntFromPrevBlks += Teuchos::as<size_t>((arrayOfMatrices[j])->getRowMap()->getLocalNumElements());
193 ncolCntFromPrevBlks += DomMapSizePerBlk[j]; // rst: check this
194 }
195 }
196 comboPRowPtr[nComboRowMap] = nnzCnt;
197
198
199 // Come up with global IDs for the coarse grid maps. We assume that each xxx
200 // block has a minimum GID of 0. Since MueLu is generally creating these
201 // GIDS, this assumption is probably correct, but we'll check it.
202
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
248 Teuchos::RCP<CrsMatrix> comboPCrs = CrsMatrixFactory::Build(A->getRowMap(), coarseColMap,maxNzPerRow);
249//comboPCrs->getCrsGraph(); //.getRowInfo(6122);
250//comboPCrs->getRowInfo(6122);
251
252// Teuchos::RCP<CrsMatrix> comboPCrs = CrsMatrixFactory::Build(A->getRowMap(), coarseColMap,nnzCombo+1000);
253
254// for (size_t i = 0; i < nComboRowMap; i++) {
255//printf("FIXME\n"); if (nComboRowMap > 6142) nComboRowMap = 6142;
256 for (size_t i = 0; i < nComboRowMap; i++) {
257 comboPCrs->insertLocalValues(i, comboPCols.view(comboPRowPtr[i], comboPRowPtr[i+1] - comboPRowPtr[i]),
258 comboPVals.view(comboPRowPtr[i], comboPRowPtr[i+1] - comboPRowPtr[i]));
259 }
260 comboPCrs->fillComplete(coarseDomainMap, A->getRowMap());
261
262 Teuchos::RCP<Matrix> comboP = Teuchos::rcp(new CrsMatrixWrap(comboPCrs));
263
264 Set(coarseLevel, "P", comboP);
265 }
266
267
268} //namespace MueLu
269
270#define MUELU_COMBINEPFACTORY_SHORT
271#endif // MUELU_COMBINEPFACTORY_DEF_HPP
MueLu::DefaultGlobalOrdinal GlobalOrdinal
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
T Get(Level &level, const std::string &varName) const
void Set(Level &level, const std::string &varName, const T &data) const
Class that holds all level-specific information.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access)....
static const NoFactory * get()
virtual const Teuchos::ParameterList & GetParameterList() const
Namespace for MueLu classes and methods.