42 #include "Tpetra_Distributor.hpp"
43 #include "Teuchos_StandardParameterEntryValidators.hpp"
44 #include "Teuchos_VerboseObjectParameterListHelpers.hpp"
50 DistributorSendTypeEnumToString (EDistributorSendType sendType)
52 if (sendType == DISTRIBUTOR_ISEND) {
55 else if (sendType == DISTRIBUTOR_RSEND) {
58 else if (sendType == DISTRIBUTOR_SEND) {
61 else if (sendType == DISTRIBUTOR_SSEND) {
65 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
"Invalid "
66 "EDistributorSendType enum value " << sendType <<
".");
74 Array<std::string> sendTypes;
75 sendTypes.push_back (
"Isend");
76 sendTypes.push_back (
"Rsend");
77 sendTypes.push_back (
"Send");
78 sendTypes.push_back (
"Ssend");
88 const bool tpetraDistributorDebugDefault =
false;
90 const bool barrierBetween_default =
false;
92 const bool useDistinctTags_default =
true;
95 int Distributor::getTag (
const int pathTag)
const {
96 return useDistinctTags_ ? pathTag : comm_->getTag ();
100 #ifdef TPETRA_DISTRIBUTOR_TIMERS
101 void Distributor::makeTimers () {
102 const std::string name_doPosts3 =
"Tpetra::Distributor: doPosts(3)";
103 const std::string name_doPosts4 =
"Tpetra::Distributor: doPosts(4)";
104 const std::string name_doWaits =
"Tpetra::Distributor: doWaits";
105 const std::string name_doPosts3_recvs =
"Tpetra::Distributor: doPosts(3): recvs";
106 const std::string name_doPosts4_recvs =
"Tpetra::Distributor: doPosts(4): recvs";
107 const std::string name_doPosts3_barrier =
"Tpetra::Distributor: doPosts(3): barrier";
108 const std::string name_doPosts4_barrier =
"Tpetra::Distributor: doPosts(4): barrier";
109 const std::string name_doPosts3_sends =
"Tpetra::Distributor: doPosts(3): sends";
110 const std::string name_doPosts4_sends =
"Tpetra::Distributor: doPosts(4): sends";
112 timer_doPosts3_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts3);
113 timer_doPosts4_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts4);
114 timer_doWaits_ = Teuchos::TimeMonitor::getNewTimer (name_doWaits);
115 timer_doPosts3_recvs_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts3_recvs);
116 timer_doPosts4_recvs_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts4_recvs);
117 timer_doPosts3_barrier_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts3_barrier);
118 timer_doPosts4_barrier_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts4_barrier);
119 timer_doPosts3_sends_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts3_sends);
120 timer_doPosts4_sends_ = Teuchos::TimeMonitor::getNewTimer (name_doPosts4_sends);
122 #endif // TPETRA_DISTRIBUTOR_TIMERS
125 Distributor::init (
const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
126 const Teuchos::RCP<Teuchos::ParameterList>& plist)
128 this->setVerbLevel (debug_ ? Teuchos::VERB_EXTREME : Teuchos::VERB_NONE);
129 this->setOStream (out_);
130 if (! plist.is_null ()) {
136 #ifdef TPETRA_DISTRIBUTOR_TIMERS
138 #endif // TPETRA_DISTRIBUTOR_TIMERS
141 Teuchos::OSTab tab (out_);
142 std::ostringstream os;
143 os << comm_->getRank ()
144 <<
": Distributor ctor done" << std::endl;
151 , out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr)))
152 , sendType_ (Details::DISTRIBUTOR_SEND)
153 , barrierBetween_ (barrierBetween_default)
154 , debug_ (tpetraDistributorDebugDefault)
156 , selfMessage_ (false)
160 , totalReceiveLength_ (0)
161 , useDistinctTags_ (useDistinctTags_default)
163 init (comm, Teuchos::null);
167 const Teuchos::RCP<Teuchos::FancyOStream>& out)
169 , out_ (out.is_null () ? Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr)) : out)
170 , sendType_ (Details::DISTRIBUTOR_SEND)
171 , barrierBetween_ (barrierBetween_default)
172 , debug_ (tpetraDistributorDebugDefault)
174 , selfMessage_ (false)
178 , totalReceiveLength_ (0)
179 , useDistinctTags_ (useDistinctTags_default)
181 init (comm, Teuchos::null);
185 const Teuchos::RCP<Teuchos::ParameterList>& plist)
187 , out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr)))
188 , sendType_ (Details::DISTRIBUTOR_SEND)
189 , barrierBetween_ (barrierBetween_default)
190 , debug_ (tpetraDistributorDebugDefault)
192 , selfMessage_ (false)
196 , totalReceiveLength_ (0)
197 , useDistinctTags_ (useDistinctTags_default)
203 const Teuchos::RCP<Teuchos::FancyOStream>& out,
204 const Teuchos::RCP<Teuchos::ParameterList>& plist)
206 , out_ (out.is_null () ? Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr)) : out)
207 , sendType_ (Details::DISTRIBUTOR_SEND)
208 , barrierBetween_ (barrierBetween_default)
209 , debug_ (tpetraDistributorDebugDefault)
211 , selfMessage_ (false)
215 , totalReceiveLength_ (0)
216 , useDistinctTags_ (useDistinctTags_default)
222 : comm_ (distributor.comm_)
223 , out_ (distributor.out_)
224 , sendType_ (distributor.sendType_)
225 , barrierBetween_ (distributor.barrierBetween_)
226 , debug_ (distributor.debug_)
227 , numExports_ (distributor.numExports_)
228 , selfMessage_ (distributor.selfMessage_)
229 , numSends_ (distributor.numSends_)
230 , imagesTo_ (distributor.imagesTo_)
231 , startsTo_ (distributor.startsTo_)
232 , lengthsTo_ (distributor.lengthsTo_)
233 , maxSendLength_ (distributor.maxSendLength_)
234 , indicesTo_ (distributor.indicesTo_)
235 , numReceives_ (distributor.numReceives_)
236 , totalReceiveLength_ (distributor.totalReceiveLength_)
237 , lengthsFrom_ (distributor.lengthsFrom_)
238 , imagesFrom_ (distributor.imagesFrom_)
239 , startsFrom_ (distributor.startsFrom_)
240 , indicesFrom_ (distributor.indicesFrom_)
241 , reverseDistributor_ (distributor.reverseDistributor_)
242 , useDistinctTags_ (distributor.useDistinctTags_)
244 using Teuchos::ParameterList;
245 using Teuchos::parameterList;
249 this->setVerbLevel (distributor.getVerbLevel ());
250 this->setOStream (out_);
260 RCP<const ParameterList> rhsList = distributor.getParameterList ();
261 if (! rhsList.is_null ()) {
262 this->setMyParamList (parameterList (* rhsList));
265 #ifdef TPETRA_DISTRIBUTOR_TIMERS
267 #endif // TPETRA_DISTRIBUTOR_TIMERS
270 Teuchos::OSTab tab (out_);
271 std::ostringstream os;
272 os << comm_->getRank ()
273 <<
": Distributor copy ctor done" << std::endl;
279 using Teuchos::ParameterList;
280 using Teuchos::parameterList;
283 std::swap (comm_, rhs.comm_);
284 std::swap (out_, rhs.out_);
285 std::swap (sendType_, rhs.sendType_);
286 std::swap (barrierBetween_, rhs.barrierBetween_);
287 std::swap (debug_, rhs.debug_);
288 std::swap (numExports_, rhs.numExports_);
289 std::swap (selfMessage_, rhs.selfMessage_);
290 std::swap (numSends_, rhs.numSends_);
291 std::swap (imagesTo_, rhs.imagesTo_);
292 std::swap (startsTo_, rhs.startsTo_);
293 std::swap (lengthsTo_, rhs.lengthsTo_);
294 std::swap (maxSendLength_, rhs.maxSendLength_);
295 std::swap (indicesTo_, rhs.indicesTo_);
296 std::swap (numReceives_, rhs.numReceives_);
297 std::swap (totalReceiveLength_, rhs.totalReceiveLength_);
298 std::swap (lengthsFrom_, rhs.lengthsFrom_);
299 std::swap (imagesFrom_, rhs.imagesFrom_);
300 std::swap (startsFrom_, rhs.startsFrom_);
301 std::swap (indicesFrom_, rhs.indicesFrom_);
302 std::swap (reverseDistributor_, rhs.reverseDistributor_);
303 std::swap (useDistinctTags_, rhs.useDistinctTags_);
306 const Teuchos::EVerbosityLevel lhsVerb = this->getVerbLevel ();
307 const Teuchos::EVerbosityLevel rhsVerb = rhs.getVerbLevel ();
308 this->setVerbLevel (rhsVerb);
309 rhs.setVerbLevel (lhsVerb);
313 this->setOStream (out_);
314 rhs.setOStream (rhs.out_);
318 RCP<ParameterList> lhsList = this->getNonconstParameterList ();
319 RCP<ParameterList> rhsList = rhs.getNonconstParameterList ();
320 if (lhsList.getRawPtr () == rhsList.getRawPtr () && ! rhsList.is_null ()) {
321 rhsList = parameterList (*rhsList);
323 if (! rhsList.is_null ()) {
324 this->setMyParamList (rhsList);
326 if (! lhsList.is_null ()) {
327 rhs.setMyParamList (lhsList);
338 TEUCHOS_TEST_FOR_EXCEPTION(requests_.size() != 0, std::runtime_error,
339 "Tpetra::Distributor: Destructor called with " << requests_.size()
340 <<
" outstanding posts (unfulfilled communication requests). There "
341 "should be none at this point. Please report this bug to the Tpetra "
348 using Teuchos::FancyOStream;
349 using Teuchos::getIntegralValue;
350 using Teuchos::includesVerbLevel;
351 using Teuchos::OSTab;
352 using Teuchos::ParameterList;
353 using Teuchos::parameterList;
358 plist->validateParametersAndSetDefaults (*validParams);
360 const bool barrierBetween =
361 plist->get<
bool> (
"Barrier between receives and sends");
362 const Details::EDistributorSendType sendType =
363 getIntegralValue<Details::EDistributorSendType> (*plist,
"Send type");
364 const bool useDistinctTags = plist->get<
bool> (
"Use distinct tags");
365 const bool debug = plist->get<
bool> (
"Debug");
371 TEUCHOS_TEST_FOR_EXCEPTION(
372 ! barrierBetween && sendType == Details::DISTRIBUTOR_RSEND,
373 std::invalid_argument,
"Tpetra::Distributor::setParameterList: " << endl
374 <<
"You specified \"Send type\"=\"Rsend\", but turned off the barrier "
375 "between receives and sends." << endl <<
"This is invalid; you must "
376 "include the barrier if you use ready sends." << endl <<
"Ready sends "
377 "require that their corresponding receives have already been posted, "
378 "and the only way to guarantee that in general is with a barrier.");
380 if (plist->isSublist (
"VerboseObject")) {
384 Teuchos::readVerboseObjectSublist (&*plist,
this);
388 sendType_ = sendType;
389 barrierBetween_ = barrierBetween;
390 useDistinctTags_ = useDistinctTags;
395 this->setMyParamList (plist);
398 Teuchos::RCP<const Teuchos::ParameterList>
401 using Teuchos::Array;
402 using Teuchos::ParameterList;
403 using Teuchos::parameterList;
405 using Teuchos::setStringToIntegralParameter;
407 const bool barrierBetween = barrierBetween_default;
408 const bool useDistinctTags = useDistinctTags_default;
409 const bool debug = tpetraDistributorDebugDefault;
412 const std::string defaultSendType (
"Send");
413 Array<Details::EDistributorSendType> sendTypeEnums;
414 sendTypeEnums.push_back (Details::DISTRIBUTOR_ISEND);
415 sendTypeEnums.push_back (Details::DISTRIBUTOR_RSEND);
416 sendTypeEnums.push_back (Details::DISTRIBUTOR_SEND);
417 sendTypeEnums.push_back (Details::DISTRIBUTOR_SSEND);
419 RCP<ParameterList> plist = parameterList (
"Tpetra::Distributor");
420 plist->set (
"Barrier between receives and sends", barrierBetween,
421 "Whether to execute a barrier between receives and sends in do"
422 "[Reverse]Posts(). Required for correctness when \"Send type\""
423 "=\"Rsend\", otherwise correct but not recommended.");
424 setStringToIntegralParameter<Details::EDistributorSendType> (
"Send type",
425 defaultSendType,
"When using MPI, the variant of send to use in "
426 "do[Reverse]Posts()", sendTypes(), sendTypeEnums(), plist.getRawPtr());
427 plist->set (
"Use distinct tags", useDistinctTags,
"Whether to use distinct "
428 "MPI message tags for different code paths.");
429 plist->set (
"Debug", debug,
"Whether to print copious debugging output on "
432 Teuchos::setupVerboseObjectSublist (&*plist);
433 return Teuchos::rcp_const_cast<
const ParameterList> (plist);
438 {
return totalReceiveLength_; }
441 {
return numReceives_; }
444 {
return selfMessage_; }
447 {
return numSends_; }
450 {
return maxSendLength_; }
453 {
return imagesFrom_; }
456 {
return lengthsFrom_; }
459 {
return imagesTo_; }
462 {
return lengthsTo_; }
464 const Teuchos::RCP<Distributor> &
466 if (reverseDistributor_ == Teuchos::null) {
468 createReverseDistributor();
470 return reverseDistributor_;
475 Distributor::createReverseDistributor()
const
477 reverseDistributor_ = Teuchos::rcp (
new Distributor (comm_));
482 size_t totalSendLength =
483 std::accumulate (lengthsTo_.begin(), lengthsTo_.end(), 0);
488 size_t maxReceiveLength = 0;
489 const int myImageID = comm_->getRank();
490 for (
size_t i=0; i < numReceives_; ++i) {
491 if (imagesFrom_[i] != myImageID) {
493 if (lengthsFrom_[i] > maxReceiveLength) {
494 maxReceiveLength = lengthsFrom_[i];
502 reverseDistributor_->lengthsTo_ = lengthsFrom_;
503 reverseDistributor_->imagesTo_ = imagesFrom_;
504 reverseDistributor_->indicesTo_ = indicesFrom_;
505 reverseDistributor_->startsTo_ = startsFrom_;
506 reverseDistributor_->lengthsFrom_ = lengthsTo_;
507 reverseDistributor_->imagesFrom_ = imagesTo_;
508 reverseDistributor_->indicesFrom_ = indicesTo_;
509 reverseDistributor_->startsFrom_ = startsTo_;
510 reverseDistributor_->numSends_ = numReceives_;
511 reverseDistributor_->numReceives_ = numSends_;
512 reverseDistributor_->selfMessage_ = selfMessage_;
513 reverseDistributor_->maxSendLength_ = maxReceiveLength;
514 reverseDistributor_->totalReceiveLength_ = totalSendLength;
523 using Teuchos::Array;
524 using Teuchos::CommRequest;
525 using Teuchos::FancyOStream;
526 using Teuchos::includesVerbLevel;
527 using Teuchos::is_null;
528 using Teuchos::OSTab;
530 using Teuchos::waitAll;
533 Teuchos::OSTab tab (out_);
535 #ifdef TPETRA_DISTRIBUTOR_TIMERS
536 Teuchos::TimeMonitor timeMon (*timer_doWaits_);
537 #endif // TPETRA_DISTRIBUTOR_TIMERS
539 const int myRank = comm_->getRank ();
542 std::ostringstream os;
543 os << myRank <<
": doWaits: # reqs = "
544 << requests_.size () << endl;
548 if (requests_.size() > 0) {
549 waitAll (*comm_, requests_());
551 #ifdef HAVE_TEUCHOS_DEBUG
553 for (Array<RCP<CommRequest<int> > >::const_iterator it = requests_.begin();
554 it != requests_.end(); ++it)
556 TEUCHOS_TEST_FOR_EXCEPTION( ! is_null (*it), std::runtime_error,
557 Teuchos::typeName(*
this) <<
"::doWaits(): Communication requests "
558 "should all be null aftr calling Teuchos::waitAll() on them, but "
559 "at least one request is not null.");
561 #endif // HAVE_TEUCHOS_DEBUG
564 requests_.resize (0);
567 #ifdef HAVE_TEUCHOS_DEBUG
569 const int localSizeNonzero = (requests_.size () != 0) ? 1 : 0;
570 int globalSizeNonzero = 0;
571 Teuchos::reduceAll<int, int> (*comm_, Teuchos::REDUCE_MAX,
573 Teuchos::outArg (globalSizeNonzero));
574 TEUCHOS_TEST_FOR_EXCEPTION(
575 globalSizeNonzero != 0, std::runtime_error,
576 "Tpetra::Distributor::doWaits: After waitAll, at least one process has "
577 "a nonzero number of outstanding posts. There should be none at this "
578 "point. Please report this bug to the Tpetra developers.");
580 #endif // HAVE_TEUCHOS_DEBUG
583 std::ostringstream os;
584 os << myRank <<
": doWaits done" << endl;
591 if (! reverseDistributor_.is_null()) {
592 reverseDistributor_->doWaits();
597 std::ostringstream oss;
598 oss << Teuchos::Describable::description();
604 const Teuchos::EVerbosityLevel verbLevel)
const
608 using Teuchos::VERB_DEFAULT;
609 using Teuchos::VERB_NONE;
610 using Teuchos::VERB_LOW;
611 using Teuchos::VERB_MEDIUM;
612 using Teuchos::VERB_HIGH;
613 using Teuchos::VERB_EXTREME;
614 Teuchos::EVerbosityLevel vl = verbLevel;
615 if (vl == VERB_DEFAULT) vl = VERB_LOW;
616 const int myImageID = comm_->getRank();
617 const int numImages = comm_->getSize();
618 Teuchos::OSTab tab (out);
620 if (vl == VERB_NONE) {
623 if (myImageID == 0) {
627 out <<
"\"Tpetra::Distributor\":" << endl;
628 Teuchos::OSTab tab2 (out);
629 const std::string label = this->getObjectLabel ();
631 out <<
"Label: " << label << endl;
633 out <<
"Parameters: " << endl;
635 Teuchos::OSTab tab3 (out);
636 out <<
"\"Send type\": "
637 << DistributorSendTypeEnumToString (sendType_) << endl
638 <<
"\"Barrier between receives and sends\": "
639 << (barrierBetween_ ?
"true" :
"false") << endl;
640 out <<
"\"Use distinct tags\": "
641 << (useDistinctTags_ ?
"true" :
"false") << endl;
642 out <<
"\"Debug\": " << (debug_ ?
"true" :
"false") << endl;
645 if (vl == VERB_LOW) {
648 Teuchos::OSTab tab2 (out);
653 for (
int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
654 if (myImageID == imageCtr) {
655 if (myImageID == 0) {
656 out <<
"Number of processes: " << numImages << endl;
658 out <<
"Process: " << myImageID << endl;
659 Teuchos::OSTab tab3 (out);
662 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
663 out <<
"imagesTo: " << toString (imagesTo_) << endl;
664 out <<
"lengthsTo: " << toString (lengthsTo_) << endl;
667 if (vl == VERB_EXTREME) {
668 out <<
"startsTo: " << toString (startsTo_) << endl;
669 out <<
"indicesTo: " << toString (indicesTo_) << endl;
671 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
674 out <<
"lengthsFrom: " << toString (lengthsFrom_) << endl;
675 out <<
"startsFrom: " << toString (startsFrom_) << endl;
676 out <<
"imagesFrom: " << toString (imagesFrom_) << endl;
693 Distributor::computeReceives ()
695 using Teuchos::Array;
697 using Teuchos::CommStatus;
698 using Teuchos::CommRequest;
699 using Teuchos::ireceive;
702 using Teuchos::REDUCE_SUM;
703 using Teuchos::receive;
704 using Teuchos::reduceAllAndScatter;
706 using Teuchos::waitAll;
709 Teuchos::OSTab tab (out_);
710 const int myRank = comm_->getRank();
711 const int numProcs = comm_->getSize();
714 const int pathTag = 2;
715 const int tag = this->getTag (pathTag);
718 std::ostringstream os;
719 os << myRank <<
": computeReceives: "
720 "{selfMessage_: " << (selfMessage_ ?
"true" :
"false")
721 <<
", tag: " << tag <<
"}" << endl;
731 Array<int> toNodesFromMe (numProcs, 0);
732 #ifdef HAVE_TEUCHOS_DEBUG
733 bool counting_error =
false;
734 #endif // HAVE_TEUCHOS_DEBUG
735 for (
size_t i = 0; i < (numSends_ + (selfMessage_ ? 1 : 0)); ++i) {
736 #ifdef HAVE_TEUCHOS_DEBUG
737 if (toNodesFromMe[imagesTo_[i]] != 0) {
738 counting_error =
true;
740 #endif // HAVE_TEUCHOS_DEBUG
741 toNodesFromMe[imagesTo_[i]] = 1;
743 #ifdef HAVE_TEUCHOS_DEBUG
745 "Tpetra::Distributor::computeReceives: There was an error on at least "
746 "one process in counting the number of messages send by that process to "
747 "the other processs. Please report this bug to the Tpetra developers.",
749 #endif // HAVE_TEUCHOS_DEBUG
752 std::ostringstream os;
753 os << myRank <<
": computeReceives: "
754 "Calling reduceAllAndScatter" << endl;
801 Array<int> counts (numProcs, 1);
802 int numReceivesAsInt = 0;
803 reduceAllAndScatter<int, int> (*comm_, REDUCE_SUM, numProcs,
804 toNodesFromMe.getRawPtr (),
807 numReceives_ = Teuchos::as<size_t> (numReceivesAsInt);
813 lengthsFrom_.assign (numReceives_, 0);
814 imagesFrom_.assign (numReceives_, 0);
830 const size_t actualNumReceives = numReceives_ - (selfMessage_ ? 1 : 0);
836 Array<RCP<CommRequest<int> > > requests (actualNumReceives);
837 Array<ArrayRCP<size_t> > lengthsFromBuffers (actualNumReceives);
838 Array<RCP<CommStatus<int> > > statuses (actualNumReceives);
842 const int anySourceProc = -1;
845 std::ostringstream os;
846 os << myRank <<
": computeReceives: Posting "
847 << actualNumReceives <<
" irecvs" << endl;
852 for (
size_t i = 0; i < actualNumReceives; ++i) {
857 lengthsFromBuffers[i].resize (1);
858 lengthsFromBuffers[i][0] = as<size_t> (0);
859 requests[i] = ireceive<int, size_t> (lengthsFromBuffers[i], anySourceProc, tag, *comm_);
861 std::ostringstream os;
862 os << myRank <<
": computeReceives: "
863 "Posted any-proc irecv w/ specified tag " << tag << endl;
869 std::ostringstream os;
870 os << myRank <<
": computeReceives: "
871 "posting " << numSends_ <<
" sends" << endl;
882 for (
size_t i = 0; i < numSends_ + (selfMessage_ ? 1 : 0); ++i) {
883 if (imagesTo_[i] != myRank) {
887 const size_t*
const lengthsTo_i = &lengthsTo_[i];
888 send<int, size_t> (lengthsTo_i, 1, as<int> (imagesTo_[i]), tag, *comm_);
890 std::ostringstream os;
891 os << myRank <<
": computeReceives: "
892 "Posted send to Proc " << imagesTo_[i] <<
" w/ specified tag "
904 lengthsFrom_[numReceives_-1] = lengthsTo_[i];
905 imagesFrom_[numReceives_-1] = myRank;
910 std::ostringstream os;
911 os << myRank <<
": computeReceives: waitAll on "
912 << requests.size () <<
" requests" << endl;
921 waitAll (*comm_, requests (), statuses ());
922 for (
size_t i = 0; i < actualNumReceives; ++i) {
923 lengthsFrom_[i] = *lengthsFromBuffers[i];
924 imagesFrom_[i] = statuses[i]->getSourceRank ();
930 sort2 (imagesFrom_.begin(), imagesFrom_.end(), lengthsFrom_.begin());
933 totalReceiveLength_ = std::accumulate (lengthsFrom_.begin(), lengthsFrom_.end(), 0);
934 indicesFrom_.clear ();
935 indicesFrom_.reserve (totalReceiveLength_);
936 for (
size_t i = 0; i < totalReceiveLength_; ++i) {
937 indicesFrom_.push_back(i);
940 startsFrom_.clear ();
941 startsFrom_.reserve (numReceives_);
942 for (
size_t i = 0, j = 0; i < numReceives_; ++i) {
943 startsFrom_.push_back(j);
944 j += lengthsFrom_[i];
952 std::ostringstream os;
953 os << myRank <<
": computeReceives: done" << endl;
961 using Teuchos::outArg;
962 using Teuchos::REDUCE_MAX;
963 using Teuchos::reduceAll;
966 Teuchos::OSTab tab (out_);
968 numExports_ = exportNodeIDs.size();
970 const int myImageID = comm_->getRank();
971 const int numImages = comm_->getSize();
973 std::ostringstream os;
974 os << myImageID <<
": createFromSends" << endl;
1016 Teuchos::Array<size_t> starts (numImages + 1, 0);
1019 size_t numActive = 0;
1020 int needSendBuff = 0;
1023 for (
size_t i = 0; i < numExports_; ++i) {
1024 int exportID = exportNodeIDs[i];
1025 if (exportID >= numImages) {
1029 else if (exportID >= 0) {
1043 if (needSendBuff==0 && starts[exportID] > 1 && exportID != exportNodeIDs[i-1]) {
1050 #ifdef HAVE_TPETRA_DEBUG
1057 reduceAll<int, int> (*comm_, REDUCE_MAX, badID, outArg (gbl_badID));
1058 TEUCHOS_TEST_FOR_EXCEPTION(gbl_badID >= 0, std::runtime_error,
1059 Teuchos::typeName(*
this) <<
"::createFromSends(): Process " << gbl_badID
1060 <<
", perhaps among other processes, got a bad send process ID.");
1078 #endif // HAVE_TPETRA_DEBUG
1080 #if defined(HAVE_TPETRA_THROW_EFFICIENCY_WARNINGS) || defined(HAVE_TPETRA_PRINT_EFFICIENCY_WARNINGS)
1082 int global_needSendBuff;
1083 reduceAll<int, int> (*comm_, REDUCE_MAX, needSendBuff,
1084 outArg (global_needSendBuff));
1086 global_needSendBuff != 0, std::runtime_error,
1087 "::createFromSends: Grouping export IDs together by process rank often "
1088 "improves performance.");
1094 if (starts[myImageID] != 0) {
1095 selfMessage_ =
true;
1098 selfMessage_ =
false;
1101 #ifdef HAVE_TEUCHOS_DEBUG
1102 bool index_neq_numActive =
false;
1103 bool send_neq_numSends =
false;
1105 if (! needSendBuff) {
1110 for (
int i = 0; i < numImages; ++i) {
1118 indicesTo_.resize(0);
1121 imagesTo_.assign(numSends_,0);
1122 startsTo_.assign(numSends_,0);
1123 lengthsTo_.assign(numSends_,0);
1130 size_t index = 0, nodeIndex = 0;
1131 for (
size_t i = 0; i < numSends_; ++i) {
1132 while (exportNodeIDs[nodeIndex] < 0) {
1135 startsTo_[i] = nodeIndex;
1136 int imageID = exportNodeIDs[nodeIndex];
1137 imagesTo_[i] = imageID;
1138 index += starts[imageID];
1139 nodeIndex += starts[imageID];
1141 #ifdef HAVE_TEUCHOS_DEBUG
1142 if (index != numActive) {
1143 index_neq_numActive =
true;
1149 if (numSends_ > 0) {
1150 sort2(imagesTo_.begin(), imagesTo_.end(), startsTo_.begin());
1154 for (
size_t i = 0; i < numSends_; ++i) {
1155 int imageID = imagesTo_[i];
1156 lengthsTo_[i] = starts[imageID];
1157 if ((imageID != myImageID) && (lengthsTo_[i] > maxSendLength_)) {
1158 maxSendLength_ = lengthsTo_[i];
1169 if (starts[0] == 0 ) {
1175 for (Teuchos::Array<size_t>::iterator i=starts.begin()+1,
1177 i != starts.end(); ++i)
1179 if (*i != 0) ++numSends_;
1185 for (Teuchos::Array<size_t>::reverse_iterator ip1=starts.rbegin(),
1186 i=starts.rbegin()+1;
1187 i != starts.rend(); ++i)
1196 indicesTo_.resize(numActive);
1198 for (
size_t i = 0; i < numExports_; ++i) {
1199 if (exportNodeIDs[i] >= 0) {
1201 indicesTo_[starts[exportNodeIDs[i]]] = i;
1203 ++starts[exportNodeIDs[i]];
1215 for (
int node = numImages-1; node != 0; --node) {
1216 starts[node] = starts[node-1];
1219 starts[numImages] = numActive;
1226 imagesTo_.resize(numSends_);
1227 startsTo_.resize(numSends_);
1228 lengthsTo_.resize(numSends_);
1235 for (
int node = 0; node < numImages; ++node ) {
1236 if (starts[node+1] != starts[node]) {
1237 lengthsTo_[snd] = starts[node+1] - starts[node];
1238 startsTo_[snd] = starts[node];
1240 if ((node != myImageID) && (lengthsTo_[snd] > maxSendLength_)) {
1241 maxSendLength_ = lengthsTo_[snd];
1243 imagesTo_[snd] = node;
1247 #ifdef HAVE_TEUCHOS_DEBUG
1248 if (snd != numSends_) {
1249 send_neq_numSends =
true;
1253 #ifdef HAVE_TEUCHOS_DEBUG
1255 "Tpetra::Distributor::createFromSends: logic error. Please notify the Tpetra team.",*comm_);
1257 "Tpetra::Distributor::createFromSends: logic error. Please notify the Tpetra team.",*comm_);
1260 if (selfMessage_) --numSends_;
1266 std::ostringstream os;
1267 os << myImageID <<
": createFromSends: done" << endl;
1270 return totalReceiveLength_;
size_t getNumReceives() const
The number of processes from which we will receive data.
std::string description() const
A simple one-line description of this object.
ArrayView< const int > getImagesTo() const
Ranks of the processes to which this process will send values.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
List of valid Distributor parameters.
void swap(Distributor &rhs)
Swap the contents of rhs with those of *this.
ArrayView< const size_t > getLengthsFrom() const
Number of values this process will receive from each process.
bool hasSelfMessage() const
Whether the calling process will send or receive messages to itself.
Sets up and executes a communication plan for a Tpetra DistObject.
size_t getTotalReceiveLength() const
Total number of values this process will receive from other processes.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
Set Distributor parameters.
size_t createFromSends(const ArrayView< const int > &exportNodeIDs)
Set up Distributor using list of process ranks to which this process will send.
#define TPETRA_EFFICIENCY_WARNING(throw_exception_test, Exception, msg)
Print or throw an efficency warning.
ArrayView< const size_t > getLengthsTo() const
Number of values this process will send to each process.
virtual ~Distributor()
Destructor (virtual for memory safety).
void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2)
Sort the first array, and apply the resulting permutation to the second array.
ArrayView< const int > getImagesFrom() const
Ranks of the processes sending values to this process.
size_t getNumSends() const
The number of processes to which we will send data.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream.
size_t getMaxSendLength() const
Maximum number of values this process will send to another single process.
#define SHARED_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg, comm)
Test for exception, with reduction over the given communicator.
Array< std::string > distributorSendTypes()
Valid values for Distributor's "Send type" parameter.
const RCP< Distributor > & getReverse() const
A reverse communication plan Distributor.
Distributor(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Construct using the specified communicator and default parameters.