// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef MUELU_ZOLTAN2INTERFACE_DEF_HPP #define MUELU_ZOLTAN2INTERFACE_DEF_HPP #include #include #include "MueLu_Zoltan2Interface_decl.hpp" #if defined(HAVE_MUELU_ZOLTAN2) && defined(HAVE_MPI) #include #include #include #include #include #include #include "MueLu_Level.hpp" #include "MueLu_Exceptions.hpp" #include "MueLu_Monitor.hpp" namespace MueLu { template Zoltan2Interface::Zoltan2Interface() { defaultZoltan2Params = rcp(new ParameterList()); defaultZoltan2Params->set("algorithm", "multijagged"); defaultZoltan2Params->set("partitioning_approach", "partition"); // Improve scaling for communication bound algorithms by premigrating // coordinates to a subset of processors. // For more information, see Github issue #1538 defaultZoltan2Params->set("mj_premigration_option", 1); } template RCP Zoltan2Interface::GetValidParameterList() const { RCP validParamList = rcp(new ParameterList()); validParamList->set >("A", Teuchos::null, "Factory of the matrix A"); validParamList->set >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory."); validParamList->set >("Coordinates", Teuchos::null, "Factory of the coordinates"); validParamList->set >("ParameterList", Teuchos::null, "Zoltan2 parameters"); validParamList->set >("repartition: heuristic target rows per process", Teuchos::null, "Factory for number of rows per process to use with MultiJagged"); return validParamList; } template void Zoltan2Interface::DeclareInput(Level& currentLevel) const { Input(currentLevel, "A"); Input(currentLevel, "number of partitions"); const ParameterList& pL = GetParameterList(); // We do this dance, because we don't want "ParameterList" to be marked as used. // Is there a better way? Teuchos::ParameterEntry entry = pL.getEntry("ParameterList"); RCP providedList = Teuchos::any_cast >(entry.getAny(false)); if (providedList != Teuchos::null && providedList->isType("algorithm")) { const std::string algo = providedList->get("algorithm"); if (algo == "multijagged") { Input(currentLevel, "Coordinates"); Input(currentLevel, "repartition: heuristic target rows per process"); } else if (algo == "rcb") { Input(currentLevel, "Coordinates"); } } else { Input(currentLevel, "repartition: heuristic target rows per process"); Input(currentLevel, "Coordinates"); } } template void Zoltan2Interface::Build(Level& level) const { FactoryMonitor m(*this, "Build", level); typedef typename Teuchos::ScalarTraits::coordinateType real_type; typedef typename Xpetra::MultiVector RealValuedMultiVector; RCP A = Get >(level, "A"); RCP rowMap = A->getRowMap(); LO blkSize = A->GetFixedBlockSize(); int numParts = Get(level, "number of partitions"); if (numParts == 1 || numParts == -1) { // Single processor, decomposition is trivial: all zeros RCP > decomposition = Xpetra::VectorFactory::Build(rowMap, true); Set(level, "Partition", decomposition); return; } /* else if (numParts == -1) { // No repartitioning RCP > decomposition = Teuchos::null; //Xpetra::VectorFactory::Build(rowMap, true); //decomposition->putScalar(Teuchos::as(rowMap->getComm()->getRank())); Set(level, "Partition", decomposition); return; }*/ const ParameterList& pL = GetParameterList(); RCP providedList = pL.get >("ParameterList"); ParameterList Zoltan2Params; if (providedList != Teuchos::null) Zoltan2Params = *providedList; // Merge defalt Zoltan2 parameters with user provided // If default and user parameters contain the same parameter name, user one is always preferred for (ParameterList::ConstIterator param = defaultZoltan2Params->begin(); param != defaultZoltan2Params->end(); param++) { const std::string& pName = defaultZoltan2Params->name(param); if (!Zoltan2Params.isParameter(pName)) Zoltan2Params.setEntry(pName, defaultZoltan2Params->getEntry(pName)); } Zoltan2Params.set("num_global_parts", Teuchos::as(numParts)); GetOStream(Runtime0) << "Zoltan2 parameters:\n----------\n" << Zoltan2Params << "----------" << std::endl; const std::string& algo = Zoltan2Params.get("algorithm"); if (algo == "multijagged" || algo == "rcb") { RCP coords = Get >(level, "Coordinates"); RCP map = coords->getMap(); GO numElements = map->getLocalNumElements(); // Check that the number of local coordinates is consistent with the #rows in A TEUCHOS_TEST_FOR_EXCEPTION(rowMap->getLocalNumElements() / blkSize != coords->getLocalLength(), Exceptions::Incompatible, "Coordinate vector length (" + toString(coords->getLocalLength()) << " is incompatible with number of block rows in A (" + toString(rowMap->getLocalNumElements() / blkSize) + "The vector length should be the same as the number of mesh points."); #ifdef HAVE_MUELU_DEBUG GO indexBase = rowMap->getIndexBase(); GetOStream(Runtime0) << "Checking consistence of row and coordinates maps" << std::endl; // Make sure that logical blocks in row map coincide with logical nodes in coordinates map ArrayView rowElements = rowMap->getLocalElementList(); ArrayView coordsElements = map->getLocalElementList(); for (LO i = 0; i < Teuchos::as(numElements); i++) TEUCHOS_TEST_FOR_EXCEPTION((coordsElements[i] - indexBase) * blkSize + indexBase != rowElements[i * blkSize], Exceptions::RuntimeError, "i = " << i << ", coords GID = " << coordsElements[i] << ", row GID = " << rowElements[i * blkSize] << ", blkSize = " << blkSize << std::endl); #endif typedef Zoltan2::XpetraMultiVectorAdapter InputAdapterType; typedef Zoltan2::PartitioningProblem ProblemType; Array weightsPerRow(numElements); for (LO i = 0; i < numElements; i++) { weightsPerRow[i] = 0.0; for (LO j = 0; j < blkSize; j++) { weightsPerRow[i] += A->getNumEntriesInLocalRow(i * blkSize + j); } } // MultiJagged: Grab the target rows per process from the Heuristic to use unless the Zoltan2 list says otherwise if (algo == "multijagged" && !Zoltan2Params.isParameter("mj_premigration_coordinate_count")) { LO heuristicTargetRowsPerProcess = Get(level, "repartition: heuristic target rows per process"); Zoltan2Params.set("mj_premigration_coordinate_count", heuristicTargetRowsPerProcess); } const bool writeZoltan2DebuggingFiles = Zoltan2Params.get("mj_debug", false); Zoltan2Params.remove("mj_debug"); std::vector strides; std::vector weights(1, weightsPerRow.getRawPtr()); RCP > dupMpiComm = rcp_dynamic_cast >(rowMap->getComm()->duplicate()); RCP > zoltanComm = dupMpiComm->getRawMpiComm(); InputAdapterType adapter(coords, weights, strides); RCP problem(new ProblemType(&adapter, &Zoltan2Params, (*zoltanComm)())); { SubFactoryMonitor m1(*this, "Zoltan2 " + toString(algo), level); if (writeZoltan2DebuggingFiles) adapter.generateFiles(("mj_debug.lvl_" + std::to_string(level.GetLevelID())).c_str(), *(rowMap->getComm())); problem->solve(); } RCP > decomposition = Xpetra::VectorFactory::Build(rowMap, false); ArrayRCP decompEntries = decomposition->getDataNonConst(0); const typename InputAdapterType::part_t* parts = problem->getSolution().getPartListView(); for (GO i = 0; i < numElements; i++) { int partNum = parts[i]; for (LO j = 0; j < blkSize; j++) decompEntries[i * blkSize + j] = partNum; } Set(level, "Partition", decomposition); } else { GO numElements = rowMap->getLocalNumElements(); typedef Zoltan2::XpetraCrsGraphAdapter InputAdapterType; typedef Zoltan2::PartitioningProblem ProblemType; RCP > dupMpiComm = rcp_dynamic_cast >(rowMap->getComm()->duplicate()); RCP > zoltanComm = dupMpiComm->getRawMpiComm(); InputAdapterType adapter(A->getCrsGraph()); RCP problem(new ProblemType(&adapter, &Zoltan2Params, (*zoltanComm)())); { SubFactoryMonitor m1(*this, "Zoltan2 " + toString(algo), level); problem->solve(); } RCP > decomposition = Xpetra::VectorFactory::Build(rowMap, false); ArrayRCP decompEntries = decomposition->getDataNonConst(0); const typename InputAdapterType::part_t* parts = problem->getSolution().getPartListView(); // For blkSize > 1, ignore solution for every row but the first ones in a block. for (GO i = 0; i < numElements / blkSize; i++) { int partNum = parts[i * blkSize]; for (LO j = 0; j < blkSize; j++) decompEntries[i * blkSize + j] = partNum; } Set(level, "Partition", decomposition); } } } // namespace MueLu #endif // if defined(HAVE_MUELU_ZOLTAN2) && defined(HAVE_MPI) #endif // MUELU_ZOLTAN2INTERFACE_DEF_HPP