// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_RAPFACTORY_DEF_HPP #define MUELU_RAPFACTORY_DEF_HPP #include #include #include #include #include #include #include "MueLu_RAPFactory_decl.hpp" #include "MueLu_MasterList.hpp" #include "MueLu_Monitor.hpp" #include "MueLu_PerfUtils.hpp" namespace MueLu { template RAPFactory::RAPFactory() : hasDeclaredInput_(false) {} template RAPFactory::~RAPFactory() = default; template RCP RAPFactory::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name)) SET_VALID_ENTRY("transpose: use implicit"); SET_VALID_ENTRY("rap: triple product"); SET_VALID_ENTRY("rap: fix zero diagonals"); SET_VALID_ENTRY("rap: fix zero diagonals threshold"); SET_VALID_ENTRY("rap: fix zero diagonals replacement"); SET_VALID_ENTRY("rap: relative diagonal floor"); #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"); validParamList->set("CheckMainDiagonal", false, "Check main diagonal for zeros"); validParamList->set("RepairMainDiagonal", false, "Repair zeros on main diagonal"); // Make sure we don't recursively validate options for the matrixmatrix kernels ParameterList norecurse; norecurse.disableRecursiveValidation(); validParamList->set("matrixmatrix: kernel params", norecurse, "MatrixMatrix kernel parameters"); return validParamList; } template void RAPFactory::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); hasDeclaredInput_ = true; } template void RAPFactory::Build(Level& fineLevel, Level& coarseLevel) const { const bool doTranspose = true; const bool doFillComplete = true; const bool doOptimizeStorage = true; RCP Ac; { FactoryMonitor m(*this, "Computing Ac", coarseLevel); std::ostringstream levelstr; levelstr << coarseLevel.GetLevelID(); std::string labelstr = FormattingHelper::getColonLabel(coarseLevel.getObjectLabel()); TEUCHOS_TEST_FOR_EXCEPTION(hasDeclaredInput_ == false, Exceptions::RuntimeError, "MueLu::RAPFactory::Build(): CallDeclareInput has not been called before Build!"); const Teuchos::ParameterList& pL = GetParameterList(); RCP A = Get >(fineLevel, "A"); RCP P = Get >(coarseLevel, "P"), AP; // We don't have a valid P (e.g., # global aggregates = 0) so we bail. // This level will ultimately be removed in MueLu_Hierarchy_defs.h via a resize() if (P == Teuchos::null) { Ac = Teuchos::null; Set(coarseLevel, "A", Ac); return; } bool isEpetra = A->getRowMap()->lib() == Xpetra::UseEpetra; bool isGPU = #ifdef KOKKOS_ENABLE_CUDA (typeid(Node).name() == typeid(Tpetra::KokkosCompat::KokkosCudaWrapperNode).name()) || #endif #ifdef KOKKOS_ENABLE_HIP (typeid(Node).name() == typeid(Tpetra::KokkosCompat::KokkosHIPWrapperNode).name()) || #endif #ifdef KOKKOS_ENABLE_SYCL (typeid(Node).name() == typeid(Tpetra::KokkosCompat::KokkosSYCLWrapperNode).name()) || #endif false; if (pL.get("rap: triple product") == false || isEpetra || isGPU) { if (pL.get("rap: triple product") && isEpetra) GetOStream(Warnings1) << "Switching from triple product to R x (A x P) since triple product has not been implemented for Epetra.\n"; #if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || defined(KOKKOS_ENABLE_SYCL) if (pL.get("rap: triple product") && isGPU) GetOStream(Warnings1) << "Switching from triple product to R x (A x P) since triple product has not been implemented for " << Node::execution_space::name() << std::endl; #endif // Reuse pattern if available (multiple solve) RCP APparams = rcp(new ParameterList); if (pL.isSublist("matrixmatrix: kernel params")) APparams = rcp(new ParameterList(pL.sublist("matrixmatrix: kernel params"))); // By default, we don't need global constants for A*P APparams->set("compute global constants: temporaries", APparams->get("compute global constants: temporaries", false)); APparams->set("compute global constants", APparams->get("compute global constants", false)); if (coarseLevel.IsAvailable("AP reuse data", this)) { GetOStream(static_cast(Runtime0 | Test)) << "Reusing previous AP data" << std::endl; APparams = coarseLevel.Get >("AP reuse data", this); if (APparams->isParameter("graph")) AP = APparams->get >("graph"); } { SubFactoryMonitor subM(*this, "MxM: A x P", coarseLevel); AP = MatrixMatrix::Multiply(*A, !doTranspose, *P, !doTranspose, AP, GetOStream(Statistics2), doFillComplete, doOptimizeStorage, labelstr + std::string("MueLu::A*P-") + levelstr.str(), APparams); } // Reuse coarse matrix memory if available (multiple solve) RCP RAPparams = rcp(new ParameterList); if (pL.isSublist("matrixmatrix: kernel params")) RAPparams = rcp(new ParameterList(pL.sublist("matrixmatrix: kernel params"))); if (coarseLevel.IsAvailable("RAP reuse data", this)) { GetOStream(static_cast(Runtime0 | Test)) << "Reusing previous RAP data" << std::endl; RAPparams = coarseLevel.Get >("RAP reuse data", this); if (RAPparams->isParameter("graph")) Ac = RAPparams->get >("graph"); // Some eigenvalue may have been cached with the matrix in the previous run. // As the matrix values will be updated, we need to reset the eigenvalue. Ac->SetMaxEigenvalueEstimate(-Teuchos::ScalarTraits::one()); } // We *always* need global constants for the RAP, but not for the temps RAPparams->set("compute global constants: temporaries", RAPparams->get("compute global constants: temporaries", false)); RAPparams->set("compute global constants", true); // Allow optimization of storage. // This is necessary for new faster Epetra MM kernels. // Seems to work with matrix modifications to repair diagonal entries. if (pL.get("transpose: use implicit") == true) { SubFactoryMonitor m2(*this, "MxM: P' x (AP) (implicit)", coarseLevel); Ac = MatrixMatrix::Multiply(*P, doTranspose, *AP, !doTranspose, Ac, GetOStream(Statistics2), doFillComplete, doOptimizeStorage, labelstr + std::string("MueLu::R*(AP)-implicit-") + levelstr.str(), RAPparams); } else { RCP R = Get >(coarseLevel, "R"); SubFactoryMonitor m2(*this, "MxM: R x (AP) (explicit)", coarseLevel); Ac = MatrixMatrix::Multiply(*R, !doTranspose, *AP, !doTranspose, Ac, GetOStream(Statistics2), doFillComplete, doOptimizeStorage, labelstr + std::string("MueLu::R*(AP)-explicit-") + levelstr.str(), RAPparams); } Teuchos::ArrayView relativeFloor = pL.get >("rap: relative diagonal floor")(); if (relativeFloor.size() > 0) { Xpetra::MatrixUtils::RelativeDiagonalBoost(Ac, relativeFloor, GetOStream(Statistics2)); } bool repairZeroDiagonals = pL.get("RepairMainDiagonal") || pL.get("rap: fix zero diagonals"); bool checkAc = pL.get("CheckMainDiagonal") || pL.get("rap: fix zero diagonals"); ; if (checkAc || repairZeroDiagonals) { using magnitudeType = typename Teuchos::ScalarTraits::magnitudeType; magnitudeType threshold; if (pL.isType("rap: fix zero diagonals threshold")) threshold = pL.get("rap: fix zero diagonals threshold"); else threshold = Teuchos::as(pL.get("rap: fix zero diagonals threshold")); Scalar replacement = Teuchos::as(pL.get("rap: fix zero diagonals replacement")); Xpetra::MatrixUtils::CheckRepairMainDiagonal(Ac, repairZeroDiagonals, GetOStream(Warnings1), threshold, replacement); } if (IsPrint(Statistics2)) { RCP params = rcp(new ParameterList()); ; params->set("printLoadBalancingInfo", true); params->set("printCommInfo", true); GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*Ac, "Ac", params); } if (!Ac.is_null()) { std::ostringstream oss; oss << "A_" << coarseLevel.GetLevelID(); Ac->setObjectLabel(oss.str()); } Set(coarseLevel, "A", Ac); if (!isGPU) { APparams->set("graph", AP); Set(coarseLevel, "AP reuse data", APparams); } if (!isGPU) { RAPparams->set("graph", Ac); Set(coarseLevel, "RAP reuse data", RAPparams); } } else { RCP RAPparams = rcp(new ParameterList); if (pL.isSublist("matrixmatrix: kernel params")) RAPparams->sublist("matrixmatrix: kernel params") = pL.sublist("matrixmatrix: kernel params"); if (coarseLevel.IsAvailable("RAP reuse data", this)) { GetOStream(static_cast(Runtime0 | Test)) << "Reusing previous RAP data" << std::endl; RAPparams = coarseLevel.Get >("RAP reuse data", this); if (RAPparams->isParameter("graph")) Ac = RAPparams->get >("graph"); // Some eigenvalue may have been cached with the matrix in the previous run. // As the matrix values will be updated, we need to reset the eigenvalue. Ac->SetMaxEigenvalueEstimate(-Teuchos::ScalarTraits::one()); } // We *always* need global constants for the RAP, but not for the temps RAPparams->set("compute global constants: temporaries", RAPparams->get("compute global constants: temporaries", false)); RAPparams->set("compute global constants", true); if (pL.get("transpose: use implicit") == true) { Ac = MatrixFactory::Build(P->getDomainMap(), Teuchos::as(0)); SubFactoryMonitor m2(*this, "MxMxM: R x A x P (implicit)", coarseLevel); Xpetra::TripleMatrixMultiply:: MultiplyRAP(*P, doTranspose, *A, !doTranspose, *P, !doTranspose, *Ac, doFillComplete, doOptimizeStorage, labelstr + std::string("MueLu::R*A*P-implicit-") + levelstr.str(), RAPparams); } else { RCP R = Get >(coarseLevel, "R"); Ac = MatrixFactory::Build(R->getRowMap(), Teuchos::as(0)); SubFactoryMonitor m2(*this, "MxMxM: R x A x P (explicit)", coarseLevel); Xpetra::TripleMatrixMultiply:: MultiplyRAP(*R, !doTranspose, *A, !doTranspose, *P, !doTranspose, *Ac, doFillComplete, doOptimizeStorage, labelstr + std::string("MueLu::R*A*P-explicit-") + levelstr.str(), RAPparams); } Teuchos::ArrayView relativeFloor = pL.get >("rap: relative diagonal floor")(); if (relativeFloor.size() > 0) { Xpetra::MatrixUtils::RelativeDiagonalBoost(Ac, relativeFloor, GetOStream(Statistics2)); } bool repairZeroDiagonals = pL.get("RepairMainDiagonal") || pL.get("rap: fix zero diagonals"); bool checkAc = pL.get("CheckMainDiagonal") || pL.get("rap: fix zero diagonals"); ; if (checkAc || repairZeroDiagonals) { using magnitudeType = typename Teuchos::ScalarTraits::magnitudeType; magnitudeType threshold; if (pL.isType("rap: fix zero diagonals threshold")) threshold = pL.get("rap: fix zero diagonals threshold"); else threshold = Teuchos::as(pL.get("rap: fix zero diagonals threshold")); Scalar replacement = Teuchos::as(pL.get("rap: fix zero diagonals replacement")); Xpetra::MatrixUtils::CheckRepairMainDiagonal(Ac, repairZeroDiagonals, GetOStream(Warnings1), threshold, replacement); } if (IsPrint(Statistics2)) { RCP params = rcp(new ParameterList()); ; params->set("printLoadBalancingInfo", true); params->set("printCommInfo", true); GetOStream(Statistics2) << PerfUtils::PrintMatrixInfo(*Ac, "Ac", params); } if (!Ac.is_null()) { std::ostringstream oss; oss << "A_" << coarseLevel.GetLevelID(); Ac->setObjectLabel(oss.str()); } Set(coarseLevel, "A", Ac); if (!isGPU) { RAPparams->set("graph", Ac); Set(coarseLevel, "RAP reuse data", RAPparams); } } } #ifdef HAVE_MUELU_DEBUG MatrixUtils::checkLocalRowMapMatchesColMap(*Ac); #endif // HAVE_MUELU_DEBUG if (transferFacts_.begin() != transferFacts_.end()) { SubFactoryMonitor m(*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) << "RAPFactory: call transfer factory: " << fac->description() << std::endl; fac->CallBuild(coarseLevel); // Coordinates transfer is marginally different from all other operations // because it is *optional*, and not required. For instance, we may need // coordinates only on level 4 if we start repartitioning from that level, // but we don't need them on level 1,2,3. As our current Hierarchy setup // assumes propagation of dependencies only through three levels, this // means that we need to rely on other methods to propagate optional data. // // The method currently used is through RAP transfer factories, which are // simply factories which are called at the end of RAP with a single goal: // transfer some fine data to coarser level. Because these factories are // kind of outside of the mainline factories, they behave different. In // particular, we call their Build method explicitly, rather than through // Get calls. This difference is significant, as the Get call is smart // enough to know when to release all factory dependencies, and Build is // dumb. This led to the following CoordinatesTransferFactory sequence: // 1. Request level 0 // 2. Request level 1 // 3. Request level 0 // 4. Release level 0 // 5. Release level 1 // // The problem is missing "6. Release level 0". Because it was missing, // we had outstanding request on "Coordinates", "Aggregates" and // "CoarseMap" on level 0. // // This was fixed by explicitly calling Release on transfer factories in // RAPFactory. I am still unsure how exactly it works, but now we have // clear data requests for all levels. coarseLevel.Release(*fac); } } } template void RAPFactory::AddTransferFactory(const RCP& factory) { // check if it's a TwoLevelFactoryBase based transfer factory TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::rcp_dynamic_cast(factory) == Teuchos::null, Exceptions::BadCast, "MueLu::RAPFactory::AddTransferFactory: Transfer factory is not derived from TwoLevelFactoryBase. " "This is very strange. (Note: you can remove this exception if there's a good reason for)"); TEUCHOS_TEST_FOR_EXCEPTION(hasDeclaredInput_, Exceptions::RuntimeError, "MueLu::RAPFactory::AddTransferFactory: Factory is being added after we have already declared input"); transferFacts_.push_back(factory); } } // namespace MueLu #define MUELU_RAPFACTORY_SHORT #endif // MUELU_RAPFACTORY_DEF_HPP