// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_BLOCKEDRAPFACTORY_DEF_HPP #define MUELU_BLOCKEDRAPFACTORY_DEF_HPP #include #include #include #include #include "MueLu_BlockedRAPFactory_decl.hpp" #include "MueLu_MasterList.hpp" #include "MueLu_Monitor.hpp" #include "MueLu_PerfUtils.hpp" #include "MueLu_RAPFactory_decl.hpp" namespace MueLu { template BlockedRAPFactory::BlockedRAPFactory() : checkAc_(false) , repairZeroDiagonals_(false) {} template RCP BlockedRAPFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name)) SET_VALID_ENTRY("transpose: use implicit"); #undef SET_VALID_ENTRY validParamList->set >("A", null, "Generating factory of the matrix A used during the prolongator smoothing process"); validParamList->set >("P", null, "Prolongator factory"); validParamList->set >("R", null, "Restrictor factory"); return validParamList; } template void BlockedRAPFactory::DeclareInput(Level &fineLevel, Level &coarseLevel) const { const Teuchos::ParameterList &pL = GetParameterList(); if (pL.get("transpose: use implicit") == false) Input(coarseLevel, "R"); Input(fineLevel, "A"); Input(coarseLevel, "P"); // call DeclareInput of all user-given transfer factories for (std::vector >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) (*it)->CallDeclareInput(coarseLevel); } template void BlockedRAPFactory::Build(Level &fineLevel, Level &coarseLevel) const { // FIXME make fineLevel const!! FactoryMonitor m(*this, "Computing Ac (block)", coarseLevel); const ParameterList &pL = GetParameterList(); RCP A = Get >(fineLevel, "A"); RCP P = Get >(coarseLevel, "P"); RCP bA = rcp_dynamic_cast(A); RCP bP = rcp_dynamic_cast(P); TEUCHOS_TEST_FOR_EXCEPTION(bA.is_null() || bP.is_null(), Exceptions::BadCast, "Matrices A and P must be of type BlockedCrsMatrix."); RCP bAP; RCP bAc; { SubFactoryMonitor subM(*this, "MxM: A x P", coarseLevel); // Triple matrix product for BlockedCrsMatrixClass TEUCHOS_TEST_FOR_EXCEPTION((bA->Cols() != bP->Rows()), Exceptions::BadCast, "Block matrix dimensions do not match: " "A is " << bA->Rows() << "x" << bA->Cols() << "P is " << bP->Rows() << "x" << bP->Cols()); bAP = MatrixMatrix::TwoMatrixMultiplyBlock(*bA, false, *bP, false, GetOStream(Statistics2), true, true); } // If we do not modify matrix later, allow optimization of storage. // This is necessary for new faster Epetra MM kernels. bool doOptimizeStorage = !checkAc_; const bool doTranspose = true; const bool doFillComplete = true; if (pL.get("transpose: use implicit") == true) { SubFactoryMonitor m2(*this, "MxM: P' x (AP) (implicit)", coarseLevel); bAc = MatrixMatrix::TwoMatrixMultiplyBlock(*bP, doTranspose, *bAP, !doTranspose, GetOStream(Statistics2), doFillComplete, doOptimizeStorage); } else { RCP R = Get >(coarseLevel, "R"); RCP bR = rcp_dynamic_cast(R); TEUCHOS_TEST_FOR_EXCEPTION(bR.is_null(), Exceptions::BadCast, "Matrix R must be of type BlockedCrsMatrix."); TEUCHOS_TEST_FOR_EXCEPTION(bA->Rows() != bR->Cols(), Exceptions::BadCast, "Block matrix dimensions do not match: " "R is " << bR->Rows() << "x" << bR->Cols() << "A is " << bA->Rows() << "x" << bA->Cols()); SubFactoryMonitor m2(*this, "MxM: R x (AP) (explicit)", coarseLevel); bAc = MatrixMatrix::TwoMatrixMultiplyBlock(*bR, !doTranspose, *bAP, !doTranspose, GetOStream(Statistics2), doFillComplete, doOptimizeStorage); } if (checkAc_) CheckMainDiagonal(bAc); GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*bAc, "Ac (blocked)"); Set >(coarseLevel, "A", bAc); if (transferFacts_.begin() != transferFacts_.end()) { SubFactoryMonitor m1(*this, "Projections", coarseLevel); // call Build of all user-given transfer factories for (std::vector >::const_iterator it = transferFacts_.begin(); it != transferFacts_.end(); ++it) { RCP fac = *it; GetOStream(Runtime0) << "BlockRAPFactory: call transfer factory: " << fac->description() << std::endl; fac->CallBuild(coarseLevel); // AP (11/11/13): I am not sure exactly why we need to call Release, but we do need it to get rid // of dangling data for CoordinatesTransferFactory coarseLevel.Release(*fac); } } } template void BlockedRAPFactory::CheckMainDiagonal(RCP &bAc, bool repairZeroDiagonals) { RCP c00 = bAc->getMatrix(0, 0); RCP Aout = MatrixFactory::Build(c00->getRowMap(), c00->getGlobalMaxNumRowEntries()); RCP diagVec = VectorFactory::Build(c00->getRowMap()); c00->getLocalDiagCopy(*diagVec); ArrayRCP diagVal = diagVec->getDataNonConst(0); // loop over local rows for (size_t row = 0; row < c00->getLocalNumRows(); row++) { // get global row id GO grid = c00->getRowMap()->getGlobalElement(row); // global row id ArrayView indices; ArrayView vals; c00->getLocalRowView(row, indices, vals); // just copy all values in output ArrayRCP indout(indices.size(), Teuchos::OrdinalTraits::zero()); ArrayRCP valout(indices.size(), Teuchos::ScalarTraits::zero()); // just copy values for (size_t i = 0; i < as(indices.size()); i++) { GO gcid = c00->getColMap()->getGlobalElement(indices[i]); // LID -> GID (column) indout[i] = gcid; valout[i] = vals[i]; } Aout->insertGlobalValues(grid, indout.view(0, indout.size()), valout.view(0, valout.size())); if (diagVal[row] == Teuchos::ScalarTraits::zero() && repairZeroDiagonals) { // always overwrite diagonal entry Aout->insertGlobalValues(grid, Teuchos::tuple(grid), Teuchos::tuple(1.0)); } } Aout->fillComplete(c00->getDomainMap(), c00->getRangeMap()); bAc->setMatrix(0, 0, Aout); } template void BlockedRAPFactory::AddTransferFactory(const RCP &factory) { // check if it's a TwoLevelFactoryBase based transfer factory TEUCHOS_TEST_FOR_EXCEPTION(rcp_dynamic_cast(factory) == Teuchos::null, Exceptions::BadCast, "Transfer factory is not derived from TwoLevelFactoryBase. This is very strange. " "(Note: you can remove this exception if there's a good reason for)"); transferFacts_.push_back(factory); } } // namespace MueLu #define MUELU_BLOCKEDRAPFACTORY_SHORT #endif // MUELU_BLOCKEDRAPFACTORY_DEF_HPP // TODO add plausibility check // TODO add CheckMainDiagonal for Blocked operator // Avoid copying block matrix! // create new empty Operator