MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_BlockedRAPFactory_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_BLOCKEDRAPFACTORY_DEF_HPP
47#define MUELU_BLOCKEDRAPFACTORY_DEF_HPP
48
49#include <Xpetra_BlockedCrsMatrix.hpp>
50#include <Xpetra_MatrixFactory.hpp>
51#include <Xpetra_Matrix.hpp>
52#include <Xpetra_MatrixMatrix.hpp>
53
55
56#include "MueLu_MasterList.hpp"
57#include "MueLu_Monitor.hpp"
58#include "MueLu_PerfUtils.hpp"
60
61namespace MueLu {
62
63 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
67
68 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70 RCP<ParameterList> validParamList = rcp(new ParameterList());
71
72#define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
73 SET_VALID_ENTRY("transpose: use implicit");
74#undef SET_VALID_ENTRY
75 validParamList->set< RCP<const FactoryBase> >("A", null, "Generating factory of the matrix A used during the prolongator smoothing process");
76 validParamList->set< RCP<const FactoryBase> >("P", null, "Prolongator factory");
77 validParamList->set< RCP<const FactoryBase> >("R", null, "Restrictor factory");
78
79 return validParamList;
80 }
81
82 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84 const Teuchos::ParameterList& pL = GetParameterList();
85 if (pL.get<bool>("transpose: use implicit") == false)
86 Input(coarseLevel, "R");
87
88 Input(fineLevel, "A");
89 Input(coarseLevel, "P");
90
91 // call DeclareInput of all user-given transfer factories
92 for (std::vector<RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it)
93 (*it)->CallDeclareInput(coarseLevel);
94 }
95
96 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
97 void BlockedRAPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(Level &fineLevel, Level &coarseLevel) const { //FIXME make fineLevel const!!
98 FactoryMonitor m(*this, "Computing Ac (block)", coarseLevel);
99
100 const ParameterList& pL = GetParameterList();
101
102 RCP<Matrix> A = Get< RCP<Matrix> >(fineLevel, "A");
103 RCP<Matrix> P = Get< RCP<Matrix> >(coarseLevel, "P");
104
105
106 RCP<BlockedCrsMatrix> bA = rcp_dynamic_cast<BlockedCrsMatrix>(A);
107 RCP<BlockedCrsMatrix> bP = rcp_dynamic_cast<BlockedCrsMatrix>(P);
108 TEUCHOS_TEST_FOR_EXCEPTION(bA.is_null() || bP.is_null(), Exceptions::BadCast, "Matrices A and P must be of type BlockedCrsMatrix.");
109
110
111 RCP<BlockedCrsMatrix> bAP;
112 RCP<BlockedCrsMatrix> bAc;
113 {
114 SubFactoryMonitor subM(*this, "MxM: A x P", coarseLevel);
115
116 // Triple matrix product for BlockedCrsMatrixClass
117 TEUCHOS_TEST_FOR_EXCEPTION((bA->Cols() != bP->Rows()), Exceptions::BadCast,
118 "Block matrix dimensions do not match: "
119 "A is " << bA->Rows() << "x" << bA->Cols() <<
120 "P is " << bP->Rows() << "x" << bP->Cols());
121
122 bAP = MatrixMatrix::TwoMatrixMultiplyBlock(*bA, false, *bP, false, GetOStream(Statistics2), true, true);
123 }
124
125
126 // If we do not modify matrix later, allow optimization of storage.
127 // This is necessary for new faster Epetra MM kernels.
128 bool doOptimizeStorage = !checkAc_;
129
130 const bool doTranspose = true;
131 const bool doFillComplete = true;
132 if (pL.get<bool>("transpose: use implicit") == true) {
133 SubFactoryMonitor m2(*this, "MxM: P' x (AP) (implicit)", coarseLevel);
134 bAc = MatrixMatrix::TwoMatrixMultiplyBlock(*bP, doTranspose, *bAP, !doTranspose, GetOStream(Statistics2), doFillComplete, doOptimizeStorage);
135
136 } else {
137 RCP<Matrix> R = Get< RCP<Matrix> >(coarseLevel, "R");
138 RCP<BlockedCrsMatrix> bR = rcp_dynamic_cast<BlockedCrsMatrix>(R);
139 TEUCHOS_TEST_FOR_EXCEPTION(bR.is_null(), Exceptions::BadCast, "Matrix R must be of type BlockedCrsMatrix.");
140
141 TEUCHOS_TEST_FOR_EXCEPTION(bA->Rows() != bR->Cols(), Exceptions::BadCast,
142 "Block matrix dimensions do not match: "
143 "R is " << bR->Rows() << "x" << bR->Cols() <<
144 "A is " << bA->Rows() << "x" << bA->Cols());
145
146 SubFactoryMonitor m2(*this, "MxM: R x (AP) (explicit)", coarseLevel);
147 bAc = MatrixMatrix::TwoMatrixMultiplyBlock(*bR, !doTranspose, *bAP, !doTranspose, GetOStream(Statistics2), doFillComplete, doOptimizeStorage);
148 }
149
150
151 if (checkAc_)
153
154 GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*bAc, "Ac (blocked)");
155
156 Set<RCP <Matrix> >(coarseLevel, "A", bAc);
157
158 if (transferFacts_.begin() != transferFacts_.end()) {
159 SubFactoryMonitor m1(*this, "Projections", coarseLevel);
160
161 // call Build of all user-given transfer factories
162 for (std::vector<RCP<const FactoryBase> >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) {
163 RCP<const FactoryBase> fac = *it;
164
165 GetOStream(Runtime0) << "BlockRAPFactory: call transfer factory: " << fac->description() << std::endl;
166
167 fac->CallBuild(coarseLevel);
168
169 // AP (11/11/13): I am not sure exactly why we need to call Release, but we do need it to get rid
170 // of dangling data for CoordinatesTransferFactory
171 coarseLevel.Release(*fac);
172 }
173 }
174 }
175
176
177 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
178 void BlockedRAPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CheckMainDiagonal(RCP<BlockedCrsMatrix> & bAc, bool repairZeroDiagonals) {
179 RCP<Matrix> c00 = bAc->getMatrix(0, 0);
180 RCP<Matrix> Aout = MatrixFactory::Build(c00->getRowMap(), c00->getGlobalMaxNumRowEntries());
181
182 RCP<Vector> diagVec = VectorFactory::Build(c00->getRowMap());
183 c00->getLocalDiagCopy(*diagVec);
184 ArrayRCP<SC> diagVal = diagVec->getDataNonConst(0);
185
186 // loop over local rows
187 for (size_t row = 0; row < c00->getLocalNumRows(); row++) {
188 // get global row id
189 GO grid = c00->getRowMap()->getGlobalElement(row); // global row id
190
191 ArrayView<const LO> indices;
192 ArrayView<const SC> vals;
193 c00->getLocalRowView(row, indices, vals);
194
195 // just copy all values in output
196 ArrayRCP<GO> indout(indices.size(), Teuchos::OrdinalTraits<GO>::zero());
197 ArrayRCP<SC> valout(indices.size(), Teuchos::ScalarTraits<SC>::zero());
198
199 // just copy values
200 for (size_t i = 0; i < as<size_t>(indices.size()); i++) {
201 GO gcid = c00->getColMap()->getGlobalElement(indices[i]); // LID -> GID (column)
202 indout [i] = gcid;
203 valout [i] = vals[i];
204 }
205
206 Aout->insertGlobalValues(grid, indout.view(0, indout.size()), valout.view(0, valout.size()));
207 if (diagVal[row] == Teuchos::ScalarTraits<SC>::zero() && repairZeroDiagonals) {
208 // always overwrite diagonal entry
209 Aout->insertGlobalValues(grid, Teuchos::tuple<GO>(grid), Teuchos::tuple<SC>(1.0));
210 }
211 }
212
213 Aout->fillComplete(c00->getDomainMap(), c00->getRangeMap());
214
215 bAc->setMatrix(0, 0, Aout);
216 }
217
218 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
220 // check if it's a TwoLevelFactoryBase based transfer factory
221 TEUCHOS_TEST_FOR_EXCEPTION(rcp_dynamic_cast<const TwoLevelFactoryBase>(factory) == Teuchos::null, Exceptions::BadCast,
222 "Transfer factory is not derived from TwoLevelFactoryBase. This is very strange. "
223 "(Note: you can remove this exception if there's a good reason for)");
224 transferFacts_.push_back(factory);
225 }
226
227} //namespace MueLu
228
229#define MUELU_BLOCKEDRAPFACTORY_SHORT
230#endif // MUELU_BLOCKEDRAPFACTORY_DEF_HPP
231
232// TODO add plausibility check
233// TODO add CheckMainDiagonal for Blocked operator
234// Avoid copying block matrix!
235// create new empty Operator
#define SET_VALID_ENTRY(name)
static void CheckMainDiagonal(RCP< BlockedCrsMatrix > &bAc, bool repairZeroDiagonals=false)
void Build(Level &fineLevel, Level &coarseLevel) const override
Build an object with this factory.
void DeclareInput(Level &fineLevel, Level &coarseLevel) const override
Input.
std::vector< RCP< const FactoryBase > > transferFacts_
list of user-defined transfer Factories
RCP< const ParameterList > GetValidParameterList() const override
Return a const parameter list of valid parameters that setParameterList() will accept.
void AddTransferFactory(const RCP< const FactoryBase > &factory)
Add transfer factory in the end of list of transfer factories in RepartitionAcFactory.
Exception indicating invalid cast attempted.
Timer to be used in factories. Similar to Monitor but with additional timers.
void Input(Level &level, const std::string &varName) const
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.
void Release(const FactoryBase &factory)
Decrement the storage counter for all the inputs of a factory.
virtual const Teuchos::ParameterList & GetParameterList() const
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
@ Statistics2
Print even more statistics.
@ Statistics1
Print more statistics.
@ Runtime0
One-liner description of what is happening.