// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /* * MueLu_IsorropiaInterface_def.hpp * * Created on: Jun 10, 2013 * Author: tobias */ #ifndef MUELU_ISORROPIAINTERFACE_DEF_HPP_ #define MUELU_ISORROPIAINTERFACE_DEF_HPP_ #include "MueLu_IsorropiaInterface_decl.hpp" #include //#include //TODO: fwd decl. //#include //TODO: fwd decl. #include #include #include #include #ifdef HAVE_MUELU_ISORROPIA #include #ifdef HAVE_MUELU_EPETRA #include #include #include #endif #include #endif // ENDIF HAVE_MUELU_ISORROPIA #include "MueLu_Level.hpp" #include "MueLu_Exceptions.hpp" #include "MueLu_Monitor.hpp" #include "MueLu_AmalgamationInfo.hpp" namespace MueLu { template RCP IsorropiaInterface::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 >("UnAmalgamationInfo", Teuchos::null, "Generating factory of UnAmalgamationInfo"); return validParamList; } template void IsorropiaInterface::DeclareInput(Level& currentLevel) const { Input(currentLevel, "A"); Input(currentLevel, "number of partitions"); Input(currentLevel, "UnAmalgamationInfo"); } template void IsorropiaInterface::Build(Level& level) const { FactoryMonitor m(*this, "Build", level); typedef Xpetra::BlockedMap BlockMap; RCP A = Get >(level, "A"); RCP amalInfo = Get >(level, "UnAmalgamationInfo"); GO numParts = Get(level, "number of partitions"); RCP rowMap = A->getRowMap(); RCP colMap = A->getColMap(); if (numParts == 1 || numParts == -1) { // Running on one processor, so decomposition is the trivial one, all zeros. RCP > decomposition = Xpetra::VectorFactory::Build(rowMap, true); Set(level, "AmalgamatedPartition", decomposition); return; } // ok, reconstruct graph information of matrix A // Note, this is the non-rebalanced matrix A and we need the graph // of the non-rebalanced matrix A. We cannot make use of the "Graph" // that is/will be built for the aggregates later for several reasons // 1) the "Graph" for the aggregates is meant to be based on the rebalanced matrix A // 2) we cannot call a CoalesceDropFactory::Build here since this is very complicated and // completely messes up the whole factory chain // 3) CoalesceDropFactory is meant to provide some minimal Graph information for the aggregation // (LWGraph), but here we need the full CrsGraph for Isorropia as input // That is, why this code is somewhat redundant to CoalesceDropFactory LO blockdim = 1; // block dim for fixed size blocks GO indexBase = rowMap->getIndexBase(); // index base of maps GO offset = 0; LO blockid = -1; // block id in strided map LO nStridedOffset = 0; // DOF offset for strided block id "blockid" (default = 0) // LO stridedblocksize = blockdim; // size of strided block id "blockid" (default = fullblocksize, only if blockid!=-1 stridedblocksize <= fullblocksize) // 1) check for blocking/striding information // fill above variables if (A->IsView("stridedMaps") && Teuchos::rcp_dynamic_cast(A->getRowMap("stridedMaps")) != Teuchos::null) { Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!) RCP strMap = Teuchos::rcp_dynamic_cast(A->getRowMap()); TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null, Exceptions::BadCast, "MueLu::IsorropiaInterface::Build: cast to strided row map failed."); blockdim = strMap->getFixedBlockSize(); offset = strMap->getOffset(); blockid = strMap->getStridedBlockId(); if (blockid > -1) { std::vector stridingInfo = strMap->getStridingData(); for (size_t j = 0; j < Teuchos::as(blockid); j++) nStridedOffset += stridingInfo[j]; // stridedblocksize = Teuchos::as(stridingInfo[blockid]); } // else { // stridedblocksize = blockdim; //} oldView = A->SwitchToView(oldView); // GetOStream(Statistics0) << "IsorropiaInterface::Build():" << " found blockdim=" << blockdim << " from strided maps (blockid=" << blockid << ", strided block size=" << stridedblocksize << "). offset=" << offset << std::endl; } else GetOStream(Statistics0) << "IsorropiaInterface::Build(): no striding information available. Use blockdim=1 with offset=0" << std::endl; // 2) get row map for amalgamated matrix (graph of A) // with same distribution over all procs as row map of A RCP nodeMap = amalInfo->getNodeRowMap(); RCP bnodeMap = Teuchos::rcp_dynamic_cast(nodeMap); if (!bnodeMap.is_null()) nodeMap = bnodeMap->getMap(); GetOStream(Statistics0) << "IsorropiaInterface:Build(): nodeMap " << nodeMap->getLocalNumElements() << "/" << nodeMap->getGlobalNumElements() << " elements" << std::endl; // 3) create graph of amalgamated matrix RCP crsGraph = CrsGraphFactory::Build(nodeMap, A->getLocalMaxNumRowEntries() * blockdim); // 4) do amalgamation. generate graph of amalgamated matrix for (LO row = 0; row < Teuchos::as(A->getRowMap()->getLocalNumElements()); row++) { // get global DOF id GO grid = rowMap->getGlobalElement(row); // translate grid to nodeid // JHU 2019-20-May this is identical to AmalgamationFactory::DOFGid2NodeId(), and is done // to break a circular dependence when libraries are built statically GO nodeId = (grid - offset - indexBase) / blockdim + indexBase; size_t nnz = A->getNumEntriesInLocalRow(row); Teuchos::ArrayView indices; Teuchos::ArrayView vals; A->getLocalRowView(row, indices, vals); RCP > cnodeIds = Teuchos::rcp(new std::vector); // global column block ids LO realnnz = 0; for (LO col = 0; col < Teuchos::as(nnz); col++) { GO gcid = colMap->getGlobalElement(indices[col]); // global column id if (vals[col] != 0.0) { // JHU 2019-20-May this is identical to AmalgamationFactory::DOFGid2NodeId(), and is done // to break a circular dependence when libraries are built statically GO cnodeId = (gcid - offset - indexBase) / blockdim + indexBase; cnodeIds->push_back(cnodeId); realnnz++; // increment number of nnz in matrix row } } Teuchos::ArrayRCP arr_cnodeIds = Teuchos::arcp(cnodeIds); if (arr_cnodeIds.size() > 0) crsGraph->insertGlobalIndices(nodeId, arr_cnodeIds()); } // fill matrix graph crsGraph->fillComplete(nodeMap, nodeMap); #ifdef HAVE_MPI #ifdef HAVE_MUELU_ISORROPIA // prepare parameter list for Isorropia Teuchos::ParameterList paramlist; paramlist.set("NUM PARTS", toString(numParts)); /*STRUCTURALLY SYMMETRIC [NO/yes] (is symmetrization required?) PARTITIONING METHOD [block/cyclic/random/rcb/rib/hsfc/graph/HYPERGRAPH] NUM PARTS [int k] (global number of parts) IMBALANCE TOL [float tol] (1.0 is perfect balance) BALANCE OBJECTIVE [ROWS/nonzeros] */ Teuchos::ParameterList& sublist = paramlist.sublist("Zoltan"); sublist.set("LB_APPROACH", "PARTITION"); #ifdef HAVE_MUELU_EPETRA RCP > epCrsGraph = Teuchos::rcp_dynamic_cast >(crsGraph); if (epCrsGraph != Teuchos::null) { RCP epetraCrsGraph = epCrsGraph->getEpetra_CrsGraph(); RCP isoPart = Teuchos::rcp(new Isorropia::Epetra::Partitioner(epetraCrsGraph, paramlist)); int size = 0; const int* array = NULL; isoPart->extractPartsView(size, array); TEUCHOS_TEST_FOR_EXCEPTION(size != Teuchos::as(nodeMap->getLocalNumElements()), Exceptions::RuntimeError, "length of array returned from extractPartsView does not match local length of rowMap"); RCP > decomposition = Xpetra::VectorFactory::Build(nodeMap, false); ArrayRCP decompEntries = decomposition->getDataNonConst(0); // fill vector with amalgamated information about partitioning for (int i = 0; i < size; i++) { decompEntries[i] = Teuchos::as(array[i]); } Set(level, "AmalgamatedPartition", decomposition); } #endif // ENDIF HAVE_MUELU_EPETRA #ifdef HAVE_MUELU_INST_DOUBLE_INT_INT RCP > tpCrsGraph = Teuchos::rcp_dynamic_cast >(crsGraph); TEUCHOS_TEST_FOR_EXCEPTION(tpCrsGraph != Teuchos::null, Exceptions::RuntimeError, "Tpetra is not supported with Isorropia."); #else TEUCHOS_TEST_FOR_EXCEPTION(false, Exceptions::RuntimeError, "Isorropia is an interface to Zoltan which only has support for LO=GO=int and SC=double."); #endif // ENDIF HAVE_MUELU_INST_DOUBLE_INT_INT #endif // HAVE_MUELU_ISORROPIA #else // if we don't have MPI // Running on one processor, so decomposition is the trivial one, all zeros. RCP > decomposition = Xpetra::VectorFactory::Build(rowMap, true); Set(level, "AmalgamatedPartition", decomposition); #endif // HAVE_MPI // throw a more helpful error message if something failed // TEUCHOS_TEST_FOR_EXCEPTION(!level.IsAvailable("AmalgamatedPartition"), Exceptions::RuntimeError, "IsorropiaInterface::Build : no \'Partition\' vector available on level. Isorropia failed to build a partition of the non-repartitioned graph of A. Please make sure, that Isorropia is correctly compiled (Epetra/Tpetra)."); } // Build() } // namespace MueLu //#endif //if defined(HAVE_MUELU_ISORROPIA) && defined(HAVE_MPI) #endif /* MUELU_ISORROPIAINTERFACE_DEF_HPP_ */