10 #ifndef MUELU_BRICKAGGREGATIONFACTORY_DEF_HPP_
11 #define MUELU_BRICKAGGREGATIONFACTORY_DEF_HPP_
16 #include <Teuchos_CommHelpers.hpp>
22 #include <Xpetra_Map.hpp>
23 #include <Xpetra_MapFactory.hpp>
26 #include <Xpetra_MultiVectorFactory.hpp>
28 #include "MueLu_Aggregates.hpp"
32 #include "MueLu_Utilities.hpp"
33 #include "MueLu_LWGraph.hpp"
35 #include "MueLu_LWGraph.hpp"
39 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
43 #define SET_VALID_ENTRY(name) validParamList->setEntry(name, MasterList::getEntry(name))
50 #undef SET_VALID_ENTRY
54 return validParamList;
57 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
59 Input(currentLevel,
"A");
60 Input(currentLevel,
"Coordinates");
107 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
115 RCP<Matrix> A = Get<RCP<Matrix> >(currentLevel,
"A");
121 int numProcs = comm->getSize();
122 int myRank = comm->getRank();
124 int numPoints = colMap->getLocalNumElements();
126 bx_ = pL.
get<
int>(
"aggregation: brick x size");
127 by_ = pL.
get<
int>(
"aggregation: brick y size");
128 bz_ = pL.
get<
int>(
"aggregation: brick z size");
130 dirichletX_ = pL.
get<
bool>(
"aggregation: brick x Dirichlet");
131 dirichletY_ = pL.
get<
bool>(
"aggregation: brick y Dirichlet");
132 dirichletZ_ = pL.
get<
bool>(
"aggregation: brick z Dirichlet");
133 if (dirichletX_) GetOStream(
Runtime0) <<
"Dirichlet boundaries in the x direction" << std::endl;
134 if (dirichletY_) GetOStream(
Runtime0) <<
"Dirichlet boundaries in the y direction" << std::endl;
135 if (dirichletZ_) GetOStream(
Runtime0) <<
"Dirichlet boundaries in the z direction" << std::endl;
151 Setup(comm, overlappedCoords, colMap);
153 GetOStream(
Runtime0) <<
"Using brick size: " << bx_
154 << (nDim_ > 1 ?
"x " +
toString(by_) :
"")
155 << (nDim_ > 2 ?
"x " +
toString(bz_) :
"") << std::endl;
158 BuildGraph(currentLevel, A);
174 std::set<GO> myAggGIDs, remoteAggGIDs;
175 for (LO LID = 0; LID < numPoints; LID++) {
176 GO aggGID = getAggGID(LID);
178 if (aggGID == GO_INVALID)
continue;
181 if ((revMap_.find(getRoot(LID)) != revMap_.end()) && rowMap->isNodeGlobalElement(colMap->getGlobalElement(revMap_[getRoot(LID)]))) {
183 vertex2AggId[LID] = aggGID;
184 myAggGIDs.insert(aggGID);
190 remoteAggGIDs.insert(aggGID);
193 size_t numAggregates = myAggGIDs.size();
194 size_t numRemote = remoteAggGIDs.size();
197 std::map<GO, LO> AggG2L;
198 std::map<GO, int> AggG2R;
200 Array<GO> myAggGIDsArray(numAggregates), remoteAggGIDsArray(numRemote);
204 for (
typename std::set<GO>::const_iterator it = myAggGIDs.begin(); it != myAggGIDs.end(); it++) {
206 AggG2R[*it] = myRank;
208 myAggGIDsArray[ind++] = *it;
213 myAggGIDsArray, 0, comm);
216 for (
typename std::set<GO>::const_iterator it = remoteAggGIDs.begin(); it != remoteAggGIDs.end(); it++)
217 remoteAggGIDsArray[ind++] = *it;
222 aggMap->getRemoteIndexList(remoteAggGIDsArray, remoteProcIDs, remoteLIDs);
225 for (
size_t i = 0; i < numRemote; i++) {
226 AggG2L[remoteAggGIDsArray[i]] = remoteLIDs[i];
227 AggG2R[remoteAggGIDsArray[i]] = remoteProcIDs[i];
231 for (LO LID = 0; LID < numPoints; LID++) {
232 if (revMap_.find(getRoot(LID)) != revMap_.end() && rowMap->isNodeGlobalElement(colMap->getGlobalElement(revMap_[getRoot(LID)]))) {
233 GO aggGID = vertex2AggId[LID];
235 vertex2AggId[LID] = AggG2L[aggGID];
236 procWinner[LID] = AggG2R[aggGID];
245 Set(currentLevel,
"Aggregates", aggregates);
250 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
253 nDim_ = coords->getNumVectors();
255 x_ = coords->getData(0);
256 xMap_ = Construct1DMap(comm, x_);
261 y_ = coords->getData(1);
262 yMap_ = Construct1DMap(comm, y_);
268 z_ = coords->getData(2);
269 zMap_ = Construct1DMap(comm, z_);
273 for (
size_t ind = 0; ind < coords->getLocalLength(); ind++) {
274 GO i = (*xMap_)[(coords->getData(0))[ind]], j = 0, k = 0;
276 j = (*yMap_)[(coords->getData(1))[ind]];
278 k = (*zMap_)[(coords->getData(2))[ind]];
280 revMap_[k * ny_ * nx_ + j * nx_ + i] = ind;
284 int xboost = dirichletX_ ? 1 : 0;
285 int yboost = dirichletY_ ? 1 : 0;
286 int zboost = dirichletZ_ ? 1 : 0;
287 naggx_ = (nx_ - 2 * xboost) / bx_ + ((nx_ - 2 * xboost) % bx_ ? 1 : 0);
290 naggy_ = (ny_ - 2 * yboost) / by_ + ((ny_ - 2 * yboost) % by_ ? 1 : 0);
295 naggz_ = (nz_ - 2 * zboost) / bz_ + ((nz_ - 2 * zboost) % bz_ ? 1 : 0);
300 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
309 for (
int i = 0; i < n; i++)
316 int numProcs = comm->getSize();
320 MPI_Comm rawComm = (*dupMpiComm->getRawMpiComm())();
322 int sendCnt = gMap->size(), cnt = 0, recvSize;
323 Array<int> recvCnt(numProcs), Displs(numProcs);
327 for (
typename container::const_iterator cit = gMap->begin(); cit != gMap->end(); cit++)
328 sendBuf[cnt++] = Teuchos::as<double>(STS::real(cit->first));
330 MPI_Allgather(&sendCnt, 1, MPI_INT, recvCnt.getRawPtr(), 1, MPI_INT, rawComm);
332 for (
int i = 0; i < numProcs - 1; i++)
333 Displs[i + 1] = Displs[i] + recvCnt[i];
334 recvSize = Displs[numProcs - 1] + recvCnt[numProcs - 1];
336 MPI_Allgatherv(sendBuf.
getRawPtr(), sendCnt, MPI_DOUBLE, recvBuf.
getRawPtr(), recvCnt.getRawPtr(), Displs.getRawPtr(), MPI_DOUBLE, rawComm);
338 for (
int i = 0; i < recvSize; i++)
339 (*gMap)[as<SC>(recvBuf[i])] = 0;
344 for (
typename container::iterator it = gMap->begin(); it != gMap->end(); it++)
350 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
353 getIJK(LID, i, j, k);
355 return (k * ny_ * nx_ + j * nx_ + i) == getRoot(LID);
358 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
360 bool boundary =
false;
362 getIJK(LID, i, j, k);
363 if (dirichletX_ && (i == 0 || i == nx_ - 1))
365 if (nDim_ > 1 && dirichletY_ && (j == 0 || j == ny_ - 1))
367 if (nDim_ > 2 && dirichletZ_ && (k == 0 || k == nz_ - 1))
373 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
375 if (isDirichlet(LID))
378 int aggI, aggJ, aggK;
379 getAggIJK(LID, aggI, aggJ, aggK);
380 int xboost = dirichletX_ ? 1 : 0;
381 int yboost = dirichletY_ ? 1 : 0;
382 int zboost = dirichletZ_ ? 1 : 0;
384 int i = xboost + aggI * bx_ + (bx_ - 1) / 2;
385 int j = (nDim_ > 1) ? yboost + aggJ * by_ + (by_ - 1) / 2 : 0;
386 int k = (nDim_ > 2) ? zboost + aggK * bz_ + (bz_ - 1) / 2 : 0;
388 return k * ny_ * nx_ + j * nx_ + i;
391 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
393 i = (*xMap_)[x_[LID]];
394 j = (nDim_ > 1) ? (*yMap_)[y_[LID]] : 0;
395 k = (nDim_ > 2) ? (*zMap_)[z_[LID]] : 0;
398 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
400 int xboost = dirichletX_ ? 1 : 0;
401 int yboost = dirichletY_ ? 1 : 0;
402 int zboost = dirichletZ_ ? 1 : 0;
403 int pointI, pointJ, pointK;
404 getIJK(LID, pointI, pointJ, pointK);
405 i = (pointI - xboost) / bx_;
408 j = (pointJ - yboost) / by_;
413 k = (pointK - zboost) / bz_;
418 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
420 bool boundary =
false;
423 getIJK(LID, i, j, k);
425 getAggIJK(LID, ii, jj, kk);
427 if (dirichletX_ && (i == 0 || i == nx_ - 1)) boundary =
true;
428 if (nDim_ > 1 && dirichletY_ && (j == 0 || j == ny_ - 1)) boundary =
true;
429 if (nDim_ > 2 && dirichletZ_ && (k == 0 || k == nz_ - 1)) boundary =
true;
441 return Teuchos::as<GlobalOrdinal>(kk * naggy_ * naggx_) + Teuchos::as<GlobalOrdinal>(jj * naggx_) + ii;
444 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
447 double dirichletThreshold = 0.0;
449 if (bx_ > 1 && (nDim_ <= 1 || by_ > 1) && (nDim_ <= 2 || bz_ > 1)) {
450 FactoryMonitor m(*
this,
"Generating Graph (trivial)", currentLevel);
458 GO numLocalBoundaryNodes = 0;
459 GO numGlobalBoundaryNodes = 0;
460 for (
size_t i = 0; i < boundaryNodes.size(); ++i)
461 if (boundaryNodes(i))
462 numLocalBoundaryNodes++;
464 MueLu_sumAll(comm, numLocalBoundaryNodes, numGlobalBoundaryNodes);
465 GetOStream(
Statistics1) <<
"Detected " << numGlobalBoundaryNodes <<
" Dirichlet nodes" << std::endl;
467 Set(currentLevel,
"DofsPerNode", 1);
468 Set(currentLevel,
"Graph", graph);
469 Set(currentLevel,
"Filtering",
false);
475 bool drop_x = (bx_ == 1);
476 bool drop_y = (nDim_ > 1 && by_ == 1);
477 bool drop_z = (nDim_ > 2 && bz_ == 1);
479 typename LWGraph::row_type::non_const_type
rows(
"rows", A->getLocalNumRows() + 1);
480 typename LWGraph::entries_type::non_const_type columns(
"columns", A->getLocalNumEntries());
482 size_t N = A->getRowMap()->getLocalNumElements();
485 auto G = A->getLocalMatrixHost().graph;
486 auto rowptr = G.row_map;
487 auto colind = G.entries;
491 for (
size_t row = 0; row < N; row++) {
494 LO row2 = A->getColMap()->getLocalElement(A->getRowMap()->getGlobalElement(row));
495 getIJK(row2, ir, jr, kr);
497 for (
size_t cidx = rowptr[row]; cidx < rowptr[row + 1]; cidx++) {
499 LO col = colind[cidx];
500 getIJK(col, ic, jc, kc);
502 if ((row2 != col) && ((drop_x && ir != ic) || (drop_y && jr != jc) || (drop_z && kr != kc))) {
521 GO numLocalBoundaryNodes = 0;
522 GO numGlobalBoundaryNodes = 0;
523 for (
size_t i = 0; i < boundaryNodes.size(); ++i)
524 if (boundaryNodes(i))
525 numLocalBoundaryNodes++;
527 MueLu_sumAll(comm, numLocalBoundaryNodes, numGlobalBoundaryNodes);
528 GetOStream(
Statistics1) <<
"Detected " << numGlobalBoundaryNodes <<
" Dirichlet nodes" << std::endl;
530 Set(currentLevel,
"DofsPerNode", 1);
531 Set(currentLevel,
"Graph", graph);
532 Set(currentLevel,
"Filtering",
true);
#define MueLu_sumAll(rcpComm, in, out)
void getAggIJK(LocalOrdinal LID, int &i, int &j, int &k) const
MueLu::DefaultLocalOrdinal LocalOrdinal
std::string toString(const T &what)
Little helper function to convert non-string types to strings.
const RCP< LOVector > & GetProcWinner() const
Returns constant vector that maps local node IDs to owning processor IDs.
KOKKOS_INLINE_FUNCTION void SetBoundaryNodeMap(const boundary_nodes_type bndry)
Set boolean array indicating which rows correspond to Dirichlet boundaries.
Container class for aggregation information.
T & get(const std::string &name, T def_value)
Timer to be used in factories. Similar to Monitor but with additional timers.
One-liner description of what is happening.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
void Build(Level ¤tLevel) const
Build aggregates.
void SetIsRoot(LO i, bool value=true)
Set root node information.
GlobalOrdinal getAggGID(LocalOrdinal LID) const
std::map< Scalar, GlobalOrdinal, compare > container
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MueLu::DefaultGlobalOrdinal GlobalOrdinal
Class that holds all level-specific information.
void Setup(const RCP< const Teuchos::Comm< int > > &comm, const RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LO, GO, NO > > &coords, const RCP< const Map > &map) const
bool isDirichlet(LocalOrdinal LID) const
#define MUELU_UNAGGREGATED
virtual void setObjectLabel(const std::string &objectLabel)
const RCP< LOMultiVector > & GetVertex2AggId() const
Returns constant vector that maps local node IDs to local aggregates IDs.
void resize(size_type new_size, const value_type &x=value_type())
bool isRoot(LocalOrdinal LID) const
void getIJK(LocalOrdinal LID, int &i, int &j, int &k) const
Lightweight MueLu representation of a compressed row storage graph.
RCP< container > Construct1DMap(const RCP< const Teuchos::Comm< int > > &comm, const ArrayRCP< const typename Teuchos::ScalarTraits< Scalar >::magnitudeType > &x) const
KOKKOS_INLINE_FUNCTION void AggregatesCrossProcessors(const bool &flag)
Record whether aggregates include DOFs from other processes.
static Kokkos::View< bool *, typename Kokkos::HostSpace > DetectDirichletRows_kokkos_host(const Matrix &A, const Magnitude &tol=Teuchos::ScalarTraits< typename Teuchos::ScalarTraits< SC >::magnitudeType >::zero(), const bool count_twos_as_dirichlet=false)
std::string description() const
Return a simple one-line description of this object.
void BuildGraph(Level ¤tLevel, const RCP< Matrix > &A) const
GlobalOrdinal getRoot(LocalOrdinal LID) const
void DeclareInput(Level ¤tLevel) const
Input.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void SetNumAggregates(LO nAggregates)
Set number of local aggregates on current processor.
#define SET_VALID_ENTRY(name)