42 #ifndef TPETRA_EXPORT_DEF_HPP
43 #define TPETRA_EXPORT_DEF_HPP
45 #ifdef DOXYGEN_USE_ONLY
46 # include <Tpetra_Export_decl.hpp>
47 #endif // DOXYGEN_USE_ONLY
49 #include <Tpetra_Distributor.hpp>
50 #include <Tpetra_Map.hpp>
51 #include <Tpetra_ImportExportData.hpp>
53 #include <Tpetra_Import.hpp>
54 #include <Teuchos_as.hpp>
58 const bool tpetraExportDebugDefault =
false;
62 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 bool debug = tpetraExportDebugDefault;
68 if (! plist.is_null ()) {
70 debug = plist->get<
bool> (
"Debug");
71 }
catch (Teuchos::Exceptions::InvalidParameter&) {}
74 ExportData_->distributor_.setParameterList (plist);
77 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
81 out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr))),
82 debug_ (tpetraExportDebugDefault)
88 if (! out_.is_null ()) {
92 std::ostringstream os;
93 const int myRank = source->getComm ()->getRank ();
94 os << myRank <<
": Export ctor" << endl;
97 ExportData_ = rcp (
new data_type (source, target, out_));
98 Teuchos::Array<GlobalOrdinal> exportGIDs;
99 setupSamePermuteExport (exportGIDs);
101 std::ostringstream os;
102 const int myRank = source->getComm ()->getRank ();
103 os << myRank <<
": Export ctor: "
104 <<
"setupSamePermuteExport done" << endl;
107 if (source->isDistributed ()) {
108 setupRemote (exportGIDs);
111 std::ostringstream os;
112 const int myRank = source->getComm ()->getRank ();
113 os << myRank <<
": Export ctor: done" << endl;
116 if (! out_.is_null ()) {
121 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
125 const RCP<Teuchos::FancyOStream>& out) :
127 debug_ (tpetraExportDebugDefault)
133 if (! out_.is_null ()) {
137 std::ostringstream os;
138 const int myRank = source->getComm ()->getRank ();
139 os << myRank <<
": Export ctor" << endl;
142 ExportData_ = rcp (
new data_type (source, target, out));
143 Teuchos::Array<GlobalOrdinal> exportGIDs;
144 setupSamePermuteExport (exportGIDs);
146 std::ostringstream os;
147 const int myRank = source->getComm ()->getRank ();
148 os << myRank <<
": Export ctor: "
149 <<
"setupSamePermuteExport done" << endl;
152 if (source->isDistributed ()) {
153 setupRemote (exportGIDs);
156 std::ostringstream os;
157 const int myRank = source->getComm ()->getRank ();
158 os << myRank <<
": Export ctor: done" << endl;
161 if (! out_.is_null ()) {
166 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
170 const Teuchos::RCP<Teuchos::ParameterList>& plist) :
171 out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr))),
172 debug_ (tpetraExportDebugDefault)
179 bool debug = tpetraExportDebugDefault;
180 if (! plist.is_null ()) {
182 debug = plist->get<
bool> (
"Debug");
183 }
catch (Teuchos::Exceptions::InvalidParameter&) {}
187 if (! out_.is_null ()) {
191 std::ostringstream os;
192 const int myRank = source->getComm ()->getRank ();
193 os << myRank <<
": Export ctor" << endl;
196 ExportData_ = rcp (
new data_type (source, target, out_, plist));
197 Teuchos::Array<GlobalOrdinal> exportGIDs;
198 setupSamePermuteExport (exportGIDs);
200 std::ostringstream os;
201 const int myRank = source->getComm ()->getRank ();
202 os << myRank <<
": Export ctor: "
203 <<
"setupSamePermuteExport done" << endl;
206 if (source->isDistributed ()) {
207 setupRemote (exportGIDs);
210 std::ostringstream os;
211 const int myRank = source->getComm ()->getRank ();
212 os << myRank <<
": Export ctor: done" << endl;
215 if (! out_.is_null ()) {
220 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
224 const RCP<Teuchos::FancyOStream>& out,
225 const Teuchos::RCP<Teuchos::ParameterList>& plist) :
226 out_ (Teuchos::getFancyOStream (Teuchos::rcpFromRef (std::cerr))),
227 debug_ (tpetraExportDebugDefault)
234 bool debug = tpetraExportDebugDefault;
235 if (! plist.is_null ()) {
237 debug = plist->get<
bool> (
"Debug");
238 }
catch (Teuchos::Exceptions::InvalidParameter&) {}
242 if (! out_.is_null ()) {
246 std::ostringstream os;
247 const int myRank = source->getComm ()->getRank ();
248 os << myRank <<
": Export ctor" << endl;
251 ExportData_ = rcp (
new data_type (source, target, out, plist));
252 Teuchos::Array<GlobalOrdinal> exportGIDs;
253 setupSamePermuteExport (exportGIDs);
255 std::ostringstream os;
256 const int myRank = source->getComm ()->getRank ();
257 os << myRank <<
": Export ctor: "
258 <<
"setupSamePermuteExport done" << endl;
261 if (source->isDistributed ()) {
262 setupRemote (exportGIDs);
265 std::ostringstream os;
266 const int myRank = source->getComm ()->getRank ();
267 os << myRank <<
": Export ctor: done" << endl;
270 if (! out_.is_null ()) {
275 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
278 : ExportData_ (rhs.ExportData_),
284 if (! out_.is_null ()) {
288 std::ostringstream os;
289 const int myRank =
getSourceMap ()->getComm ()->getRank ();
290 os << myRank <<
": Export copy ctor (done)" << endl;
293 if (! out_.is_null ()) {
298 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
301 : out_ (importer.out_)
302 , debug_ (importer.debug_)
304 if(!importer.ImportData_.is_null()) ExportData_ = importer.ImportData_->reverseClone();
307 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
311 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
313 return ExportData_->numSameIDs_;
316 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
318 return ExportData_->permuteFromLIDs_.size();
321 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
322 ArrayView<const LocalOrdinal>
324 return ExportData_->permuteFromLIDs_();
327 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
328 ArrayView<const LocalOrdinal>
330 return ExportData_->permuteToLIDs_();
333 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
335 return ExportData_->remoteLIDs_.size();
338 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
339 ArrayView<const LocalOrdinal>
341 return ExportData_->remoteLIDs_();
344 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
346 return ExportData_->exportLIDs_.size();
349 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
350 ArrayView<const LocalOrdinal>
352 return ExportData_->exportLIDs_();
355 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
358 return ExportData_->exportPIDs_();
361 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
362 const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
364 return ExportData_->source_;
367 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
368 const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &
370 return ExportData_->target_;
373 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
376 return ExportData_->distributor_;
379 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
384 ExportData_ = rhs.ExportData_;
389 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
394 using Teuchos::getFancyOStream;
396 using Teuchos::rcpFromRef;
397 using Teuchos::toString;
400 RCP<const Comm<int> > comm = getSourceMap ()->getComm ();
401 const int myImageID = comm->getRank ();
402 const int numImages = comm->getSize ();
403 for (
int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
404 if (myImageID == imageCtr) {
406 if (myImageID == 0) {
407 os <<
"Export Data Members:" << endl;
409 os <<
"Image ID : " << myImageID << endl;
411 os <<
"permuteFromLIDs: " << toString (getPermuteFromLIDs ()) << endl;
412 os <<
"permuteToLIDs : " << toString (getPermuteToLIDs ()) << endl;
413 os <<
"remoteLIDs : " << toString (getRemoteLIDs ()) << endl;
414 os <<
"exportLIDs : " << toString (getExportLIDs ()) << endl;
415 os <<
"exportPIDs : " << toString (getExportPIDs ()) << endl;
417 os <<
"numSameIDs : " << getNumSameIDs () << endl;
418 os <<
"numPermuteIDs : " << getNumPermuteIDs () << endl;
419 os <<
"numRemoteIDs : " << getNumRemoteIDs () << endl;
420 os <<
"numExportIDs : " << getNumExportIDs () << endl;
427 if (myImageID == 0) {
428 os << endl << endl <<
"Source Map:" << endl << std::flush;
431 os << *getSourceMap();
434 if (myImageID == 0) {
435 os << endl << endl <<
"Target Map:" << endl << std::flush;
438 os << *getTargetMap();
444 if (myImageID == 0) {
445 os << endl << endl <<
"Distributor:" << endl << std::flush;
448 getDistributor().describe (*(getFancyOStream (rcpFromRef (os))),
449 Teuchos::VERB_EXTREME);
453 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
459 using Teuchos::Array;
460 using Teuchos::ArrayRCP;
461 using Teuchos::ArrayView;
464 typedef LocalOrdinal LO;
465 typedef GlobalOrdinal GO;
466 typedef typename ArrayView<const GO>::size_type size_type;
472 #ifdef HAVE_TPETRA_DEBUG
473 ArrayView<const GO> rawSrcGids = sourceGIDs;
474 ArrayView<const GO> rawTgtGids = targetGIDs;
476 const GO*
const rawSrcGids = sourceGIDs.getRawPtr ();
477 const GO*
const rawTgtGids = targetGIDs.getRawPtr ();
478 #endif // HAVE_TPETRA_DEBUG
479 const size_type numSrcGids = sourceGIDs.size ();
480 const size_type numTgtGids = targetGIDs.size ();
481 const size_type numGids = std::min (numSrcGids, numTgtGids);
489 size_type numSameGids = 0;
490 for ( ; numSameGids < numGids && rawSrcGids[numSameGids] == rawTgtGids[numSameGids]; ++numSameGids)
492 ExportData_->numSameIDs_ = numSameGids;
504 exportGIDs.resize (0);
505 Array<LO>& permuteToLIDs = ExportData_->permuteToLIDs_;
506 Array<LO>& permuteFromLIDs = ExportData_->permuteFromLIDs_;
507 Array<LO>& exportLIDs = ExportData_->exportLIDs_;
508 const LO LINVALID = Teuchos::OrdinalTraits<LO>::invalid ();
509 const LO numSrcLids = as<LO> (numSrcGids);
512 for (LO srcLid = numSameGids; srcLid < numSrcLids; ++srcLid) {
513 const GO curSrcGid = rawSrcGids[srcLid];
517 if (tgtLid != LINVALID) {
518 permuteToLIDs.push_back (tgtLid);
519 permuteFromLIDs.push_back (srcLid);
521 exportGIDs.push_back (curSrcGid);
522 exportLIDs.push_back (srcLid);
536 "::setupSamePermuteExport(): Source has export LIDs but Source is not "
537 "distributed globally." << std::endl
538 <<
"Exporting to a submap of the target map.");
552 ExportData_->exportPIDs_.resize(exportGIDs.size ());
558 ExportData_->exportPIDs_ ());
560 "::setupSamePermuteExport(): The source Map has GIDs not found "
561 "in the target Map.");
566 typedef typename ArrayRCP<int>::difference_type size_type;
568 const size_type numInvalidExports =
569 std::count_if (ExportData_->exportPIDs_().begin(),
570 ExportData_->exportPIDs_().end(),
571 std::bind1st (std::equal_to<int>(), -1));
574 const size_type totalNumExports = ExportData_->exportPIDs_.size();
575 if (numInvalidExports == totalNumExports) {
577 exportGIDs.resize(0);
578 ExportData_->exportLIDs_.resize(0);
579 ExportData_->exportPIDs_.resize(0);
584 size_type numValidExports = 0;
585 for (size_type e = 0; e < totalNumExports; ++e) {
586 if (ExportData_->exportPIDs_[e] != -1) {
587 exportGIDs[numValidExports] = exportGIDs[e];
588 ExportData_->exportLIDs_[numValidExports] = ExportData_->exportLIDs_[e];
589 ExportData_->exportPIDs_[numValidExports] = ExportData_->exportPIDs_[e];
593 exportGIDs.resize (numValidExports);
594 ExportData_->exportLIDs_.resize (numValidExports);
595 ExportData_->exportPIDs_.resize (numValidExports);
601 template <
class LocalOrdinal,
class GlobalOrdinal,
class Node>
603 Export<LocalOrdinal,GlobalOrdinal,Node>::setupRemote(Teuchos::Array<GlobalOrdinal> & exportGIDs)
606 const Map<LocalOrdinal,GlobalOrdinal,Node>& target = * (getTargetMap ());
607 const int myRank = target.
getComm ()->getRank ();
609 if (! out_.is_null ()) {
613 std::ostringstream os;
614 os << myRank <<
": Export::setupRemote" << endl;
617 if (! out_.is_null ()) {
625 sort3 (ExportData_->exportPIDs_.begin(),
626 ExportData_->exportPIDs_.end(),
628 ExportData_->exportLIDs_.begin());
631 std::ostringstream os;
632 os << myRank <<
": Export::setupRemote: Calling createFromSends" << endl;
643 numRemoteIDs = ExportData_->distributor_.createFromSends (ExportData_->exportPIDs_ ());
646 std::ostringstream os;
647 os << myRank <<
": Export::setupRemote: Calling doPostsAndWaits" << endl;
654 Array<GlobalOrdinal> remoteGIDs (numRemoteIDs);
655 ExportData_->distributor_.doPostsAndWaits (exportGIDs().getConst (), 1, remoteGIDs());
659 ExportData_->remoteLIDs_.resize (numRemoteIDs);
661 typename Array<GlobalOrdinal>::const_iterator i = remoteGIDs.begin();
662 typename Array<LocalOrdinal>::iterator j = ExportData_->remoteLIDs_.begin();
663 while (i != remoteGIDs.end()) {
664 *j++ = target.getLocalElement(*i++);
668 if (! out_.is_null ()) {
672 std::ostringstream os;
673 os << myRank <<
": Export::setupRemote: done" << endl;
676 if (! out_.is_null ()) {
689 #define TPETRA_EXPORT_INSTANT(LO, GO, NODE) \
691 template class Export< LO , GO , NODE >;
693 #endif // TPETRA_EXPORT_DEF_HPP
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
ArrayView< const LocalOrdinal > getExportLIDs() const
List of entries in the source Map that will be sent to other processes.
ArrayView< const int > getExportPIDs() const
List of processes to which entries will be sent.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getSourceMap() const
The source Map used to construct this Export.
virtual void print(std::ostream &os) const
Print the Export's data to the given output stream.
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
size_t getNumSameIDs() const
Number of initial identical IDs.
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.
size_t getNumPermuteIDs() const
Number of IDs to permute but not to communicate.
bool isDistributed() const
Whether this Map is globally distributed or locally replicated.
ArrayView< const LocalOrdinal > getPermuteFromLIDs() const
List of local IDs in the source Map that are permuted.
ArrayView< const LocalOrdinal > getPermuteToLIDs() const
List of local IDs in the target Map that are permuted.
Export(const Teuchos::RCP< const map_type > &source, const Teuchos::RCP< const map_type > &target)
Construct a Export object from the source and target Map.
Export< LocalOrdinal, GlobalOrdinal, Node > & operator=(const Export< LocalOrdinal, GlobalOrdinal, Node > &rhs)
Assignment operator.
Communication plan for data redistribution from a (possibly) multiply-owned to a uniquely-owned distr...
size_t getNumExportIDs() const
Number of entries that must be sent by the calling process to other processes.
#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...
Implementation detail of Import and Export.
Sets up and executes a communication plan for a Tpetra DistObject.
size_t getNumRemoteIDs() const
Number of entries not on the calling process.
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.
Teuchos::ArrayView< const GlobalOrdinal > getNodeElementList() const
Return a view of the global indices owned by this process.
LocalOrdinal getLocalElement(GlobalOrdinal globalIndex) const
The local index corresponding to the given global index.
Describes a parallel distribution of objects over processes.
ArrayView< const LocalOrdinal > getRemoteLIDs() const
List of entries in the target Map to receive from other processes.
Stand-alone utility functions and macros.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getTargetMap() const
The target Map used to construct this Export.
Distributor & getDistributor() const
The Distributor that this Export object uses to move data.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
Set parameters.