| 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_SETUPREGIONVECTOR_DEF_HPP |
| 11 |
#define MUELU_SETUPREGIONVECTOR_DEF_HPP |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
#include <iostream> |
| 15 |
#include <numeric> |
| 16 |
|
| 17 |
#ifdef HAVE_MPI |
| 18 |
#include "mpi.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <Tpetra_KokkosCompat_DefaultNode.hpp> |
| 22 |
|
| 23 |
#include <Xpetra_ConfigDefs.hpp> |
| 24 |
#include <Xpetra_Export.hpp> |
| 25 |
#include <Xpetra_Import.hpp> |
| 26 |
#include <Xpetra_Map.hpp> |
| 27 |
#include <Xpetra_MultiVector.hpp> |
| 28 |
#include <Xpetra_Vector.hpp> |
| 29 |
|
| 30 |
using Teuchos::Array; |
| 31 |
using Teuchos::ArrayRCP; |
| 32 |
using Teuchos::RCP; |
| 33 |
|
| 34 |
/*! \brief Transform composite vector to regional layout |
| 35 |
* |
| 36 |
* Starting from a vector in composite layout, we |
| 37 |
* 1. import it into an auxiliary vector in the quasiRegional layout |
| 38 |
* 2. replace the quasiRegional map of the auxiliary vector with the regional map |
| 39 |
*/ |
| 40 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 41 |
void compositeToRegional(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > compVec, ///< Vector in composite layout [in] |
| 42 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& quasiRegVecs, ///< Vector in quasiRegional layout [in/out] |
| 43 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVecs, ///< Vector in regional layout [in/out] |
| 44 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] |
| 45 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in] |
| 46 |
) { |
| 47 |
#include "Xpetra_UseShortNames.hpp" |
| 48 |
|
| 49 |
// quasiRegional layout |
| 50 |
// create empty vectors and fill it by extracting data from composite vector |
| 51 |
quasiRegVecs = VectorFactory::Build(rowImport->getTargetMap(), true); |
| 52 |
TEUCHOS_ASSERT(!quasiRegVecs.is_null()); |
| 53 |
quasiRegVecs->doImport(*compVec, *(rowImport), Xpetra::INSERT); |
| 54 |
|
| 55 |
// regional layout |
| 56 |
// create regVecs vector (copy from quasiRegVecs and swap the map) |
| 57 |
regVecs = quasiRegVecs; // assignment operator= does deep copy in Xpetra |
| 58 |
TEUCHOS_ASSERT(!regVecs.is_null()); |
| 59 |
regVecs->replaceMap(revisedRowMap); |
| 60 |
|
| 61 |
return; |
| 62 |
} // compositeToRegional |
| 63 |
|
| 64 |
/*! \brief Transform composite vector to regional layout |
| 65 |
* |
| 66 |
* Starting from a vector in composite layout, we |
| 67 |
* 1. import it into an auxiliary vector in the quasiRegional layout |
| 68 |
* 2. replace the quasiRegional map of the auxiliary vector with the regional map |
| 69 |
*/ |
| 70 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 71 |
void compositeToRegional(RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > compVec, ///< Vector in composite layout [in] |
| 72 |
RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& quasiRegVecs, ///< Vector in quasiRegional layout [in/out] |
| 73 |
RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVecs, ///< Vector in regional layout [in/out] |
| 74 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] |
| 75 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in] |
| 76 |
) { |
| 77 |
#include "Xpetra_UseShortNames.hpp" |
| 78 |
|
| 79 |
// quasiRegional layout |
| 80 |
// create empty vectors and fill it by extracting data from composite vector |
| 81 |
quasiRegVecs = MultiVectorFactory::Build(rowImport->getTargetMap(), compVec->getNumVectors(), true); |
| 82 |
TEUCHOS_ASSERT(!quasiRegVecs.is_null()); |
| 83 |
quasiRegVecs->doImport(*compVec, *(rowImport), Xpetra::INSERT); |
| 84 |
|
| 85 |
// regional layout |
| 86 |
// create regVecs vector (copy from quasiRegVecs and swap the map) |
| 87 |
regVecs = quasiRegVecs; // assignment operator= does deep copy in Xpetra |
| 88 |
TEUCHOS_ASSERT(!regVecs.is_null()); |
| 89 |
regVecs->replaceMap(revisedRowMap); |
| 90 |
|
| 91 |
return; |
| 92 |
} // compositeToRegional |
| 93 |
|
| 94 |
/*! \brief Transform regional vector to composite layout |
| 95 |
* |
| 96 |
* Starting from a \c Epetra_Vector in regional layout, we |
| 97 |
* 1. replace the regional map with the quasiRegional map |
| 98 |
* 2. export it into a vector with composite layout using the \c CombineMode \c Xpetra::ADD. |
| 99 |
* Note: on-process values also have to be added to account for region interfaces inside a process. |
| 100 |
* |
| 101 |
* \note We also need the capability to add processor-local values. This is not supported by |
| 102 |
* available CombineMode options in Xpetra/Tpetra, so we use a manual implementation here. |
| 103 |
*/ |
| 104 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 105 |
void regionalToComposite(const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, ///< Vector in region layout [in] |
| 106 |
RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > compVec, ///< Vector in composite layout [in/out] |
| 107 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in] |
| 108 |
) { |
| 109 |
/* Let's fake an ADD combine mode that also adds local values by |
| 110 |
* 1. exporting quasiRegional vectors to auxiliary composite vectors (1 per group) |
| 111 |
* 2. add all auxiliary vectors together |
| 112 |
*/ |
| 113 |
#include "Xpetra_UseShortNames.hpp" |
| 114 |
using Teuchos::TimeMonitor; |
| 115 |
|
| 116 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 1 - compVec setup"))); |
| 117 |
|
| 118 |
const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 119 |
|
| 120 |
const size_t compVecLocalLength = compVec->getLocalLength(); |
| 121 |
compVec->putScalar(SC_ZERO); |
| 122 |
|
| 123 |
tm = Teuchos::null; |
| 124 |
|
| 125 |
{ |
| 126 |
RCP<Vector> quasiRegVec; |
| 127 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 2 - quasiRegVec"))); |
| 128 |
quasiRegVec = regVec; |
| 129 |
TEUCHOS_ASSERT(Teuchos::nonnull(quasiRegVec)); |
| 130 |
quasiRegVec->replaceMap(rowImport->getTargetMap()); |
| 131 |
|
| 132 |
tm = Teuchos::null; |
| 133 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 3 - partialCompVec"))); |
| 134 |
|
| 135 |
RCP<Vector> partialCompVec = VectorFactory::Build(rowImport->getSourceMap(), true); |
| 136 |
TEUCHOS_ASSERT(Teuchos::nonnull(partialCompVec)); |
| 137 |
TEUCHOS_ASSERT(partialCompVec->getLocalLength() == compVecLocalLength); |
| 138 |
partialCompVec->doExport(*quasiRegVec, *(rowImport), Xpetra::ADD); |
| 139 |
|
| 140 |
tm = Teuchos::null; |
| 141 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 4 - compVec->sumIntoLocalValue"))); |
| 142 |
|
| 143 |
ArrayRCP<const SC> partialCompVecData = partialCompVec->getData(0); |
| 144 |
ArrayRCP<SC> compVecData = compVec->getDataNonConst(0); |
| 145 |
for (size_t entryIdx = 0; entryIdx < compVecLocalLength; ++entryIdx) { |
| 146 |
compVecData[entryIdx] += partialCompVecData[entryIdx]; |
| 147 |
} |
| 148 |
|
| 149 |
tm = Teuchos::null; |
| 150 |
} |
| 151 |
|
| 152 |
return; |
| 153 |
} // regionalToComposite |
| 154 |
|
| 155 |
/*! \brief Transform regional vector to composite layout |
| 156 |
* |
| 157 |
* Starting from a \c Epetra_Vector in regional layout, we |
| 158 |
* 1. replace the regional map with the quasiRegional map |
| 159 |
* 2. export it into a vector with composite layout using the \c CombineMode \c Xpetra::ADD. |
| 160 |
* Note: on-process values also have to be added to account for region interfaces inside a process. |
| 161 |
* |
| 162 |
* \note We also need the capability to add processor-local values. This is not supported by |
| 163 |
* available CombineMode options in Xpetra/Tpetra, so we use a manual implementation here. |
| 164 |
*/ |
| 165 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 166 |
void regionalToComposite(const RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, ///< Vector in region layout [in] |
| 167 |
RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> > compVec, ///< Vector in composite layout [in/out] |
| 168 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in] |
| 169 |
) { |
| 170 |
/* Let's fake an ADD combine mode that also adds local values by |
| 171 |
* 1. exporting quasiRegional vectors to auxiliary composite vectors (1 per group) |
| 172 |
* 2. add all auxiliary vectors together |
| 173 |
*/ |
| 174 |
#include "Xpetra_UseShortNames.hpp" |
| 175 |
using Teuchos::TimeMonitor; |
| 176 |
|
| 177 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 1 - compVec setup"))); |
| 178 |
|
| 179 |
const SC SC_ZERO = Teuchos::ScalarTraits<Scalar>::zero(); |
| 180 |
|
| 181 |
const size_t compVecLocalLength = compVec->getLocalLength(); |
| 182 |
compVec->putScalar(SC_ZERO); |
| 183 |
|
| 184 |
tm = Teuchos::null; |
| 185 |
|
| 186 |
{ |
| 187 |
RCP<MultiVector> quasiRegVec; |
| 188 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 2 - quasiRegVec"))); |
| 189 |
quasiRegVec = regVec; |
| 190 |
TEUCHOS_ASSERT(Teuchos::nonnull(quasiRegVec)); |
| 191 |
quasiRegVec->replaceMap(rowImport->getTargetMap()); |
| 192 |
|
| 193 |
tm = Teuchos::null; |
| 194 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 3 - partialCompVec"))); |
| 195 |
|
| 196 |
RCP<MultiVector> partialCompVec = MultiVectorFactory::Build(rowImport->getSourceMap(), quasiRegVec->getNumVectors(), true); |
| 197 |
TEUCHOS_ASSERT(Teuchos::nonnull(partialCompVec)); |
| 198 |
TEUCHOS_ASSERT(partialCompVec->getLocalLength() == compVecLocalLength); |
| 199 |
partialCompVec->doExport(*quasiRegVec, *(rowImport), Xpetra::ADD); |
| 200 |
|
| 201 |
tm = Teuchos::null; |
| 202 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("regionalToComposite: 4 - compVec->sumIntoLocalValue"))); |
| 203 |
|
| 204 |
for (LO vecIdx = 0; vecIdx < static_cast<LO>(partialCompVec->getNumVectors()); ++vecIdx) { |
| 205 |
ArrayRCP<const SC> partialCompVecData = partialCompVec->getData(vecIdx); |
| 206 |
ArrayRCP<SC> compVecData = compVec->getDataNonConst(vecIdx); |
| 207 |
for (size_t entryIdx = 0; entryIdx < compVecLocalLength; ++entryIdx) { |
| 208 |
compVecData[entryIdx] += partialCompVecData[entryIdx]; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
tm = Teuchos::null; |
| 213 |
} |
| 214 |
|
| 215 |
return; |
| 216 |
} // regionalToComposite |
| 217 |
|
| 218 |
/*! \brief Sum region interface values |
| 219 |
* |
| 220 |
* Sum values of interface GIDs using the underlying Export() routines. Technically, we perform the |
| 221 |
* exchange/summation of interface data by exporting a regional vector to the composite layout and |
| 222 |
* then immediately importing it back to the regional layout. The Export() involved when going to the |
| 223 |
* composite layout takes care of the summation of interface values. |
| 224 |
*/ |
| 225 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 226 |
void sumInterfaceValues(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, |
| 227 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] |
| 228 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in]) |
| 229 |
) { |
| 230 |
#include "Xpetra_UseShortNames.hpp" |
| 231 |
using Teuchos::TimeMonitor; |
| 232 |
|
| 233 |
// Composite map is the same in every group, so just take the first one. |
| 234 |
const RCP<const Map> compMap = rowImport->getSourceMap(); |
| 235 |
|
| 236 |
RCP<Vector> compVec = VectorFactory::Build(compMap, true); |
| 237 |
TEUCHOS_ASSERT(!compVec.is_null()); |
| 238 |
|
| 239 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("sumInterfaceValues: 1 - regionalToComposite"))); |
| 240 |
regionalToComposite(regVec, compVec, rowImport); |
| 241 |
|
| 242 |
tm = Teuchos::null; |
| 243 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("sumInterfaceValues: 2 - compositeToRegional"))); |
| 244 |
|
| 245 |
Teuchos::RCP<Vector> quasiRegVec; |
| 246 |
compositeToRegional(compVec, quasiRegVec, regVec, |
| 247 |
revisedRowMap, rowImport); |
| 248 |
|
| 249 |
tm = Teuchos::null; |
| 250 |
|
| 251 |
return; |
| 252 |
} // sumInterfaceValues |
| 253 |
|
| 254 |
/*! \brief Sum region interface values |
| 255 |
* |
| 256 |
* Sum values of interface GIDs using the underlying Export() routines. Technically, we perform the |
| 257 |
* exchange/summation of interface data by exporting a regional vector to the composite layout and |
| 258 |
* then immediately importing it back to the regional layout. The Export() involved when going to the |
| 259 |
* composite layout takes care of the summation of interface values. |
| 260 |
*/ |
| 261 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 262 |
void sumInterfaceValues(RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, |
| 263 |
const RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > revisedRowMap, ///< revised row maps in region layout [in] |
| 264 |
const RCP<Xpetra::Import<LocalOrdinal, GlobalOrdinal, Node> > rowImport ///< row importer in region layout [in]) |
| 265 |
) { |
| 266 |
#include "Xpetra_UseShortNames.hpp" |
| 267 |
using Teuchos::TimeMonitor; |
| 268 |
|
| 269 |
// Composite map is the same in every group, so just take the first one. |
| 270 |
const RCP<const Map> compMap = rowImport->getSourceMap(); |
| 271 |
|
| 272 |
RCP<MultiVector> compVec = MultiVectorFactory::Build(compMap, regVec->getNumVectors(), true); |
| 273 |
TEUCHOS_ASSERT(!compVec.is_null()); |
| 274 |
|
| 275 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("sumInterfaceValues: 1 - regionalToComposite"))); |
| 276 |
regionalToComposite(regVec, compVec, rowImport); |
| 277 |
|
| 278 |
tm = Teuchos::null; |
| 279 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("sumInterfaceValues: 2 - compositeToRegional"))); |
| 280 |
|
| 281 |
Teuchos::RCP<MultiVector> quasiRegVec; |
| 282 |
compositeToRegional(compVec, quasiRegVec, regVec, |
| 283 |
revisedRowMap, rowImport); |
| 284 |
|
| 285 |
tm = Teuchos::null; |
| 286 |
|
| 287 |
return; |
| 288 |
} // sumInterfaceValues |
| 289 |
|
| 290 |
/*! \brief Apply scaling to interface DOFs |
| 291 |
* |
| 292 |
* The vector scalingFactors contains the number of adjacent regions for every DOFs. |
| 293 |
* In the interior of a region, this is always 1. On region interfaces, this is >1. |
| 294 |
* |
| 295 |
* We often need to scale interface entries by 1/N with N being the number of adjacent regions. |
| 296 |
* This can be achieved by setting \c inverseScaling to \c true. |
| 297 |
*/ |
| 298 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 299 |
void scaleInterfaceDOFs(RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& regVec, ///< Vector to be scaled |
| 300 |
const RCP<Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >& scalingFactors, ///< Vector with scaling factors |
| 301 |
bool inverseScaling ///< Divide by scaling factors (yes/no?) |
| 302 |
) { |
| 303 |
using Vector = Xpetra::Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 304 |
using VectorFactory = Xpetra::VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>; |
| 305 |
|
| 306 |
const Scalar zero = Teuchos::ScalarTraits<Scalar>::zero(); |
| 307 |
const Scalar one = Teuchos::ScalarTraits<Scalar>::one(); |
| 308 |
|
| 309 |
if (inverseScaling) { |
| 310 |
RCP<Vector> inverseScalingFactors = VectorFactory::Build(scalingFactors->getMap()); |
| 311 |
inverseScalingFactors->reciprocal(*scalingFactors); |
| 312 |
regVec->elementWiseMultiply(one, *regVec, *inverseScalingFactors, zero); |
| 313 |
} else { |
| 314 |
regVec->elementWiseMultiply(one, *regVec, *scalingFactors, zero); |
| 315 |
} |
| 316 |
} // scaleInterfaceDOFs |
| 317 |
|
| 318 |
#endif // MUELU_SETUPREGIONVECTOR_DEF_HPP |