46 #ifndef MUELU_COUPLEDAGGREGATIONCOMMHELPER_DEF_HPP 47 #define MUELU_COUPLEDAGGREGATIONCOMMHELPER_DEF_HPP 49 #include <Xpetra_MapFactory.hpp> 50 #include <Xpetra_BlockedMultiVector.hpp> 51 #include <Xpetra_BlockedVector.hpp> 52 #include <Xpetra_VectorFactory.hpp> 53 #include <Xpetra_ImportFactory.hpp> 54 #include <Xpetra_ExportFactory.hpp> 58 #include "MueLu_Utilities.hpp" 62 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
64 import_ = ImportFactory::Build(uniqueMap, nonUniqueMap);
65 tempVec_ = VectorFactory::Build(uniqueMap,
false);
68 myPID_ = uniqueMap->getComm()->getRank();
71 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
73 const RCP<const Map> weightMap = weight_.getMap();
74 const size_t nodeNumElements = weightMap->getNodeNumElements();
75 const RCP<const Teuchos::Comm<int> > & comm = weightMap->getComm();
76 int MyPid = comm->getRank();
80 if (comm->getSize() == 1) {
81 ArrayRCP<SC> serialWeight = weight_.getDataNonConst(0);
82 ArrayRCP<LO> serialProcWinner = procWinner_.getDataNonConst(0);
83 for (
size_t i=0; i < nodeNumElements; ++i) {
84 if (serialWeight[i] > 0) {
86 serialProcWinner[i] = MyPid;
93 #ifdef COMPARE_IN_OUT_VECTORS 94 RCP<Vector> in_weight_ = VectorFactory::Build(weight_.getMap());
96 ArrayRCP<SC> in_weight = in_weight_->getDataNonConst(0);
97 ArrayRCP<SC> weight = weight_.getDataNonConst(0);
98 for (
size_t i=0; i < nodeNumElements; ++i) in_weight[i] = weight[i];
100 RCP<LOVector> in_procWinner_ = LOVectorFactory::Build(procWinner_.getMap());
102 ArrayRCP<LO> in_procWinner = in_procWinner_->getDataNonConst(0);
103 ArrayRCP<LO> procWinner = procWinner_.getDataNonConst(0);
104 for (
size_t i=0; i < nodeNumElements; ++i) in_procWinner[i] = procWinner[i];
106 RCP<LOVector> in_companion;
108 if (companion != NULL) {
109 in_companion = LOVectorFactory::Build(companion->getMap());
110 ArrayRCP<LO> in_comp = in_companion->getDataNonConst(0);
111 ArrayRCP<LO> comp = companion->getDataNonConst(0);
112 for (
size_t i=0; i < nodeNumElements; ++i) in_comp[i] = comp[i];
117 typedef Teuchos::ScalarTraits<SC> ST;
120 if (perturbWt_ == Teuchos::null || !perturbWt_->getMap()->isSameAs(*weightMap)) {
121 perturbWt_ = VectorFactory::Build(weightMap,
false);
125 ST::seedrandom( Teuchos::as<unsigned int>(MyPid*47) );
126 for (
int i = 0; i < 10; ++i) ST::random();
131 ArrayRCP<SC> lperturbWt = perturbWt_->getDataNonConst(0);
132 for (
size_t i=0; i < nodeNumElements; ++i)
133 lperturbWt[i] = 1e-7*fabs(ST::random());
134 #ifdef COMPARE_IN_OUT_VECTORS 135 ArrayRCP<SC> locperturbWt = perturbWt_->getDataNonConst(0);
136 for (
size_t i=0; i < nodeNumElements; ++i)
137 printf(
"perturbWt[%d] = %15.10e\n",i,locperturbWt[i]);
141 ArrayRCP<SC> weight = weight_.getDataNonConst(0);
142 ArrayRCP<SC> perturbWt = perturbWt_->getDataNonConst(0);
146 SC largestGlobalWeight = weight_.normInf();
147 for (
size_t i=0; i < nodeNumElements; ++i) {
148 if (weight[i] != 0.) {
149 weight[i] += largestGlobalWeight*perturbWt[i];
161 if (postComm_ == Teuchos::null || !postComm_->getMap()->isSameAs(*weightMap) )
162 postComm_ = VectorFactory::Build(weightMap);
166 NonUnique2NonUnique(weight_, *postComm_, Xpetra::ABSMAX);
182 if (candidateWinners_ == Teuchos::null || !candidateWinners_->getMap()->isSameAs(*weightMap) )
183 candidateWinners_ = VectorFactory::Build(weightMap,
false);
186 ArrayRCP<SC> weight = weight_.getDataNonConst(0);
189 ArrayRCP<SC> candidateWinners = candidateWinners_->getDataNonConst(0);
190 ArrayRCP<SC> postComm = postComm_->getDataNonConst(0);
191 for (
size_t i=0; i < nodeNumElements; ++i) {
192 if (postComm[i] == weight[i]) candidateWinners[i] = (SC) MyPid+1;
193 else candidateWinners[i] = 0;
194 weight[i]=postComm[i];
197 NonUnique2NonUnique(*candidateWinners_, *postComm_, Xpetra::ABSMAX);
204 int numMyWinners = 0;
205 ArrayRCP<LO> procWinner = procWinner_.getDataNonConst(0);
207 ArrayRCP<SC> postComm = postComm_->getDataNonConst(0);
208 for (
size_t i=0; i < nodeNumElements; ++i) {
209 if ( weight[i] != 0.) procWinner[i] = ((int) (postComm[i])) - 1;
212 if (procWinner[i] == MyPid) ++numMyWinners;
216 weight = Teuchos::null;
218 if (companion != NULL) {
229 ArrayView<const GO> myGids = weightMap->getNodeElementList();
231 if (numMyWinners != numMyWinners_ || winnerMap_ == Teuchos::null) {
233 myWinners_ = ArrayRCP<GO>(numMyWinners);
236 numMyWinners_ = numMyWinners;
240 procWinner = Teuchos::null;
241 std::cout << MyPid <<
": nodeNumElements=" << nodeNumElements << std::endl;
242 std::cout << MyPid <<
": procWinner=" << procWinner_ << std::endl;
243 procWinner = procWinner_.getDataNonConst(0);
249 for (
size_t i = 0; i < nodeNumElements; ++i) {
250 if (procWinner[i] == MyPid) {
251 myWinners_[numMyWinners++] = myGids[i];
257 bool entryMismatch=
false;
259 for (
size_t i = 0; i < nodeNumElements; ++i) {
260 if (procWinner[i] == MyPid) {
261 if (myWinners_[numMyWinners++] != myGids[i]) {
268 if (entryMismatch ==
true) {
272 for (
size_t i = 0; i < nodeNumElements; ++i) {
273 if (procWinner[i] == MyPid) {
274 myWinners_[numMyWinners++] = myGids[i];
280 procWinner = Teuchos::null;
283 std::cout << MyPid <<
": numMyWinners=" << numMyWinners << std::endl;
284 std::cout << MyPid <<
": myWinners_" << myWinners_ << std::endl;
285 for(
int i=0;i<numMyWinners; i++)
286 std::cout << MyPid <<
": myWinners_[locId=" << i <<
"] = " << myWinners_[i] << std::endl;
292 int irealloc,orealloc;
293 if (realloc) irealloc=1;
296 if (orealloc == 1) realloc=
true;
302 const Xpetra::global_size_t GSTI = Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid();
303 winnerMap_ = MapFactory::Build(weightMap->lib(), GSTI, myWinners_(), 0, weightMap->getComm());
309 RCP<LOVector> justWinners = LOVectorFactory::Build(winnerMap_);
312 RCP<Teuchos::FancyOStream> out = rcp(
new Teuchos::FancyOStream(rcp(&std::cout,
false)));
313 std::cout << MyPid <<
": justWinners(Vector in)=" << *justWinners << std::endl;
314 justWinners->describe(*out, Teuchos::VERB_EXTREME);
317 if ( winnerImport_ == Teuchos::null
318 || !winnerImport_->getSourceMap()->isSameAs(*weightMap)
319 || !winnerImport_->getTargetMap()->isSameAs(*winnerMap_) )
320 winnerImport_ = ImportFactory::Build(weightMap, winnerMap_);
321 RCP<const Import> winnerImport = winnerImport_;
324 justWinners->doImport(*companion, *winnerImport, Xpetra::INSERT);
326 catch(std::exception& e)
328 std::cout << MyPid <<
": ERR2: An exception occurred." << std::endl;
337 RCP<Teuchos::FancyOStream> fos = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
338 fos->setOutputToRootOnly(-1);
339 if (!weightMap->getComm()->getRank())
340 std::cout <<
"------ winnerMap_ ------" << std::endl;
341 winnerMap_->describe(*fos,Teuchos::VERB_EXTREME);
342 if (!weightMap->getComm()->getRank())
343 std::cout <<
"------ weightMap ------" << std::endl;
344 weightMap->getComm()->barrier();
345 weightMap->describe(*fos,Teuchos::VERB_EXTREME);
355 if ( pushWinners_ == Teuchos::null
356 || !pushWinners_->getSourceMap()->isSameAs(*winnerMap_)
357 || !pushWinners_->getTargetMap()->isSameAs(*weightMap) )
358 pushWinners_ = ImportFactory::Build(winnerMap_,weightMap);
359 RCP<Import> pushWinners = pushWinners_;
364 companion->doImport(*justWinners, *pushWinners, Xpetra::INSERT);
373 catch(std::exception& e)
384 std::cout << MyPid <<
": numMyWinners=" << numMyWinners << std::endl;
386 std::cout << MyPid <<
": justWinners(Vector in)=" << std::endl;
387 justWinners->describe(*out, Teuchos::VERB_EXTREME);
389 std::cout << MyPid <<
": companion(Vector out)=" << std::endl;
390 companion->describe(*out, Teuchos::VERB_EXTREME);
393 std::cout << MyPid <<
": winnerMap_=" << *winnerMap_ << std::endl;
394 std::cout << MyPid <<
": weight_.Map=" << *weightMap << std::endl;
400 #ifdef COMPARE_IN_OUT_VECTORS 402 std::cout <<
"==============" << std::endl;
403 std::cout <<
"call #" << numCalls <<
" (1-based)" << std::endl;
404 std::cout <<
"==============" << std::endl;
411 std::string sameOrDiff;
413 ArrayRCP<SC> in_weight = in_weight_->getDataNonConst(0);
414 ArrayRCP<SC> weight = weight_.getDataNonConst(0);
415 if (MyPid == 0) std::cout <<
"==============\nweight\n==============\n" << std::endl;
416 for (
size_t i=0; i < weight_.getLocalLength(); ++i) {
417 if (in_weight[i] - weight[i] != 0) sameOrDiff =
" <<<<";
418 else sameOrDiff =
" ";
419 std::cout << std::setw(3) << i<<
": " << in_weight[i] <<
" " << weight[i] << sameOrDiff << in_weight[i] - weight[i] << std::endl;
432 ArrayRCP<LO> in_procWinner = in_procWinner_->getDataNonConst(0);
433 ArrayRCP<LO> procWinner = procWinner_.getDataNonConst(0);
434 if (MyPid == 0) std::cout <<
"==============\nprocWinner\n==============\n" << std::endl;
435 for (
size_t i=0; i < procWinner_.getLocalLength(); ++i) {
436 if (in_procWinner[i] != procWinner[i]) sameOrDiff =
" <<<<";
437 else sameOrDiff =
" ";
438 std::cout << std::setw(3) << i<<
": " << in_procWinner[i] <<
" " << procWinner[i] << sameOrDiff << std::endl;
451 if (companion != NULL) {
452 ArrayRCP<LO> in_comp = in_companion->getDataNonConst(0);
453 ArrayRCP<LO> comp = companion->getDataNonConst(0);
454 if (MyPid == 0) std::cout <<
"==============\ncompanion\n==============\n" << std::endl;
455 for (
size_t i=0; i < companion->getLocalLength(); ++i) {
456 if (in_comp[i] != comp[i]) sameOrDiff =
" <<<<";
457 else sameOrDiff =
" ";
458 std::cout << std::setw(3) << i<<
": " << in_comp[i] <<
" " << comp[i] << sameOrDiff << std::endl;
473 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
475 tempVec_->putScalar(0.);
479 tempVec_->doExport(source, *import_, what);
480 dest.doImport(*tempVec_, *import_, Xpetra::INSERT);
482 catch(std::exception& e)
484 int MyPid = tempVec_->getMap()->getComm()->getRank();
485 std::cout << MyPid <<
": ERR1: An exception occurred." << std::endl;
492 #endif // MUELU_COUPLEDAGGREGATIONCOMMHELPER_DEF_HPP #define MueLu_maxAll(rcpComm, in, out)
Namespace for MueLu classes and methods.
void NonUnique2NonUnique(const Vector &source, Vector &dest, const Xpetra::CombineMode what) const
Redistribute data in source to dest where both source and dest might have multiple copies of the same...
CoupledAggregationCommHelper(const RCP< const Map > &uniqueMap, const RCP< const Map > &nonUniqueMap)
Constructor.
void realloc(DynRankView< T, P... > &v, const size_t n0=~size_t(0), const size_t n1=~size_t(0), const size_t n2=~size_t(0), const size_t n3=~size_t(0), const size_t n4=~size_t(0), const size_t n5=~size_t(0), const size_t n6=~size_t(0), const size_t n7=~size_t(0))
void ArbitrateAndCommunicate(Vector &weights, Aggregates &aggregates, const bool perturb) const
This method assigns unknowns to aggregates.