| 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_SETUPREGIONMATRIX_DEF_HPP |
| 11 |
#define MUELU_SETUPREGIONMATRIX_DEF_HPP |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
#include <iostream> |
| 15 |
|
| 16 |
#define RegionsSpanProcs 1 |
| 17 |
#define MultipleRegionsPerProc 2 |
| 18 |
|
| 19 |
#include <Tpetra_KokkosCompat_DefaultNode.hpp> |
| 20 |
#include <KokkosSparse_spmv.hpp> |
| 21 |
|
| 22 |
#include <Xpetra_ConfigDefs.hpp> |
| 23 |
#include <Xpetra_Export.hpp> |
| 24 |
#include <Xpetra_Import.hpp> |
| 25 |
#include <Xpetra_Map.hpp> |
| 26 |
#include <Xpetra_MultiVector.hpp> |
| 27 |
#include <Xpetra_Vector.hpp> |
| 28 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 29 |
|
| 30 |
using Teuchos::Array; |
| 31 |
using Teuchos::ArrayRCP; |
| 32 |
using Teuchos::RCP; |
| 33 |
|
| 34 |
/*! \brief Find common regions of two nodes |
| 35 |
* |
| 36 |
*/ |
| 37 |
template <class LocalOrdinal, class GlobalOrdinal, class Node> |
| 38 |
Teuchos::Array<int> findCommonRegions(const GlobalOrdinal nodeA, ///< GID of first node |
| 39 |
const GlobalOrdinal nodeB, ///< GID of second node |
| 40 |
const Array<ArrayRCP<const LocalOrdinal>> nodesToRegions, ///< mapping of nodes to regions |
| 41 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> nodesToRegionsMap) { |
| 42 |
#include "Xpetra_UseShortNamesOrdinal.hpp" |
| 43 |
using Teuchos::TimeMonitor; |
| 44 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("findCommonRegions: 1 - Extract regions"))); |
| 45 |
|
| 46 |
// extract node-to-regions mapping for both nodes A and B |
| 47 |
Array<int> regionsA, regionsB; |
| 48 |
{ |
| 49 |
LO nodeALID = nodesToRegionsMap->getLocalElement(nodeA); |
| 50 |
LO nodeBLID = nodesToRegionsMap->getLocalElement(nodeB); |
| 51 |
for (int i = 0; i < nodesToRegions.size(); ++i) { |
| 52 |
regionsA.push_back(nodesToRegions[i][nodeALID]); |
| 53 |
regionsB.push_back(nodesToRegions[i][nodeBLID]); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
tm = Teuchos::null; |
| 58 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("findCommonRegions: 2 - Sort regions"))); |
| 59 |
|
| 60 |
// identify common regions |
| 61 |
std::vector<int> commonRegions(nodesToRegions.size()); |
| 62 |
std::sort(regionsA.begin(), regionsA.end()); |
| 63 |
std::sort(regionsB.begin(), regionsB.end()); |
| 64 |
|
| 65 |
tm = Teuchos::null; |
| 66 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("findCommonRegions: 3 - Find commons"))); |
| 67 |
|
| 68 |
std::vector<int>::iterator it = std::set_intersection(regionsA.begin(), |
| 69 |
regionsA.end(), regionsB.begin(), regionsB.end(), commonRegions.begin()); |
| 70 |
commonRegions.resize(it - commonRegions.begin()); |
| 71 |
|
| 72 |
tm = Teuchos::null; |
| 73 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("findCommonRegions: 4 - Clean-up output"))); |
| 74 |
|
| 75 |
// remove '-1' entries |
| 76 |
Teuchos::Array<int> finalCommonRegions; |
| 77 |
for (std::size_t i = 0; i < commonRegions.size(); ++i) { |
| 78 |
if (commonRegions[i] != -1) |
| 79 |
finalCommonRegions.push_back(commonRegions[i]); |
| 80 |
} |
| 81 |
|
| 82 |
tm = Teuchos::null; |
| 83 |
|
| 84 |
return finalCommonRegions; |
| 85 |
} |
| 86 |
|
| 87 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 88 |
void MakeQuasiregionMatrices(const RCP<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>> AComp, |
| 89 |
RCP<Xpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>> regionsPerGIDWithGhosts, |
| 90 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> rowMap, |
| 91 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> colMap, |
| 92 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& rowImport, |
| 93 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& quasiRegionMats, |
| 94 |
const Teuchos::ArrayRCP<LocalOrdinal>& regionMatVecLIDs) { |
| 95 |
#include "Xpetra_UseShortNames.hpp" |
| 96 |
using Teuchos::RCP; |
| 97 |
using Teuchos::TimeMonitor; |
| 98 |
|
| 99 |
Array<ArrayRCP<const LO>> regionPerGIDWithGhostsData(regionsPerGIDWithGhosts->getNumVectors()); |
| 100 |
for (size_t vecIdx = 0; vecIdx < regionsPerGIDWithGhosts->getNumVectors(); ++vecIdx) { |
| 101 |
regionPerGIDWithGhostsData[vecIdx] = regionsPerGIDWithGhosts->getData(vecIdx); |
| 102 |
} |
| 103 |
|
| 104 |
/* We use the edge-based splitting, i.e. we first modify off-diagonal |
| 105 |
* entries in the composite matrix, then decompose it into region matrices |
| 106 |
* and finally take care of diagonal entries by enforcing the nullspace |
| 107 |
* preservation constraint. |
| 108 |
*/ |
| 109 |
|
| 110 |
// Import data from AComp into the quasiRegion matrices |
| 111 |
// Since the stencil size does not grow between composite and region format |
| 112 |
// use AComp->getCrsGraph()->getLocalMaxNumRowEntries() to get an upper |
| 113 |
// bound of the number of nonzeros per row in quasiRegionMat |
| 114 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeQuasiregionMatrices: 1 - Create Matrix"))); |
| 115 |
|
| 116 |
quasiRegionMats = MatrixFactory::Build(rowMap, |
| 117 |
colMap, |
| 118 |
AComp->getCrsGraph()->getLocalMaxNumRowEntries()); |
| 119 |
|
| 120 |
tm = Teuchos::null; |
| 121 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeQuasiregionMatrices: 2 - Import data"))); |
| 122 |
|
| 123 |
quasiRegionMats->doImport(*AComp, |
| 124 |
*(rowImport), |
| 125 |
Xpetra::INSERT); |
| 126 |
|
| 127 |
tm = Teuchos::null; |
| 128 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeQuasiregionMatrices: 3 - Scale interface entries"))); |
| 129 |
|
| 130 |
RCP<CrsMatrixWrap> quasiRegionCrsWrap = Teuchos::rcp_dynamic_cast<CrsMatrixWrap>(quasiRegionMats); |
| 131 |
RCP<CrsMatrix> quasiRegionCrs = quasiRegionCrsWrap->getCrsMatrix(); |
| 132 |
|
| 133 |
// Grab first and last element of sorted interface LIDs |
| 134 |
Array<LO> interfaceLIDs(regionMatVecLIDs()); |
| 135 |
std::sort(interfaceLIDs.begin(), interfaceLIDs.end()); |
| 136 |
auto vecEnd = std::unique(interfaceLIDs.begin(), interfaceLIDs.end()); |
| 137 |
auto vecStart = interfaceLIDs.begin(); |
| 138 |
|
| 139 |
GO rowGID; |
| 140 |
LocalOrdinal col; |
| 141 |
GlobalOrdinal colGID; |
| 142 |
std::size_t sizeOfCommonRegions; |
| 143 |
std::size_t numEntries = 0; |
| 144 |
for (auto row = vecStart; row < vecEnd; ++row) { |
| 145 |
rowGID = rowMap->getGlobalElement(*row); |
| 146 |
numEntries = quasiRegionMats->getNumEntriesInLocalRow(*row); // number of entries in this row |
| 147 |
Array<SC> values(numEntries); // non-zeros in this row |
| 148 |
Array<LO> colInds(numEntries); // local column indices |
| 149 |
quasiRegionMats->getLocalRowCopy(*row, colInds, values, numEntries); |
| 150 |
|
| 151 |
for (std::size_t entryIdx = 0; entryIdx < numEntries; ++entryIdx) { // loop over all entries in this row |
| 152 |
col = colInds[entryIdx]; |
| 153 |
colGID = colMap->getGlobalElement(col); |
| 154 |
Array<int> commonRegions; |
| 155 |
if (rowGID != colGID) { // Skip the diagonal entry. It will be processed later. |
| 156 |
commonRegions = findCommonRegions(rowGID, colGID, regionPerGIDWithGhostsData, regionsPerGIDWithGhosts->getMap()); |
| 157 |
} |
| 158 |
|
| 159 |
sizeOfCommonRegions = commonRegions.size(); |
| 160 |
if (1 < sizeOfCommonRegions) { |
| 161 |
values[entryIdx] /= Teuchos::as<double>(sizeOfCommonRegions); |
| 162 |
} |
| 163 |
} |
| 164 |
quasiRegionMats->replaceLocalValues(*row, colInds, values); |
| 165 |
} |
| 166 |
|
| 167 |
tm = Teuchos::null; |
| 168 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeQuasiregionMatrices: 4 - fillComplete"))); |
| 169 |
|
| 170 |
quasiRegionMats->fillComplete(); |
| 171 |
|
| 172 |
tm = Teuchos::null; |
| 173 |
} // MakeQuasiregionMatrices |
| 174 |
|
| 175 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 176 |
void MakeRegionMatrices(const RCP<Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>> AComp, |
| 177 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> mapComp, |
| 178 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> rowMap, |
| 179 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> revisedRowMap, |
| 180 |
RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> revisedColMap, |
| 181 |
RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& rowImport, |
| 182 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& quasiRegionMats, |
| 183 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& regionMats) { |
| 184 |
#include "Xpetra_UseShortNames.hpp" |
| 185 |
using Teuchos::RCP; |
| 186 |
using Teuchos::TimeMonitor; |
| 187 |
const SC SC_ONE = Teuchos::ScalarTraits<SC>::one(); |
| 188 |
const SC SC_ZERO = Teuchos::ScalarTraits<SC>::zero(); |
| 189 |
|
| 190 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeRegionMatrices: 1 - Create Matrix"))); |
| 191 |
|
| 192 |
// Copy data from quasiRegionMats, but into new map layout |
| 193 |
{ |
| 194 |
regionMats = rcp(new CrsMatrixWrap(revisedRowMap, revisedColMap, 9)); |
| 195 |
|
| 196 |
// Extract current region CrsMatrix |
| 197 |
RCP<CrsMatrix> regionCrsMat = toCrsMatrix(regionMats); |
| 198 |
|
| 199 |
// Extract current quasi-region CrsMatrix |
| 200 |
RCP<CrsMatrix> quasiRegionCrsMat = toCrsMatrix(quasiRegionMats); |
| 201 |
|
| 202 |
// Pull out the data from the quasi-region CrsMatrix |
| 203 |
ArrayRCP<const size_t> rowptrQuasiRegion; |
| 204 |
ArrayRCP<const LocalOrdinal> colindQuasiRegion; |
| 205 |
ArrayRCP<const Scalar> valuesQuasiRegion; |
| 206 |
quasiRegionCrsMat->getAllValues(rowptrQuasiRegion, colindQuasiRegion, valuesQuasiRegion); |
| 207 |
|
| 208 |
// Do a deep copy of values |
| 209 |
// (at least we've been doing deep copies so far, maybe we could do shallow copies to save time?) |
| 210 |
ArrayRCP<size_t> rowptrRegion(rowptrQuasiRegion.size()); |
| 211 |
ArrayRCP<LocalOrdinal> colindRegion(colindQuasiRegion.size()); |
| 212 |
ArrayRCP<Scalar> valuesRegion(valuesQuasiRegion.size()); |
| 213 |
|
| 214 |
regionCrsMat->allocateAllValues(valuesRegion.size(), rowptrRegion, colindRegion, valuesRegion); |
| 215 |
|
| 216 |
for (LocalOrdinal idx = 0; idx < static_cast<LocalOrdinal>(rowptrRegion.size()); ++idx) { |
| 217 |
rowptrRegion[idx] = rowptrQuasiRegion[idx]; |
| 218 |
} |
| 219 |
|
| 220 |
for (LocalOrdinal idx = 0; idx < static_cast<LocalOrdinal>(colindRegion.size()); ++idx) { |
| 221 |
colindRegion[idx] = colindQuasiRegion[idx]; |
| 222 |
valuesRegion[idx] = valuesQuasiRegion[idx]; |
| 223 |
} |
| 224 |
|
| 225 |
// Set and fillComplete the region CrsMatrix |
| 226 |
regionCrsMat->setAllValues(rowptrRegion, colindRegion, valuesRegion); |
| 227 |
regionCrsMat->expertStaticFillComplete(revisedRowMap, revisedRowMap); |
| 228 |
} |
| 229 |
|
| 230 |
tm = Teuchos::null; |
| 231 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeRegionMatrices: 2 - Enforce nullspace constraint"))); |
| 232 |
|
| 233 |
// enforce nullspace constraint |
| 234 |
RCP<Vector> regNspViolation; |
| 235 |
{ |
| 236 |
// compute violation of nullspace property close to DBCs |
| 237 |
RCP<Vector> nspVec = VectorFactory::Build(mapComp); |
| 238 |
nspVec->putScalar(SC_ONE); |
| 239 |
RCP<Vector> nspViolation = VectorFactory::Build(mapComp, true); |
| 240 |
AComp->apply(*nspVec, *nspViolation); |
| 241 |
|
| 242 |
// move to regional layout |
| 243 |
RCP<Vector> quasiRegNspViolation = VectorFactory::Build(rowMap, true); |
| 244 |
regNspViolation = VectorFactory::Build(revisedRowMap, true); |
| 245 |
compositeToRegional(nspViolation, quasiRegNspViolation, regNspViolation, |
| 246 |
revisedRowMap, rowImport); |
| 247 |
|
| 248 |
/* The nullspace violation computed in the composite layout needs to be |
| 249 |
* transfered to the regional layout. Since we use this to compute |
| 250 |
* the splitting of the diagonal values, we need to split the nullspace |
| 251 |
* violation. We'd like to use the 'regInterfaceScaling', though this is |
| 252 |
* not setup at this point. So, let's compute it right now. |
| 253 |
* |
| 254 |
* ToDo: Move setup of 'regInterfaceScaling' up front to use it here. |
| 255 |
*/ |
| 256 |
{ |
| 257 |
// initialize region vector with all ones. |
| 258 |
RCP<Vector> interfaceScaling = VectorFactory::Build(revisedRowMap); |
| 259 |
interfaceScaling->putScalar(SC_ONE); |
| 260 |
|
| 261 |
// transform to composite layout while adding interface values via the Export() combine mode |
| 262 |
RCP<Vector> compInterfaceScalingSum = VectorFactory::Build(mapComp, true); |
| 263 |
regionalToComposite(interfaceScaling, compInterfaceScalingSum, rowImport); |
| 264 |
|
| 265 |
/* transform composite layout back to regional layout. Now, GIDs associated |
| 266 |
* with region interface should carry a scaling factor (!= 1). |
| 267 |
*/ |
| 268 |
RCP<Vector> quasiRegInterfaceScaling; |
| 269 |
compositeToRegional(compInterfaceScalingSum, quasiRegInterfaceScaling, |
| 270 |
interfaceScaling, |
| 271 |
revisedRowMap, rowImport); |
| 272 |
|
| 273 |
// modify its interface entries |
| 274 |
RCP<Vector> inverseInterfaceScaling = VectorFactory::Build(interfaceScaling->getMap(), true); |
| 275 |
inverseInterfaceScaling->reciprocal(*interfaceScaling); |
| 276 |
regNspViolation->elementWiseMultiply(SC_ONE, *regNspViolation, *inverseInterfaceScaling, SC_ZERO); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
tm = Teuchos::null; |
| 281 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("MakeRegionMatrices: 3 - Replace diagonal"))); |
| 282 |
|
| 283 |
RCP<Vector> regNsp; |
| 284 |
regNsp = VectorFactory::Build(revisedRowMap); |
| 285 |
regNsp->putScalar(SC_ONE); |
| 286 |
|
| 287 |
RCP<Vector> regCorrection; |
| 288 |
regCorrection = VectorFactory::Build(revisedRowMap, true); |
| 289 |
regionMats->apply(*regNsp, *regCorrection); |
| 290 |
regionMats->SetFixedBlockSize(AComp->GetFixedBlockSize()); |
| 291 |
|
| 292 |
RCP<Vector> regDiag = Teuchos::null; |
| 293 |
regDiag = VectorFactory::Build(revisedRowMap, true); |
| 294 |
regionMats->getLocalDiagCopy(*regDiag); |
| 295 |
regDiag->update(-SC_ONE, *regCorrection, SC_ONE, *regNspViolation, SC_ONE); |
| 296 |
|
| 297 |
// Extract current region matrix in as CrsMatrix |
| 298 |
RCP<CrsMatrix> regionCrsMat = toCrsMatrix(regionMats); |
| 299 |
regionCrsMat->replaceDiag(*regDiag); |
| 300 |
|
| 301 |
tm = Teuchos::null; |
| 302 |
} // MakeRegionMatrices |
| 303 |
|
| 304 |
/*! \brief Transform regional matrix to composite layout |
| 305 |
* |
| 306 |
* Starting from a \c Matrix in regional layout, we |
| 307 |
* 1. copy data from regional matrix into quasiRegional matrix and set all maps |
| 308 |
* to be quasiRegional maps |
| 309 |
* 2. export it into a \c Matrix with composite layout using the given \c combineMode. |
| 310 |
* Note: on-process values also have to be added to account for region interfaces inside a process. |
| 311 |
* |
| 312 |
* \note We also need the capability to add processor-local values. This is not supported by |
| 313 |
* available CombineMode options in Xpetra/Tpetra, so we use a manual implementation here. |
| 314 |
* |
| 315 |
* \return Composite matrix that is fill-completed |
| 316 |
*/ |
| 317 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 318 |
void regionalToComposite(const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& regMat, ///< Matrix in region layout [in] |
| 319 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> rowMap, ///< row maps in quasiRegion layout [in] |
| 320 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>> colMap, ///< col maps in quasiRegion layout [in] |
| 321 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>> rowImport, ///< row importer in region layout [in] |
| 322 |
const Xpetra::CombineMode combineMode, ///< Combine mode for import/export [in] |
| 323 |
RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& compMat ///< Matrix in composite layout [in/out] |
| 324 |
) { |
| 325 |
#include "Xpetra_UseShortNames.hpp" |
| 326 |
using std::size_t; |
| 327 |
using Teuchos::rcp; |
| 328 |
using Teuchos::TimeMonitor; |
| 329 |
|
| 330 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: Matrix"))); |
| 331 |
|
| 332 |
const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 333 |
const SC SC_ONE = Teuchos::ScalarTraits<Scalar>::one(); |
| 334 |
|
| 335 |
// Make sure we add into an zero composite matrix |
| 336 |
compMat->setAllToScalar(SC_ZERO); |
| 337 |
|
| 338 |
/* Let's fake an ADD combine mode that also adds local values by |
| 339 |
* 1. exporting quasiRegional matrices to auxiliary composite matrices (1 per group) |
| 340 |
* 2. add all auxiliary matrices together |
| 341 |
*/ |
| 342 |
|
| 343 |
// Copy data from regMat into quasiRegMat |
| 344 |
RCP<Matrix> quasiRegMat; |
| 345 |
{ |
| 346 |
quasiRegMat = rcp(new CrsMatrixWrap(rowMap, |
| 347 |
colMap, |
| 348 |
regMat->getCrsGraph()->getLocalMaxNumRowEntries())); |
| 349 |
|
| 350 |
// Extract current quasi-region CrsMatrix |
| 351 |
RCP<CrsMatrix> quasiRegionCrsMat = toCrsMatrix(quasiRegMat); |
| 352 |
|
| 353 |
// Extract current region CrsMatrix |
| 354 |
RCP<CrsMatrix> regionCrsMat = toCrsMatrix(regMat); |
| 355 |
|
| 356 |
// Pull out the data from the region CrsMatrix |
| 357 |
Teuchos::ArrayRCP<const size_t> rowptrRegion; |
| 358 |
Teuchos::ArrayRCP<const LocalOrdinal> colindRegion; |
| 359 |
Teuchos::ArrayRCP<const Scalar> valuesRegion; |
| 360 |
regionCrsMat->getAllValues(rowptrRegion, colindRegion, valuesRegion); |
| 361 |
|
| 362 |
// Do a deep copy of values |
| 363 |
// (at least we've been doing deep copies so far, maybe we could do shallow copies to save time?) |
| 364 |
Teuchos::ArrayRCP<size_t> rowptrQuasiRegion(rowptrRegion.size()); |
| 365 |
Teuchos::ArrayRCP<LocalOrdinal> colindQuasiRegion(colindRegion.size()); |
| 366 |
Teuchos::ArrayRCP<Scalar> valuesQuasiRegion(valuesRegion.size()); |
| 367 |
|
| 368 |
quasiRegionCrsMat->allocateAllValues(valuesQuasiRegion.size(), rowptrQuasiRegion, colindQuasiRegion, valuesQuasiRegion); |
| 369 |
|
| 370 |
for (LocalOrdinal idx = 0; idx < static_cast<LocalOrdinal>(rowptrQuasiRegion.size()); ++idx) { |
| 371 |
rowptrQuasiRegion[idx] = rowptrRegion[idx]; |
| 372 |
} |
| 373 |
|
| 374 |
for (LocalOrdinal idx = 0; idx < static_cast<LocalOrdinal>(colindQuasiRegion.size()); ++idx) { |
| 375 |
colindQuasiRegion[idx] = colindRegion[idx]; |
| 376 |
valuesQuasiRegion[idx] = valuesRegion[idx]; |
| 377 |
} |
| 378 |
|
| 379 |
// Set and fillComplete the quasiRegion CrsMatrix |
| 380 |
quasiRegionCrsMat->setAllValues(rowptrQuasiRegion, colindQuasiRegion, valuesQuasiRegion); |
| 381 |
quasiRegionCrsMat->expertStaticFillComplete(rowMap, rowMap); |
| 382 |
} |
| 383 |
|
| 384 |
// Export from quasiRegional format to composite layout |
| 385 |
RCP<Matrix> partialCompMat; |
| 386 |
partialCompMat = MatrixFactory::Build(compMat->getRowMap(), |
| 387 |
8 * regMat->getCrsGraph()->getLocalMaxNumRowEntries()); |
| 388 |
partialCompMat->doExport(*(quasiRegMat), *(rowImport), Xpetra::INSERT); |
| 389 |
partialCompMat->fillComplete(); |
| 390 |
|
| 391 |
// Add all partialCompMat together |
| 392 |
MatrixMatrix::TwoMatrixAdd(*partialCompMat, false, SC_ONE, *compMat, SC_ONE); |
| 393 |
|
| 394 |
compMat->fillComplete(); |
| 395 |
|
| 396 |
return; |
| 397 |
} // regionalToComposite |
| 398 |
|
| 399 |
/*! \brief Compute local data needed to perform a MatVec in region format |
| 400 |
|
| 401 |
*/ |
| 402 |
template <class LocalOrdinal, class GlobalOrdinal, class Node> |
| 403 |
void SetupMatVec(const Teuchos::RCP<Xpetra::MultiVector<GlobalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>& interfaceGIDsMV, |
| 404 |
const Teuchos::RCP<Xpetra::MultiVector<LocalOrdinal, LocalOrdinal, GlobalOrdinal, Node>>& regionsPerGIDWithGhosts, |
| 405 |
const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>>& regionRowMap, |
| 406 |
const Teuchos::RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& rowImport, |
| 407 |
Teuchos::ArrayRCP<LocalOrdinal>& regionMatVecLIDs, |
| 408 |
Teuchos::RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node>>& regionInterfaceImporter) { |
| 409 |
#include "Xpetra_UseShortNamesOrdinal.hpp" |
| 410 |
using Teuchos::TimeMonitor; |
| 411 |
|
| 412 |
RCP<TimeMonitor> tm; |
| 413 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("SetupMatVec: 1 - sumInterfaceValues"))); |
| 414 |
|
| 415 |
const LO maxRegPerGID = static_cast<LO>(regionsPerGIDWithGhosts->getNumVectors()); |
| 416 |
const int myRank = regionRowMap->getComm()->getRank(); |
| 417 |
interfaceGIDsMV->replaceMap(regionRowMap); |
| 418 |
RCP<Xpetra::MultiVector<GO, LO, GO, NO>> interfaceGIDs; |
| 419 |
interfaceGIDs = interfaceGIDsMV; |
| 420 |
sumInterfaceValues(interfaceGIDs, regionRowMap, rowImport); |
| 421 |
|
| 422 |
tm = Teuchos::null; |
| 423 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("SetupMatVec: 2 - build regionInterfaceMap"))); |
| 424 |
|
| 425 |
Teuchos::Array<LO> regionMatVecLIDstmp; |
| 426 |
Teuchos::Array<GO> regionMatVecGIDs; |
| 427 |
Array<ArrayRCP<const LO>> regionsPerGIDWithGhostsData(maxRegPerGID); |
| 428 |
Array<ArrayRCP<const GO>> interfaceGIDsData(maxRegPerGID); |
| 429 |
for (LO regionIdx = 0; regionIdx < maxRegPerGID; ++regionIdx) { |
| 430 |
regionsPerGIDWithGhostsData[regionIdx] = regionsPerGIDWithGhosts->getData(regionIdx); |
| 431 |
interfaceGIDsData[regionIdx] = interfaceGIDs->getData(regionIdx); |
| 432 |
for (LO idx = 0; idx < static_cast<LO>(regionsPerGIDWithGhostsData[regionIdx].size()); ++idx) { |
| 433 |
if ((regionsPerGIDWithGhostsData[regionIdx][idx] != -1) && (regionsPerGIDWithGhostsData[regionIdx][idx] != myRank)) { |
| 434 |
regionMatVecLIDstmp.push_back(idx); |
| 435 |
regionMatVecGIDs.push_back(interfaceGIDsData[regionIdx][idx]); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// Copy the temporary regionMatVecLIDstmp into an ArrayRCP |
| 441 |
// so we can store it and retrieve it easily later on. |
| 442 |
regionMatVecLIDs.deepCopy(regionMatVecLIDstmp()); |
| 443 |
|
| 444 |
RCP<Map> regionInterfaceMap = Xpetra::MapFactory<LO, GO, Node>::Build(regionRowMap->lib(), |
| 445 |
Teuchos::OrdinalTraits<GO>::invalid(), |
| 446 |
regionMatVecGIDs(), |
| 447 |
regionRowMap->getIndexBase(), |
| 448 |
regionRowMap->getComm()); |
| 449 |
|
| 450 |
tm = Teuchos::null; |
| 451 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("SetupMatVec: 3 - Build importer"))); |
| 452 |
|
| 453 |
regionInterfaceImporter = ImportFactory::Build(regionRowMap, regionInterfaceMap); |
| 454 |
|
| 455 |
tm = Teuchos::null; |
| 456 |
} // SetupMatVec |
| 457 |
|
| 458 |
/*! \brief Compute the residual \f$r = b - Ax\f$ with pre-computed communication patterns |
| 459 |
* |
| 460 |
* The residual is computed based on matrices and vectors in a regional layout. |
| 461 |
* 1. Compute and sum interface values of y = A*x in regional layout using the fast MatVev |
| 462 |
* 2. Compute residual via r = b - y |
| 463 |
*/ |
| 464 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 465 |
void computeResidual(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& regRes, ///< residual (to be evaluated) |
| 466 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regX, ///< left-hand side (solution) |
| 467 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regB, ///< right-hand side (forcing term) |
| 468 |
const RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> regionMats, ///< matrix in region format |
| 469 |
const Teuchos::ParameterList& params ///< parameter with fast MatVec parameters and pre-computed communication patterns |
| 470 |
) { |
| 471 |
#include "Xpetra_UseShortNames.hpp" |
| 472 |
using TST = Teuchos::ScalarTraits<Scalar>; |
| 473 |
using Teuchos::TimeMonitor; |
| 474 |
|
| 475 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("computeResidual: use fast MatVec"))); |
| 476 |
|
| 477 |
// Get pre-communicated communication patterns for the fast MatVec |
| 478 |
const ArrayRCP<LocalOrdinal> regionInterfaceLIDs = params.get<ArrayRCP<LO>>("Fast MatVec: interface LIDs"); |
| 479 |
const RCP<Import> regionInterfaceImporter = params.get<RCP<Import>>("Fast MatVec: interface importer"); |
| 480 |
|
| 481 |
// Step 1: Compute region version of y = Ax and store it in regRes |
| 482 |
regionMats->apply(*regX, *regRes, Teuchos::NO_TRANS, TST::one(), TST::zero(), true, regionInterfaceImporter, regionInterfaceLIDs); |
| 483 |
|
| 484 |
// Step 2: Compute region version of r = b - y |
| 485 |
regRes->update(TST::one(), *regB, -TST::one(), *regRes, TST::zero()); |
| 486 |
|
| 487 |
tm = Teuchos::null; |
| 488 |
} // computeResidual |
| 489 |
|
| 490 |
#endif // MUELU_SETUPREGIONMATRIX_DEF_HPP |