| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#ifndef MUELU_SETUPREGIONHIERARCHY_DEF_HPP |
| 11 |
#define MUELU_SETUPREGIONHIERARCHY_DEF_HPP |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
#include <iostream> |
| 15 |
|
| 16 |
#include <Tpetra_KokkosCompat_DefaultNode.hpp> |
| 17 |
|
| 18 |
#include <Teuchos_RCP.hpp> |
| 19 |
|
| 20 |
#include <Xpetra_ConfigDefs.hpp> |
| 21 |
#include <Xpetra_Export.hpp> |
| 22 |
#include <Xpetra_Import.hpp> |
| 23 |
#include <Xpetra_Map.hpp> |
| 24 |
#include <Xpetra_MultiVector.hpp> |
| 25 |
#include <Xpetra_Vector.hpp> |
| 26 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 27 |
|
| 28 |
#include <MueLu_CreateXpetraPreconditioner.hpp> |
| 29 |
#include <MueLu_Utilities.hpp> |
| 30 |
|
| 31 |
#if defined(HAVE_MUELU_ZOLTAN2) && defined(HAVE_MPI) |
| 32 |
#include <MueLu_RepartitionFactory.hpp> |
| 33 |
#include <MueLu_RepartitionHeuristicFactory.hpp> |
| 34 |
#include <MueLu_Zoltan2Interface.hpp> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "SetupRegionVector_def.hpp" |
| 38 |
#include "SetupRegionMatrix_def.hpp" |
| 39 |
#include "SetupRegionSmoothers_def.hpp" |
| 40 |
|
| 41 |
#if defined(HAVE_MUELU_AMESOS2) |
| 42 |
#include <Amesos2_config.h> |
| 43 |
#include <Amesos2.hpp> |
| 44 |
#endif |
| 45 |
|
| 46 |
using Teuchos::Array; |
| 47 |
using Teuchos::ArrayRCP; |
| 48 |
using Teuchos::ArrayView; |
| 49 |
using Teuchos::ParameterList; |
| 50 |
using Teuchos::RCP; |
| 51 |
|
| 52 |
/*! \brief Create coarse level maps with continuous GIDs |
| 53 |
* |
| 54 |
* The direct solver requires maps with continuous GIDs. Starting from the |
| 55 |
* coarse level composite maps with discontinuous GIDs, we create a new row map |
| 56 |
* and a matching column map. |
| 57 |
* |
| 58 |
* Range and Domain map happen to correspond to the Row map, so we don't have |
| 59 |
* to deal with them in particular. |
| 60 |
*/ |
| 61 |
template <class LocalOrdinal, class GlobalOrdinal, class Node> |
| 62 |
void createContinuousCoarseLevelMaps(const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> rowMap, ///< row map |
| 63 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> colMap, ///< column map |
| 64 |
RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& contRowMap, ///< row map with continuous GIDs |
| 65 |
RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& contColMap ///< column map with continuous GIDs |
| 66 |
) { |
| 67 |
#include "Xpetra_UseShortNamesOrdinal.hpp" |
| 68 |
// /!\ This function is pure ordinal, no scalar type is passed as input |
| 69 |
// This means that use only three template paramters and that we |
| 70 |
// do not use the Scalar dependent short names! |
| 71 |
|
| 72 |
// Create row map with continuous GIDs |
| 73 |
contRowMap = MapFactory::Build(rowMap->lib(), |
| 74 |
rowMap->getGlobalNumElements(), |
| 75 |
rowMap->getLocalNumElements(), |
| 76 |
rowMap->getIndexBase(), |
| 77 |
rowMap->getComm()); |
| 78 |
|
| 79 |
return; |
| 80 |
} // createContinuousCoarseLevelMaps |
| 81 |
|
| 82 |
/* Reconstruct coarse-level maps (assuming fully structured grids) |
| 83 |
* |
| 84 |
* We know the regional map on the coarse levels since they are just the |
| 85 |
* row maps of the coarse level operators. Though, we need to re-construct |
| 86 |
* the quasiRegional and composite maps ourselves. |
| 87 |
* |
| 88 |
* We ultimately are only interested in the composite map on the coarsest level. |
| 89 |
* Intermediate levels are dealt with along the way, because we go through all |
| 90 |
* levels recursively. |
| 91 |
* |
| 92 |
* Assumptions: |
| 93 |
* - fully structured grid |
| 94 |
* - only on region per proc and group |
| 95 |
*/ |
| 96 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 97 |
void MakeCoarseLevelMaps(const int maxRegPerGID, |
| 98 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regHierarchy) { |
| 99 |
#include "Xpetra_UseShortNames.hpp" |
| 100 |
#include "MueLu_UseShortNames.hpp" |
| 101 |
|
| 102 |
using MT = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 103 |
|
| 104 |
RCP<Level> level0 = regHierarchy->GetLevel(0); |
| 105 |
|
| 106 |
const GO GO_INV = Teuchos::OrdinalTraits<GO>::invalid(); |
| 107 |
const int numLevels = regHierarchy->GetNumLevels(); |
| 108 |
|
| 109 |
// RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 110 |
// Teuchos::FancyOStream& out = *fancy; |
| 111 |
const int myRank = level0->GetComm()->getRank(); |
| 112 |
|
| 113 |
Teuchos::Array<LO> coarseCompositeToRegionLIDs; |
| 114 |
Teuchos::ArrayView<LO> compositeToRegionLIDs = level0->Get<ArrayView<LO>>("compositeToRegionLIDs"); |
| 115 |
|
| 116 |
for (int currentLevel = 1; currentLevel < numLevels; ++currentLevel) { |
| 117 |
RCP<Level> level = regHierarchy->GetLevel(currentLevel); |
| 118 |
RCP<Matrix> regProlong = level->Get<RCP<Matrix>>("P"); |
| 119 |
RCP<const Map> regRowMap = regProlong->getColMap(); |
| 120 |
|
| 121 |
RCP<Level> levelFine = regHierarchy->GetLevel(currentLevel - 1); |
| 122 |
RCP<Import> regRowImportFine = levelFine->Get<RCP<Import>>("rowImport"); |
| 123 |
RCP<Matrix> regMatFine = levelFine->Get<RCP<Matrix>>("A"); |
| 124 |
RCP<const Map> regRowMapFine = regMatFine->getRowMap(); |
| 125 |
|
| 126 |
// Extracting some basic information about local mesh in composite/region format |
| 127 |
const size_t numFineRegionNodes = regProlong->getLocalNumRows(); |
| 128 |
const size_t numFineCompositeNodes = compositeToRegionLIDs.size(); |
| 129 |
const size_t numFineDuplicateNodes = numFineRegionNodes - numFineCompositeNodes; |
| 130 |
|
| 131 |
const size_t numCoarseRegionNodes = regProlong->getColMap()->getLocalNumElements(); |
| 132 |
|
| 133 |
// Find the regionLIDs associated with local duplicated nodes |
| 134 |
// This will allow us to later loop only on duplicated nodes |
| 135 |
size_t countComposites = 0, countDuplicates = 0; |
| 136 |
Array<LO> fineDuplicateLIDs(numFineDuplicateNodes); |
| 137 |
for (size_t regionIdx = 0; regionIdx < numFineRegionNodes; ++regionIdx) { |
| 138 |
if (compositeToRegionLIDs[countComposites] == static_cast<LO>(regionIdx)) { |
| 139 |
++countComposites; |
| 140 |
} else { |
| 141 |
fineDuplicateLIDs[countDuplicates] = regionIdx; |
| 142 |
++countDuplicates; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// We gather the coarse GIDs associated with each fine point in the local composite mesh part. |
| 147 |
RCP<Xpetra::Vector<MT, LO, GO, NO>> coarseCompositeGIDs = Xpetra::VectorFactory<MT, LO, GO, NO>::Build(regRowImportFine->getSourceMap(), false); |
| 148 |
Teuchos::ArrayRCP<MT> coarseCompositeGIDsData = coarseCompositeGIDs->getDataNonConst(0); |
| 149 |
|
| 150 |
for (size_t compositeNodeIdx = 0; compositeNodeIdx < numFineCompositeNodes; ++compositeNodeIdx) { |
| 151 |
ArrayView<const LO> coarseRegionLID; // Should contain a single value |
| 152 |
ArrayView<const SC> dummyData; // Should contain a single value |
| 153 |
regProlong->getLocalRowView(compositeToRegionLIDs[compositeNodeIdx], |
| 154 |
coarseRegionLID, |
| 155 |
dummyData); |
| 156 |
if (coarseRegionLID.size() == 1) { |
| 157 |
coarseCompositeGIDsData[compositeNodeIdx] = regProlong->getColMap()->getGlobalElement(coarseRegionLID[0]); |
| 158 |
} else { |
| 159 |
coarseCompositeGIDsData[compositeNodeIdx] = -1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// We communicate the above GIDs to their duplicate so that we can replace GIDs of the region |
| 164 |
// column map and form the quasiRegion column map. |
| 165 |
RCP<Xpetra::Vector<MT, LO, GO, NO>> coarseQuasiregionGIDs; |
| 166 |
RCP<Xpetra::Vector<MT, LO, GO, NO>> coarseRegionGIDs; |
| 167 |
compositeToRegional(coarseCompositeGIDs, |
| 168 |
coarseQuasiregionGIDs, |
| 169 |
coarseRegionGIDs, |
| 170 |
regRowMapFine, |
| 171 |
regRowImportFine); |
| 172 |
|
| 173 |
RCP<Xpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> regionsPerGIDWithGhosts = |
| 174 |
Xpetra::MultiVectorFactory<LO, LO, GO, NO>::Build(regRowMapFine, |
| 175 |
maxRegPerGID, |
| 176 |
false); |
| 177 |
RCP<Xpetra::MultiVector<GO, LO, GO, NO>> interfaceGIDs = Xpetra::MultiVectorFactory<GO, LO, GO, NO>::Build(regRowMapFine, |
| 178 |
maxRegPerGID, |
| 179 |
false); |
| 180 |
|
| 181 |
Array<ArrayRCP<const LO>> regionsPerGIDWithGhostsFine(maxRegPerGID); |
| 182 |
Array<ArrayRCP<LO>> regionsPerGIDWithGhostsCoarse(maxRegPerGID); |
| 183 |
Array<ArrayRCP<GO>> interfaceGIDsCoarse(maxRegPerGID); |
| 184 |
for (size_t idx = 0; idx < static_cast<size_t>(maxRegPerGID); ++idx) { |
| 185 |
regionsPerGIDWithGhostsFine[idx] = levelFine->Get<RCP<Xpetra::MultiVector<LO, LO, GO, NO>>>("regionsPerGIDWithGhosts")->getData(idx); |
| 186 |
regionsPerGIDWithGhostsCoarse[idx] = regionsPerGIDWithGhosts->getDataNonConst(idx); |
| 187 |
interfaceGIDsCoarse[idx] = interfaceGIDs->getDataNonConst(idx); |
| 188 |
for (size_t coarseIdx = 0; |
| 189 |
coarseIdx < regionsPerGIDWithGhosts->getLocalLength(); ++coarseIdx) { |
| 190 |
regionsPerGIDWithGhostsCoarse[idx][coarseIdx] = -1; |
| 191 |
interfaceGIDsCoarse[idx][coarseIdx] = 0; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
for (size_t fineIdx = 0; fineIdx < numFineRegionNodes; ++fineIdx) { |
| 196 |
ArrayView<const LO> coarseRegionLID; // Should contain a single value |
| 197 |
ArrayView<const SC> dummyData; // Should contain a single value |
| 198 |
regProlong->getLocalRowView(fineIdx, |
| 199 |
coarseRegionLID, |
| 200 |
dummyData); |
| 201 |
const LO coarseIdx = coarseRegionLID[0]; |
| 202 |
|
| 203 |
// Now fill regionPerGIDWithGhostsCoarse[:][coarseRegionLID] |
| 204 |
// with data from regionPerGIDWithGhostsFine[:][fineIdx]. |
| 205 |
// The problem is we might have more then maxRegPerGID on currentLevel |
| 206 |
// then on currentLevel-1... we might need to do a union or something? |
| 207 |
// I guess technically using the restriction operator here would be more |
| 208 |
// helpful than the prolongator, this way we could find all the fine interface |
| 209 |
// points easily and compute the union of the PIDs they belong too. |
| 210 |
// This can actually be done extracting the local matrix and compute the transpose. |
| 211 |
// For now let us assume that maxRegPerGID is constant and hope for the best. |
| 212 |
LO countFinePIDs = 0; |
| 213 |
LO countCoarsePIDs = 0; |
| 214 |
for (LO idx = 0; idx < maxRegPerGID; ++idx) { |
| 215 |
if (-1 < regionsPerGIDWithGhostsFine[idx][fineIdx]) { |
| 216 |
++countFinePIDs; |
| 217 |
} |
| 218 |
if (-1 < regionsPerGIDWithGhostsCoarse[idx][coarseIdx]) { |
| 219 |
++countCoarsePIDs; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (countCoarsePIDs < countFinePIDs) { |
| 224 |
for (LO idx = 0; idx < countFinePIDs; ++idx) { |
| 225 |
regionsPerGIDWithGhostsCoarse[idx][coarseIdx] = regionsPerGIDWithGhostsFine[idx][fineIdx]; |
| 226 |
if (regionsPerGIDWithGhostsCoarse[idx][coarseIdx] == myRank) { |
| 227 |
interfaceGIDsCoarse[idx][coarseIdx] = regRowMap->getGlobalElement(coarseIdx); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
Array<GO> fineRegionDuplicateCoarseLIDs(numFineDuplicateNodes); |
| 234 |
Array<GO> fineRegionDuplicateCoarseGIDs(numFineDuplicateNodes); |
| 235 |
for (size_t duplicateIdx = 0; duplicateIdx < numFineDuplicateNodes; ++duplicateIdx) { |
| 236 |
ArrayView<const LO> coarseRegionLID; // Should contain a single value |
| 237 |
ArrayView<const SC> dummyData; // Should contain a single value |
| 238 |
regProlong->getLocalRowView(fineDuplicateLIDs[duplicateIdx], |
| 239 |
coarseRegionLID, |
| 240 |
dummyData); |
| 241 |
fineRegionDuplicateCoarseLIDs[duplicateIdx] = regProlong->getColMap()->getGlobalElement(coarseRegionLID[0]); |
| 242 |
fineRegionDuplicateCoarseGIDs[duplicateIdx] = (coarseQuasiregionGIDs->getDataNonConst(0))[fineDuplicateLIDs[duplicateIdx]]; |
| 243 |
} |
| 244 |
|
| 245 |
// Create the coarseQuasiregRowMap, it will be based on the coarseRegRowMap |
| 246 |
LO countCoarseComposites = 0; |
| 247 |
coarseCompositeToRegionLIDs.resize(numCoarseRegionNodes); |
| 248 |
Array<GO> coarseQuasiregRowMapData = regProlong->getColMap()->getLocalElementList(); |
| 249 |
Array<GO> coarseCompRowMapData(numCoarseRegionNodes, -1); |
| 250 |
for (size_t regionIdx = 0; regionIdx < numCoarseRegionNodes; ++regionIdx) { |
| 251 |
const GO initialValue = coarseQuasiregRowMapData[regionIdx]; |
| 252 |
for (size_t duplicateIdx = 0; duplicateIdx < numFineDuplicateNodes; ++duplicateIdx) { |
| 253 |
if ((initialValue == fineRegionDuplicateCoarseLIDs[duplicateIdx]) && |
| 254 |
(fineRegionDuplicateCoarseGIDs[duplicateIdx] < coarseQuasiregRowMapData[regionIdx]) && |
| 255 |
(-1 < fineRegionDuplicateCoarseGIDs[duplicateIdx])) { |
| 256 |
coarseQuasiregRowMapData[regionIdx] = fineRegionDuplicateCoarseGIDs[duplicateIdx]; |
| 257 |
} |
| 258 |
} |
| 259 |
if (initialValue == coarseQuasiregRowMapData[regionIdx]) { |
| 260 |
coarseCompRowMapData[countCoarseComposites] = coarseQuasiregRowMapData[regionIdx]; |
| 261 |
coarseCompositeToRegionLIDs[countCoarseComposites] = regionIdx; |
| 262 |
++countCoarseComposites; |
| 263 |
} |
| 264 |
} |
| 265 |
coarseCompRowMapData.resize(countCoarseComposites); |
| 266 |
coarseCompositeToRegionLIDs.resize(countCoarseComposites); |
| 267 |
|
| 268 |
// We are now ready to fill up the outputs |
| 269 |
RCP<const Map> regRowMapCurrent = regProlong->getColMap(); |
| 270 |
|
| 271 |
RCP<Map> quasiRegRowMap = MapFactory::Build(regProlong->getColMap()->lib(), |
| 272 |
GO_INV, |
| 273 |
coarseQuasiregRowMapData(), |
| 274 |
regProlong->getColMap()->getIndexBase(), |
| 275 |
regProlong->getColMap()->getComm()); |
| 276 |
|
| 277 |
RCP<Map> compRowMap = MapFactory::Build(regProlong->getColMap()->lib(), |
| 278 |
GO_INV, |
| 279 |
coarseCompRowMapData(), |
| 280 |
regProlong->getColMap()->getIndexBase(), |
| 281 |
regProlong->getColMap()->getComm()); |
| 282 |
|
| 283 |
RCP<Import> regRowImportCurrent = ImportFactory::Build(compRowMap, quasiRegRowMap); |
| 284 |
|
| 285 |
// Now generate matvec data |
| 286 |
Teuchos::ArrayRCP<LocalOrdinal> regionMatVecLIDs; |
| 287 |
Teuchos::RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>> regionInterfaceImporter; |
| 288 |
SetupMatVec(interfaceGIDs, regionsPerGIDWithGhosts, |
| 289 |
regRowMapCurrent, regRowImportCurrent, |
| 290 |
regionMatVecLIDs, regionInterfaceImporter); |
| 291 |
|
| 292 |
// Fill level with the outputs |
| 293 |
level->Set<Teuchos::ArrayRCP<LO>>("regionMatVecLIDs", regionMatVecLIDs); |
| 294 |
level->Set<RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>>("regionInterfaceImporter", regionInterfaceImporter); |
| 295 |
level->Set<RCP<Xpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>>("interfaceGIDs", interfaceGIDs); |
| 296 |
level->Set<RCP<Xpetra::MultiVector<LO, LO, GO, NO>>>("regionsPerGIDWithGhosts", regionsPerGIDWithGhosts); |
| 297 |
level->Set<RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>>("rowImport", regRowImportCurrent); |
| 298 |
|
| 299 |
// Finally reset compositeToRegionLIDs |
| 300 |
compositeToRegionLIDs = coarseCompositeToRegionLIDs(); |
| 301 |
} // Loop over numLevels |
| 302 |
} // MakeCoarseLevelMaps |
| 303 |
|
| 304 |
// Form the composite coarse level operator |
| 305 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 306 |
void MakeCoarseCompositeOperator(RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& compRowMap, |
| 307 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& quasiRegRowMap, |
| 308 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& quasiRegColMap, |
| 309 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& regRowImporter, |
| 310 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& regMatrix, |
| 311 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>>& regCoarseCoordinates, |
| 312 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& coarseCompOp, |
| 313 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>>& compCoarseCoordinates, |
| 314 |
const bool makeCompCoords) { |
| 315 |
#include "Xpetra_UseShortNames.hpp" |
| 316 |
using CoordType = typename Teuchos::ScalarTraits<Scalar>::coordinateType; |
| 317 |
coarseCompOp = MatrixFactory::Build(compRowMap, |
| 318 |
// This estimate is very conservative and probably costs us lots of memory... |
| 319 |
8 * regMatrix->getCrsGraph()->getLocalMaxNumRowEntries()); |
| 320 |
regionalToComposite(regMatrix, |
| 321 |
quasiRegRowMap, |
| 322 |
quasiRegColMap, |
| 323 |
regRowImporter, Xpetra::ADD, |
| 324 |
coarseCompOp); |
| 325 |
|
| 326 |
const int dofsPerNode = regMatrix->GetFixedBlockSize(); |
| 327 |
coarseCompOp->SetFixedBlockSize(dofsPerNode); |
| 328 |
coarseCompOp->setObjectLabel("coarse composite operator"); |
| 329 |
|
| 330 |
// Create coarse composite coordinates for repartitioning |
| 331 |
if (makeCompCoords) { |
| 332 |
const int check = regMatrix->getRowMap()->getLocalNumElements() % regCoarseCoordinates->getMap()->getLocalNumElements(); |
| 333 |
TEUCHOS_ASSERT(check == 0); |
| 334 |
|
| 335 |
RCP<const Map> compCoordMap; |
| 336 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>> regCoordImporter; |
| 337 |
if (dofsPerNode == 1) { |
| 338 |
compCoordMap = compRowMap; |
| 339 |
regCoordImporter = regRowImporter; |
| 340 |
} else { |
| 341 |
using size_type = typename Teuchos::Array<GlobalOrdinal>::size_type; |
| 342 |
Array<GlobalOrdinal> compCoordMapData(compRowMap->getLocalNumElements() / dofsPerNode); |
| 343 |
ArrayView<const GlobalOrdinal> compRowMapData = compRowMap->getLocalElementList(); |
| 344 |
for (size_type nodeIdx = 0; nodeIdx < compCoordMapData.size(); ++nodeIdx) { |
| 345 |
compCoordMapData[nodeIdx] = compRowMapData[nodeIdx * dofsPerNode] / dofsPerNode; |
| 346 |
} |
| 347 |
compCoordMap = MapFactory::Build(compRowMap->lib(), |
| 348 |
compRowMap->getGlobalNumElements() / dofsPerNode, |
| 349 |
compCoordMapData(), |
| 350 |
compRowMap->getIndexBase(), |
| 351 |
compRowMap->getComm()); |
| 352 |
|
| 353 |
RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> quasiRegCoordMap; |
| 354 |
Array<GlobalOrdinal> quasiRegCoordMapData(quasiRegRowMap->getLocalNumElements() / dofsPerNode); |
| 355 |
ArrayView<const GlobalOrdinal> quasiRegRowMapData = quasiRegRowMap->getLocalElementList(); |
| 356 |
for (size_type nodeIdx = 0; nodeIdx < quasiRegCoordMapData.size(); ++nodeIdx) { |
| 357 |
quasiRegCoordMapData[nodeIdx] = quasiRegRowMapData[nodeIdx * dofsPerNode] / dofsPerNode; |
| 358 |
} |
| 359 |
quasiRegCoordMap = MapFactory::Build(quasiRegRowMap->lib(), |
| 360 |
quasiRegRowMap->getGlobalNumElements() / dofsPerNode, |
| 361 |
quasiRegCoordMapData(), |
| 362 |
quasiRegRowMap->getIndexBase(), |
| 363 |
quasiRegRowMap->getComm()); |
| 364 |
regCoordImporter = ImportFactory::Build(compCoordMap, quasiRegCoordMap); |
| 365 |
} |
| 366 |
compCoarseCoordinates = Xpetra::MultiVectorFactory<CoordType, LocalOrdinal, GlobalOrdinal, Node>::Build(compCoordMap, regCoarseCoordinates->getNumVectors()); |
| 367 |
TEUCHOS_ASSERT(Teuchos::nonnull(compCoarseCoordinates)); |
| 368 |
|
| 369 |
// The following looks like regionalToComposite for Vector |
| 370 |
// but it is a bit different since we do not want to add |
| 371 |
// entries in the coordinate MultiVector as we would for |
| 372 |
// a solution or residual vector. |
| 373 |
RCP<Xpetra::MultiVector<CoordType, LocalOrdinal, GlobalOrdinal, Node>> quasiRegCoarseCoordinates; |
| 374 |
quasiRegCoarseCoordinates = regCoarseCoordinates; |
| 375 |
TEUCHOS_ASSERT(Teuchos::nonnull(quasiRegCoarseCoordinates)); |
| 376 |
quasiRegCoarseCoordinates->replaceMap(regCoordImporter->getTargetMap()); |
| 377 |
compCoarseCoordinates->doExport(*quasiRegCoarseCoordinates, *(regCoordImporter), Xpetra::INSERT); |
| 378 |
} |
| 379 |
} // MakeCoarseCompositeOperator |
| 380 |
|
| 381 |
/* Create a direct solver for a composite operator |
| 382 |
* |
| 383 |
* Create the solver object and compute symbolic and numeric factorization. |
| 384 |
* Finally, the solver object will be ready to be applied during the V-cycle call. |
| 385 |
* |
| 386 |
* \note For now, we're limited to Tpetra/Amesos2. From Amesos2, we use KLU as direct solver. |
| 387 |
*/ |
| 388 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 389 |
RCP<Amesos2::Solver<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>> |
| 390 |
MakeCompositeDirectSolver(RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& compOp) { |
| 391 |
using Tpetra_CrsMatrix = Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 392 |
using Tpetra_MultiVector = Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 393 |
using Utilities = MueLu::Utilities<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 394 |
using Teuchos::TimeMonitor; |
| 395 |
|
| 396 |
RCP<Amesos2::Solver<Tpetra_CrsMatrix, Tpetra_MultiVector>> coarseSolver; |
| 397 |
{ |
| 398 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeCompositeDirectSolver: 1 - Setup"))); |
| 399 |
|
| 400 |
// convert matrix to Tpetra |
| 401 |
RCP<Tpetra_CrsMatrix> tMat = toTpetra(compOp); |
| 402 |
|
| 403 |
// Amesos2-specific key phrase that denote smoother type |
| 404 |
std::string amesos2SolverName = "KLU2"; |
| 405 |
TEUCHOS_ASSERT(Amesos2::query(amesos2SolverName)); |
| 406 |
coarseSolver = Amesos2::create<Tpetra_CrsMatrix, Tpetra_MultiVector>(amesos2SolverName, tMat); |
| 407 |
|
| 408 |
Teuchos::ParameterList amesos2_params("Amesos2"); |
| 409 |
amesos2_params.sublist(amesos2SolverName).set("IsContiguous", false, "Are GIDs Contiguous"); |
| 410 |
coarseSolver->setParameters(Teuchos::rcpFromRef(amesos2_params)); |
| 411 |
} |
| 412 |
|
| 413 |
{ |
| 414 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeCompositeDirectSolver: 2 - Factorization"))); |
| 415 |
|
| 416 |
coarseSolver->symbolicFactorization(); |
| 417 |
coarseSolver->numericFactorization(); |
| 418 |
} |
| 419 |
|
| 420 |
return coarseSolver; |
| 421 |
} // MakeCorseCompositeDirectSolver |
| 422 |
|
| 423 |
/* Rebalance coarse operator |
| 424 |
* |
| 425 |
*/ |
| 426 |
#if defined(HAVE_MUELU_ZOLTAN2) && defined(HAVE_MPI) |
| 427 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 428 |
void RebalanceCoarseCompositeOperator(const int rebalanceNumPartitions, |
| 429 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& coarseCompOp, |
| 430 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>>& compCoarseCoordinates, |
| 431 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& rebalancedCompOp, |
| 432 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>>& rebalancedCoordinates, |
| 433 |
RCP<const Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& rebalanceImporter) { |
| 434 |
#include "MueLu_UseShortNames.hpp" |
| 435 |
using CoordType = typename Teuchos::ScalarTraits<Scalar>::coordinateType; |
| 436 |
using Teuchos::TimeMonitor; |
| 437 |
|
| 438 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("RebalanceCoarseCompositeOperator: "))); |
| 439 |
|
| 440 |
RCP<Teuchos::FancyOStream> fos = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 441 |
fos->setOutputToRootOnly(0); |
| 442 |
*fos << "Rebalancing coarse composite operator." << std::endl; |
| 443 |
|
| 444 |
const int numPartitions = rebalanceNumPartitions; |
| 445 |
|
| 446 |
// We build a fake level |
| 447 |
Level level; |
| 448 |
level.SetLevelID(1); |
| 449 |
|
| 450 |
RCP<FactoryManagerBase> factoryHandler = rcp(new FactoryManager()); |
| 451 |
level.SetFactoryManager(factoryHandler); |
| 452 |
|
| 453 |
RCP<TimeMonitor> tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("rebalanceCoarse: Zoltan construction:"))); |
| 454 |
|
| 455 |
RCP<Zoltan2Interface> zoltan = rcp(new Zoltan2Interface()); |
| 456 |
|
| 457 |
level.Set<RCP<Matrix>>("A", coarseCompOp); |
| 458 |
level.Set<RCP<MultiVector>>("Coordinates", compCoarseCoordinates); |
| 459 |
// int numPartitions = Get<int>(level, "number of partitions"); |
| 460 |
|
| 461 |
RCP<RepartitionFactory> repart = rcp(new RepartitionFactory()); |
| 462 |
Teuchos::ParameterList paramList; |
| 463 |
paramList.set("repartition: remap parts", false); |
| 464 |
if (numPartitions > 0) { // If number of coarse rebalance partitions was provided by the user. |
| 465 |
level.Set<int>("number of partitions", numPartitions); |
| 466 |
} else { |
| 467 |
Teuchos::ParameterList paramListHeuristic; |
| 468 |
paramListHeuristic.set("repartition: start level", 1); |
| 469 |
RCP<RepartitionHeuristicFactory> repartHeuristic = rcp(new RepartitionHeuristicFactory()); |
| 470 |
repartHeuristic->SetParameterList(paramListHeuristic); |
| 471 |
repart->SetFactory("number of partitions", repartHeuristic); |
| 472 |
} |
| 473 |
repart->SetParameterList(paramList); |
| 474 |
repart->SetFactory("Partition", zoltan); |
| 475 |
|
| 476 |
// Build |
| 477 |
level.Request("Importer", repart.get()); |
| 478 |
repart->Build(level); |
| 479 |
|
| 480 |
tmLocal = Teuchos::null; |
| 481 |
|
| 482 |
// Build importer for rebalancing |
| 483 |
level.Get("Importer", rebalanceImporter, repart.get()); |
| 484 |
|
| 485 |
ParameterList XpetraList; |
| 486 |
XpetraList.set("Restrict Communicator", true); |
| 487 |
XpetraList.set("Timer Label", "MueLu::RebalanceAc-for-coarseAMG"); |
| 488 |
|
| 489 |
// Build rebalanced coarse composite operator |
| 490 |
rebalancedCompOp = MatrixFactory::Build(coarseCompOp, *rebalanceImporter, *rebalanceImporter, rebalanceImporter->getTargetMap(), rebalanceImporter->getTargetMap(), rcp(&XpetraList, false)); |
| 491 |
if (!rebalancedCompOp.is_null()) { |
| 492 |
rebalancedCompOp->SetFixedBlockSize(coarseCompOp->GetFixedBlockSize()); |
| 493 |
} |
| 494 |
|
| 495 |
// Build rebalanced coarse coordinates (The following code is borrowed from MueLu_RebalanceTransferFactory_def.hpp) |
| 496 |
LO blkSize = coarseCompOp->GetFixedBlockSize(); |
| 497 |
RCP<const Import> coordImporter; |
| 498 |
if (blkSize == 1) { |
| 499 |
coordImporter = rebalanceImporter; |
| 500 |
|
| 501 |
} else { |
| 502 |
// NOTE: there is an implicit assumption here: we assume that dof any node are enumerated consequently |
| 503 |
// Proper fix would require using decomposition similar to how we construct importer in the |
| 504 |
// RepartitionFactory |
| 505 |
RCP<const Map> origMap = compCoarseCoordinates->getMap(); |
| 506 |
GO indexBase = origMap->getIndexBase(); |
| 507 |
|
| 508 |
ArrayView<const GO> OEntries = rebalanceImporter->getTargetMap()->getLocalElementList(); |
| 509 |
LO numEntries = OEntries.size() / blkSize; |
| 510 |
ArrayRCP<GO> Entries(numEntries); |
| 511 |
for (LO i = 0; i < numEntries; i++) |
| 512 |
Entries[i] = (OEntries[i * blkSize] - indexBase) / blkSize + indexBase; |
| 513 |
|
| 514 |
RCP<const Map> targetMap = MapFactory::Build(origMap->lib(), origMap->getGlobalNumElements(), Entries(), indexBase, origMap->getComm()); |
| 515 |
coordImporter = ImportFactory::Build(origMap, targetMap); |
| 516 |
} |
| 517 |
rebalancedCoordinates = Xpetra::MultiVectorFactory<CoordType, LocalOrdinal, GlobalOrdinal, Node>::Build(coordImporter->getTargetMap(), compCoarseCoordinates->getNumVectors()); |
| 518 |
rebalancedCoordinates->doImport(*compCoarseCoordinates, *coordImporter, Xpetra::INSERT); |
| 519 |
rebalancedCoordinates->replaceMap(rebalancedCoordinates->getMap()->removeEmptyProcesses()); |
| 520 |
|
| 521 |
return; |
| 522 |
} // RebalanceCoarseCompositeOperator |
| 523 |
#endif |
| 524 |
|
| 525 |
/* Create an AMG hierarchy for a composite operator |
| 526 |
* |
| 527 |
* Create the hierarchy object and perform the multigrid setup. |
| 528 |
* Finally, the hierarhcy object will be ready to be applied during the region MG V-cycle call. |
| 529 |
*/ |
| 530 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 531 |
RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>> |
| 532 |
MakeCompositeAMGHierarchy(RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& compOp, |
| 533 |
const std::string& xmlFileName, |
| 534 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>> coordinates) { |
| 535 |
#include "MueLu_UseShortNames.hpp" |
| 536 |
using coordinates_type = typename Teuchos::ScalarTraits<Scalar>::coordinateType; |
| 537 |
|
| 538 |
const Scalar one = Teuchos::ScalarTraits<Scalar>::one(); |
| 539 |
|
| 540 |
RCP<Teuchos::FancyOStream> fos = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout)); |
| 541 |
fos->setOutputToRootOnly(0); |
| 542 |
*fos << "Attempting to setup AMG hierarchy for the composite coarse grid problem" << std::endl; |
| 543 |
|
| 544 |
// Get parameter list for AMG hierarchy |
| 545 |
RCP<ParameterList> mueluParams = Teuchos::rcp(new ParameterList()); |
| 546 |
Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFileName, mueluParams.ptr(), |
| 547 |
*compOp->getRowMap()->getComm()); |
| 548 |
|
| 549 |
// Get the user data sublist |
| 550 |
const std::string userName = "user data"; |
| 551 |
Teuchos::ParameterList& userParamList = mueluParams->sublist(userName); |
| 552 |
|
| 553 |
// Add nullspace information |
| 554 |
{ |
| 555 |
// Compute nullspace |
| 556 |
RCP<MultiVector> nullspace; |
| 557 |
if ((compOp->GetFixedBlockSize() == 1) || Teuchos::is_null(coordinates)) { // Scalar problem, constant nullspace |
| 558 |
nullspace = MultiVectorFactory::Build(compOp->getRowMap(), 1); |
| 559 |
nullspace->putScalar(one); |
| 560 |
} else if (compOp->GetFixedBlockSize() == 2) { // 2D Elasticity |
| 561 |
nullspace = MultiVectorFactory::Build(compOp->getRowMap(), 3); |
| 562 |
Array<ArrayRCP<SC>> nullspaceData(3); |
| 563 |
Array<ArrayRCP<const coordinates_type>> coordinateData(2); |
| 564 |
|
| 565 |
// Calculate center |
| 566 |
const coordinates_type cx = coordinates->getVector(0)->meanValue(); |
| 567 |
const coordinates_type cy = coordinates->getVector(1)->meanValue(); |
| 568 |
|
| 569 |
coordinateData[0] = coordinates->getData(0); |
| 570 |
coordinateData[1] = coordinates->getData(1); |
| 571 |
|
| 572 |
for (int vecIdx = 0; vecIdx < 3; ++vecIdx) { |
| 573 |
nullspaceData[vecIdx] = nullspace->getDataNonConst(vecIdx); |
| 574 |
} |
| 575 |
|
| 576 |
for (size_t nodeIdx = 0; nodeIdx < coordinates->getLocalLength(); ++nodeIdx) { |
| 577 |
// translations |
| 578 |
nullspaceData[0][2 * nodeIdx + 0] = one; |
| 579 |
nullspaceData[1][2 * nodeIdx + 1] = one; |
| 580 |
|
| 581 |
// rotation about z axis |
| 582 |
nullspaceData[2][2 * nodeIdx + 0] = -(coordinateData[1][nodeIdx] - cy); |
| 583 |
nullspaceData[2][2 * nodeIdx + 1] = (coordinateData[0][nodeIdx] - cx); |
| 584 |
} |
| 585 |
|
| 586 |
} else if (compOp->GetFixedBlockSize() == 3) { // 3D Elasticity |
| 587 |
nullspace = MultiVectorFactory::Build(compOp->getRowMap(), 6); |
| 588 |
Array<ArrayRCP<SC>> nullspaceData(6); |
| 589 |
Array<ArrayRCP<const coordinates_type>> coordinateData(3); |
| 590 |
|
| 591 |
// Calculate center |
| 592 |
const coordinates_type cx = coordinates->getVector(0)->meanValue(); |
| 593 |
const coordinates_type cy = coordinates->getVector(1)->meanValue(); |
| 594 |
const coordinates_type cz = coordinates->getVector(2)->meanValue(); |
| 595 |
|
| 596 |
coordinateData[0] = coordinates->getData(0); |
| 597 |
coordinateData[1] = coordinates->getData(1); |
| 598 |
coordinateData[2] = coordinates->getData(2); |
| 599 |
|
| 600 |
for (int vecIdx = 0; vecIdx < 6; ++vecIdx) { |
| 601 |
nullspaceData[vecIdx] = nullspace->getDataNonConst(vecIdx); |
| 602 |
} |
| 603 |
|
| 604 |
for (size_t nodeIdx = 0; nodeIdx < coordinates->getLocalLength(); ++nodeIdx) { |
| 605 |
// translations |
| 606 |
nullspaceData[0][3 * nodeIdx + 0] = one; |
| 607 |
nullspaceData[1][3 * nodeIdx + 1] = one; |
| 608 |
nullspaceData[2][3 * nodeIdx + 2] = one; |
| 609 |
|
| 610 |
// rotation about z axis |
| 611 |
nullspaceData[3][3 * nodeIdx + 0] = -(coordinateData[1][nodeIdx] - cy); |
| 612 |
nullspaceData[3][3 * nodeIdx + 1] = (coordinateData[0][nodeIdx] - cx); |
| 613 |
|
| 614 |
// rotation about x axis |
| 615 |
nullspaceData[4][3 * nodeIdx + 1] = -(coordinateData[2][nodeIdx] - cz); |
| 616 |
nullspaceData[4][3 * nodeIdx + 2] = (coordinateData[1][nodeIdx] - cy); |
| 617 |
|
| 618 |
// rotation about y axis |
| 619 |
nullspaceData[5][3 * nodeIdx + 0] = (coordinateData[2][nodeIdx] - cz); |
| 620 |
nullspaceData[5][3 * nodeIdx + 2] = -(coordinateData[0][nodeIdx] - cx); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
// Equalize norms of all vectors to that of the first one |
| 625 |
// We do not normalize them as a vector of ones seems nice |
| 626 |
Teuchos::Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> norms2(nullspace->getNumVectors()); |
| 627 |
nullspace->norm2(norms2); |
| 628 |
Teuchos::Array<Scalar> norms2scalar(nullspace->getNumVectors()); |
| 629 |
for (size_t vectorIdx = 0; vectorIdx < nullspace->getNumVectors(); ++vectorIdx) { |
| 630 |
norms2scalar[vectorIdx] = norms2[0] / norms2[vectorIdx]; |
| 631 |
} |
| 632 |
nullspace->scale(norms2scalar); |
| 633 |
|
| 634 |
// Insert into parameter list |
| 635 |
userParamList.set("Nullspace", nullspace); |
| 636 |
} |
| 637 |
|
| 638 |
// Add coordinate information for rebalancing |
| 639 |
{ |
| 640 |
if (Teuchos::nonnull(coordinates)) { |
| 641 |
userParamList.set("Coordinates", coordinates); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
// Create an AMG hierarchy based on the composite coarse level operator from the region MG scheme |
| 646 |
RCP<Hierarchy> compOpHiearchy = MueLu::CreateXpetraPreconditioner(compOp, *mueluParams); |
| 647 |
|
| 648 |
// We will use the hiearchy as a solver |
| 649 |
compOpHiearchy->IsPreconditioner(false); |
| 650 |
compOpHiearchy->SetVerbLevel(MueLu::VERB_NONE); |
| 651 |
|
| 652 |
return compOpHiearchy; |
| 653 |
} // MakeCompositeAMGHierarchy |
| 654 |
|
| 655 |
// Make interface scaling factors recursively |
| 656 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 657 |
void MakeInterfaceScalingFactors(const int numLevels, |
| 658 |
Teuchos::RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regHierarchy) { |
| 659 |
#include "Xpetra_UseShortNames.hpp" |
| 660 |
#include "MueLu_UseShortNames.hpp" |
| 661 |
|
| 662 |
const SC SC_ONE = Teuchos::ScalarTraits<SC>::one(); |
| 663 |
|
| 664 |
TEUCHOS_TEST_FOR_EXCEPT_MSG(!(numLevels > 0), "We require numLevel > 0. Probably, numLevel has not been set, yet."); |
| 665 |
|
| 666 |
for (int l = 0; l < numLevels; l++) { |
| 667 |
RCP<Level> level = regHierarchy->GetLevel(l); |
| 668 |
RCP<Matrix> regMat = level->Get<RCP<Matrix>>("A"); |
| 669 |
RCP<const Map> regRowMap = regMat->getRowMap(); |
| 670 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>> regRowImporters = level->Get<RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>>("rowImport"); |
| 671 |
// initialize region vector with all ones. |
| 672 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regInterfaceScalings = VectorFactory::Build(regRowMap); |
| 673 |
regInterfaceScalings->putScalar(SC_ONE); |
| 674 |
|
| 675 |
// transform to composite layout while adding interface values via the Export() combine mode |
| 676 |
RCP<Vector> compInterfaceScalingSum = VectorFactory::Build(regRowImporters->getSourceMap(), true); |
| 677 |
regionalToComposite(regInterfaceScalings, compInterfaceScalingSum, regRowImporters); |
| 678 |
|
| 679 |
/* transform composite layout back to regional layout. Now, GIDs associated |
| 680 |
* with region interface should carry a scaling factor (!= 1). |
| 681 |
*/ |
| 682 |
RCP<Vector> quasiRegInterfaceScaling; // Is that vector really needed? |
| 683 |
compositeToRegional(compInterfaceScalingSum, quasiRegInterfaceScaling, |
| 684 |
regInterfaceScalings, |
| 685 |
regRowMap, regRowImporters); |
| 686 |
|
| 687 |
level->Set<RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>>("regInterfaceScalings", regInterfaceScalings); |
| 688 |
} |
| 689 |
} // MakeInterfaceScalingFactors |
| 690 |
|
| 691 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 692 |
void createRegionHierarchy(const int numDimensions, |
| 693 |
const Array<int> lNodesPerDim, |
| 694 |
const std::string aggregationRegionType, |
| 695 |
RCP<Teuchos::ParameterList>& interfaceParams, |
| 696 |
const int maxRegPerGID, |
| 697 |
RCP<Teuchos::ParameterList>& coarseSolverData, |
| 698 |
Array<RCP<Teuchos::ParameterList>>& smootherParams, |
| 699 |
RCP<Teuchos::ParameterList> hierarchyData, |
| 700 |
RCP<MueLu::Hierarchy<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& regHierarchy, |
| 701 |
const bool keepCoarseCoords) { |
| 702 |
#include "Xpetra_UseShortNames.hpp" |
| 703 |
#include "MueLu_UseShortNames.hpp" |
| 704 |
using Teuchos::TimeMonitor; |
| 705 |
// This monitor times everything and gets the overall setting cost |
| 706 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy"))); |
| 707 |
|
| 708 |
using Hierarchy = MueLu::Hierarchy<SC, LO, GO, NO>; |
| 709 |
using Utilities = MueLu::Utilities<SC, LO, GO, NO>; |
| 710 |
using DirectCoarseSolver = Amesos2::Solver<Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>, Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>; |
| 711 |
|
| 712 |
// std::cout << mapComp->getComm()->getRank() << " | Setting up MueLu hierarchies ..." << std::endl; |
| 713 |
int numLevels = 0; |
| 714 |
|
| 715 |
RCP<TimeMonitor> tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy: ExtractData"))); |
| 716 |
// resize Arrays and vectors |
| 717 |
{ |
| 718 |
// resize level containers |
| 719 |
numLevels = regHierarchy->GetNumLevels(); |
| 720 |
smootherParams.resize(numLevels); |
| 721 |
|
| 722 |
// resize group containers on each level |
| 723 |
for (int l = 0; l < numLevels; ++l) { |
| 724 |
// Also doing some initialization in the smootherParams |
| 725 |
if (l > 0) { |
| 726 |
smootherParams[l] = rcp(new Teuchos::ParameterList(*smootherParams[0])); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
RCP<Level> level0 = regHierarchy->GetLevel(0); |
| 732 |
RCP<Matrix> regMat = level0->Get<RCP<Matrix>>("A"); |
| 733 |
RCP<const Map> revisedRowMap = regMat->getRowMap(); |
| 734 |
/* Get coarse level matrices and prolongators from MueLu hierarchy |
| 735 |
* Note: fine level has been dealt with previously, so we start at level 1 here. |
| 736 |
*/ |
| 737 |
using real_type = typename Teuchos::ScalarTraits<Scalar>::coordinateType; |
| 738 |
using realvaluedmultivector_type = Xpetra::MultiVector<real_type, LocalOrdinal, GlobalOrdinal, Node>; |
| 739 |
RCP<realvaluedmultivector_type> regCoarseCoordinates; |
| 740 |
for (int l = 1; l < numLevels; ++l) { // Note: we start at level 1 (which is the first coarse level) |
| 741 |
RCP<Level> level = regHierarchy->GetLevel(l); |
| 742 |
|
| 743 |
if (keepCoarseCoords && (l == numLevels - 1)) { |
| 744 |
regCoarseCoordinates = level->Get<RCP<realvaluedmultivector_type>>("Coordinates2", MueLu::NoFactory::get()); |
| 745 |
} |
| 746 |
|
| 747 |
RCP<Matrix> regMatrices = level->Get<RCP<Matrix>>("A", MueLu::NoFactory::get()); |
| 748 |
|
| 749 |
// Create residual and solution vectors and cache them for vCycle apply |
| 750 |
std::string levelName("level"); |
| 751 |
levelName += std::to_string(l); |
| 752 |
ParameterList& levelList = hierarchyData->sublist(levelName, false, "list of data on current level"); |
| 753 |
RCP<Vector> regRes = VectorFactory::Build(revisedRowMap, true); |
| 754 |
RCP<Vector> regSol = VectorFactory::Build(revisedRowMap, true); |
| 755 |
|
| 756 |
levelList.set<RCP<Vector>>("residual", regRes, "Cached residual vector"); |
| 757 |
levelList.set<RCP<Vector>>("solution", regSol, "Cached solution vector"); |
| 758 |
} |
| 759 |
|
| 760 |
// std::cout << mapComp->getComm()->getRank() << " | MakeCoarseLevelMaps ..." << std::endl; |
| 761 |
|
| 762 |
tmLocal = Teuchos::null; |
| 763 |
tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy: MakeCoarseLevel"))); |
| 764 |
|
| 765 |
MakeCoarseLevelMaps(maxRegPerGID, |
| 766 |
regHierarchy); |
| 767 |
|
| 768 |
tmLocal = Teuchos::null; |
| 769 |
tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy: MakeInterfaceScaling"))); |
| 770 |
|
| 771 |
MakeInterfaceScalingFactors(numLevels, |
| 772 |
regHierarchy); |
| 773 |
|
| 774 |
// std::cout << mapComp->getComm()->getRank() << " | Setup smoothers ..." << std::endl; |
| 775 |
|
| 776 |
tmLocal = Teuchos::null; |
| 777 |
tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy: SmootherSetup"))); |
| 778 |
// Only set the smoother up to the last but one level |
| 779 |
// if we want to use a smoother on the coarse level |
| 780 |
// we will handle that separately with "coarse solver type" |
| 781 |
for (int levelIdx = 0; levelIdx < numLevels - 1; ++levelIdx) { |
| 782 |
RCP<Level> level = regHierarchy->GetLevel(levelIdx); |
| 783 |
RCP<Matrix> regMatrix = level->Get<RCP<Matrix>>("A", MueLu::NoFactory::get()); |
| 784 |
RCP<const Map> regRowMap = regMatrix->getRowMap(); |
| 785 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>> regRowImporter = level->Get<RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>>("rowImport"); |
| 786 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regInterfaceScalings = level->Get<RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>>("regInterfaceScalings"); |
| 787 |
|
| 788 |
smootherParams[levelIdx]->set("smoother: level", levelIdx); |
| 789 |
smootherSetup(smootherParams[levelIdx], regRowMap, |
| 790 |
regMatrix, regInterfaceScalings, |
| 791 |
regRowImporter); |
| 792 |
} |
| 793 |
|
| 794 |
// std::cout << mapComp->getComm()->getRank() << " | CreateCoarseSolver ..." << std::endl; |
| 795 |
|
| 796 |
tmLocal = Teuchos::null; |
| 797 |
tmLocal = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("createRegionHierarchy: CreateCoarseSolver"))); |
| 798 |
const std::string coarseSolverType = coarseSolverData->get<std::string>("coarse solver type"); |
| 799 |
if (coarseSolverType == "smoother") { |
| 800 |
RCP<Level> level = regHierarchy->GetLevel(numLevels - 1); |
| 801 |
RCP<Matrix> regMatrix = level->Get<RCP<Matrix>>("A", MueLu::NoFactory::get()); |
| 802 |
RCP<const Map> regRowMap = regMatrix->getRowMap(); |
| 803 |
RCP<Import> regRowImporter = level->Get<RCP<Import>>("rowImport"); |
| 804 |
RCP<Vector> regInterfaceScalings = level->Get<RCP<Vector>>("regInterfaceScalings"); |
| 805 |
|
| 806 |
// Set the smoother on the coarsest level. |
| 807 |
const std::string smootherXMLFileName = coarseSolverData->get<std::string>("smoother xml file"); |
| 808 |
RCP<ParameterList> coarseSmootherParams = smootherParams[numLevels - 1]; |
| 809 |
Teuchos::updateParametersFromXmlFileAndBroadcast(smootherXMLFileName, coarseSmootherParams.ptr(), *level->GetComm()); |
| 810 |
coarseSmootherParams->set("smoother: level", numLevels - 1); |
| 811 |
coarseSmootherParams->print(); |
| 812 |
|
| 813 |
smootherSetup(smootherParams[numLevels - 1], regRowMap, |
| 814 |
regMatrix, regInterfaceScalings, |
| 815 |
regRowImporter); |
| 816 |
} else if ((coarseSolverType == "direct") || (coarseSolverType == "amg")) { |
| 817 |
// A composite coarse matrix is needed |
| 818 |
|
| 819 |
// std::cout << mapComp->getComm()->getRank() << " | MakeCoarseCompositeOperator ..." << std::endl; |
| 820 |
|
| 821 |
RCP<Level> level = regHierarchy->GetLevel(numLevels - 1); |
| 822 |
RCP<Matrix> regMatrix = level->Get<RCP<Matrix>>("A", MueLu::NoFactory::get()); |
| 823 |
RCP<Import> regRowImporter = level->Get<RCP<Import>>("rowImport"); |
| 824 |
RCP<const Map> compRowMap = regRowImporter->getSourceMap(); |
| 825 |
RCP<const Map> quasiRegRowMap = regRowImporter->getTargetMap(); |
| 826 |
RCP<const Map> quasiRegColMap = regRowImporter->getTargetMap(); // col map same as row map. |
| 827 |
|
| 828 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> coarseCompOp; |
| 829 |
RCP<realvaluedmultivector_type> compCoarseCoordinates; |
| 830 |
MakeCoarseCompositeOperator(compRowMap, |
| 831 |
quasiRegRowMap, |
| 832 |
quasiRegColMap, |
| 833 |
regRowImporter, |
| 834 |
regMatrix, |
| 835 |
regCoarseCoordinates, |
| 836 |
coarseCompOp, |
| 837 |
compCoarseCoordinates, |
| 838 |
keepCoarseCoords); |
| 839 |
|
| 840 |
coarseSolverData->set<RCP<const Map>>("compCoarseRowMap", coarseCompOp->getRowMap()); |
| 841 |
|
| 842 |
// std::cout << mapComp->getComm()->getRank() << " | MakeCoarseCompositeSolver ..." << std::endl; |
| 843 |
if (coarseSolverType == "direct") { |
| 844 |
RCP<DirectCoarseSolver> coarseDirectSolver = MakeCompositeDirectSolver(coarseCompOp); |
| 845 |
coarseSolverData->set<RCP<DirectCoarseSolver>>("direct solver object", coarseDirectSolver); |
| 846 |
} else if (coarseSolverType == "amg") { |
| 847 |
if (keepCoarseCoords == false) { |
| 848 |
std::cout << "WARNING: you requested a coarse AMG solver but you did not request coarse coordinates to be kept, repartitioning is not possible!" << std::endl; |
| 849 |
} |
| 850 |
|
| 851 |
RCP<Hierarchy> coarseAMGHierarchy; |
| 852 |
std::string amgXmlFileName = coarseSolverData->get<std::string>("amg xml file"); |
| 853 |
#if defined(HAVE_MUELU_ZOLTAN2) && defined(HAVE_MPI) |
| 854 |
const bool coarseSolverRebalance = coarseSolverData->get<bool>("coarse solver rebalance"); |
| 855 |
if (keepCoarseCoords == true && coarseSolverRebalance == true) { |
| 856 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> rebalancedCompOp; |
| 857 |
RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::coordinateType, LocalOrdinal, GlobalOrdinal, Node>> rebalancedCoordinates; |
| 858 |
RCP<const Import> rebalanceImporter; |
| 859 |
|
| 860 |
const int rebalanceNumPartitions = coarseSolverData->get<int>("coarse rebalance num partitions"); |
| 861 |
RebalanceCoarseCompositeOperator(rebalanceNumPartitions, |
| 862 |
coarseCompOp, |
| 863 |
compCoarseCoordinates, |
| 864 |
rebalancedCompOp, |
| 865 |
rebalancedCoordinates, |
| 866 |
rebalanceImporter); |
| 867 |
coarseSolverData->set<RCP<const Import>>("rebalanceImporter", rebalanceImporter); |
| 868 |
|
| 869 |
if (!rebalancedCompOp.is_null()) |
| 870 |
coarseAMGHierarchy = MakeCompositeAMGHierarchy(rebalancedCompOp, amgXmlFileName, rebalancedCoordinates); |
| 871 |
|
| 872 |
} else { |
| 873 |
coarseAMGHierarchy = MakeCompositeAMGHierarchy(coarseCompOp, amgXmlFileName, compCoarseCoordinates); |
| 874 |
} |
| 875 |
#else |
| 876 |
coarseAMGHierarchy = MakeCompositeAMGHierarchy(coarseCompOp, amgXmlFileName, compCoarseCoordinates); |
| 877 |
#endif |
| 878 |
coarseSolverData->set<RCP<Hierarchy>>("amg hierarchy object", coarseAMGHierarchy); |
| 879 |
} |
| 880 |
} else { |
| 881 |
TEUCHOS_TEST_FOR_EXCEPT_MSG(false, "Unknown coarse solver type."); |
| 882 |
} |
| 883 |
|
| 884 |
} // createRegionHierarchy |
| 885 |
|
| 886 |
#endif // MUELU_SETUPREGIONHIERARCHY_DEF_HPP |