10 #ifndef MUELU_GEOMETRICINTERPOLATIONPFACTORY_KOKKOS_DEF_HPP
11 #define MUELU_GEOMETRICINTERPOLATIONPFACTORY_KOKKOS_DEF_HPP
18 #include "MueLu_IndexManager_kokkos.hpp"
20 #include "Xpetra_TpetraCrsMatrix.hpp"
27 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
31 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
33 #undef SET_VALID_ENTRY
37 "Generating factory of the matrix A");
39 "Graph generated by StructuredAggregationFactory used to construct a piece-linear prolongator.");
41 "Fine level coordinates used to construct piece-wise linear prolongator and coarse level coordinates.");
43 "Fine level nullspace used to construct the coarse level nullspace.");
45 "Number of spacial dimensions in the problem.");
47 "Number of nodes per spatial dimension on the coarse grid.");
49 "The index manager associated with the local mesh.");
51 "Interpolation order for constructing the prolongator.");
53 return validParamList;
56 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
61 Input(fineLevel,
"A");
62 Input(fineLevel,
"Nullspace");
63 Input(fineLevel,
"numDimensions");
64 Input(fineLevel,
"prolongatorGraph");
65 Input(fineLevel,
"lCoarseNodesPerDim");
66 Input(fineLevel,
"structuredInterpolationOrder");
68 if (pL.
get<
bool>(
"interp: build coarse coordinates") ||
69 Get<int>(fineLevel,
"structuredInterpolationOrder") == 1) {
70 Input(fineLevel,
"Coordinates");
71 Input(fineLevel,
"indexManager");
75 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
78 return BuildP(fineLevel, coarseLevel);
81 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
88 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
89 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
95 *out <<
"Starting GeometricInterpolationPFactory_kokkos::BuildP." << std::endl;
99 const bool buildCoarseCoordinates = pL.
get<
bool>(
"interp: build coarse coordinates");
100 const int interpolationOrder = Get<int>(fineLevel,
"structuredInterpolationOrder");
101 const int numDimensions = Get<int>(fineLevel,
"numDimensions");
111 if (buildCoarseCoordinates || (interpolationOrder == 1)) {
116 fineCoordinates = Get<RCP<realvaluedmultivector_type> >(fineLevel,
"Coordinates");
119 RCP<const Map> coarseCoordsMap = MapFactory::Build(fineCoordinates->getMap()->lib(),
122 fineCoordinates->getMap()->getIndexBase(),
123 fineCoordinates->getMap()->getComm());
125 Build(coarseCoordsMap, fineCoordinates->getNumVectors());
130 fineCoordinates->getDeviceLocalView(Xpetra::Access::ReadWrite),
131 coarseCoordinates->getDeviceLocalView(Xpetra::Access::OverwriteAll));
132 Kokkos::parallel_for(
"GeometricInterpolation: build coarse coordinates",
134 myCoarseCoordinatesBuilder);
136 Set(coarseLevel,
"Coordinates", coarseCoordinates);
139 *out <<
"Fine and coarse coordinates have been loaded from the fine level and set on the coarse level." << std::endl;
141 if (interpolationOrder == 0) {
144 BuildConstantP(P, prolongatorGraph, A);
145 }
else if (interpolationOrder == 1) {
149 fineCoordinates->getNumVectors());
150 RCP<const Import> ghostImporter = ImportFactory::Build(coarseCoordinates->getMap(),
151 prolongatorGraph->getColMap());
152 ghostCoordinates->doImport(*coarseCoordinates, *ghostImporter,
Xpetra::INSERT);
155 BuildLinearP(A, prolongatorGraph, fineCoordinates, ghostCoordinates, numDimensions, P);
158 *out <<
"The prolongator matrix has been built." << std::endl;
163 RCP<MultiVector> fineNullspace = Get<RCP<MultiVector> >(fineLevel,
"Nullspace");
164 RCP<MultiVector> coarseNullspace = MultiVectorFactory::Build(P->getDomainMap(),
165 fineNullspace->getNumVectors());
168 Set(coarseLevel,
"Nullspace", coarseNullspace);
171 *out <<
"The coarse nullspace is constructed and set on the coarse level." << std::endl;
173 Array<LO> lNodesPerDir = Get<Array<LO> >(fineLevel,
"lCoarseNodesPerDim");
174 Set(coarseLevel,
"numDimensions", numDimensions);
175 Set(coarseLevel,
"lNodesPerDim", lNodesPerDir);
176 Set(coarseLevel,
"P", P);
178 *out <<
"GeometricInterpolationPFactory_kokkos::BuildP has completed." << std::endl;
182 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
187 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
188 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
194 *out <<
"BuildConstantP" << std::endl;
196 std::vector<size_t> strideInfo(1);
197 strideInfo[0] = A->GetFixedBlockSize();
199 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
201 *out <<
"Call prolongator constructor" << std::endl;
203 if (helpers::isTpetraBlockCrs(A)) {
204 LO NSDim = A->GetStorageBlockSize();
211 for (
LO i = 0, ct = 0; i < block_dofs.
size(); i++) {
212 for (
LO j = 0; j < NSDim; j++) {
213 point_dofs[ct] = block_dofs[i] * NSDim + j;
219 BlockMap->getGlobalNumElements() * NSDim,
221 BlockMap->getIndexBase(),
222 BlockMap->getComm());
223 strideInfo[0] = A->GetFixedBlockSize();
228 if (P_tpetra.is_null())
throw std::runtime_error(
"BuildConstantP: Matrix factory did not return a Tpetra::BlockCrsMatrix");
231 const LO stride = strideInfo[0] * strideInfo[0];
232 const LO in_stride = strideInfo[0];
233 typename CrsMatrix::local_graph_type localGraph = prolongatorGraph->getLocalGraphDevice();
234 auto rowptr = localGraph.row_map;
235 auto indices = localGraph.entries;
236 auto values = P_tpetra->getTpetra_BlockCrsMatrix()->getValuesDeviceNonConst();
241 const Kokkos::TeamPolicy<execution_space> policy(prolongatorGraph->getLocalNumRows(), 1);
243 Kokkos::parallel_for(
244 "MueLu:GeoInterpFact::BuildConstantP::fill", policy,
245 KOKKOS_LAMBDA(
const typename Kokkos::TeamPolicy<execution_space>::member_type& thread) {
246 auto row = thread.league_rank();
247 for (
LO j = (
LO)rowptr[row]; j < (
LO)rowptr[row + 1]; j++) {
248 LO block_offset = j * stride;
249 for (
LO k = 0; k < in_stride; k++)
250 values[block_offset + k * (in_stride + 1)] = one;
255 if (A->IsView(
"stridedMaps") ==
true) {
256 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedPointMap);
258 P->CreateView(
"stridedMaps", P->getRangeMap(), PointMap);
263 P =
rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
264 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
265 PCrs->setAllToScalar(1.0);
266 PCrs->fillComplete();
269 if (A->IsView(
"stridedMaps") ==
true) {
270 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
272 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
278 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
286 if (
const char* dbg = std::getenv(
"MUELU_GEOMETRICINTERPOLATIONPFACTORY_DEBUG")) {
287 out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
293 *out <<
"Entering BuildLinearP" << std::endl;
296 const LO numFineNodes = fineCoordinates->getLocalLength();
297 const LO numGhostNodes = ghostCoordinates->getLocalLength();
300 const real_type realZero = Teuchos::as<real_type>(0.0);
303 for (
int dim = 0; dim < 3; ++dim) {
304 if (dim < numDimensions) {
305 fineCoords[dim] = fineCoordinates->getData(dim);
306 ghostCoords[dim] = ghostCoordinates->getData(dim);
308 fineCoords[dim] = fineZero;
309 ghostCoords[dim] = ghostZero;
313 *out <<
"Coordinates extracted from the multivectors!" << std::endl;
316 const int numInterpolationPoints = 1 << numDimensions;
317 const int dofsPerNode = A->GetFixedBlockSize();
319 std::vector<size_t> strideInfo(1);
320 strideInfo[0] = dofsPerNode;
322 StridedMapFactory::Build(prolongatorGraph->getDomainMap(), strideInfo);
324 *out <<
"The maps of P have been computed" << std::endl;
327 P =
rcp(
new CrsMatrixWrap(prolongatorGraph, dummyList));
328 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
331 LO interpolationNodeIdx = 0, rowIdx = 0;
336 for (
LO nodeIdx = 0; nodeIdx < numFineNodes; ++nodeIdx) {
337 if (PCrs->getNumEntriesInLocalRow(nodeIdx * dofsPerNode) == 1) {
340 for (
LO dof = 0; dof < dofsPerNode; ++dof) {
341 rowIdx = nodeIdx * dofsPerNode + dof;
342 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
343 PCrs->replaceLocalValues(rowIdx, colIndices, values());
349 for (
int dim = 0; dim < 3; ++dim) {
350 coords[0][dim] = fineCoords[dim][nodeIdx];
352 prolongatorGraph->getLocalRowView(nodeIdx * dofsPerNode, colIndices);
353 for (
int interpolationIdx = 0; interpolationIdx < numInterpolationPoints; ++interpolationIdx) {
354 coords[interpolationIdx + 1].
resize(3);
355 interpolationNodeIdx = colIndices[interpolationIdx] / dofsPerNode;
356 for (
int dim = 0; dim < 3; ++dim) {
357 coords[interpolationIdx + 1][dim] = ghostCoords[dim][interpolationNodeIdx];
360 ComputeLinearInterpolationStencil(numDimensions, numInterpolationPoints, coords, stencil);
361 values.
resize(numInterpolationPoints);
362 for (
LO valueIdx = 0; valueIdx < numInterpolationPoints; ++valueIdx) {
363 values[valueIdx] = Teuchos::as<SC>(stencil[valueIdx]);
367 for (
LO dof = 0; dof < dofsPerNode; ++dof) {
368 rowIdx = nodeIdx * dofsPerNode + dof;
369 prolongatorGraph->getLocalRowView(rowIdx, colIndices);
370 PCrs->replaceLocalValues(rowIdx, colIndices, values());
375 *out <<
"The calculation of the interpolation stencils has completed." << std::endl;
377 PCrs->fillComplete();
379 *out <<
"All values in P have been set and expertStaticFillComplete has been performed." << std::endl;
382 if (A->IsView(
"stridedMaps") ==
true) {
383 P->CreateView(
"stridedMaps", A->getRowMap(
"stridedMaps"), stridedDomainMap);
385 P->CreateView(
"stridedMaps", P->getRangeMap(), stridedDomainMap);
390 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
415 int iter = 0, max_iter = 5;
416 real_type functions[4][8], norm_ref = 1.0, norm2 = 1.0, tol = 1.0e-5;
417 paramCoords.
size(numDimensions);
419 while ((iter < max_iter) && (norm2 > tol * norm_ref)) {
422 solutionDirection.
size(numDimensions);
423 residual.
size(numDimensions);
427 GetInterpolationFunctions(numDimensions, paramCoords, functions);
428 for (
LO i = 0; i < numDimensions; ++i) {
429 residual(i) = coord[0][i];
430 for (
LO k = 0; k < numInterpolationPoints; ++k) {
431 residual(i) -= functions[0][k] * coord[k + 1][i];
434 norm_ref += residual(i) * residual(i);
435 if (i == numDimensions - 1) {
436 norm_ref = std::sqrt(norm_ref);
440 for (
LO j = 0; j < numDimensions; ++j) {
441 for (
LO k = 0; k < numInterpolationPoints; ++k) {
442 Jacobian(i, j) += functions[j + 1][k] * coord[k + 1][i];
455 for (
LO i = 0; i < numDimensions; ++i) {
456 paramCoords(i) = paramCoords(i) + solutionDirection(i);
460 GetInterpolationFunctions(numDimensions, paramCoords, functions);
461 for (
LO i = 0; i < numDimensions; ++i) {
463 for (
LO k = 0; k < numInterpolationPoints; ++k) {
464 tmp -= functions[0][k] * coord[k + 1][i];
469 norm2 = std::sqrt(norm2);
473 for (
LO i = 0; i < numInterpolationPoints; ++i) {
474 stencil[i] = functions[0][i];
479 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
484 real_type xi = 0.0, eta = 0.0, zeta = 0.0, denominator = 0.0;
485 if (numDimensions == 1) {
486 xi = parametricCoordinates[0];
488 }
else if (numDimensions == 2) {
489 xi = parametricCoordinates[0];
490 eta = parametricCoordinates[1];
492 }
else if (numDimensions == 3) {
493 xi = parametricCoordinates[0];
494 eta = parametricCoordinates[1];
495 zeta = parametricCoordinates[2];
499 functions[0][0] = (1.0 - xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
500 functions[0][1] = (1.0 + xi) * (1.0 - eta) * (1.0 - zeta) / denominator;
501 functions[0][2] = (1.0 - xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
502 functions[0][3] = (1.0 + xi) * (1.0 + eta) * (1.0 - zeta) / denominator;
503 functions[0][4] = (1.0 - xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
504 functions[0][5] = (1.0 + xi) * (1.0 - eta) * (1.0 + zeta) / denominator;
505 functions[0][6] = (1.0 - xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
506 functions[0][7] = (1.0 + xi) * (1.0 + eta) * (1.0 + zeta) / denominator;
508 functions[1][0] = -(1.0 - eta) * (1.0 - zeta) / denominator;
509 functions[1][1] = (1.0 - eta) * (1.0 - zeta) / denominator;
510 functions[1][2] = -(1.0 + eta) * (1.0 - zeta) / denominator;
511 functions[1][3] = (1.0 + eta) * (1.0 - zeta) / denominator;
512 functions[1][4] = -(1.0 - eta) * (1.0 + zeta) / denominator;
513 functions[1][5] = (1.0 - eta) * (1.0 + zeta) / denominator;
514 functions[1][6] = -(1.0 + eta) * (1.0 + zeta) / denominator;
515 functions[1][7] = (1.0 + eta) * (1.0 + zeta) / denominator;
517 functions[2][0] = -(1.0 - xi) * (1.0 - zeta) / denominator;
518 functions[2][1] = -(1.0 + xi) * (1.0 - zeta) / denominator;
519 functions[2][2] = (1.0 - xi) * (1.0 - zeta) / denominator;
520 functions[2][3] = (1.0 + xi) * (1.0 - zeta) / denominator;
521 functions[2][4] = -(1.0 - xi) * (1.0 + zeta) / denominator;
522 functions[2][5] = -(1.0 + xi) * (1.0 + zeta) / denominator;
523 functions[2][6] = (1.0 - xi) * (1.0 + zeta) / denominator;
524 functions[2][7] = (1.0 + xi) * (1.0 + zeta) / denominator;
526 functions[3][0] = -(1.0 - xi) * (1.0 - eta) / denominator;
527 functions[3][1] = -(1.0 + xi) * (1.0 - eta) / denominator;
528 functions[3][2] = -(1.0 - xi) * (1.0 + eta) / denominator;
529 functions[3][3] = -(1.0 + xi) * (1.0 + eta) / denominator;
530 functions[3][4] = (1.0 - xi) * (1.0 - eta) / denominator;
531 functions[3][5] = (1.0 + xi) * (1.0 - eta) / denominator;
532 functions[3][6] = (1.0 - xi) * (1.0 + eta) / denominator;
533 functions[3][7] = (1.0 + xi) * (1.0 + eta) / denominator;
537 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
543 , fineCoordView_(fineCoordView)
544 , coarseCoordView_(coarseCoordView) {
547 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
551 LO nodeCoarseTuple[3] = {0, 0, 0};
552 LO nodeFineTuple[3] = {0, 0, 0};
553 auto coarseningRate = geoData_.getCoarseningRates();
554 auto fineNodesPerDir = geoData_.getLocalFineNodesPerDir();
555 auto coarseNodesPerDir = geoData_.getCoarseNodesPerDir();
556 geoData_.getCoarseLID2CoarseTuple(coarseNodeIdx, nodeCoarseTuple);
557 for (
int dim = 0; dim < 3; ++dim) {
558 if (nodeCoarseTuple[dim] == coarseNodesPerDir(dim) - 1) {
559 nodeFineTuple[dim] = fineNodesPerDir(dim) - 1;
561 nodeFineTuple[dim] = nodeCoarseTuple[dim] * coarseningRate(dim);
565 fineNodeIdx = nodeFineTuple[2] * fineNodesPerDir(1) * fineNodesPerDir(0) + nodeFineTuple[1] * fineNodesPerDir(0) + nodeFineTuple[0];
567 for (
int dim = 0; dim < fineCoordView_.extent_int(1); ++dim) {
568 coarseCoordView_(coarseNodeIdx, dim) = fineCoordView_(fineNodeIdx, dim);
574 #endif // MUELU_GEOMETRICINTERPOLATIONPFACTORY_KOKKOS_DEF_HPP
typename BMV::impl_scalar_type impl_scalar_type
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
basic_FancyOStream & setShowProcRank(const bool showProcRank)
coarseCoordinatesBuilderFunctor(RCP< IndexManager_kokkos > geoData, coord_view_type fineCoordView, coord_view_type coarseCoordView)
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
LO getNumCoarseNodes() const
void GetInterpolationFunctions(const LO numDimensions, const Teuchos::SerialDenseVector< LO, real_type > parametricCoordinates, real_type functions[4][8]) const
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
typename Teuchos::ScalarTraits< SC >::coordinateType real_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Class that holds all level-specific information.
void ComputeLinearInterpolationStencil(const int numDimensions, const int numInterpolationPoints, const Array< Array< real_type > > coord, Array< real_type > &stencil) const
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void factorWithEquilibration(bool flag)
typename Kokkos::View< impl_scalar_type **, Kokkos::LayoutLeft, device_type > coord_view_type
void BuildConstantP(RCP< Matrix > &P, RCP< const CrsGraph > &prolongatorGraph, RCP< Matrix > &A) const
#define SET_VALID_ENTRY(name)
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
void resize(size_type new_size, const value_type &x=value_type())
int size(OrdinalType length_in)
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
void BuildLinearP(RCP< Matrix > &A, RCP< const CrsGraph > &prolongatorGraph, RCP< realvaluedmultivector_type > &fineCoordinates, RCP< realvaluedmultivector_type > &ghostCoordinates, const int numDimensions, RCP< Matrix > &P) const
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void residual(const Operator< SC, LO, GO, NO > &Aop, const MultiVector< SC, LO, GO, NO > &X_in, const MultiVector< SC, LO, GO, NO > &B_in, MultiVector< SC, LO, GO, NO > &R_in)
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
KOKKOS_INLINE_FUNCTION void operator()(const LO nodeIdx) const
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)