Tpetra Matrix/Vector Services  Version of the Day
Tpetra_Distributor.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_DISTRIBUTOR_HPP
43 #define TPETRA_DISTRIBUTOR_HPP
44 
45 #include "Tpetra_Util.hpp"
46 #include <Teuchos_as.hpp>
47 #include <Teuchos_Describable.hpp>
48 #include <Teuchos_ParameterListAcceptorDefaultBase.hpp>
49 #include <Teuchos_VerboseObject.hpp>
50 
51 // If TPETRA_DISTRIBUTOR_TIMERS is defined, Distributor will time
52 // doPosts (both versions) and doWaits, and register those timers with
53 // Teuchos::TimeMonitor so that summarize() or report() will show
54 // results.
55 
56 // #ifndef TPETRA_DISTRIBUTOR_TIMERS
57 // # define TPETRA_DISTRIBUTOR_TIMERS 1
58 // #endif // TPETRA_DISTRIBUTOR_TIMERS
59 
60 #ifdef TPETRA_DISTRIBUTOR_TIMERS
61 # undef TPETRA_DISTRIBUTOR_TIMERS
62 #endif // TPETRA_DISTRIBUTOR_TIMERS
63 
64 
65 namespace Tpetra {
66 
67  namespace Details {
72  enum EDistributorSendType {
73  DISTRIBUTOR_ISEND, // Use MPI_Isend (Teuchos::isend)
74  DISTRIBUTOR_RSEND, // Use MPI_Rsend (Teuchos::readySend)
75  DISTRIBUTOR_SEND, // Use MPI_Send (Teuchos::send)
76  DISTRIBUTOR_SSEND // Use MPI_Ssend (Teuchos::ssend)
77  };
78 
83  std::string
84  DistributorSendTypeEnumToString (EDistributorSendType sendType);
85 
86  } // namespace Details
87 
95 
163  class Distributor :
164  public Teuchos::Describable,
166  public Teuchos::VerboseObject<Distributor> {
167  public:
169 
170 
178  explicit Distributor (const Teuchos::RCP<const Teuchos::Comm<int> >& comm);
179 
189  Distributor (const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
191 
204  Distributor (const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
206 
221  Distributor (const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
224 
226  Distributor (const Distributor &distributor);
227 
229  virtual ~Distributor ();
230 
236  void swap (Distributor& rhs);
237 
239 
241 
246  void setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist);
247 
252  Teuchos::RCP<const Teuchos::ParameterList> getValidParameters () const;
253 
255 
257 
277  size_t createFromSends (const ArrayView<const int>& exportNodeIDs);
278 
312  template <class Ordinal>
313  void
314  createFromRecvs (const ArrayView<const Ordinal>& remoteIDs,
315  const ArrayView<const int>& remoteNodeIDs,
316  Array<Ordinal>& exportIDs,
317  Array<int>& exportNodeIDs);
318 
320 
322 
326  size_t getNumReceives() const;
327 
331  size_t getNumSends() const;
332 
334  bool hasSelfMessage() const;
335 
337  size_t getMaxSendLength() const;
338 
340  size_t getTotalReceiveLength() const;
341 
346  ArrayView<const int> getImagesFrom() const;
347 
352  ArrayView<const int> getImagesTo() const;
353 
361  ArrayView<const size_t> getLengthsFrom() const;
362 
370  ArrayView<const size_t> getLengthsTo() const;
371 
373 
375 
386  const RCP<Distributor>& getReverse() const;
387 
389 
391 
412  template <class Packet>
413  void
414  doPostsAndWaits (const ArrayView<const Packet> &exports,
415  size_t numPackets,
416  const ArrayView<Packet> &imports);
417 
439  template <class Packet>
440  void
441  doPostsAndWaits (const ArrayView<const Packet> &exports,
442  const ArrayView<size_t> &numExportPacketsPerLID,
443  const ArrayView<Packet> &imports,
444  const ArrayView<size_t> &numImportPacketsPerLID);
445 
470  template <class Packet>
471  void
472  doPosts (const ArrayRCP<const Packet> &exports,
473  size_t numPackets,
474  const ArrayRCP<Packet> &imports);
475 
494  template <class Packet>
495  void
496  doPosts (const ArrayRCP<const Packet> &exports,
497  const ArrayView<size_t> &numExportPacketsPerLID,
498  const ArrayRCP<Packet> &imports,
499  const ArrayView<size_t> &numImportPacketsPerLID);
500 
507  void doWaits ();
508 
513  template <class Packet>
514  void
515  doReversePostsAndWaits (const ArrayView<const Packet> &exports,
516  size_t numPackets,
517  const ArrayView<Packet> &imports);
518 
523  template <class Packet>
524  void
525  doReversePostsAndWaits (const ArrayView<const Packet> &exports,
526  const ArrayView<size_t> &numExportPacketsPerLID,
527  const ArrayView<Packet> &imports,
528  const ArrayView<size_t> &numImportPacketsPerLID);
529 
534  template <class Packet>
535  void
536  doReversePosts (const ArrayRCP<const Packet> &exports,
537  size_t numPackets,
538  const ArrayRCP<Packet> &imports);
539 
544  template <class Packet>
545  void
546  doReversePosts (const ArrayRCP<const Packet> &exports,
547  const ArrayView<size_t> &numExportPacketsPerLID,
548  const ArrayRCP<Packet> &imports,
549  const ArrayView<size_t> &numImportPacketsPerLID);
550 
557  void doReverseWaits ();
558 
560 
562 
564  std::string description() const;
565 
568 
570 
571  private:
573  RCP<const Comm<int> > comm_;
574 
577 
579 
580 
582  Details::EDistributorSendType sendType_;
583 
585  bool barrierBetween_;
586 
588  bool debug_;
590 
599  size_t numExports_;
600 
602  bool selfMessage_;
603 
613  size_t numSends_;
614 
619  Array<int> imagesTo_;
620 
629  Array<size_t> startsTo_;
630 
636  Array<size_t> lengthsTo_;
637 
641  size_t maxSendLength_;
642 
658  Array<size_t> indicesTo_;
659 
669  size_t numReceives_;
670 
677  size_t totalReceiveLength_;
678 
684  Array<size_t> lengthsFrom_;
685 
691  Array<int> imagesFrom_;
692 
698  Array<size_t> startsFrom_;
699 
706  Array<size_t> indicesFrom_;
707 
715 
720  mutable RCP<Distributor> reverseDistributor_;
721 
722 #ifdef TPETRA_DISTRIBUTOR_TIMERS
723  Teuchos::RCP<Teuchos::Time> timer_doPosts3_;
724  Teuchos::RCP<Teuchos::Time> timer_doPosts4_;
725  Teuchos::RCP<Teuchos::Time> timer_doWaits_;
726  Teuchos::RCP<Teuchos::Time> timer_doPosts3_recvs_;
727  Teuchos::RCP<Teuchos::Time> timer_doPosts4_recvs_;
728  Teuchos::RCP<Teuchos::Time> timer_doPosts3_barrier_;
729  Teuchos::RCP<Teuchos::Time> timer_doPosts4_barrier_;
730  Teuchos::RCP<Teuchos::Time> timer_doPosts3_sends_;
731  Teuchos::RCP<Teuchos::Time> timer_doPosts4_sends_;
732 
734  void makeTimers ();
735 #endif // TPETRA_DISTRIBUTOR_TIMERS
736 
748  bool useDistinctTags_;
749 
754  int getTag (const int pathTag) const;
755 
769  void
770  init (const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
772 
783  void computeReceives ();
784 
797  template <class Ordinal>
798  void computeSends (const ArrayView<const Ordinal> &importIDs,
799  const ArrayView<const int> &importNodeIDs,
800  Array<Ordinal> &exportIDs,
801  Array<int> &exportNodeIDs);
802 
804  void createReverseDistributor() const;
805 
806  }; // class Distributor
807 
808 
809  template <class Packet>
810  void Distributor::
812  size_t numPackets,
813  const ArrayView<Packet>& imports)
814  {
815  using Teuchos::as;
816  using Teuchos::arcp;
817  using Teuchos::ArrayRCP;
818 
819  TEUCHOS_TEST_FOR_EXCEPTION(requests_.size() != 0, std::runtime_error,
820  Teuchos::typeName(*this) << "::doPostsAndWaits(): There are "
821  << requests_.size() << " outstanding nonblocking messages pending. It is "
822  "incorrect to call doPostsAndWaits with posts outstanding.");
823 
824  // doPosts() accepts the exports and imports arrays as ArrayRCPs,
825  // requiring that the memory location is persisting (as is
826  // necessary for nonblocking receives). However, it need only
827  // persist until doWaits() completes, so it is safe for us to use
828  // a nonpersisting reference in this case. The use of a
829  // nonpersisting reference is purely a performance optimization.
830 
831  typedef typename ArrayRCP<const Packet>::size_type size_type;
832  //const Packet* exportsPtr = exports.getRawPtr();
833  //ArrayRCP<const Packet> exportsArcp (exportsPtr, as<size_type> (0), exports.size(), false);
834  ArrayRCP<const Packet> exportsArcp (exports.getRawPtr(), as<size_type> (0),
835  exports.size(), false);
836 
837  // For some reason, neither of the options below (that use arcp)
838  // compile for Packet=std::complex<double> with GCC 4.5.1. The
839  // issue only arises with the exports array. This is why we
840  // construct a separate nonowning ArrayRCP.
841 
842  // doPosts (arcp<const Packet> (exports.getRawPtr(), 0, exports.size(), false),
843  // numPackets,
844  // arcp<Packet> (imports.getRawPtr(), 0, imports.size(), false));
845  // doPosts (arcp<const Packet> (exportsPtr, 0, exports.size(), false),
846  // numPackets,
847  // arcp<Packet> (imports.getRawPtr(), 0, imports.size(), false));
848  doPosts (exportsArcp,
849  numPackets,
850  arcp<Packet> (imports.getRawPtr(), 0, imports.size(), false));
851  doWaits();
852  }
853 
854  template <class Packet>
855  void Distributor::
857  const ArrayView<size_t> &numExportPacketsPerLID,
858  const ArrayView<Packet> &imports,
859  const ArrayView<size_t> &numImportPacketsPerLID)
860  {
861  using Teuchos::arcp;
862  using Teuchos::ArrayRCP;
863  using Teuchos::as;
864 
865  TEUCHOS_TEST_FOR_EXCEPTION(requests_.size() != 0, std::runtime_error,
866  Teuchos::typeName(*this) << "::doPostsAndWaits(): There are "
867  << requests_.size() << " outstanding nonblocking messages pending. It is "
868  "incorrect to call doPostsAndWaits with posts outstanding.");
869 
870  // doPosts() accepts the exports and imports arrays as ArrayRCPs,
871  // requiring that the memory location is persisting (as is
872  // necessary for nonblocking receives). However, it need only
873  // persist until doWaits() completes, so it is safe for us to use
874  // a nonpersisting reference in this case.
875 
876  // mfh 04 Apr 2012: For some reason, calling arcp<const Packet>
877  // for Packet=std::complex<T> (e.g., T=float) fails to compile
878  // with some versions of GCC. The issue only arises with the
879  // exports array. This is why we construct a separate nonowning
880  // ArrayRCP.
881  typedef typename ArrayRCP<const Packet>::size_type size_type;
882  ArrayRCP<const Packet> exportsArcp (exports.getRawPtr(), as<size_type> (0),
883  exports.size(), false);
884  // mfh 04 Apr 2012: This is the offending code. This statement
885  // would normally be in place of "exportsArcp" in the
886  // doPosts() call below.
887  //arcp<const Packet> (exports.getRawPtr(), 0, exports.size(), false),
888  doPosts (exportsArcp,
889  numExportPacketsPerLID,
890  arcp<Packet> (imports.getRawPtr(), 0, imports.size(), false),
891  numImportPacketsPerLID);
892  doWaits();
893  }
894 
895 
896  template <class Packet>
897  void Distributor::
899  size_t numPackets,
900  const ArrayRCP<Packet>& imports)
901  {
902  using Teuchos::Array;
903  using Teuchos::as;
904  using Teuchos::FancyOStream;
906  using Teuchos::ireceive;
907  using Teuchos::isend;
908  using Teuchos::OSTab;
909  using Teuchos::readySend;
910  using Teuchos::send;
911  using Teuchos::ssend;
913  using Teuchos::typeName;
914  using std::endl;
915  typedef Array<size_t>::size_type size_type;
916 
917  Teuchos::OSTab tab (out_);
918 
919 #ifdef TPETRA_DISTRIBUTOR_TIMERS
920  Teuchos::TimeMonitor timeMon (*timer_doPosts3_);
921 #endif // TPETRA_DISTRIBUTOR_TIMERS
922 
923  // Run-time configurable parameters that come from the input
924  // ParameterList set by setParameterList().
925  const Details::EDistributorSendType sendType = sendType_;
926  const bool doBarrier = barrierBetween_;
927 
928 // #ifdef HAVE_TEUCHOS_DEBUG
929 // // Prepare for verbose output, if applicable.
930 // Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel ();
931 // (void) verbLevel; // Silence "unused variable" compiler warning.
932 // RCP<FancyOStream> out = this->getOStream ();
933 // // const bool doPrint = out.get () && (comm_->getRank () == 0) &&
934 // // includesVerbLevel (verbLevel, Teuchos::VERB_EXTREME, true);
935 // const bool doPrint = out.get () && (comm_->getRank () == 0);
936 
937 // if (doPrint) {
938 // // Only need one process to print out parameters.
939 // *out << "Distributor::doPosts (3 args)" << endl;
940 // }
941 // // Add one tab level. We declare this outside the doPrint scopes
942 // // so that the tab persists until the end of this method.
943 // OSTab tab = this->getOSTab ();
944 // if (doPrint) {
945 // *out << "Parameters:" << endl;
946 // {
947 // OSTab tab2 (out);
948 // *out << "sendType: " << DistributorSendTypeEnumToString (sendType)
949 // << endl << "barrierBetween: " << doBarrier << endl;
950 // }
951 // }
952 // #endif // HAVE_TEUCHOS_DEBUG
953 
955  sendType == Details::DISTRIBUTOR_RSEND && ! doBarrier,
956  std::logic_error, "Ready send implementation requires a barrier between "
957  "posting receives and posting ready sends. This should have been checked "
958  "before. Please report this bug to the Tpetra developers.");
959 
960  const int myImageID = comm_->getRank();
961  size_t selfReceiveOffset = 0;
962 
963 #ifdef HAVE_TEUCHOS_DEBUG
964  // Each message has the same number of packets.
965  const size_t totalNumImportPackets = totalReceiveLength_ * numPackets;
966  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t> (imports.size ()) != totalNumImportPackets,
967  std::runtime_error, typeName (*this) << "::doPosts(): imports must be "
968  "large enough to store the imported data. imports.size() = "
969  << imports.size() << ", but total number of import packets = "
970  << totalNumImportPackets << ".");
971 #endif // HAVE_TEUCHOS_DEBUG
972 
973  // MPI tag for nonblocking receives and blocking sends in this
974  // method. Some processes might take the "fast" path
975  // (indicesTo_.empty()) and others might take the "slow" path for
976  // the same doPosts() call, so the path tag must be the same for
977  // both.
978  const int pathTag = 0;
979  const int tag = this->getTag (pathTag);
980 
981  if (debug_) {
983  requests_.size () != 0, std::logic_error, "doPosts(3): Process "
984  << myImageID << ": requests_.size() = " << requests_.size ()
985  << " != 0.");
986  std::ostringstream os;
987  os << myImageID << ": doPosts(3,"
988  << (indicesTo_.empty () ? "fast" : "slow") << ")" << endl;
989  *out_ << os.str ();
990  }
991 
992  // Distributor uses requests_.size() as the number of outstanding
993  // nonblocking message requests, so we resize to zero to maintain
994  // this invariant.
995  //
996  // numReceives_ does _not_ include the self message, if there is
997  // one. Here, we do actually send a message to ourselves, so we
998  // include any self message in the "actual" number of receives to
999  // post.
1000  //
1001  // NOTE (mfh 19 Mar 2012): Epetra_MpiDistributor::DoPosts()
1002  // doesn't (re)allocate its array of requests. That happens in
1003  // CreateFromSends(), ComputeRecvs_(), DoReversePosts() (on
1004  // demand), or Resize_().
1005  const size_type actualNumReceives = as<size_type> (numReceives_) +
1006  as<size_type> (selfMessage_ ? 1 : 0);
1007  requests_.resize (0);
1008 
1009  // Post the nonblocking receives. It's common MPI wisdom to post
1010  // receives before sends. In MPI terms, this means favoring
1011  // adding to the "posted queue" (of receive requests) over adding
1012  // to the "unexpected queue" (of arrived messages not yet matched
1013  // with a receive).
1014  {
1015 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1016  Teuchos::TimeMonitor timeMonRecvs (*timer_doPosts3_recvs_);
1017 #endif // TPETRA_DISTRIBUTOR_TIMERS
1018 
1019  size_t curBufferOffset = 0;
1020  for (size_type i = 0; i < actualNumReceives; ++i) {
1021  if (imagesFrom_[i] != myImageID) {
1022  // If my process is receiving these packet(s) from another
1023  // process (not a self-receive):
1024  //
1025  // 1. Set up the persisting view (recvBuf) of the imports
1026  // array, given the offset and size (total number of
1027  // packets from process imagesFrom_[i]).
1028  // 2. Start the Irecv and save the resulting request.
1029  ArrayRCP<Packet> recvBuf =
1030  imports.persistingView (curBufferOffset, lengthsFrom_[i]*numPackets);
1031  requests_.push_back (ireceive<int, Packet> (recvBuf, imagesFrom_[i],
1032  tag, *comm_));
1033  if (debug_) {
1034  std::ostringstream os;
1035  os << myImageID << ": doPosts(3,"
1036  << (indicesTo_.empty () ? "fast" : "slow") << "): "
1037  << "Posted irecv from Proc " << imagesFrom_[i] << " with "
1038  "specified tag " << tag << endl;
1039  *out_ << os.str ();
1040  }
1041  }
1042  else { // Receiving from myself
1043  selfReceiveOffset = curBufferOffset; // Remember the self-recv offset
1044  }
1045  curBufferOffset += lengthsFrom_[i]*numPackets;
1046  }
1047  }
1048 
1049  if (doBarrier) {
1050 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1051  Teuchos::TimeMonitor timeMonBarrier (*timer_doPosts3_barrier_);
1052 #endif // TPETRA_DISTRIBUTOR_TIMERS
1053  // If we are using ready sends (MPI_Rsend) below, we need to do
1054  // a barrier before we post the ready sends. This is because a
1055  // ready send requires that its matching receive has already
1056  // been posted before the send has been posted. The only way to
1057  // guarantee that in this case is to use a barrier.
1058  comm_->barrier ();
1059  }
1060 
1061 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1062  Teuchos::TimeMonitor timeMonSends (*timer_doPosts3_sends_);
1063 #endif // TPETRA_DISTRIBUTOR_TIMERS
1064 
1065  // setup scan through imagesTo_ list starting with higher numbered images
1066  // (should help balance message traffic)
1067  //
1068  // FIXME (mfh 20 Feb 2013) Why haven't we precomputed this?
1069  // It doesn't depend on the input at all.
1070  size_t numBlocks = numSends_ + selfMessage_;
1071  size_t imageIndex = 0;
1072  while ((imageIndex < numBlocks) && (imagesTo_[imageIndex] < myImageID)) {
1073  ++imageIndex;
1074  }
1075  if (imageIndex == numBlocks) {
1076  imageIndex = 0;
1077  }
1078 
1079  size_t selfNum = 0;
1080  size_t selfIndex = 0;
1081 
1082  if (indicesTo_.empty()) {
1083  if (debug_) {
1084  std::ostringstream os;
1085  os << myImageID << ": doPosts(3,fast): posting sends" << endl;
1086  *out_ << os.str ();
1087  }
1088 
1089  // Data are already blocked (laid out) by process, so we don't
1090  // need a separate send buffer (besides the exports array).
1091  for (size_t i = 0; i < numBlocks; ++i) {
1092  size_t p = i + imageIndex;
1093  if (p > (numBlocks - 1)) {
1094  p -= numBlocks;
1095  }
1096 
1097  if (imagesTo_[p] != myImageID) {
1098  ArrayView<const Packet> tmpSend =
1099  exports.view (startsTo_[p]*numPackets, lengthsTo_[p]*numPackets);
1100 
1101  if (sendType == Details::DISTRIBUTOR_SEND) {
1102  send<int, Packet> (tmpSend.getRawPtr (),
1103  as<int> (tmpSend.size ()),
1104  imagesTo_[p], tag, *comm_);
1105  }
1106  else if (sendType == Details::DISTRIBUTOR_ISEND) {
1107  ArrayRCP<const Packet> tmpSendBuf =
1108  exports.persistingView (startsTo_[p] * numPackets,
1109  lengthsTo_[p] * numPackets);
1110  requests_.push_back (isend<int, Packet> (tmpSendBuf, imagesTo_[p],
1111  tag, *comm_));
1112  }
1113  else if (sendType == Details::DISTRIBUTOR_RSEND) {
1114  readySend<int, Packet> (tmpSend.getRawPtr (),
1115  as<int> (tmpSend.size ()),
1116  imagesTo_[p], tag, *comm_);
1117  }
1118  else if (sendType == Details::DISTRIBUTOR_SSEND) {
1119  ssend<int, Packet> (tmpSend.getRawPtr (),
1120  as<int> (tmpSend.size ()),
1121  imagesTo_[p], tag, *comm_);
1122  } else {
1123  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Tpetra::"
1124  "Distributor (3 args): Invalid send type. We should never get "
1125  "here. Please report this bug to the Tpetra developers.");
1126  }
1127 
1128  if (debug_) {
1129  std::ostringstream os;
1130  os << myImageID << ": doPosts(3,fast): "
1131  << "Posted send to Proc " << imagesTo_[i]
1132  << " w/ specified tag " << tag << endl;
1133  *out_ << os.str ();
1134  }
1135  }
1136  else { // "Sending" the message to myself
1137  selfNum = p;
1138  }
1139  }
1140 
1141  if (selfMessage_) {
1142  // This is how we "send a message to ourself": we copy from
1143  // the export buffer to the import buffer. That saves
1144  // Teuchos::Comm implementations other than MpiComm (in
1145  // particular, SerialComm) the trouble of implementing self
1146  // messages correctly. (To do this right, SerialComm would
1147  // need internal buffer space for messages, keyed on the
1148  // message's tag.)
1149  std::copy (exports.begin()+startsTo_[selfNum]*numPackets,
1150  exports.begin()+startsTo_[selfNum]*numPackets+lengthsTo_[selfNum]*numPackets,
1151  imports.begin()+selfReceiveOffset);
1152  }
1153  if (debug_) {
1154  std::ostringstream os;
1155  os << myImageID << ": doPosts(3,fast) done" << endl;
1156  *out_ << os.str ();
1157  }
1158  }
1159  else { // data are not blocked by image, use send buffer
1160  if (debug_) {
1161  std::ostringstream os;
1162  os << myImageID << ": doPosts(3,slow): posting sends" << endl;
1163  *out_ << os.str ();
1164  }
1165 
1166  // FIXME (mfh 05 Mar 2013) This is broken for Isend (nonblocking
1167  // sends), because the buffer is only long enough for one send.
1168  ArrayRCP<Packet> sendArray (maxSendLength_ * numPackets); // send buffer
1169 
1171  sendType == Details::DISTRIBUTOR_ISEND, std::logic_error,
1172  "Tpetra::Distributor::doPosts<" << TypeNameTraits<Packet>::name()
1173  << "> (3-argument version):" << endl
1174  << "The \"send buffer\" code path doesn't currently work with nonblocking sends.");
1175 
1176  for (size_t i = 0; i < numBlocks; ++i) {
1177  size_t p = i + imageIndex;
1178  if (p > (numBlocks - 1)) {
1179  p -= numBlocks;
1180  }
1181 
1182  if (imagesTo_[p] != myImageID) {
1183  typename ArrayView<const Packet>::iterator srcBegin, srcEnd;
1184  size_t sendArrayOffset = 0;
1185  size_t j = startsTo_[p];
1186  for (size_t k = 0; k < lengthsTo_[p]; ++k, ++j) {
1187  srcBegin = exports.begin() + indicesTo_[j]*numPackets;
1188  srcEnd = srcBegin + numPackets;
1189  std::copy (srcBegin, srcEnd, sendArray.begin()+sendArrayOffset);
1190  sendArrayOffset += numPackets;
1191  }
1192  ArrayView<const Packet> tmpSend =
1193  sendArray.view (0, lengthsTo_[p]*numPackets);
1194 
1195  if (sendType == Details::DISTRIBUTOR_SEND) {
1196  send<int, Packet> (tmpSend.getRawPtr (),
1197  as<int> (tmpSend.size ()),
1198  imagesTo_[p], tag, *comm_);
1199  }
1200  else if (sendType == Details::DISTRIBUTOR_ISEND) {
1201  ArrayRCP<const Packet> tmpSendBuf =
1202  sendArray.persistingView (0, lengthsTo_[p] * numPackets);
1203  requests_.push_back (isend<int, Packet> (tmpSendBuf, imagesTo_[p],
1204  tag, *comm_));
1205  }
1206  else if (sendType == Details::DISTRIBUTOR_RSEND) {
1207  readySend<int, Packet> (tmpSend.getRawPtr (),
1208  as<int> (tmpSend.size ()),
1209  imagesTo_[p], tag, *comm_);
1210  }
1211  else if (sendType == Details::DISTRIBUTOR_SSEND) {
1212  ssend<int, Packet> (tmpSend.getRawPtr (),
1213  as<int> (tmpSend.size ()),
1214  imagesTo_[p], tag, *comm_);
1215  }
1216  else {
1217  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Tpetra::"
1218  "Distributor (3 args): Invalid send type. We should never get "
1219  "here. Please report this bug to the Tpetra developers.");
1220  }
1221 
1222  if (debug_) {
1223  std::ostringstream os;
1224  os << myImageID << ": doPosts(3,slow): "
1225  << "Posted send to Proc " << imagesTo_[i]
1226  << " w/ specified tag " << tag << endl;
1227  *out_ << os.str ();
1228  }
1229  }
1230  else { // "Sending" the message to myself
1231  selfNum = p;
1232  selfIndex = startsTo_[p];
1233  }
1234  }
1235 
1236  if (selfMessage_) {
1237  for (size_t k = 0; k < lengthsTo_[selfNum]; ++k) {
1238  std::copy (exports.begin()+indicesTo_[selfIndex]*numPackets,
1239  exports.begin()+indicesTo_[selfIndex]*numPackets + numPackets,
1240  imports.begin() + selfReceiveOffset);
1241  ++selfIndex;
1242  selfReceiveOffset += numPackets;
1243  }
1244  }
1245  if (debug_) {
1246  std::ostringstream os;
1247  os << myImageID << ": doPosts(3,slow) done" << endl;
1248  *out_ << os.str ();
1249  }
1250  }
1251  }
1252 
1253  template <class Packet>
1254  void Distributor::
1256  const ArrayView<size_t>& numExportPacketsPerLID,
1257  const ArrayRCP<Packet>& imports,
1258  const ArrayView<size_t>& numImportPacketsPerLID)
1259  {
1260  using Teuchos::Array;
1261  using Teuchos::as;
1262  using Teuchos::ireceive;
1263  using Teuchos::isend;
1264  using Teuchos::readySend;
1265  using Teuchos::send;
1266  using Teuchos::ssend;
1268 #ifdef HAVE_TEUCHOS_DEBUG
1269  using Teuchos::OSTab;
1270 #endif // HAVE_TEUCHOS_DEBUG
1271  using std::endl;
1272  typedef Array<size_t>::size_type size_type;
1273 
1274  Teuchos::OSTab tab (out_);
1275 
1276 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1277  Teuchos::TimeMonitor timeMon (*timer_doPosts4_);
1278 #endif // TPETRA_DISTRIBUTOR_TIMERS
1279 
1280  // Run-time configurable parameters that come from the input
1281  // ParameterList set by setParameterList().
1282  const Details::EDistributorSendType sendType = sendType_;
1283  const bool doBarrier = barrierBetween_;
1284 
1285 // #ifdef HAVE_TEUCHOS_DEBUG
1286 // // Prepare for verbose output, if applicable.
1287 // Teuchos::EVerbosityLevel verbLevel = this->getVerbLevel ();
1288 // RCP<Teuchos::FancyOStream> out = this->getOStream ();
1289 // const bool doPrint = out.get () && (comm_->getRank () == 0) &&
1290 // includesVerbLevel (verbLevel, Teuchos::VERB_EXTREME, true);
1291 
1292 // if (doPrint) {
1293 // // Only need one process to print out parameters.
1294 // *out << "Distributor::doPosts (4 args)" << endl;
1295 // }
1296 // // Add one tab level. We declare this outside the doPrint scopes
1297 // // so that the tab persists until the end of this method.
1298 // Teuchos::OSTab tab = this->getOSTab ();
1299 // if (doPrint) {
1300 // *out << "Parameters:" << endl;
1301 // {
1302 // OSTab tab2 (out);
1303 // *out << "sendType: " << DistributorSendTypeEnumToString (sendType)
1304 // << endl << "barrierBetween: " << doBarrier << endl;
1305 // }
1306 // }
1307 // #endif // HAVE_TEUCHOS_DEBUG
1308 
1309  TEUCHOS_TEST_FOR_EXCEPTION(sendType == Details::DISTRIBUTOR_RSEND && ! doBarrier,
1310  std::logic_error, "Ready send implementation requires a barrier between "
1311  "posting receives and posting ready sends. This should have been checked "
1312  "before. Please report this bug to the Tpetra developers.");
1313 
1314  const int myImageID = comm_->getRank();
1315  size_t selfReceiveOffset = 0;
1316 
1317 #ifdef HAVE_TEUCHOS_DEBUG
1318  // Different messages may have different numbers of packets.
1319  size_t totalNumImportPackets = 0;
1320  for (int ii = 0; ii < numImportPacketsPerLID.size(); ++ii) {
1321  totalNumImportPackets += numImportPacketsPerLID[ii];
1322  }
1323  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t> (imports.size ()) != totalNumImportPackets,
1324  std::runtime_error, Teuchos::typeName (*this) << "::doPosts(): The "
1325  "imports array argument must be large enough to store the imported "
1326  "data. imports.size() = " << imports.size() << ", but the total number "
1327  "of packets is " << totalNumImportPackets << ".");
1328 #endif // HAVE_TEUCHOS_DEBUG
1329 
1330  // MPI tag for nonblocking receives and blocking sends in this
1331  // method. Some processes might take the "fast" path
1332  // (indicesTo_.empty()) and others might take the "slow" path for
1333  // the same doPosts() call, so the path tag must be the same for
1334  // both.
1335  const int pathTag = 1;
1336  const int tag = this->getTag (pathTag);
1337 
1338  if (debug_) {
1340  requests_.size () != 0, std::logic_error, "doPosts(4): Process "
1341  << myImageID << ": requests_.size() = " << requests_.size ()
1342  << " != 0.");
1343  std::ostringstream os;
1344  os << myImageID << ": doPosts(4,"
1345  << (indicesTo_.empty () ? "fast" : "slow") << ")" << endl;
1346  *out_ << os.str ();
1347  }
1348 
1349  // Distributor uses requests_.size() as the number of outstanding
1350  // nonblocking message requests, so we resize to zero to maintain
1351  // this invariant.
1352  //
1353  // numReceives_ does _not_ include the self message, if there is
1354  // one. Here, we do actually send a message to ourselves, so we
1355  // include any self message in the "actual" number of receives to
1356  // post.
1357  //
1358  // NOTE (mfh 19 Mar 2012): Epetra_MpiDistributor::DoPosts()
1359  // doesn't (re)allocate its array of requests. That happens in
1360  // CreateFromSends(), ComputeRecvs_(), DoReversePosts() (on
1361  // demand), or Resize_().
1362  const size_type actualNumReceives = as<size_type> (numReceives_) +
1363  as<size_type> (selfMessage_ ? 1 : 0);
1364  requests_.resize (0);
1365 
1366  // Post the nonblocking receives. It's common MPI wisdom to post
1367  // receives before sends. In MPI terms, this means favoring
1368  // adding to the "posted queue" (of receive requests) over adding
1369  // to the "unexpected queue" (of arrived messages not yet matched
1370  // with a receive).
1371  {
1372 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1373  Teuchos::TimeMonitor timeMonRecvs (*timer_doPosts4_recvs_);
1374 #endif // TPETRA_DISTRIBUTOR_TIMERS
1375 
1376  size_t curBufferOffset = 0;
1377  size_t curLIDoffset = 0;
1378  for (size_type i = 0; i < actualNumReceives; ++i) {
1379  size_t totalPacketsFrom_i = 0;
1380  for (size_t j = 0; j < lengthsFrom_[i]; ++j) {
1381  totalPacketsFrom_i += numImportPacketsPerLID[curLIDoffset+j];
1382  }
1383  curLIDoffset += lengthsFrom_[i];
1384  if (imagesFrom_[i] != myImageID && totalPacketsFrom_i) {
1385  // If my process is receiving these packet(s) from another
1386  // process (not a self-receive), and if there is at least
1387  // one packet to receive:
1388  //
1389  // 1. Set up the persisting view (recvBuf) into the imports
1390  // array, given the offset and size (total number of
1391  // packets from process imagesFrom_[i]).
1392  // 2. Start the Irecv and save the resulting request.
1393  ArrayRCP<Packet> recvBuf =
1394  imports.persistingView (curBufferOffset, totalPacketsFrom_i);
1395  requests_.push_back (ireceive<int, Packet> (recvBuf, imagesFrom_[i],
1396  tag, *comm_));
1397  }
1398  else { // Receiving these packet(s) from myself
1399  selfReceiveOffset = curBufferOffset; // Remember the offset
1400  }
1401  curBufferOffset += totalPacketsFrom_i;
1402  }
1403  }
1404 
1405  if (doBarrier) {
1406 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1407  Teuchos::TimeMonitor timeMonBarrier (*timer_doPosts4_barrier_);
1408 #endif // TPETRA_DISTRIBUTOR_TIMERS
1409  // If we are using ready sends (MPI_Rsend) below, we need to do
1410  // a barrier before we post the ready sends. This is because a
1411  // ready send requires that its matching receive has already
1412  // been posted before the send has been posted. The only way to
1413  // guarantee that in this case is to use a barrier.
1414  comm_->barrier ();
1415  }
1416 
1417 #ifdef TPETRA_DISTRIBUTOR_TIMERS
1418  Teuchos::TimeMonitor timeMonSends (*timer_doPosts4_sends_);
1419 #endif // TPETRA_DISTRIBUTOR_TIMERS
1420 
1421  // setup arrays containing starting-offsets into exports for each send,
1422  // and num-packets-to-send for each send.
1423  Array<size_t> sendPacketOffsets(numSends_,0), packetsPerSend(numSends_,0);
1424  size_t maxNumPackets = 0;
1425  size_t curPKToffset = 0;
1426  for (size_t pp=0; pp<numSends_; ++pp) {
1427  sendPacketOffsets[pp] = curPKToffset;
1428  size_t numPackets = 0;
1429  for (size_t j=startsTo_[pp]; j<startsTo_[pp]+lengthsTo_[pp]; ++j) {
1430  numPackets += numExportPacketsPerLID[j];
1431  }
1432  if (numPackets > maxNumPackets) maxNumPackets = numPackets;
1433  packetsPerSend[pp] = numPackets;
1434  curPKToffset += numPackets;
1435  }
1436 
1437  // setup scan through imagesTo_ list starting with higher numbered images
1438  // (should help balance message traffic)
1439  size_t numBlocks = numSends_+ selfMessage_;
1440  size_t imageIndex = 0;
1441  while ((imageIndex < numBlocks) && (imagesTo_[imageIndex] < myImageID)) {
1442  ++imageIndex;
1443  }
1444  if (imageIndex == numBlocks) {
1445  imageIndex = 0;
1446  }
1447 
1448  size_t selfNum = 0;
1449  size_t selfIndex = 0;
1450 
1451  if (indicesTo_.empty()) {
1452  if (debug_) {
1453  std::ostringstream os;
1454  os << myImageID << ": doPosts(4,fast): posting sends" << endl;
1455  *out_ << os.str ();
1456  }
1457 
1458  // Data are already blocked (laid out) by process, so we don't
1459  // need a separate send buffer (besides the exports array).
1460  for (size_t i = 0; i < numBlocks; ++i) {
1461  size_t p = i + imageIndex;
1462  if (p > (numBlocks - 1)) {
1463  p -= numBlocks;
1464  }
1465 
1466  if (imagesTo_[p] != myImageID && packetsPerSend[p] > 0) {
1467  ArrayView<const Packet> tmpSend =
1468  exports.view (sendPacketOffsets[p], packetsPerSend[p]);
1469 
1470  if (sendType == Details::DISTRIBUTOR_SEND) { // the default, so put it first
1471  send<int, Packet> (tmpSend.getRawPtr (),
1472  as<int> (tmpSend.size ()),
1473  imagesTo_[p], tag, *comm_);
1474  }
1475  else if (sendType == Details::DISTRIBUTOR_RSEND) {
1476  readySend<int, Packet> (tmpSend.getRawPtr (),
1477  as<int> (tmpSend.size ()),
1478  imagesTo_[p], tag, *comm_);
1479  }
1480  else if (sendType == Details::DISTRIBUTOR_ISEND) {
1481  ArrayRCP<const Packet> tmpSendBuf =
1482  exports.persistingView (sendPacketOffsets[p], packetsPerSend[p]);
1483  requests_.push_back (isend<int, Packet> (tmpSendBuf, imagesTo_[p],
1484  tag, *comm_));
1485  }
1486  else if (sendType == Details::DISTRIBUTOR_SSEND) {
1487  ssend<int, Packet> (tmpSend.getRawPtr (),
1488  as<int> (tmpSend.size ()),
1489  imagesTo_[p], tag, *comm_);
1490  }
1491  else {
1492  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Tpetra::"
1493  "Distributor (4 args): Invalid send type. We should never get "
1494  "here. Please report this bug to the Tpetra developers.");
1495  }
1496  }
1497  else { // "Sending" the message to myself
1498  selfNum = p;
1499  }
1500  }
1501 
1502  if (selfMessage_) {
1503  std::copy (exports.begin()+sendPacketOffsets[selfNum],
1504  exports.begin()+sendPacketOffsets[selfNum]+packetsPerSend[selfNum],
1505  imports.begin()+selfReceiveOffset);
1506  }
1507  if (debug_) {
1508  std::ostringstream os;
1509  os << myImageID << ": doPosts(4,fast) done" << endl;
1510  *out_ << os.str ();
1511  }
1512  }
1513  else { // data are not blocked by image, use send buffer
1514  if (debug_) {
1515  std::ostringstream os;
1516  os << myImageID << ": doPosts(4,slow): posting sends" << endl;
1517  *out_ << os.str ();
1518  }
1519 
1520  // FIXME (mfh 05 Mar 2013) This may be broken for Isend.
1521  ArrayRCP<Packet> sendArray (maxNumPackets); // send buffer
1522 
1524  sendType == Details::DISTRIBUTOR_ISEND, std::logic_error,
1525  "Tpetra::Distributor::doPosts<" << TypeNameTraits<Packet>::name()
1526  << "> (3-argument version):" << endl
1527  << "The \"send buffer\" code path may not necessarily work with nonblocking sends.");
1528 
1529  Array<size_t> indicesOffsets (numExportPacketsPerLID.size(), 0);
1530  size_t ioffset = 0;
1531  for (int j=0; j<numExportPacketsPerLID.size(); ++j) {
1532  indicesOffsets[j] = ioffset;
1533  ioffset += numExportPacketsPerLID[j];
1534  }
1535 
1536  for (size_t i = 0; i < numBlocks; ++i) {
1537  size_t p = i + imageIndex;
1538  if (p > (numBlocks - 1)) {
1539  p -= numBlocks;
1540  }
1541 
1542  if (imagesTo_[p] != myImageID) {
1543  typename ArrayView<const Packet>::iterator srcBegin, srcEnd;
1544  size_t sendArrayOffset = 0;
1545  size_t j = startsTo_[p];
1546  size_t numPacketsTo_p = 0;
1547  for (size_t k = 0; k < lengthsTo_[p]; ++k, ++j) {
1548  srcBegin = exports.begin() + indicesOffsets[j];
1549  srcEnd = srcBegin + numExportPacketsPerLID[j];
1550  numPacketsTo_p += numExportPacketsPerLID[j];
1551  std::copy (srcBegin, srcEnd, sendArray.begin()+sendArrayOffset);
1552  sendArrayOffset += numExportPacketsPerLID[j];
1553  }
1554  if (numPacketsTo_p > 0) {
1555  ArrayView<const Packet> tmpSend =
1556  sendArray.view (0, numPacketsTo_p);
1557 
1558  if (sendType == Details::DISTRIBUTOR_RSEND) {
1559  readySend<int, Packet> (tmpSend.getRawPtr (),
1560  as<int> (tmpSend.size ()),
1561  imagesTo_[p], tag, *comm_);
1562  }
1563  else if (sendType == Details::DISTRIBUTOR_ISEND) {
1564  ArrayRCP<const Packet> tmpSendBuf =
1565  sendArray.persistingView (0, numPacketsTo_p);
1566  requests_.push_back (isend<int, Packet> (tmpSendBuf, imagesTo_[p],
1567  tag, *comm_));
1568  }
1569  else if (sendType == Details::DISTRIBUTOR_SSEND) {
1570  ssend<int, Packet> (tmpSend.getRawPtr (),
1571  as<int> (tmpSend.size ()),
1572  imagesTo_[p], tag, *comm_);
1573  }
1574  else { // if (sendType == Details::DISTRIBUTOR_SSEND)
1575  send<int, Packet> (tmpSend.getRawPtr (),
1576  as<int> (tmpSend.size ()),
1577  imagesTo_[p], tag, *comm_);
1578  }
1579  }
1580  }
1581  else { // "Sending" the message to myself
1582  selfNum = p;
1583  selfIndex = startsTo_[p];
1584  }
1585  }
1586 
1587  if (selfMessage_) {
1588  for (size_t k = 0; k < lengthsTo_[selfNum]; ++k) {
1589  std::copy (exports.begin()+indicesOffsets[selfIndex],
1590  exports.begin()+indicesOffsets[selfIndex]+numExportPacketsPerLID[selfIndex],
1591  imports.begin() + selfReceiveOffset);
1592  selfReceiveOffset += numExportPacketsPerLID[selfIndex];
1593  ++selfIndex;
1594  }
1595  }
1596  if (debug_) {
1597  std::ostringstream os;
1598  os << myImageID << ": doPosts(4,slow) done" << endl;
1599  *out_ << os.str ();
1600  }
1601  }
1602  }
1603 
1604  template <class Packet>
1605  void Distributor::
1607  size_t numPackets,
1608  const ArrayView<Packet>& imports)
1609  {
1610  using Teuchos::as;
1611 
1612  // doReversePosts() takes exports and imports as ArrayRCPs,
1613  // requiring that the memory locations are persisting. However,
1614  // they need only persist within the scope of that routine, so it
1615  // is safe for us to use nonpersisting references in this case.
1616 
1617  // mfh 04 Apr 2012: For some reason, calling arcp<const Packet>
1618  // for Packet=std::complex<T> (e.g., T=float) fails to compile
1619  // with some versions of GCC. The issue only arises with the
1620  // exports array. This is why we construct a separate nonowning
1621  // ArrayRCP.
1622  typedef typename ArrayRCP<const Packet>::size_type size_type;
1623  ArrayRCP<const Packet> exportsArcp (exports.getRawPtr(), as<size_type> (0),
1624  exports.size(), false);
1625  // mfh 04 Apr 2012: This is the offending code. This statement
1626  // would normally be in place of "exportsArcp" in the
1627  // doReversePosts() call below.
1628  //arcp<const Packet> (exports.getRawPtr(), 0, exports.size(), false)
1629  doReversePosts (exportsArcp,
1630  numPackets,
1631  arcp<Packet> (imports.getRawPtr (), 0, imports.size (), false));
1632  doReverseWaits ();
1633  }
1634 
1635  template <class Packet>
1636  void Distributor::
1638  const ArrayView<size_t> &numExportPacketsPerLID,
1639  const ArrayView<Packet> &imports,
1640  const ArrayView<size_t> &numImportPacketsPerLID)
1641  {
1642  using Teuchos::as;
1643  using Teuchos::arcp;
1644  using Teuchos::ArrayRCP;
1645 
1646  TEUCHOS_TEST_FOR_EXCEPTION(requests_.size() != 0, std::runtime_error,
1647  Teuchos::typeName(*this) << "::doReversePostsAndWaits(): There are "
1648  << requests_.size() << " outstanding nonblocking messages pending. It is "
1649  "incorrect to call doReversePostsAndWaits with posts outstanding.");
1650 
1651  // doReversePosts() accepts the exports and imports arrays as
1652  // ArrayRCPs, requiring that the memory location is persisting (as
1653  // is necessary for nonblocking receives). However, it need only
1654  // persist until doReverseWaits() completes, so it is safe for us
1655  // to use a nonpersisting reference in this case. The use of a
1656  // nonpersisting reference is purely a performance optimization.
1657 
1658  // mfh 02 Apr 2012: For some reason, calling arcp<const Packet>
1659  // for Packet=std::complex<double> fails to compile with some
1660  // versions of GCC. The issue only arises with the exports array.
1661  // This is why we construct a separate nonowning ArrayRCP.
1662  typedef typename ArrayRCP<const Packet>::size_type size_type;
1663  ArrayRCP<const Packet> exportsArcp (exports.getRawPtr (), as<size_type> (0),
1664  exports.size (), false);
1665  doReversePosts (exportsArcp,
1666  numExportPacketsPerLID,
1667  arcp<Packet> (imports.getRawPtr (), 0, imports.size (), false),
1668  numImportPacketsPerLID);
1669  doReverseWaits ();
1670  }
1671 
1672  template <class Packet>
1673  void Distributor::
1675  size_t numPackets,
1676  const ArrayRCP<Packet>& imports)
1677  {
1678  // FIXME (mfh 29 Mar 2012) WHY?
1679  TEUCHOS_TEST_FOR_EXCEPTION(! indicesTo_.empty (), std::runtime_error,
1680  Teuchos::typeName (*this) << "::doReversePosts(): Can only do reverse "
1681  "communication when original data are blocked by process.");
1682  if (reverseDistributor_.is_null ()) {
1683  createReverseDistributor ();
1684  }
1685  reverseDistributor_->doPosts (exports, numPackets, imports);
1686  }
1687 
1688  template <class Packet>
1689  void Distributor::
1691  const ArrayView<size_t>& numExportPacketsPerLID,
1692  const ArrayRCP<Packet>& imports,
1693  const ArrayView<size_t>& numImportPacketsPerLID)
1694  {
1695  // FIXME (mfh 29 Mar 2012) WHY?
1696  TEUCHOS_TEST_FOR_EXCEPTION(! indicesTo_.empty (), std::runtime_error,
1697  Teuchos::typeName (*this) << "::doReversePosts(): Can only do reverse "
1698  "communication when original data are blocked by process.");
1699  if (reverseDistributor_.is_null ()) {
1700  createReverseDistributor ();
1701  }
1702  reverseDistributor_->doPosts (exports, numExportPacketsPerLID,
1703  imports, numImportPacketsPerLID);
1704  }
1705 
1706  template <class OrdinalType>
1707  void Distributor::
1708  computeSends (const ArrayView<const OrdinalType> & importIDs,
1709  const ArrayView<const int> & importNodeIDs,
1710  Array<OrdinalType> & exportIDs,
1711  Array<int> & exportNodeIDs)
1712  {
1713  // NOTE (mfh 19 Apr 2012): There was a note on this code saying:
1714  // "assumes that size_t >= Ordinal". The code certainly does
1715  // assume that sizeof(size_t) >= sizeof(OrdinalType) as well as
1716  // sizeof(size_t) >= sizeof(int). This is because it casts the
1717  // OrdinalType elements of importIDs (along with their
1718  // corresponding process IDs, as int) to size_t, and does a
1719  // doPostsAndWaits<size_t>() to send the packed data.
1720  using Teuchos::as;
1721  using std::endl;
1722 
1723  Teuchos::OSTab tab (out_);
1724 
1725  const int myRank = comm_->getRank();
1726  if (debug_) {
1727  std::ostringstream os;
1728  os << myRank << ": computeSends" << endl;
1729  *out_ << os.str ();
1730  }
1731 
1732  const size_t numImports = importNodeIDs.size();
1733  TEUCHOS_TEST_FOR_EXCEPTION(as<size_t> (importIDs.size ()) < numImports,
1734  std::invalid_argument, "Tpetra::Distributor::computeSends: importNodeIDs."
1735  "size() = " << importNodeIDs.size () << " != importIDs.size() = "
1736  << importIDs.size () << ".");
1737 
1738  Array<size_t> importObjs (2*numImports);
1739  // Pack pairs (importIDs[i], my process ID) to send into importObjs.
1740  for (size_t i = 0; i < numImports; ++i ) {
1741  importObjs[2*i] = as<size_t> (importIDs[i]);
1742  importObjs[2*i+1] = as<size_t> (myRank);
1743  }
1744  //
1745  // Use a temporary Distributor to send the (importIDs[i], myRank)
1746  // pairs to importNodeIDs[i].
1747  //
1748  size_t numExports;
1749  Distributor tempPlan (comm_, out_);
1750  if (debug_) {
1751  std::ostringstream os;
1752  os << myRank << ": computeSends: tempPlan.createFromSends" << endl;
1753  *out_ << os.str ();
1754  }
1755  numExports = tempPlan.createFromSends (importNodeIDs);
1756  if (numExports > 0) {
1757  exportIDs.resize(numExports);
1758  exportNodeIDs.resize(numExports);
1759  }
1760  Array<size_t> exportObjs (tempPlan.getTotalReceiveLength () * 2);
1761  if (debug_) {
1762  std::ostringstream os;
1763  os << myRank << ": computeSends: tempPlan.doPostsAndWaits" << endl;
1764  *out_ << os.str ();
1765  }
1766  tempPlan.doPostsAndWaits<size_t> (importObjs (), 2, exportObjs ());
1767 
1768  // Unpack received (GID, PID) pairs into exportIDs resp. exportNodeIDs.
1769  for (size_t i = 0; i < numExports; ++i) {
1770  exportIDs[i] = as<OrdinalType>(exportObjs[2*i]);
1771  exportNodeIDs[i] = exportObjs[2*i+1];
1772  }
1773 
1774  if (debug_) {
1775  std::ostringstream os;
1776  os << myRank << ": computeSends done" << endl;
1777  *out_ << os.str ();
1778  }
1779  }
1780 
1781  template <class OrdinalType>
1782  void Distributor::
1784  const ArrayView<const int> &remoteImageIDs,
1785  Array<OrdinalType> &exportGIDs,
1786  Array<int> &exportNodeIDs)
1787  {
1788  using std::endl;
1789 
1790  Teuchos::OSTab tab (out_);
1791  const int myRank = comm_->getRank();
1792 
1793  if (debug_) {
1794  *out_ << myRank << ": createFromRecvs" << endl;
1795  }
1796 
1797 #ifdef HAVE_TPETRA_DEBUG
1798  using Teuchos::outArg;
1799  using Teuchos::reduceAll;
1800 
1801  const int errProc =
1802  (remoteIDs.size () != remoteImageIDs.size ()) ? myRank : -1;
1803  int maxErrProc = -1;
1804  reduceAll<int, int> (*comm_, Teuchos::REDUCE_MAX, errProc, outArg (maxErrProc));
1805  TEUCHOS_TEST_FOR_EXCEPTION(maxErrProc != -1, std::runtime_error,
1806  Teuchos::typeName (*this) << "::createFromRecvs(): lists of remote IDs "
1807  "and remote process IDs must have the same size on all participating "
1808  "processes. Maximum process ID with error: " << maxErrProc << ".");
1809 #endif // HAVE_TPETRA_DEBUG
1810 
1811  computeSends (remoteIDs, remoteImageIDs, exportGIDs, exportNodeIDs);
1812  (void) createFromSends (exportNodeIDs ());
1813 
1814  if (debug_) {
1815  *out_ << myRank << ": createFromRecvs done" << endl;
1816  }
1817  }
1818 
1819 } // namespace Tpetra
1820 
1821 #endif // TPETRA_DISTRIBUTOR_HPP
Namespace Tpetra contains the class and methods constituting the Tpetra library.
ArrayRCP< T > persistingView(size_type lowerOffset, size_type size) const
basic_OSTab< char > OSTab
basic_FancyOStream< char > FancyOStream
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
void doPosts(const ArrayRCP< const Packet > &exports, size_t numPackets, const ArrayRCP< Packet > &imports)
Post the data for a forward plan, but do not execute the waits yet.
size_type size() const
Implementation details of Tpetra.
void doPostsAndWaits(const ArrayView< const Packet > &exports, size_t numPackets, const ArrayView< Packet > &imports)
Execute the (forward) communication plan.
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.
size_t createFromSends(const ArrayView< const int > &exportNodeIDs)
Set up Distributor using list of process ranks to which this process will send.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
void resize(size_type new_size, const value_type &x=value_type())
T * getRawPtr() const
void doReversePosts(const ArrayRCP< const Packet > &exports, size_t numPackets, const ArrayRCP< Packet > &imports)
Post the data for a reverse plan, but do not execute the waits yet.
TEUCHOSCORE_LIB_DLL_EXPORT bool includesVerbLevel(const EVerbosityLevel verbLevel, const EVerbosityLevel requestedVerbLevel, const bool isDefaultLevel=false)
static const EVerbosityLevel verbLevel_default
RCP< CommRequest< Ordinal > > ireceive(const ArrayRCP< Packet > &recvBuffer, const int sourceRank, const int tag, const Comm< Ordinal > &comm)
void doReversePostsAndWaits(const ArrayView< const Packet > &exports, size_t numPackets, const ArrayView< Packet > &imports)
Execute the reverse communication plan.
TypeTo as(const TypeFrom &t)
Stand-alone utility functions and macros.
void ssend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
RCP< CommRequest< Ordinal > > isend(const ArrayRCP< const Packet > &sendBuffer, const int destRank, const int tag, const Comm< Ordinal > &comm)
Array< std::string > distributorSendTypes()
Valid values for Distributor&#39;s "Send type" parameter.
void createFromRecvs(const ArrayView< const Ordinal > &remoteIDs, const ArrayView< const int > &remoteNodeIDs, Array< Ordinal > &exportIDs, Array< int > &exportNodeIDs)
Set up Distributor using list of process ranks from which to receive.
void readySend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
iterator begin() const
std::string typeName(const T &t)
ArrayView< T > view(size_type lowerOffset, size_type size) const