Tpetra Matrix/Vector Services  Version of the Day
Tpetra_Import_def.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_IMPORT_DEF_HPP
43 #define TPETRA_IMPORT_DEF_HPP
44 
45 #ifdef DOXYGEN_USE_ONLY
46 # include <Tpetra_Import_decl.hpp>
47 #endif // DOXYGEN_USE_ONLY
48 
49 #include <Tpetra_Distributor.hpp>
50 #include <Tpetra_Map.hpp>
51 #include <Tpetra_ImportExportData.hpp>
52 #include <Tpetra_Util.hpp>
53 #include <Tpetra_Export.hpp>
54 #include <Teuchos_as.hpp>
55 
56 
57 namespace {
58  // Default value of Import's "Debug" parameter.
59  const bool tpetraImportDebugDefault = false;
60 } // namespace (anonymous)
61 
62 namespace Tpetra {
63 
64  template <class LocalOrdinal, class GlobalOrdinal, class Node>
65  void
66  Import<LocalOrdinal,GlobalOrdinal,Node>::
67  setParameterList (const Teuchos::RCP<Teuchos::ParameterList>& plist)
68  {
69  bool debug = tpetraImportDebugDefault;
70  if (! plist.is_null ()) {
71  try {
72  debug = plist->get<bool> ("Debug");
74  }
75  debug_ = debug;
76  ImportData_->distributor_.setParameterList (plist);
77  }
78 
79  template <class LocalOrdinal, class GlobalOrdinal, class Node>
80  void
85  {
86  using Teuchos::null;
87  using Teuchos::Ptr;
88  using Teuchos::rcp;
89  using std::endl;
91 
92  // Read "Debug" parameter from the input ParameterList.
93  bool debug = tpetraImportDebugDefault;
94  if (! plist.is_null ()) {
95  try {
96  debug = plist->get<bool> ("Debug");
98  }
99  debug_ = debug;
100 
101  if (! out_.is_null ()) {
102  out_->pushTab ();
103  }
104  if (debug_) {
105  std::ostringstream os;
106  const int myRank = source->getComm ()->getRank ();
107  os << myRank << ": Import ctor" << endl;
108  *out_ << os.str ();
109  }
110  ImportData_ = rcp (new data_type (source, target, out_, plist));
111 
112  Array<GlobalOrdinal> remoteGIDs;
113  setupSamePermuteRemote (remoteGIDs);
114  if (debug_) {
115  std::ostringstream os;
116  const int myRank = source->getComm ()->getRank ();
117  os << myRank << ": Import ctor: "
118  << "setupSamePermuteRemote done" << endl;
119  *out_ << os.str ();
120  }
121  if (source->isDistributed ()) {
122  setupExport (remoteGIDs);
123  }
124  if (debug_) {
125  std::ostringstream os;
126  const int myRank = source->getComm ()->getRank ();
127  os << myRank << ": Import ctor: done" << endl;
128  *out_ << os.str ();
129  }
130  if (! out_.is_null ()) {
131  out_->popTab ();
132  }
133  }
134 
135  template <class LocalOrdinal, class GlobalOrdinal, class Node>
138  const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& target) :
139  out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr))),
140  debug_ (tpetraImportDebugDefault)
141  {
142  init (source, target, Teuchos::null);
143  }
144 
145  template <class LocalOrdinal, class GlobalOrdinal, class Node>
148  const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& target,
149  const RCP<Teuchos::FancyOStream>& out) :
150  out_ (out),
151  debug_ (tpetraImportDebugDefault)
152  {
153  init (source, target, Teuchos::null);
154  }
155 
156  template <class LocalOrdinal, class GlobalOrdinal, class Node>
161  out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr))),
162  debug_ (tpetraImportDebugDefault)
163  {
164  init (source, target, plist);
165  }
166 
167  template <class LocalOrdinal, class GlobalOrdinal, class Node>
171  const RCP<Teuchos::FancyOStream>& out,
173  out_ (out),
174  debug_ (tpetraImportDebugDefault)
175  {
176  init (source, target, plist);
177  }
178 
179  template <class LocalOrdinal, class GlobalOrdinal, class Node>
182  : ImportData_ (rhs.ImportData_)
183  , out_ (rhs.out_)
184  , debug_ (rhs.debug_)
185  {}
186 
187  template <class LocalOrdinal, class GlobalOrdinal, class Node>
190  : out_ (exporter.out_)
191  , debug_ (exporter.debug_)
192  {
193  if (! exporter.ExportData_.is_null ()) {
194  ImportData_ = exporter.ExportData_->reverseClone ();
195  }
196  }
197 
198  template <class LocalOrdinal, class GlobalOrdinal, class Node>
201  const Teuchos::RCP<const map_type>& target,
202  const size_t numSameIDs,
203  Teuchos::Array<LocalOrdinal>& permuteToLIDs,
204  Teuchos::Array<LocalOrdinal>& permuteFromLIDs,
205  Teuchos::Array<LocalOrdinal>& remoteLIDs,
206  Teuchos::Array<LocalOrdinal>& exportLIDs,
207  Teuchos::Array<int>& exportPIDs,
208  Distributor& distributor,
211  out_ (out.is_null () ? Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr)) : out),
212  debug_ (tpetraImportDebugDefault)
213  {
214  using Teuchos::null;
215  using Teuchos::Ptr;
216  using Teuchos::rcp;
217  using std::cerr;
218  using std::endl;
220 
221  // Read "Debug" parameter from the input ParameterList.
222  bool debug = tpetraImportDebugDefault;
223  if (! plist.is_null ()) {
224  try {
225  debug = plist->get<bool> ("Debug");
227  }
228  debug_ = debug;
229 
230  if (! out_.is_null ()) {
231  out_->pushTab ();
232  }
233  if (debug_ && ! out_.is_null ()) {
234  std::ostringstream os;
235  const int myRank = source->getComm ()->getRank ();
236  os << myRank << ": Import expert ctor" << endl;
237  *out_ << os.str ();
238  }
239  ImportData_ = rcp (new data_type (source, target, out_, plist));
240 
241  ImportData_->numSameIDs_ = numSameIDs;
242  ImportData_->permuteToLIDs_.swap (permuteToLIDs);
243  ImportData_->permuteFromLIDs_.swap (permuteFromLIDs);
244  ImportData_->remoteLIDs_.swap (remoteLIDs);
245  ImportData_->distributor_.swap (distributor);
246  ImportData_->exportLIDs_.swap (exportLIDs);
247  ImportData_->exportPIDs_.swap (exportPIDs);
248  }
249 
250  template <class LocalOrdinal, class GlobalOrdinal, class Node>
252  {}
253 
254  template <class LocalOrdinal, class GlobalOrdinal, class Node>
256  return ImportData_->numSameIDs_;
257  }
258 
259  template <class LocalOrdinal, class GlobalOrdinal, class Node>
261  return ImportData_->permuteFromLIDs_.size();
262  }
263 
264  template <class LocalOrdinal, class GlobalOrdinal, class Node>
267  return ImportData_->permuteFromLIDs_();
268  }
269 
270  template <class LocalOrdinal, class GlobalOrdinal, class Node>
273  return ImportData_->permuteToLIDs_();
274  }
275 
276  template <class LocalOrdinal, class GlobalOrdinal, class Node>
278  return ImportData_->remoteLIDs_.size();
279  }
280 
281  template <class LocalOrdinal, class GlobalOrdinal, class Node>
284  return ImportData_->remoteLIDs_();
285  }
286 
287  template <class LocalOrdinal, class GlobalOrdinal, class Node>
289  return ImportData_->exportLIDs_.size();
290  }
291 
292  template <class LocalOrdinal, class GlobalOrdinal, class Node>
295  return ImportData_->exportLIDs_();
296  }
297 
298  template <class LocalOrdinal, class GlobalOrdinal, class Node>
301  return ImportData_->exportPIDs_();
302  }
303 
304  template <class LocalOrdinal, class GlobalOrdinal, class Node>
307  return ImportData_->source_;
308  }
309 
310  template <class LocalOrdinal, class GlobalOrdinal, class Node>
313  return ImportData_->target_;
314  }
315 
316  template <class LocalOrdinal, class GlobalOrdinal, class Node>
317  Distributor &
319  return ImportData_->distributor_;
320  }
321 
322  template <class LocalOrdinal, class GlobalOrdinal, class Node>
326  if (&rhs != this) {
327  ImportData_ = rhs.ImportData_;
328  }
329  return *this;
330  }
331 
332  template <class LocalOrdinal, class GlobalOrdinal, class Node>
334  print (std::ostream& os) const
335  {
336  using Teuchos::Comm;
337  using Teuchos::getFancyOStream;
338  using Teuchos::RCP;
339  using Teuchos::rcpFromRef;
340  using Teuchos::toString;
341  using std::endl;
342 
343  RCP<const Comm<int> > comm = getSourceMap ()->getComm ();
344  const int myImageID = comm->getRank ();
345  const int numImages = comm->getSize ();
346  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
347  if (myImageID == imageCtr) {
348  os << endl;
349  if (myImageID == 0) { // I'm the root node (only output this info once)
350  os << "Import Data Members:" << endl;
351  }
352  os << "Image ID : " << myImageID << endl;
353 
354  os << "permuteFromLIDs: " << toString (getPermuteFromLIDs ()) << endl;
355  os << "permuteToLIDs : " << toString (getPermuteToLIDs ()) << endl;
356  os << "remoteLIDs : " << toString (getRemoteLIDs ()) << endl;
357  os << "exportLIDs : " << toString (getExportLIDs ()) << endl;
358  os << "exportPIDs : " << toString (getExportPIDs ()) << endl;
359 
360  os << "numSameIDs : " << getNumSameIDs () << endl;
361  os << "numPermuteIDs : " << getNumPermuteIDs () << endl;
362  os << "numRemoteIDs : " << getNumRemoteIDs () << endl;
363  os << "numExportIDs : " << getNumExportIDs () << endl;
364  }
365  // A few global barriers give output a chance to complete.
366  comm->barrier();
367  comm->barrier();
368  comm->barrier();
369  }
370 
371  const bool printMaps = false;
372  if (printMaps) {
373  if (myImageID == 0) {
374  os << endl << endl << "Source Map:" << endl << std::flush;
375  }
376  comm->barrier();
377  os << *getSourceMap();
378  comm->barrier();
379 
380  if (myImageID == 0) {
381  os << endl << endl << "Target Map:" << endl << std::flush;
382  }
383  comm->barrier();
384  os << *getTargetMap();
385  comm->barrier();
386  }
387 
388  // It's also helpful for debugging to print the Distributor
389  // object. Epetra_Import::Print() does this, so we can do a
390  // side-by-side comparison.
391  if (myImageID == 0) {
392  os << endl << endl << "Distributor:" << endl << std::flush;
393  }
394  comm->barrier();
395  getDistributor().describe (*(getFancyOStream (rcpFromRef (os))),
397  }
398 
399 
400  template <class LocalOrdinal, class GlobalOrdinal, class Node>
401  void
404  {
405  using Teuchos::arcp;
406  using Teuchos::Array;
407  using Teuchos::ArrayRCP;
408  using Teuchos::ArrayView;
409  using Teuchos::as;
410  using Teuchos::null;
411  typedef LocalOrdinal LO;
412  typedef GlobalOrdinal GO;
413  typedef typename ArrayView<const GO>::size_type size_type;
414  const Map<LO,GO,Node>& source = * (getSourceMap ());
415  const Map<LO,GO,Node>& target = * (getTargetMap ());
416  ArrayView<const GO> sourceGIDs = source.getNodeElementList ();
417  ArrayView<const GO> targetGIDs = target.getNodeElementList ();
418 
419 #ifdef HAVE_TPETRA_DEBUG
420  ArrayView<const GO> rawSrcGids = sourceGIDs;
421  ArrayView<const GO> rawTgtGids = targetGIDs;
422 #else
423  const GO* const rawSrcGids = sourceGIDs.getRawPtr ();
424  const GO* const rawTgtGids = targetGIDs.getRawPtr ();
425 #endif // HAVE_TPETRA_DEBUG
426  const size_type numSrcGids = sourceGIDs.size ();
427  const size_type numTgtGids = targetGIDs.size ();
428  const size_type numGids = std::min (numSrcGids, numTgtGids);
429 
430  // Compute numSameIDs_: the number of initial GIDs that are the
431  // same (and occur in the same order) in both Maps. The point of
432  // numSameIDs_ is for the common case of an Import where all the
433  // overlapping GIDs are at the end of the target Map, but
434  // otherwise the source and target Maps are the same. This allows
435  // a fast contiguous copy for the initial "same IDs."
436  size_type numSameGids = 0;
437  for ( ; numSameGids < numGids && rawSrcGids[numSameGids] == rawTgtGids[numSameGids]; ++numSameGids)
438  {} // third clause of 'for' does everything
439  ImportData_->numSameIDs_ = numSameGids;
440 
441  // Compute permuteToLIDs_, permuteFromLIDs_, remoteGIDs, and
442  // remoteLIDs_. The first two arrays are IDs to be permuted, and
443  // the latter two arrays are IDs to be received ("imported"),
444  // called "remote" IDs.
445  //
446  // IDs to permute are in both the source and target Maps, which
447  // means we don't have to send or receive them, but we do have to
448  // rearrange (permute) them in general. IDs to receive are in the
449  // target Map, but not the source Map.
450 
451  Array<LO>& permuteToLIDs = ImportData_->permuteToLIDs_;
452  Array<LO>& permuteFromLIDs = ImportData_->permuteFromLIDs_;
453  Array<LO>& remoteLIDs = ImportData_->remoteLIDs_;
454  const LO LINVALID = Teuchos::OrdinalTraits<LO>::invalid ();
455  const LO numTgtLids = as<LO> (numTgtGids);
456  // Iterate over the target Map's LIDs, since we only need to do
457  // GID -> LID lookups for the source Map.
458  for (LO tgtLid = numSameGids; tgtLid < numTgtLids; ++tgtLid) {
459  const GO curTargetGid = rawTgtGids[tgtLid];
460  // getLocalElement() returns LINVALID if the GID isn't in the source Map.
461  // This saves us a lookup (which isNodeGlobalElement() would do).
462  const LO srcLid = source.getLocalElement (curTargetGid);
463  if (srcLid != LINVALID) { // if source.isNodeGlobalElement (curTargetGid)
464  permuteToLIDs.push_back (tgtLid);
465  permuteFromLIDs.push_back (srcLid);
466  } else {
467  remoteGIDs.push_back (curTargetGid);
468  remoteLIDs.push_back (tgtLid);
469  }
470  }
471 
473  getNumRemoteIDs() > 0 && ! source.isDistributed(),
474  std::runtime_error,
475  "::setupSamePermuteRemote(): Target has remote LIDs but Source is not "
476  "distributed globally." << std::endl
477  << "Importing to a submap of the target map.");
478  }
479 
480 
481  template <class LocalOrdinal, class GlobalOrdinal, class Node>
484  {
485  using Teuchos::arcp;
486  using Teuchos::Array;
487  using Teuchos::ArrayRCP;
488  using Teuchos::ArrayView;
489  using Teuchos::null;
490  using std::endl;
491  typedef LocalOrdinal LO;
492  typedef GlobalOrdinal GO;
493  typedef typename Array<int>::difference_type size_type;
494  const Map<LO, GO, Node> & source = *getSourceMap ();
495 
496  Teuchos::OSTab tab (out_);
497 
498  if (debug_) {
499  std::ostringstream os;
500  const int myRank = source.getComm ()->getRank ();
501  os << myRank << ": Import::setupExport" << endl;
502  *out_ << os.str ();
503  }
504 
505  // For each entry remoteGIDs[i], remoteProcIDs[i] will contain
506  // the process ID of the process that owns that GID.
507  ArrayView<GO> remoteGIDsView = remoteGIDs ();
508  Array<int> remoteProcIDs (remoteGIDsView.size ());
509 
510  // lookup == IDNotPresent means that the source Map wasn't able to
511  // figure out to which processes one or more of the GIDs in the
512  // given list of remote GIDs belong.
513  //
514  // The previous abuse warning said "The target Map has GIDs not
515  // found in the source Map." This statement could be confusing,
516  // because it doesn't refer to ownership by the current process,
517  // but rather to ownership by _any_ process participating in the
518  // Map. (It could not possibly refer to ownership by the current
519  // process, since remoteGIDs is exactly the list of GIDs owned by
520  // the target Map but not owned by the source Map. It was
521  // constructed that way by setupSamePermuteRemote().)
522  //
523  // What this statement means is that the source and target Maps
524  // don't contain the same set of GIDs globally (over all
525  // processes). That is, there is at least one GID owned by some
526  // process in the target Map, which is not owned by _any_ process
527  // in the source Map.
528  const LookupStatus lookup =
529  source.getRemoteIndexList (remoteGIDsView, remoteProcIDs ());
530  if (debug_) {
531  std::ostringstream os;
532  const int myRank = source.getComm ()->getRank ();
533  os << myRank << ": Import::setupExport: finished lookup" << endl;
534  *out_ << os.str ();
535  }
536 
537  TPETRA_ABUSE_WARNING( lookup == IDNotPresent, std::runtime_error,
538  "::setupExport(): the source Map wasn't able to figure out which process "
539  "owns one or more of the GIDs in the list of remote GIDs. This probably "
540  "means that there is at least one GID owned by some process in the target"
541  " Map which is not owned by any process in the source Map. (That is, the"
542  " source and target Maps do not contain the same set of GIDs globally.)");
543 
544  // Ignore remote GIDs that aren't owned by any process in the
545  // source Map. getRemoteIndexList() gives each of these a process
546  // ID of -1.
547  if (lookup == IDNotPresent) {
548  const size_type numInvalidRemote =
549  std::count_if (remoteProcIDs.begin (), remoteProcIDs.end (),
550  std::bind1st (std::equal_to<int> (), -1));
551  // If all of them are invalid, we can delete the whole array.
552  const size_type totalNumRemote = getNumRemoteIDs ();
553  if (numInvalidRemote == totalNumRemote) {
554  // all remotes are invalid; we have no remotes; we can delete the remotes
555  remoteProcIDs.clear ();
556  remoteGIDs.clear (); // This invalidates the view remoteGIDsView
557  ImportData_->remoteLIDs_.clear();
558  }
559  else {
560  // Some remotes are valid; we need to keep the valid ones.
561  // Pack and resize remoteProcIDs, remoteGIDs, and remoteLIDs_.
562  size_type numValidRemote = 0;
563 #ifdef HAVE_TPETRA_DEBUG
564  ArrayView<GlobalOrdinal> remoteGIDsPtr = remoteGIDsView;
565 #else
566  GlobalOrdinal* const remoteGIDsPtr = remoteGIDsView.getRawPtr ();
567 #endif // HAVE_TPETRA_DEBUG
568  for (size_type r = 0; r < totalNumRemote; ++r) {
569  // Pack in all the valid remote PIDs and GIDs.
570  if (remoteProcIDs[r] != -1) {
571  remoteProcIDs[numValidRemote] = remoteProcIDs[r];
572  remoteGIDsPtr[numValidRemote] = remoteGIDsPtr[r];
573  ImportData_->remoteLIDs_[numValidRemote] = ImportData_->remoteLIDs_[r];
574  ++numValidRemote;
575  }
576  }
578  numValidRemote != totalNumRemote - numInvalidRemote, std::logic_error,
579  "Tpetra::Import::setupExport(): After removing invalid remote GIDs and"
580  " packing the valid remote GIDs, numValidRemote = " << numValidRemote
581  << " != totalNumRemote - numInvalidRemote = "
582  << totalNumRemote - numInvalidRemote
583  << ". Please report this bug to the Tpetra developers.");
584 
585  remoteProcIDs.resize (numValidRemote);
586  remoteGIDs.resize (numValidRemote);
587  ImportData_->remoteLIDs_.resize (numValidRemote);
588  }
589  // Revalidate the view after clear or resize.
590  remoteGIDsView = remoteGIDs ();
591  }
592 
593  // Sort remoteProcIDs in ascending order, and apply the resulting
594  // permutation to remoteGIDs and remoteLIDs_. This ensures that
595  // remoteProcIDs[i], remoteGIDs[i], and remoteLIDs_[i] all refer
596  // to the same thing.
597  sort3 (remoteProcIDs.begin (),
598  remoteProcIDs.end (),
599  remoteGIDsView.begin (),
600  ImportData_->remoteLIDs_.begin ());
601 
602  // Call the Distributor's createFromRecvs() method to turn the
603  // remote GIDs and their owning processes into a send-and-receive
604  // communication plan. remoteGIDs and remoteProcIDs_ are input;
605  // exportGIDs and exportProcIDs_ are output arrays which are
606  // allocated by createFromRecvs().
607  Array<GO> exportGIDs;
608  ImportData_->distributor_.createFromRecvs (remoteGIDsView ().getConst (),
609  remoteProcIDs, exportGIDs,
610  ImportData_->exportPIDs_);
611 
612  // Find the LIDs corresponding to the (outgoing) GIDs in
613  // exportGIDs. For sparse matrix-vector multiply, this tells the
614  // calling process how to index into the source vector to get the
615  // elements which it needs to send.
616  const size_type numExportIDs = exportGIDs.size ();
617  if (numExportIDs > 0) {
618  ImportData_->exportLIDs_.resize(numExportIDs);
619 
620  ArrayView<const GO> expGIDs = exportGIDs ();
621  ArrayView<LO> expLIDs = ImportData_->exportLIDs_ ();
622  for (size_type k = 0; k < numExportIDs; ++k) {
623  expLIDs[k] = source.getLocalElement (expGIDs[k]);
624  }
625  }
626  if (debug_) {
627  std::ostringstream os;
628  const int myRank = source.getComm ()->getRank ();
629  os << myRank << ": Import::setupExport: done" << endl;
630  *out_ << os.str ();
631  }
632  }
633 
634  template <class LocalOrdinal, class GlobalOrdinal, class Node>
638  {
639  // mfh 22 Apr 2013: We plan to optimize this in the future, but
640  // provide an unoptimized implementation for now to allow
641  // development to proceed.
642  typedef Tpetra::global_size_t GST;
643  using Teuchos::Array;
644  using Teuchos::ArrayView;
645  using Teuchos::Comm;
646  using Teuchos::RCP;
647  using Teuchos::rcp;
648  using Teuchos::outArg;
649  using Teuchos::REDUCE_MIN;
650  using Teuchos::reduceAll;
651  typedef LocalOrdinal LO;
652  typedef GlobalOrdinal GO;
653  typedef Import<LO, GO, Node> import_type;
654 
655  RCP<const map_type> srcMap = this->getSourceMap ();
656  RCP<const map_type> tgtMap1 = this->getTargetMap ();
657  RCP<const map_type> tgtMap2 = rhs.getTargetMap ();
658  RCP<const Comm<int> > comm = srcMap->getComm ();
659 #ifdef HAVE_TPETRA_DEBUG
661  ! srcMap->isSameAs (* (rhs.getSourceMap ())), std::invalid_argument,
662  "Tpetra::Import::setUnion: The source Map of the input Import must be the "
663  "same as (in the sense of Map::isSameAs) the source Map of this Import.");
665  ! Details::congruent (* (tgtMap1->getComm ()), * (tgtMap2->getComm ())),
666  std::invalid_argument, "Tpetra::Import::setUnion: "
667  "The target Maps must have congruent communicators.");
668 #endif // HAVE_TPETRA_DEBUG
669 
670  ArrayView<const GO> gids1 = tgtMap1->getNodeElementList ();
671  ArrayView<const GO> gids2 = tgtMap2->getNodeElementList ();
672 
673  // Union needs sorted sequences.
674  // Only copy a view if it's not sorted.
675  Array<GO> gids1Copy, gids2Copy;
676  if (! SortDetails::isAlreadySorted (gids1.begin (), gids1.end ())) {
677  gids1Copy.reserve (gids1.size ());
678  gids1Copy.assign (gids1.begin (), gids1.end ());
679  std::sort (gids1Copy.begin (), gids1Copy.end ());
680  gids1 = gids1Copy ();
681  }
682  if (! SortDetails::isAlreadySorted (gids2.begin (), gids2.end ())) {
683  gids2Copy.reserve (gids2.size ());
684  gids2Copy.assign (gids2.begin (), gids2.end ());
685  std::sort (gids2Copy.begin (), gids2Copy.end ());
686  gids2 = gids2Copy ();
687  }
688  // Compute union. Reservation is only a reasonable guess.
689  Array<GO> gidsUnion;
690  gidsUnion.reserve (std::max (gids1.size (), gids2.size ()));
691  std::set_union (gids1.begin (), gids1.end (), gids2.begin (), gids2.end (),
692  std::back_inserter (gidsUnion));
693  // Find the index base, which must also be the Map's global min
694  // GID. This is inefficient, because we could have done that
695  // while computing the set union above.
696  typename Array<GO>::const_iterator minPos =
697  std::min_element (gidsUnion.begin (), gidsUnion.end ());
698  const GO myMinGid = (minPos == gidsUnion.end ()) ? 0 : *minPos;
699  GO globalMinGid = 0;
700  reduceAll<int, GO> (*comm, REDUCE_MIN, myMinGid, outArg (globalMinGid));
701  const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
702  RCP<const map_type> unionMap =
703  rcp (new map_type (INVALID, gidsUnion (), globalMinGid,
704  comm, srcMap->getNode ()));
705  return rcp (new import_type (srcMap, unionMap));
706  }
707 
708  template <class LocalOrdinal, class GlobalOrdinal, class Node>
712  {
713  return setUnionImpl (rhs);
714  }
715 
716 
717  template <class LocalOrdinal, class GlobalOrdinal, class Node>
721  {
722  typedef Tpetra::global_size_t GST;
723  using Teuchos::Array;
724  using Teuchos::ArrayView;
725  using Teuchos::as;
726  using Teuchos::Comm;
727  using Teuchos::RCP;
728  using Teuchos::rcp;
729  using Teuchos::outArg;
730  using Teuchos::REDUCE_MIN;
731  using Teuchos::reduceAll;
732  typedef LocalOrdinal LO;
733  typedef GlobalOrdinal GO;
734  typedef Import<LO, GO, Node> import_type;
735  typedef typename Array<GO>::size_type size_type;
736 
737 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
738  using Teuchos::toString;
739  using std::cerr;
740  using std::endl;
741 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
742 
743  RCP<const map_type> srcMap = this->getSourceMap ();
744  RCP<const map_type> tgtMap1 = this->getTargetMap ();
745  RCP<const map_type> tgtMap2 = rhs.getTargetMap ();
746  RCP<const Comm<int> > comm = srcMap->getComm ();
747 
748 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
749  const int myRank = comm->getRank ();
750 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
751 
752 #ifdef HAVE_TPETRA_DEBUG
754  ! srcMap->isSameAs (* (rhs.getSourceMap ())), std::invalid_argument,
755  "Tpetra::Import::setUnion: The source Map of the input Import must be the "
756  "same as (in the sense of Map::isSameAs) the source Map of this Import.");
758  ! Details::congruent (* (tgtMap1->getComm ()), * (tgtMap2->getComm ())),
759  std::invalid_argument, "Tpetra::Import::setUnion: "
760  "The target Maps must have congruent communicators.");
761 #endif // HAVE_TPETRA_DEBUG
762 
763  // It's probably worth the one all-reduce to check whether the two
764  // Maps are the same. If so, we can just return a copy of *this.
765  // isSameAs() bypasses the all-reduce if the pointers are equal.
766  if (tgtMap1->isSameAs (*tgtMap2)) {
767  return rcp (new import_type (*this));
768  }
769  // Alas, the two target Maps are not the same. That means we have
770  // to compute their union, and the union Import object.
771 
772  ArrayView<const GO> srcGIDs = srcMap->getNodeElementList ();
773  ArrayView<const GO> tgtGIDs1 = tgtMap1->getNodeElementList ();
774  ArrayView<const GO> tgtGIDs2 = tgtMap2->getNodeElementList ();
775 
776 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
777  comm->barrier ();
778  {
779  std::ostringstream os;
780  os << myRank << ": srcGIDs: " << toString (srcGIDs) << endl;
781  os << myRank << ": tgtGIDs1: " << toString (tgtGIDs1) << endl;
782  os << myRank << ": tgtGIDs2: " << toString (tgtGIDs2) << endl;
783  cerr << os.str ();
784  }
785  comm->barrier ();
786 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
787 
788 
789  // Fill this as we go with the union target Map's GIDs, in the
790  // desired order. We'll need them for the Map constructor.
791  Array<GO> unionTgtGIDs;
792  // Upper bound on the number of union target Map GIDs. This
793  // happens to be strict, but doesn't have to be. Setting some
794  // reasonable upper bound avoids reallocation in loops that do
795  // push_back operations.
796  unionTgtGIDs.reserve (tgtGIDs1.size () + tgtGIDs2.size ());
797 
798 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
799  if (myRank == 0) {
800  cerr << endl;
801  }
802  comm->barrier ();
803  comm->barrier ();
804  comm->barrier ();
805  cerr << myRank << ": Computing \"same\" GIDs" << endl;
806 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
807 
808  // Compute the initial sequence of "same" GIDs in the union
809  // import. The number of "same" GIDs in the union is the maximum
810  // of the lengths of this in the two inputs.
811 
812  const size_type numSameGIDs1 = this->getNumSameIDs ();
813  const size_type numSameGIDs2 = rhs.getNumSameIDs ();
814  ArrayView<const GO> sameGIDs1 = tgtGIDs1 (0, numSameGIDs1);
815  ArrayView<const GO> sameGIDs2 = tgtGIDs2 (0, numSameGIDs2);
816 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
817  {
818  std::ostringstream os;
819  os << myRank << ": same IDs for target Map 1: " << toString (sameGIDs1) << endl;
820  os << myRank << ": same IDs for target Map 2: " << toString (sameGIDs2) << endl;
821  cerr << os.str ();
822  }
823 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
824  // For the input target Map with fewer "same" GIDs, that Map's
825  // permute IDs could include some of the other input target Map's
826  // "same" GIDs. We have to make sure not to include them twice.
827  // To do so, keep a view of them for now, and remove them (via set
828  // intersection) from the permute ID list. Keeping track of which
829  // GID set had the max number of "same" IDs avoids unnecessary set
830  // intersection operations later.
831  ArrayView<const GO> doubleCountedSameGIDs;
832  size_type numSameIDsUnion;
833  bool tgtMap1HadMaxSameGIDs;
834  if (numSameGIDs1 >= numSameGIDs2) {
835  tgtMap1HadMaxSameGIDs = true;
836  numSameIDsUnion = numSameGIDs1;
837  std::copy (sameGIDs1.begin (), sameGIDs1.end (), std::back_inserter (unionTgtGIDs));
838  // There could be GIDs in target Map 2 that are not included in
839  // the "same" IDs, but are included in Import 2's permute IDs.
840  // Keep track of them so we don't double-count them when
841  // building the list of permute IDs.
842  doubleCountedSameGIDs = tgtGIDs1 (numSameGIDs2, numSameGIDs1 - numSameGIDs2);
843  } else {
844  tgtMap1HadMaxSameGIDs = false;
845  numSameIDsUnion = numSameGIDs2;
846  std::copy (sameGIDs2.begin (), sameGIDs2.end (), std::back_inserter (unionTgtGIDs));
847  // There could be GIDs in target Map 1 that are not included in
848  // the "same" IDs, but are included in Import 1's permute IDs.
849  // Keep track of them so we don't double-count them when
850  // building the list of permute IDs.
851  doubleCountedSameGIDs = tgtGIDs2 (numSameGIDs1, numSameGIDs2 - numSameGIDs1);
852  }
853 
854 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
855  {
856  std::ostringstream os;
857  os << myRank << ": union Map's same GIDs: " << toString (unionTgtGIDs ()) << endl;
858  os << myRank << ": doubleCountedSameGIDs: " << toString (doubleCountedSameGIDs) << endl;
859  cerr << os.str ();
860  }
861  if (myRank == 0) {
862  cerr << endl;
863  }
864  comm->barrier ();
865  comm->barrier ();
866  comm->barrier ();
867  cerr << myRank << ": Computing permute IDs" << endl;
868 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
869 
870  // Each input Import knows its permute-from LIDs (in the source
871  // Map) and permute-to LIDs (in its target Map). We will have to
872  // reassign LIDs in the union target Map, but we can use these
873  // permute-to LIDs to construct the union Import's permute-to IDs.
874  Array<LO> permuteFromLIDsUnion;
875  Array<LO> permuteToLIDsUnion;
876  LO curTgtLid = as<LO> (numSameIDsUnion);
877  {
878  // Permute-to LIDs in the two input target Maps.
879  ArrayView<const LO> permuteToLIDs1 = this->getPermuteToLIDs ();
880  ArrayView<const LO> permuteToLIDs2 = rhs.getPermuteToLIDs ();
881  const size_type numPermuteIDs1 = this->getNumPermuteIDs ();
882  const size_type numPermuteIDs2 = rhs.getNumPermuteIDs ();
883 
884 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
885  cerr << myRank << ": Converting permute-to LIDs to GIDs" << endl;
886 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
887 
888  // Convert the permute-to LID lists to GIDs, so that we can
889  // later reassign LIDs for the (output) union target Map.
890  Array<GO> permuteGIDs1 (numPermuteIDs1);
891  for (size_type k = 0; k < numPermuteIDs1; ++k) {
892  permuteGIDs1[k] = tgtMap1->getGlobalElement (permuteToLIDs1[k]);
893  }
894  Array<GO> permuteGIDs2 (numPermuteIDs2);
895  for (size_type k = 0; k < numPermuteIDs2; ++k) {
896  permuteGIDs2[k] = tgtMap2->getGlobalElement (permuteToLIDs2[k]);
897  }
898 
899 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
900  {
901  std::ostringstream os;
902  os << myRank << ": permuteGIDs1: " << toString (permuteGIDs1) << endl;
903  os << myRank << ": permuteGIDs2: " << toString (permuteGIDs2) << endl;
904  cerr << os.str ();
905  }
906  cerr << myRank << ": Sorting and merging permute GID lists" << endl;
907 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
908 
909  // Sort the two permute GID lists, remove the GIDs that we don't
910  // want to double-count, and merge the result into the global
911  // list of GIDs in the target union Map.
912  std::sort (permuteGIDs1.begin (), permuteGIDs1.end ());
913  std::sort (permuteGIDs2.begin (), permuteGIDs2.end ());
914 
915  typename Array<GO>::iterator permuteGIDs1_beg = permuteGIDs1.begin ();
916  typename Array<GO>::iterator permuteGIDs1_end = permuteGIDs1.end ();
917  typename Array<GO>::iterator permuteGIDs2_beg = permuteGIDs2.begin ();
918  typename Array<GO>::iterator permuteGIDs2_end = permuteGIDs2.end ();
919  if (tgtMap1HadMaxSameGIDs) {
920  // This operation allows the last (output) argument to alias the first.
921  permuteGIDs2_end =
922  std::set_intersection (permuteGIDs2_beg,
923  permuteGIDs2_end,
924  doubleCountedSameGIDs.begin (),
925  doubleCountedSameGIDs.end (),
926  permuteGIDs2_beg);
927  } else {
928  // This operation allows the last (output) argument to alias the first.
929  permuteGIDs1_end =
930  std::set_intersection (permuteGIDs1_beg,
931  permuteGIDs1_end,
932  doubleCountedSameGIDs.begin (),
933  doubleCountedSameGIDs.end (),
934  permuteGIDs1_beg);
935  }
936  std::set_union (permuteGIDs1_beg, permuteGIDs1_end,
937  permuteGIDs2_beg, permuteGIDs2_end,
938  std::back_inserter (unionTgtGIDs));
939  const size_type numPermuteIDsUnion =
940  unionTgtGIDs.size () - numSameIDsUnion;
941  ArrayView<const GO> permuteGIDsUnion =
942  unionTgtGIDs (numSameIDsUnion, numPermuteIDsUnion).getConst ();
943 
944 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
945  {
946  std::ostringstream os;
947  os << myRank << ": permuteGIDsUnion: " << toString (permuteGIDsUnion) << endl;
948  cerr << os.str ();
949  }
950 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
951 
952 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
953  comm->barrier ();
954  cerr << myRank << ": Computing permute-to LIDs" << endl;
955 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
956 
957  // Compute the permute-to LIDs (in the union target Map).
958  permuteToLIDsUnion.resize (numPermuteIDsUnion);
959  for (size_type k = 0; k < numPermuteIDsUnion; ++k) {
960  permuteToLIDsUnion[k] = curTgtLid++;
961  }
962 
963 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
964  {
965  std::ostringstream os;
966  os << myRank << ": permuteToLIDsUnion: " << toString (permuteToLIDsUnion) << endl;
967  cerr << os.str ();
968  }
969 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
970 
971 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
972  comm->barrier ();
973  cerr << myRank << ": Computing permute-from LIDs" << endl;
974 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
975 
976  // Convert the permute GIDs to permute-from LIDs in the source Map.
977  permuteFromLIDsUnion.resize (numPermuteIDsUnion);
978  for (size_type k = 0; k < numPermuteIDsUnion; ++k) {
979  permuteFromLIDsUnion[k] = srcMap->getLocalElement (permuteGIDsUnion[k]);
980  }
981 
982 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
983  {
984  std::ostringstream os;
985  os << myRank << ": permuteFromLIDsUnion: " << toString (permuteFromLIDsUnion) << endl;
986  cerr << os.str ();
987  }
988 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
989  }
990 
991  // Thus far, we have computed the following in the union Import:
992  // - getNumSameIDs()
993  // - getNumPermuteIDs()
994  // - getPermuteFromLIDs ()
995  // - getPermuteToLIDs ()
996  //
997  // Now it's time to compute the remote IDs. By definition, none
998  // of these IDs are in the source Map (on the calling process), so
999  // they can't possibly overlap with any of the "same" or permute
1000  // IDs in either target Map.
1001  //
1002  // After the first numSameIDsUnion IDs, we get to control the
1003  // order of GIDs in the union target Map. We'll put the permute
1004  // IDs first (which we already did above) and the remote IDs last
1005  // (which we are about to do). We'll sort the remote IDs by
1006  // process rank, so that Distributor doesn't have to pack buffers.
1007  // (That way, doPosts() will always take the "fast path" on all
1008  // processes.)
1009 
1010 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1011  if (myRank == 0) {
1012  cerr << endl;
1013  }
1014  comm->barrier ();
1015  comm->barrier ();
1016  comm->barrier ();
1017  cerr << myRank << ": Computing remote IDs" << endl;
1018 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1019 
1020  ArrayView<GO> remoteGIDsUnion;
1021  Array<int> remotePIDsUnion;
1022  Array<LO> remoteLIDsUnion;
1023  size_type numRemoteIDsUnion = 0;
1024  {
1025  // Distributor::createFromRecvs takes remote IDs and PIDs as
1026  // input, and computes exportIDs and exportPIDs. The easiest
1027  // way to get the remote PIDs is to imitate setupExport by using
1028  // getRemoteIndexList(). We could try to get them out of the
1029  // Distributor via getImagesFrom(), but Distributor reorders
1030  // them in some not entirely transparent way.
1031 
1032  ArrayView<const LO> remoteLIDs1 = this->getRemoteLIDs ();
1033  ArrayView<const LO> remoteLIDs2 = rhs.getRemoteLIDs ();
1034 
1035 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1036  cerr << myRank << ": Converting remote LIDs to GIDs" << endl;
1037 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1038 
1039  // We'll have to recompute LIDs for the target Map anyway, so
1040  // start by converting back to GIDs. Sort each one in
1041  // preparation for the merge below.
1042  Array<GO> remoteGIDs1 (remoteLIDs1.size ());
1043  for (size_type k = 0; k < remoteLIDs1.size (); ++k) {
1044  remoteGIDs1[k] = tgtMap1->getGlobalElement (remoteLIDs1[k]);
1045  }
1046  std::sort (remoteGIDs1.begin (), remoteGIDs1.end ());
1047 
1048 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1049  {
1050  std::ostringstream os;
1051  os << myRank << ": remoteGIDs1: " << toString (remoteGIDs1 ()) << endl;
1052  cerr << os.str ();
1053  }
1054 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1055 
1056  Array<GO> remoteGIDs2 (remoteLIDs2.size ());
1057  for (size_type k = 0; k < remoteLIDs2.size (); ++k) {
1058  remoteGIDs2[k] = tgtMap2->getGlobalElement (remoteLIDs2[k]);
1059  }
1060  std::sort (remoteGIDs2.begin (), remoteGIDs2.end ());
1061 
1062 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1063  {
1064  std::ostringstream os;
1065  os << myRank << ": remoteGIDs2: " << toString (remoteGIDs2 ()) << endl;
1066  cerr << os.str ();
1067  }
1068  cerr << myRank << ": Merging remote GID lists" << endl;
1069 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1070 
1071  // Merge the two GID lists to make the list of remote GIDs for
1072  // the union Import.
1073  const size_type oldSize = unionTgtGIDs.size ();
1074  std::set_union (remoteGIDs1.begin (), remoteGIDs1.end (),
1075  remoteGIDs2.begin (), remoteGIDs2.end (),
1076  std::back_inserter (unionTgtGIDs));
1077  const size_type newSize = unionTgtGIDs.size ();
1078  numRemoteIDsUnion = newSize - oldSize;
1079  remoteGIDsUnion = unionTgtGIDs (oldSize, numRemoteIDsUnion);
1080 
1081 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1082  {
1083  std::ostringstream os;
1084  os << myRank << ": remoteGIDsUnion: " << toString (remoteGIDsUnion ()) << endl;
1085  cerr << os.str ();
1086  }
1087  cerr << myRank << ": Looking up remote GIDs' PIDs in the source Map" << endl;
1088 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1089 
1090  // Look up the remote GIDs' PIDs in the source Map.
1091  remotePIDsUnion.resize (numRemoteIDsUnion);
1092  const LookupStatus lookup =
1093  srcMap->getRemoteIndexList (remoteGIDsUnion (), remotePIDsUnion ());
1094 
1095 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1096  {
1097  std::ostringstream os;
1098  os << myRank << ": remotePIDsUnion: " << toString (remotePIDsUnion ()) << endl;
1099  cerr << os.str ();
1100  }
1101 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1102 
1103  // Ignore remote GIDs that aren't owned by any process in the
1104  // source Map. getRemoteIndexList() gives these a PID of -1.
1105  if (lookup == IDNotPresent) {
1106 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1107  cerr << myRank << ": Filtering remote GIDs not owned by any process in the source Map" << endl;
1108 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1109 
1110  const size_type numInvalidRemote =
1111  std::count_if (remotePIDsUnion.begin (), remotePIDsUnion.end (),
1112  std::bind1st (std::equal_to<int> (), -1));
1113  if (numInvalidRemote == numRemoteIDsUnion) {
1114  // If all of them are invalid, then there are no remote IDs.
1115  unionTgtGIDs.resize (oldSize);
1116  numRemoteIDsUnion = 0;
1117  remoteGIDsUnion = ArrayView<GO> (NULL, 0);
1118  { // C++ idiom to force Array to free storage; clear() doesn't free
1119  Array<int> newRemotePIDsUnion;
1120  std::swap (remotePIDsUnion, newRemotePIDsUnion);
1121  }
1122  }
1123  else {
1124  // Some remotes are valid; we need to keep the valid ones.
1125  // Pack and resize remotePIDs and remoteGIDs.
1126  size_type numValidRemote = 0;
1127  for (size_type r = 0; r < numRemoteIDsUnion; ++r) {
1128  // Pack in all the valid remote PIDs and GIDs.
1129  if (remotePIDsUnion[r] != -1) {
1130  remotePIDsUnion[numValidRemote] = remotePIDsUnion[r];
1131  remoteGIDsUnion[numValidRemote] = remoteGIDsUnion[r];
1132  ++numValidRemote;
1133  }
1134  }
1136  numValidRemote != numRemoteIDsUnion - numInvalidRemote, std::logic_error,
1137  "Tpetra::Import::setUnion(): After removing invalid remote GIDs and"
1138  " packing the valid remote GIDs, numValidRemote = " << numValidRemote
1139  << " != numRemoteIDsUnion - numInvalidRemote = "
1140  << numRemoteIDsUnion - numInvalidRemote
1141  << ". Please report this bug to the Tpetra developers.");
1142 
1143  unionTgtGIDs.resize (oldSize + numValidRemote);
1144  numRemoteIDsUnion = numValidRemote;
1145  remoteGIDsUnion = unionTgtGIDs (oldSize, numValidRemote);
1146  remotePIDsUnion.resize (numValidRemote);
1147  }
1148 
1149 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1150  {
1151  std::ostringstream os;
1152  os << myRank << ": remoteGIDsUnion after filtering: " << toString (remoteGIDsUnion ()) << endl;
1153  os << myRank << ": remotePIDsUnion after filtering: " << toString (remotePIDsUnion ()) << endl;
1154  cerr << os.str ();
1155  }
1156 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1157  }
1158 
1159 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1160  cerr << myRank << ": Assigning LIDs (in union target Map) to remote IDs" << endl;
1161 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1162 
1163  // Now remoteGIDsUnion, remotePIDsUnion, and numRemoteIDsUnion
1164  // have been set. We just need to assign LIDs to those PIDs,
1165  // and then sort all three arrays together by PID.
1166  remoteLIDsUnion.resize (numRemoteIDsUnion);
1167  for (size_type k = 0; k < numRemoteIDsUnion; ++k) {
1168  remoteLIDsUnion[k] = curTgtLid++;
1169  }
1170 
1171 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1172  {
1173  std::ostringstream os;
1174  os << myRank << ": remoteLIDsUnion: " << toString (remoteLIDsUnion ()) << endl;
1175  cerr << os.str ();
1176  }
1177 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1178 
1179 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1180  cerr << myRank << ": Sorting remote PIDs, GIDs, and LIDs jointly by PID" << endl;
1181 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1182 
1183  sort3 (remotePIDsUnion.begin (), remotePIDsUnion.end (),
1184  remoteGIDsUnion.begin (), remoteLIDsUnion.begin ());
1185 
1186 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1187  {
1188  std::ostringstream os;
1189  os << myRank << ": remoteGIDsUnion sorted: " << toString (remoteGIDsUnion ()) << endl;
1190  os << myRank << ": remotePIDsUnion sorted: " << toString (remotePIDsUnion ()) << endl;
1191  os << myRank << ": remoteLIDsUnion sorted: " << toString (remoteLIDsUnion ()) << endl;
1192  cerr << os.str ();
1193  }
1194 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1195  }
1196 
1197 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1198  {
1199  std::ostringstream os;
1200  os << myRank << ": unionTgtGIDs after remotes: "
1201  << toString (unionTgtGIDs ()) << endl;
1202  cerr << os.str ();
1203  }
1204 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1205 
1206  // Find the union target Map's index base, which must also be the
1207  // union target Map's global min GID. Thus, by definition, it
1208  // must be the minimum of the two input target Maps' index bases.
1209  // We already know these, so we don't have to do another
1210  // all-reduce to find it.
1211  const GO indexBaseUnion =
1212  std::min (tgtMap1->getIndexBase (), tgtMap2->getIndexBase ());
1213 
1214 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1215  cerr << myRank << "Creating union target Map" << endl;
1216 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1217 
1218  // Create the union target Map.
1219  //
1220  // FIXME (mfh 01 May 2013) It would be handy to have a Map
1221  // constructor that takes the global min and max GIDs; that would
1222  // obviate the need for Map to compute them (with an all-reduce).
1223  const GST INVALID = Teuchos::OrdinalTraits<GST>::invalid ();
1224  RCP<const map_type> unionTgtMap =
1225  rcp (new map_type (INVALID, unionTgtGIDs (), indexBaseUnion,
1226  comm, srcMap->getNode ()));
1227 
1228 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1229  if (myRank == 0) {
1230  cerr << endl;
1231  }
1232  comm->barrier ();
1233  comm->barrier ();
1234  comm->barrier ();
1235  cerr << myRank << ": Computing export IDs and Distributor" << endl;
1236 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1237 
1238  // Thus far, we have computed the following in the union Import:
1239  // - numSameIDs
1240  // - numPermuteIDs and permuteFromLIDs
1241  // - numRemoteIDs, remoteGIDs, remoteLIDs, and remotePIDs
1242  //
1243  // Now it's time to compute the export IDs and initialize the
1244  // Distributor.
1245 
1246  Array<GO> exportGIDsUnion;
1247  Array<LO> exportLIDsUnion;
1248  Array<int> exportPIDsUnion;
1249  Distributor distributor (comm, this->out_);
1250 
1251 #ifdef TPETRA_IMPORT_SETUNION_USE_CREATE_FROM_SENDS
1252  // Compute the export IDs without communication, by merging the
1253  // lists of (export LID, export PID) pairs from the two input
1254  // Import objects. The export LIDs in both input Import objects
1255  // are LIDs in the source Map. Then, use the export PIDs to
1256  // initialize the Distributor via createFromSends.
1257 
1258  // const size_type numExportIDs1 = this->getNumExportIDs ();
1259  ArrayView<const LO> exportLIDs1 = this->getExportLIDs ();
1260  ArrayView<const LO> exportPIDs1 = this->getExportPIDs ();
1261 
1262  // const size_type numExportIDs2 = rhs.getNumExportIDs ();
1263  ArrayView<const LO> exportLIDs2 = rhs.getExportLIDs ();
1264  ArrayView<const LO> exportPIDs2 = rhs.getExportPIDs ();
1265 
1266  // We have to keep the export LIDs in PID-sorted order, then merge
1267  // them. So, first key-value merge (LID,PID) pairs, treating PIDs
1268  // as values, merging values by replacement. Then, sort the
1269  // (LID,PID) pairs again by PID.
1270 
1271  // Sort (LID,PID) pairs by LID for the later merge, and make
1272  // each sequence unique by LID.
1273  Array<LO> exportLIDs1Copy (exportLIDs1.begin (), exportLIDs1.end ());
1274  Array<int> exportPIDs1Copy (exportLIDs1.begin (), exportLIDs1.end ());
1275  sort2 (exportLIDs1Copy.begin (), exportLIDs1Copy.end (),
1276  exportPIDs1Copy.begin ());
1277  typename ArrayView<LO>::iterator exportLIDs1_end = exportLIDs1Copy.end ();
1278  typename ArrayView<LO>::iterator exportPIDs1_end = exportPIDs1Copy.end ();
1279  merge2 (exportLIDs1_end, exportPIDs1_end,
1280  exportLIDs1Copy.begin (), exportLIDs1_end,
1281  exportPIDs1Copy.begin (), exportPIDs1_end,
1282  project1st<LO, LO> ());
1283 
1284  Array<LO> exportLIDs2Copy (exportLIDs2.begin (), exportLIDs2.end ());
1285  Array<int> exportPIDs2Copy (exportLIDs2.begin (), exportLIDs2.end ());
1286  sort2 (exportLIDs2Copy.begin (), exportLIDs2Copy.end (),
1287  exportPIDs2Copy.begin ());
1288  typename ArrayView<LO>::iterator exportLIDs2_end = exportLIDs2Copy.end ();
1289  typename ArrayView<LO>::iterator exportPIDs2_end = exportPIDs2Copy.end ();
1290  merge2 (exportLIDs2_end, exportPIDs2_end,
1291  exportLIDs2Copy.begin (), exportLIDs2_end,
1292  exportPIDs2Copy.begin (), exportPIDs2_end,
1293  project1st<LO, LO> ());
1294 
1295  // Merge export (LID,PID) pairs. In this merge operation, the
1296  // LIDs are the "keys" and the PIDs their "values." We combine
1297  // the "values" (PIDs) in the pairs by replacement, rather than
1298  // by adding them together.
1299  keyValueMerge (exportLIDs1Copy.begin (), exportLIDs1Copy.end (),
1300  exportPIDs1Copy.begin (), exportPIDs1Copy.end (),
1301  exportLIDs2Copy.begin (), exportLIDs2Copy.end (),
1302  exportPIDs2Copy.begin (), exportPIDs2Copy.end (),
1303  std::back_inserter (exportLIDsUnion),
1304  std::back_inserter (exportPIDsUnion),
1306 
1307  // Resort the merged (LID,PID) pairs by PID.
1308  sort2 (exportPIDsUnion.begin (), exportPIDsUnion.end (),
1309  exportLIDsUnion.begin ());
1310 
1311  // Initialize the Distributor. Using createFromSends instead of
1312  // createFromRecvs avoids the initialization and use of a
1313  // temporary Distributor object.
1314  (void) distributor.createFromSends (exportPIDsUnion ().getConst ());
1315 #else // NOT TPETRA_IMPORT_SETUNION_USE_CREATE_FROM_SENDS
1316 
1317  // Call the Distributor's createFromRecvs() method to turn the
1318  // remote GIDs and their owning processes into a send-and-receive
1319  // communication plan. remoteGIDsUnion and remotePIDsUnion are
1320  // input; exportGIDsUnion and exportPIDsUnion are output arrays
1321  // which are allocated by createFromRecvs().
1322  distributor.createFromRecvs (remoteGIDsUnion.getConst (),
1323  remotePIDsUnion ().getConst (),
1324  exportGIDsUnion, exportPIDsUnion);
1325 
1326  // Find the (source Map) LIDs corresponding to the export GIDs.
1327  const size_type numExportIDsUnion = exportGIDsUnion.size ();
1328  exportLIDsUnion.resize (numExportIDsUnion);
1329  for (size_type k = 0; k < numExportIDsUnion; ++k) {
1330  exportLIDsUnion[k] = srcMap->getLocalElement (exportGIDsUnion[k]);
1331  }
1332 #endif // TPETRA_IMPORT_SETUNION_USE_CREATE_FROM_SENDS
1333 
1334 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1335  {
1336  std::ostringstream os;
1337  os << myRank << ": exportGIDsUnion: " << toString (exportGIDsUnion ()) << endl;
1338  os << myRank << ": exportPIDsUnion: " << toString (exportPIDsUnion ()) << endl;
1339  os << myRank << ": exportLIDsUnion: " << toString (exportLIDsUnion ()) << endl;
1340  cerr << os.str ();
1341  }
1342  comm->barrier ();
1343  cerr << "Creating union Import" << endl;
1344 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1345 
1346  // Create and return the union Import.
1347  RCP<const import_type> unionImport =
1348  rcp (new import_type (srcMap, unionTgtMap,
1349  as<size_t> (numSameIDsUnion),
1350  permuteToLIDsUnion, permuteFromLIDsUnion,
1351  remoteLIDsUnion, exportLIDsUnion,
1352  exportPIDsUnion, distributor, this->out_));
1353 #ifdef HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1354  comm->barrier ();
1355  cerr << "Created union Import; done!" << endl;
1356 #endif // HAVE_TPETRA_IMPORT_SETUNION_EXTRA_DEBUG_OUTPUT
1357 
1358  return unionImport;
1359  }
1360 } // namespace Tpetra
1361 
1362 // Explicit instantiation macro.
1363 // Only invoke this when in the Tpetra namespace.
1364 // Most users do not need to use this.
1365 //
1366 // LO: The local ordinal type.
1367 // GO: The global ordinal type.
1368 // NODE: The Kokkos Node type.
1369 #define TPETRA_IMPORT_INSTANT(LO, GO, NODE) \
1370  \
1371  template class Import< LO , GO , NODE >;
1372 
1373 #endif // TPETRA_IMPORT_DEF_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
Namespace Tpetra contains the class and methods constituting the Tpetra library.
void reserve(size_type n)
ArrayView< const int > getExportPIDs() const
List of processes to which entries will be sent.
ArrayView< const LocalOrdinal > getRemoteLIDs() const
List of entries in the target Map to receive from other processes.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getTargetMap() const
The Target Map used to construct this Import object.
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
void sort3(const IT1 &first1, const IT1 &last1, const IT2 &first2, const IT3 &first3)
Sort the first array, and apply the same permutation to the second and third arrays.
iterator begin() const
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
size_type size() const
GlobalOrdinal getIndexBase() const
The index base for this Map.
size_t getNumExportIDs() const
Number of entries that must be sent by the calling process to other processes.
bool isDistributed() const
Whether this Map is globally distributed or locally replicated.
void pushTab(const int tabs=1)
~Import()
Destructor.
size_t global_size_t
Global size_t object.
void merge2(IT1 &indResultOut, IT2 &valResultOut, IT1 indBeg, IT1 indEnd, IT2 valBeg, IT2 valEnd)
Merge values in place, additively, with the same index.
Import(const Teuchos::RCP< const map_type > &source, const Teuchos::RCP< const map_type > &target)
Construct an Import from the source and target Maps.
Ordinal size_type
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void keyValueMerge(KeyInputIterType keyBeg1, KeyInputIterType keyEnd1, ValueInputIterType valBeg1, ValueInputIterType valEnd1, KeyInputIterType keyBeg2, KeyInputIterType keyEnd2, ValueInputIterType valBeg2, ValueInputIterType valEnd2, KeyOutputIterType keyOut, ValueOutputIterType valOut, BinaryFunction f)
Merge two sorted (by keys) sequences of unique (key,value) pairs by combining pairs with equal keys...
ArrayView< const LocalOrdinal > getPermuteFromLIDs() const
List of local IDs in the source Map that are permuted.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
#define TPETRA_ABUSE_WARNING(throw_exception_test, Exception, msg)
Handle an abuse warning, according to HAVE_TPETRA_THROW_ABUSE_WARNINGS and HAVE_TPETRA_PRINT_ABUSE_WA...
Import< LocalOrdinal, GlobalOrdinal, Node > & operator=(const Import< LocalOrdinal, GlobalOrdinal, Node > &Source)
Assignment operator.
Implementation detail of Import and Export.
Sets up and executes a communication plan for a Tpetra DistObject.
GlobalOrdinal getGlobalElement(LocalOrdinal localIndex) const
The global index corresponding to the given local index.
TEUCHOSCORE_LIB_DLL_EXPORT std::string toString(const EVerbosityLevel verbLevel)
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)
LookupStatus getRemoteIndexList(const Teuchos::ArrayView< const GlobalOrdinal > &GIDList, const Teuchos::ArrayView< int > &nodeIDList, const Teuchos::ArrayView< LocalOrdinal > &LIDList) const
Return the process ranks and corresponding local indices for the given global indices.
ArrayView< const LocalOrdinal > getPermuteToLIDs() const
List of local IDs in the target Map that are permuted.
void resize(size_type new_size, const value_type &x=value_type())
T * getRawPtr() const
Teuchos::ArrayView< const GlobalOrdinal > getNodeElementList() const
Return a view of the global indices owned by this process.
iterator end()
std::vector< T >::const_iterator const_iterator
ArrayView< const LocalOrdinal > getExportLIDs() const
List of entries in the source Map that will be sent to other processes.
LocalOrdinal getLocalElement(GlobalOrdinal globalIndex) const
The local index corresponding to the given global index.
void push_back(const value_type &x)
void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2)
Sort the first array, and apply the resulting permutation to the second array.
Describes a parallel distribution of objects over processes.
bool isSameAs(const Map< LocalOrdinal, GlobalOrdinal, Node > &map) const
True if and only if map is identical to this Map.
iterator end() const
size_type size() const
TypeTo as(const TypeFrom &t)
Stand-alone utility functions and macros.
size_t getNumPermuteIDs() const
Number of IDs to permute but not to communicate.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getSourceMap() const
The Source Map used to construct this Import object.
size_t getNumSameIDs() const
Number of initial identical IDs.
Distributor & getDistributor() const
The Distributor that this Import object uses to move data.
virtual void print(std::ostream &os) const
Print the Import&#39;s data to the given output stream.
void assign(size_type n, const value_type &val)
size_t getNumRemoteIDs() const
Number of entries not on the calling process.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream.
const T & getConst(T &t)
std::vector< T >::iterator iterator
iterator begin()
const Teuchos::RCP< Node > & getNode() const
Get this Map&#39;s Node object.
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.
ArrayView< const T > getConst() const
Binary function that returns its first argument.
std::string toString(const T &t)
Teuchos::RCP< const Import< LocalOrdinal, GlobalOrdinal, Node > > setUnion(const Import< LocalOrdinal, GlobalOrdinal, Node > &rhs) const
Return the union of this Import and rhs.
bool is_null() const