10 #ifndef MUELU_SEMICOARSENPFACTORY_KOKKOS_DEF_HPP
11 #define MUELU_SEMICOARSENPFACTORY_KOKKOS_DEF_HPP
15 #include <Kokkos_Core.hpp>
17 #include <KokkosBatched_LU_Decl.hpp>
18 #include <KokkosBatched_LU_Serial_Impl.hpp>
19 #include <KokkosBatched_Trsm_Decl.hpp>
20 #include <KokkosBatched_Trsm_Serial_Impl.hpp>
21 #include <KokkosBatched_Util.hpp>
22 #include <KokkosSparse_CrsMatrix.hpp>
24 #include <Xpetra_CrsMatrixWrap.hpp>
27 #include <Xpetra_MultiVectorFactory.hpp>
37 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
39 RCP<const ParameterList>
44 std::string name =
"semicoarsen: coarsen rate";
47 "A", Teuchos::null,
"Generating factory of the matrix A");
49 "Nullspace", Teuchos::null,
"Generating factory of the nullspace");
51 "Coordinates", Teuchos::null,
"Generating factory for coordinates");
54 "LineDetection_VertLineIds", Teuchos::null,
55 "Generating factory for LineDetection vertical line ids");
57 "LineDetection_Layers", Teuchos::null,
58 "Generating factory for LineDetection layer ids");
60 "CoarseNumZLayers", Teuchos::null,
61 "Generating factory for number of coarse z-layers");
63 return validParamList;
72 Input(fineLevel,
"A");
73 Input(fineLevel,
"Nullspace");
75 Input(fineLevel,
"LineDetection_VertLineIds");
76 Input(fineLevel,
"LineDetection_Layers");
77 Input(fineLevel,
"CoarseNumZLayers");
85 bTransferCoordinates_ =
true;
87 }
else if (bTransferCoordinates_ ==
true) {
92 if (myCoordsFact == Teuchos::null) {
107 return BuildP(fineLevel, coarseLevel);
121 Get<RCP<MultiVector>>(fineLevel,
"Nullspace");
125 LO CoarsenRate = as<LO>(pL.get<
int>(
"semicoarsen: coarsen rate"));
128 "semicoarsen: coarsen rate must be greater than 1");
131 LO BlkSize = A->GetFixedBlockSize();
133 LO Ndofs = rowMap->getLocalNumElements();
134 LO Nnodes = Ndofs / BlkSize;
138 LO FineNumZLayers = Get<LO>(fineLevel,
"CoarseNumZLayers");
140 Get<Teuchos::ArrayRCP<LO>>(fineLevel,
"LineDetection_VertLineIds");
142 Get<Teuchos::ArrayRCP<LO>>(fineLevel,
"LineDetection_Layers");
146 "Cannot coarsen further");
148 LO CoarseNumZLayers =
149 (
LO)floor(((coordT)(FineNumZLayers + 1)) / ((coordT)CoarsenRate) - 1.0);
150 if (CoarseNumZLayers < 1)
151 CoarseNumZLayers = 1;
156 BuildSemiCoarsenP(coarseLevel, Ndofs, Nnodes, BlkSize, FineNumZLayers,
157 CoarseNumZLayers, TLayerId, TVertLineId, A, fineNullspace, P,
163 coarseLevel.
Set(
"NumZLayers", Teuchos::as<LO>(CoarseNumZLayers),
167 Set(coarseLevel,
"P", P);
168 Set(coarseLevel,
"Nullspace", coarseNullspace);
171 if (bTransferCoordinates_) {
177 if (fineLevel.GetLevelID() == 0 &&
182 if (myCoordsFact == Teuchos::null) {
183 myCoordsFact = fineLevel.GetFactoryManager()->GetFactory(
"Coordinates");
185 if (fineLevel.IsAvailable(
"Coordinates", myCoordsFact.
get())) {
187 fineLevel.Get<
RCP<xdMV>>(
"Coordinates", myCoordsFact.
get());
191 TEUCHOS_TEST_FOR_EXCEPTION(fineCoords == Teuchos::null,
193 "No Coordinates found provided by the user.");
195 TEUCHOS_TEST_FOR_EXCEPTION(fineCoords->getNumVectors() != 3,
197 "Three coordinates arrays must be supplied if "
198 "line detection orientation not given.");
200 fineCoords->getDataNonConst(0);
202 fineCoords->getDataNonConst(1);
204 fineCoords->getDataNonConst(2);
208 typename Teuchos::ScalarTraits<Scalar>::coordinateType zval_max =
210 typename Teuchos::ScalarTraits<Scalar>::coordinateType>::one() /
212 typename Teuchos::ScalarTraits<Scalar>::coordinateType>::sfmin();
213 typename Teuchos::ScalarTraits<Scalar>::coordinateType zval_min =
215 typename Teuchos::ScalarTraits<Scalar>::coordinateType>::one() /
217 typename Teuchos::ScalarTraits<Scalar>::coordinateType>::sfmin();
218 for (
auto it = z.
begin(); it != z.
end(); ++it) {
225 LO myCoarseZLayers = Teuchos::as<LO>(CoarseNumZLayers);
228 myZLayerCoords = Teuchos::arcp<
229 typename Teuchos::ScalarTraits<Scalar>::coordinateType>(
231 if (myCoarseZLayers == 1) {
232 myZLayerCoords[0] = zval_min;
234 typename Teuchos::ScalarTraits<Scalar>::coordinateType dz =
235 (zval_max - zval_min) / (myCoarseZLayers - 1);
236 for (LO k = 0; k < myCoarseZLayers; ++k) {
237 myZLayerCoords[k] = k * dz;
245 LO numVertLines = Nnodes / FineNumZLayers;
246 LO numLocalCoarseNodes = numVertLines * myCoarseZLayers;
249 fineCoords->getMap()->lib(),
251 Teuchos::as<size_t>(numLocalCoarseNodes),
252 fineCoords->getMap()->getIndexBase(), fineCoords->getMap()->getComm());
254 typename Teuchos::ScalarTraits<Scalar>::coordinateType, LO, GO,
255 NO>::Build(coarseCoordMap, fineCoords->getNumVectors());
256 coarseCoords->putScalar(-1.0);
258 coarseCoords->getDataNonConst(0);
260 coarseCoords->getDataNonConst(1);
262 coarseCoords->getDataNonConst(2);
265 LO cntCoarseNodes = 0;
266 for (LO vt = 0; vt < TVertLineId.size(); ++vt) {
268 LO curVertLineId = TVertLineId[vt];
270 if (cx[curVertLineId * myCoarseZLayers] == -1.0 &&
271 cy[curVertLineId * myCoarseZLayers] == -1.0) {
273 for (LO n = 0; n < myCoarseZLayers; ++n) {
274 cx[curVertLineId * myCoarseZLayers + n] = x[vt];
275 cy[curVertLineId * myCoarseZLayers + n] = y[vt];
276 cz[curVertLineId * myCoarseZLayers + n] = myZLayerCoords[n];
278 cntCoarseNodes += myCoarseZLayers;
281 TEUCHOS_TEST_FOR_EXCEPTION(cntCoarseNodes > numLocalCoarseNodes,
283 "number of coarse nodes is inconsistent.");
284 if (cntCoarseNodes == numLocalCoarseNodes)
289 Set(coarseLevel,
"Coordinates", coarseCoords);
298 BuildSemiCoarsenP(
Level &coarseLevel,
const LO NFRows,
const LO NFNodes,
299 const LO DofsPerNode,
const LO NFLayers,
305 using impl_SC =
typename Kokkos::ArithTraits<SC>::val_type;
306 using impl_ATS = Kokkos::ArithTraits<impl_SC>;
307 using LOView1D = Kokkos::View<LO *, DeviceType>;
308 using LOView2D = Kokkos::View<LO **, DeviceType>;
312 const auto FCol2LayerVector =
314 const auto localTemp =
317 if (importer == Teuchos::null)
318 importer = ImportFactory::Build(Amat->getDomainMap(), Amat->getColMap());
321 const auto localTempHost = localTemp->getHostLocalView(Xpetra::Access::ReadWrite);
322 for (
int row = 0; row < NFRows; row++)
323 localTempHost(row, 0) = LayerId[row / DofsPerNode];
324 const auto localTempView = localTemp->getDeviceLocalView(Xpetra::Access::ReadWrite);
325 Kokkos::deep_copy(localTempView, localTempHost);
326 FCol2LayerVector->doImport(*localTemp, *(importer),
Xpetra::INSERT);
328 const auto FCol2LayerView = FCol2LayerVector->getDeviceLocalView(Xpetra::Access::ReadOnly);
329 const auto FCol2Layer = Kokkos::subview(FCol2LayerView, Kokkos::ALL(), 0);
333 const auto FCol2DofVector =
337 const auto localTempHost = localTemp->getHostLocalView(Xpetra::Access::ReadWrite);
338 for (
int row = 0; row < NFRows; row++)
339 localTempHost(row, 0) = row % DofsPerNode;
340 const auto localTempView = localTemp->getDeviceLocalView(Xpetra::Access::ReadWrite);
341 Kokkos::deep_copy(localTempView, localTempHost);
342 FCol2DofVector->doImport(*localTemp, *(importer),
Xpetra::INSERT);
344 const auto FCol2DofView = FCol2DofVector->getDeviceLocalView(Xpetra::Access::ReadOnly);
345 const auto FCol2Dof = Kokkos::subview(FCol2DofView, Kokkos::ALL(), 0);
351 NVertLines = VertLineId[0];
352 for (
int node = 1; node < NFNodes; ++node)
353 if (VertLineId[node] > NVertLines)
354 NVertLines = VertLineId[node];
358 LOView2D LineLayer2Node(
"LineLayer2Node", NVertLines, NFLayers);
359 typename LOView2D::HostMirror LineLayer2NodeHost =
360 Kokkos::create_mirror_view(LineLayer2Node);
361 for (
int node = 0; node < NFNodes; ++node)
362 LineLayer2NodeHost(VertLineId[node], LayerId[node]) = node;
363 Kokkos::deep_copy(LineLayer2Node, LineLayer2NodeHost);
366 LOView1D CLayer2FLayer(
"CLayer2FLayer", NCLayers);
367 typename LOView1D::HostMirror CLayer2FLayerHost =
368 Kokkos::create_mirror_view(CLayer2FLayer);
370 const LO FirstStride =
371 (
LO)ceil(((coordT)(NFLayers + 1)) / ((coordT)(NCLayers + 1)));
372 const coordT RestStride =
373 ((coordT)(NFLayers - FirstStride + 1)) / ((coordT)NCLayers);
375 (
LO)floor((((coordT)(NFLayers - FirstStride + 1)) / RestStride) + .00001);
377 "sizes do not match.");
378 coordT stride = (coordT)FirstStride;
379 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
380 CLayer2FLayerHost(clayer) = (
LO)floor(stride) - 1;
381 stride += RestStride;
383 Kokkos::deep_copy(CLayer2FLayer, CLayer2FLayerHost);
387 int MaxStencilSize = 1;
388 LOView1D CLayer2StartLayer(
"CLayer2StartLayer", NCLayers);
389 LOView1D CLayer2StencilSize(
"CLayer2StencilSize", NCLayers);
390 typename LOView1D::HostMirror CLayer2StartLayerHost =
391 Kokkos::create_mirror_view(CLayer2StartLayer);
392 typename LOView1D::HostMirror CLayer2StencilSizeHost =
393 Kokkos::create_mirror_view(CLayer2StencilSize);
394 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
395 const int startLayer = (clayer > 0) ? CLayer2FLayerHost(clayer - 1) + 1 : 0;
396 const int stencilSize = (clayer < NCLayers - 1)
397 ? CLayer2FLayerHost(clayer + 1) - startLayer
398 : NFLayers - startLayer;
400 if (MaxStencilSize < stencilSize)
401 MaxStencilSize = stencilSize;
402 CLayer2StartLayerHost(clayer) = startLayer;
403 CLayer2StencilSizeHost(clayer) = stencilSize;
405 Kokkos::deep_copy(CLayer2StartLayer, CLayer2StartLayerHost);
406 Kokkos::deep_copy(CLayer2StencilSize, CLayer2StencilSizeHost);
413 int Nmax = MaxStencilSize * DofsPerNode;
414 Kokkos::View<impl_SC ***, DeviceType> BandMat(
415 "BandMat", NVertLines, Nmax, Nmax);
416 Kokkos::View<impl_SC ***, DeviceType> BandSol(
417 "BandSol", NVertLines, Nmax, DofsPerNode);
423 for (
int clayer = 0; clayer < NCLayers; ++clayer)
424 NnzP += CLayer2StencilSizeHost(clayer);
425 NnzP *= NVertLines * DofsPerNode * DofsPerNode;
426 Kokkos::View<impl_SC *, DeviceType> Pvals(
"Pvals", NnzP);
427 Kokkos::View<LO *, DeviceType> Pcols(
"Pcols", NnzP);
432 Kokkos::View<size_t *, DeviceType> Pptr(
"Pptr", NFRows + 1);
433 typename Kokkos::View<size_t *, DeviceType>::HostMirror PptrHost =
434 Kokkos::create_mirror_view(Pptr);
435 Kokkos::deep_copy(PptrHost, 0);
436 for (
int line = 0; line < NVertLines; ++line) {
437 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
438 const int stencilSize = CLayer2StencilSizeHost(clayer);
439 const int startLayer = CLayer2StartLayerHost(clayer);
440 for (
int snode = 0; snode < stencilSize; ++snode) {
441 for (
int dofi = 0; dofi < DofsPerNode; ++dofi) {
442 const int layer = startLayer + snode;
443 const int AmatBlkRow = LineLayer2NodeHost(line, layer);
444 const int AmatRow = AmatBlkRow * DofsPerNode + dofi;
445 PptrHost(AmatRow + 1) += DofsPerNode;
450 for (
int i = 2; i < NFRows + 1; ++i)
451 PptrHost(i) += PptrHost(i - 1);
454 "Number of nonzeros in P does not match");
455 Kokkos::deep_copy(Pptr, PptrHost);
459 Kokkos::View<LO *, Kokkos::DefaultHostExecutionSpace> layerBuckets(
460 "layerBuckets", NFLayers);
461 Kokkos::deep_copy(layerBuckets, 0);
462 LOView2D CLayerSNode2PptrOffset(
"CLayerSNode2PptrOffset", NCLayers,
464 typename LOView2D::HostMirror CLayerSNode2PptrOffsetHost =
465 Kokkos::create_mirror_view(CLayerSNode2PptrOffset);
466 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
467 const int stencilSize = CLayer2StencilSizeHost(clayer);
468 const int startLayer = CLayer2StartLayerHost(clayer);
469 for (
int snode = 0; snode < stencilSize; ++snode) {
470 const int layer = startLayer + snode;
471 CLayerSNode2PptrOffsetHost(clayer, snode) = layerBuckets(layer);
472 layerBuckets(layer)++;
475 Kokkos::deep_copy(CLayerSNode2PptrOffset, CLayerSNode2PptrOffsetHost);
480 const auto localAmat = Amat->getLocalMatrixDevice();
481 const auto zero = impl_ATS::zero();
482 const auto one = impl_ATS::one();
484 using range_policy = Kokkos::RangePolicy<execution_space>;
485 Kokkos::parallel_for(
486 "MueLu::SemiCoarsenPFactory_kokkos::BuildSemiCoarsenP Fill P",
487 range_policy(0, NVertLines), KOKKOS_LAMBDA(
const int line) {
488 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
491 Kokkos::subview(BandSol, line, Kokkos::ALL(), Kokkos::ALL());
492 for (
int row = 0; row < Nmax; ++row)
493 for (
int dof = 0; dof < DofsPerNode; ++dof)
494 bandSol(row, dof) = zero;
497 const int stencilSize = CLayer2StencilSize(clayer);
498 const int N = stencilSize * DofsPerNode;
500 Kokkos::subview(BandMat, line, Kokkos::ALL(), Kokkos::ALL());
501 for (
int row = 0; row < Nmax; ++row)
502 for (
int col = 0; col < Nmax; ++col)
504 (row == col && row >= N) ? one : zero;
507 const int flayer = CLayer2FLayer(clayer);
508 const int startLayer = CLayer2StartLayer(clayer);
509 for (
int snode = 0; snode < stencilSize; ++snode) {
510 const int layer = startLayer + snode;
511 if (layer == flayer) {
512 for (
int dof = 0; dof < DofsPerNode; ++dof) {
513 const int row = snode * DofsPerNode + dof;
514 bandMat(row, row) = one;
515 bandSol(row, dof) = one;
518 const int AmatBlkRow = LineLayer2Node(line, layer);
519 for (
int dofi = 0; dofi < DofsPerNode; ++dofi) {
521 const int AmatRow = AmatBlkRow * DofsPerNode + dofi;
522 const auto localAmatRow = localAmat.rowConst(AmatRow);
523 const int AmatRowLeng = localAmatRow.length;
525 const int row = snode * DofsPerNode + dofi;
526 for (
int dofj = 0; dofj < DofsPerNode; ++dofj) {
527 const int col = snode * DofsPerNode + dofj;
532 for (
int i = 0; i < AmatRowLeng; ++i) {
533 const int colidx = localAmatRow.colidx(i);
534 if (FCol2Layer(colidx) == layer &&
535 FCol2Dof(colidx) == dofj)
536 val += localAmatRow.value(i);
538 bandMat(row, col) = val;
544 for (
int i = 0; i < AmatRowLeng; ++i) {
545 const int colidx = localAmatRow.colidx(i);
546 if (FCol2Layer(colidx) == layer - 1 &&
547 FCol2Dof(colidx) == dofj)
548 val += localAmatRow.value(i);
550 bandMat(row, col - DofsPerNode) = val;
553 if (snode < stencilSize - 1) {
557 for (
int i = 0; i < AmatRowLeng; ++i) {
558 const int colidx = localAmatRow.colidx(i);
559 if (FCol2Layer(colidx) == layer + 1 &&
560 FCol2Dof(colidx) == dofj)
561 val += localAmatRow.value(i);
563 bandMat(row, col + DofsPerNode) = val;
571 namespace KB = KokkosBatched;
572 using lu_type =
typename KB::SerialLU<KB::Algo::LU::Unblocked>;
573 lu_type::invoke(bandMat);
575 typename KB::SerialTrsm<KB::Side::Left, KB::Uplo::Lower,
576 KB::Trans::NoTranspose, KB::Diag::Unit,
577 KB::Algo::Trsm::Unblocked>;
578 trsv_l_type::invoke(one, bandMat, bandSol);
579 using trsv_u_type =
typename KB::SerialTrsm<
580 KB::Side::Left, KB::Uplo::Upper, KB::Trans::NoTranspose,
581 KB::Diag::NonUnit, KB::Algo::Trsm::Unblocked>;
582 trsv_u_type::invoke(one, bandMat, bandSol);
585 for (
int snode = 0; snode < stencilSize; ++snode) {
586 for (
int dofj = 0; dofj < DofsPerNode; ++dofj) {
587 for (
int dofi = 0; dofi < DofsPerNode; ++dofi) {
588 const int layer = startLayer + snode;
589 const int AmatBlkRow = LineLayer2Node(line, layer);
590 const int AmatRow = AmatBlkRow * DofsPerNode + dofi;
592 const int pptrOffset = CLayerSNode2PptrOffset(clayer, snode);
594 Pptr(AmatRow) + pptrOffset * DofsPerNode + dofj;
597 line * NCLayers + clayer;
598 Pcols(pptr) = col * DofsPerNode + dofj;
599 Pvals(pptr) = bandSol(snode * DofsPerNode + dofi, dofj);
611 std::vector<size_t> stridingInfo_{(size_t)DofsPerNode};
613 rowMap->lib(), NCLayers * itemp, NCLayers * NVertLines * DofsPerNode, 0,
614 stridingInfo_, rowMap->getComm(), -1, 0);
615 P =
rcp(
new CrsMatrixWrap(rowMap, coarseMap, 0));
616 RCP<CrsMatrix> PCrs = rcp_dynamic_cast<CrsMatrixWrap>(P)->getCrsMatrix();
617 PCrs->setAllValues(Pptr, Pcols, Pvals);
618 PCrs->expertStaticFillComplete(coarseMap, Amat->getDomainMap());
621 if (Amat->IsView(
"stridedMaps") ==
true)
622 P->CreateView(
"stridedMaps", Amat->getRowMap(
"stridedMaps"), coarseMap);
624 P->CreateView(
"stridedMaps", P->getRangeMap(), coarseMap);
628 MultiVectorFactory::Build(coarseMap, fineNullspace->getNumVectors());
629 const int numVectors = fineNullspace->getNumVectors();
630 const auto fineNullspaceView = fineNullspace->getDeviceLocalView(Xpetra::Access::ReadOnly);
631 const auto coarseNullspaceView = coarseNullspace->getDeviceLocalView(Xpetra::Access::ReadWrite);
632 using range_policy = Kokkos::RangePolicy<execution_space>;
633 Kokkos::parallel_for(
634 "MueLu::SemiCoarsenPFactory_kokkos::BuildSemiCoarsenP Inject Nullspace",
635 range_policy(0, NVertLines), KOKKOS_LAMBDA(
const int line) {
636 for (
int clayer = 0; clayer < NCLayers; ++clayer) {
637 const int layer = CLayer2FLayer(clayer);
638 const int AmatBlkRow =
639 LineLayer2Node(line, layer);
641 line * NCLayers + clayer;
642 for (
int k = 0; k < numVectors; ++k) {
643 for (
int dofi = 0; dofi < DofsPerNode; ++dofi) {
644 const int fRow = AmatBlkRow * DofsPerNode + dofi;
645 const int cRow = col * DofsPerNode + dofi;
646 coarseNullspaceView(cRow, k) = fineNullspaceView(fRow, k);
655 #define MUELU_SEMICOARSENPFACTORY_KOKKOS_SHORT
656 #endif // MUELU_SEMICOARSENPFACTORY_KOKKOS_DEF_HPP
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
ParameterList & setEntry(const std::string &name, U &&entry)
MueLu::DefaultLocalOrdinal LocalOrdinal
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
Timer to be used in factories. Similar to Monitor but with additional timers.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static const NoFactory * get()
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
static const Teuchos::ParameterEntry & getEntry(const std::string &name)
Returns default entry from the "master" list corresponding to the specified name. ...
const RCP< const FactoryManagerBase > GetFactoryManager()
returns the current factory manager
int GetLevelID() const
Return level number.
Exception throws to report errors in the internal logical of the program.
Prolongator factory performing semi-coarsening.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.