10#ifndef TPETRA_DETAILS_TRANSFER_DEF_HPP
11#define TPETRA_DETAILS_TRANSFER_DEF_HPP
14#include "Tpetra_Distributor.hpp"
15#include "Tpetra_ImportExportData.hpp"
16#include "Tpetra_Map.hpp"
17#include "Teuchos_CommHelpers.hpp"
18#include "Teuchos_TypeNameTraits.hpp"
24template <
class ElementType,
class DeviceType>
25Teuchos::ArrayView<const ElementType>
26makeConstArrayViewFromDualView(
const Kokkos::DualView<ElementType*, DeviceType>& dv) {
27 TEUCHOS_ASSERT(!dv.need_sync_host());
28 auto hostView = dv.view_host();
29 const auto size = hostView.extent(0);
30 return Teuchos::ArrayView<const ElementType>(size == 0 ?
nullptr : hostView.data(), size);
33template <
class DeviceType,
class LocalOrdinal>
34struct OrderedViewFunctor {
35 OrderedViewFunctor(
const Kokkos::View<LocalOrdinal*, DeviceType>& viewToCheck)
36 : viewToCheck_(viewToCheck) {}
37 KOKKOS_INLINE_FUNCTION
void operator()(
const size_t i,
unsigned int& isUnordered)
const {
38 isUnordered |=
static_cast<unsigned int>(viewToCheck_(i) + 1 != viewToCheck_(i + 1));
40 Kokkos::View<const LocalOrdinal*, DeviceType> viewToCheck_;
43template <
class DeviceType,
class LocalOrdinal>
44bool isViewOrdered(
const Kokkos::View<LocalOrdinal*, DeviceType>& viewToCheck) {
45 using Kokkos::parallel_reduce;
46 typedef DeviceType DT;
47 typedef typename DT::execution_space DES;
48 typedef Kokkos::RangePolicy<DES, size_t> range_type;
50 const size_t size = viewToCheck.extent(0);
51 unsigned int isUnordered = 0;
53 parallel_reduce(
"isViewOrdered",
54 range_type(0, size - 1),
55 OrderedViewFunctor<DeviceType, LocalOrdinal>(viewToCheck),
57 return isUnordered == 0;
65template <
class LO,
class GO,
class NT>
68 const Teuchos::RCP<const map_type>&
target,
69 const Teuchos::RCP<Teuchos::FancyOStream>&
out,
70 const Teuchos::RCP<Teuchos::ParameterList>&
plist,
77template <
class LO,
class GO,
class NT>
81 this->TransferData_ =
rhs.TransferData_->reverseClone();
85template <
class LO,
class GO,
class NT>
89 using ::Tpetra::Details::Behavior;
95 if (!
plist.is_null()) {
98 if (
plist->isType<
bool>(
"Verbose")) {
100 }
else if (
plist->isType<
bool>(
"Debug")) {
107template <
class LO,
class GO,
class NT>
111 return TransferData_->numSameIDs_;
114template <
class LO,
class GO,
class NT>
118 return static_cast<size_t>(TransferData_->permuteFromLIDs_.extent(0));
121template <
class LO,
class GO,
class NT>
122Kokkos::DualView<const LO*, typename Transfer<LO, GO, NT>::device_type>
125 const auto&
dv = TransferData_->permuteFromLIDs_;
127 "Tpetra::Details::Transfer::getPermuteFromLIDs_dv: "
128 "DualView needs sync to device");
130 "Tpetra::Details::Transfer::getPermuteFromLIDs_dv: "
131 "DualView needs sync to host");
135template <
class LO,
class GO,
class NT>
136Teuchos::ArrayView<const LO>
142template <
class LO,
class GO,
class NT>
143Kokkos::DualView<const LO*, typename Transfer<LO, GO, NT>::device_type>
146 const auto&
dv = TransferData_->permuteToLIDs_;
148 "Tpetra::Details::Transfer::getPermuteToLIDs_dv: "
149 "DualView needs sync to device");
151 "Tpetra::Details::Transfer::getPermuteToLIDs_dv: "
152 "DualView needs sync to host");
156template <
class LO,
class GO,
class NT>
157Teuchos::ArrayView<const LO>
163template <
class LO,
class GO,
class NT>
167 return static_cast<size_t>(TransferData_->remoteLIDs_.extent(0));
170template <
class LO,
class GO,
class NT>
171Kokkos::DualView<const LO*, typename Transfer<LO, GO, NT>::device_type>
174 const auto&
dv = TransferData_->remoteLIDs_;
176 "Tpetra::Details::Transfer::getRemoteLIDs_dv: "
177 "DualView needs sync to device");
179 "Tpetra::Details::Transfer::getRemoteLIDs_dv: "
180 "DualView needs sync to host");
184template <
class LO,
class GO,
class NT>
185Teuchos::ArrayView<const LO>
191template <
class LO,
class GO,
class NT>
195 return static_cast<size_t>(TransferData_->exportLIDs_.extent(0));
198template <
class LO,
class GO,
class NT>
199Kokkos::DualView<const LO*, typename Transfer<LO, GO, NT>::device_type>
202 const auto&
dv = TransferData_->exportLIDs_;
204 "Tpetra::Details::Transfer::getExportLIDs_dv: "
205 "DualView needs sync to device");
207 "Tpetra::Details::Transfer::getExportLIDs_dv: "
208 "DualView needs sync to host");
212template <
class LO,
class GO,
class NT>
213Teuchos::ArrayView<const LO>
219template <
class LO,
class GO,
class NT>
220Teuchos::ArrayView<const int>
223 return TransferData_->exportPIDs_();
226template <
class LO,
class GO,
class NT>
227Teuchos::RCP<const typename Transfer<LO, GO, NT>::map_type>
230 return TransferData_->source_;
233template <
class LO,
class GO,
class NT>
234Teuchos::RCP<const typename Transfer<LO, GO, NT>::map_type>
237 return TransferData_->target_;
240template <
class LO,
class GO,
class NT>
244 return TransferData_->distributor_;
247template <
class LO,
class GO,
class NT>
250 return TransferData_->isLocallyComplete_;
253template <
class LO,
class GO,
class NT>
256 return (getNumSameIDs() == std::min(getSourceMap()->getLocalNumElements(),
257 getTargetMap()->getLocalNumElements()));
260template <
class LO,
class GO,
class NT>
266 bool ordered = (getNumSameIDs() == std::min(getSourceMap()->getLocalNumElements(),
267 getTargetMap()->getLocalNumElements()));
268 ordered &= (getTargetMap()->getLocalNumElements() == getNumSameIDs() + getNumRemoteIDs());
270 const auto&
dv = TransferData_->remoteLIDs_;
272 "Tpetra::Details::Transfer::getRemoteLIDs_dv: "
273 "DualView needs sync to device");
274 auto v_d =
dv.view_device();
277 TransferData_->remoteLIDsContiguous_ =
ordered;
279 ordered = (getNumSameIDs() == std::min(getSourceMap()->getLocalNumElements(),
280 getTargetMap()->getLocalNumElements()));
281 ordered &= (getSourceMap()->getLocalNumElements() == getNumSameIDs() + getNumExportIDs());
283 const auto&
dv = TransferData_->exportLIDs_;
285 "Tpetra::Details::Transfer::getRemoteLIDs_dv: "
286 "DualView needs sync to device");
287 auto v_d =
dv.view_device();
290 TransferData_->exportLIDsContiguous_ = ordered;
293template <
class LO,
class GO,
class NT>
294bool Transfer<LO, GO, NT>::
295 areRemoteLIDsContiguous()
const {
296 return TransferData_->remoteLIDsContiguous_;
299template <
class LO,
class GO,
class NT>
300bool Transfer<LO, GO, NT>::
301 areExportLIDsContiguous()
const {
302 return TransferData_->exportLIDsContiguous_;
305template <
class LO,
class GO,
class NT>
308 const Teuchos::EVerbosityLevel
verbLevel)
const {
309 this->describeImpl(
out,
"Tpetra::Details::Transfer",
verbLevel);
312template <
class LO,
class GO,
class NT>
313Teuchos::FancyOStream&
316 Teuchos::FancyOStream*
outPtr = TransferData_->out_.getRawPtr();
321template <
class LO,
class GO,
class NT>
324 return TransferData_->verbose_;
327template <
class LO,
class GO,
class NT>
331 const Teuchos::EVerbosityLevel
verbLevel)
const {
333 using Teuchos::TypeNameTraits;
334 using Teuchos::VERB_DEFAULT;
335 using Teuchos::VERB_LOW;
336 using Teuchos::VERB_NONE;
337 const Teuchos::EVerbosityLevel
vl =
347 auto srcMap = this->getSourceMap();
351 auto comm =
srcMap->getComm();
352 if (comm.is_null()) {
355 if (this->getTargetMap().
is_null() ||
356 this->getTargetMap()->getComm().
is_null()) {
360 const int myRank = comm->getRank();
361 const int numProcs = comm->getSize();
368 Teuchos::RCP<Teuchos::OSTab>
tab0,
tab1;
374 tab0 = Teuchos::rcp(
new Teuchos::OSTab(
out));
377 tab1 = Teuchos::rcp(
new Teuchos::OSTab(
out));
380 out <<
"Template parameters:" <<
endl;
389 out <<
"Label: " << label <<
endl;
398 this->globalDescribe(
out,
vl);
413 this->getSourceMap()->describe(
out,
vl);
419 this->getTargetMap()->describe(
out,
vl);
424 this->getDistributor().describe(
out,
vl);
427template <
class LO,
class GO,
class NT>
430 const Teuchos::EVerbosityLevel
vl)
const {
433 using Teuchos::OSTab;
435 using Teuchos::toString;
441 auto srcMap = this->getSourceMap();
446 if (comm.is_null()) {
450 const std::string myStr = localDescribeToString(vl);
454template <
class LO,
class GO,
class NT>
456Transfer<LO, GO, NT>::
457 localDescribeToString(
const Teuchos::EVerbosityLevel vl)
const {
459 using Teuchos::OSTab;
462 RCP<std::ostringstream> outString(
new std::ostringstream);
463 RCP<Teuchos::FancyOStream> outp = Teuchos::getFancyOStream(outString);
464 Teuchos::FancyOStream& out = *outp;
466 RCP<const Teuchos::Comm<int> > comm = this->getSourceMap()->getComm();
467 if (this->getSourceMap().is_null() ||
468 this->getSourceMap()->getComm().is_null()) {
473 return std::string(
"");
475 const int myRank = comm->getRank();
476 const int numProcs = comm->getSize();
478 out <<
"Process " << myRank <<
" of " << numProcs <<
":" << endl;
481 out <<
"numSameIDs: " << getNumSameIDs() << endl;
482 out <<
"numPermuteIDs: " << getNumPermuteIDs() << endl;
483 out <<
"numRemoteIDs: " << getNumRemoteIDs() << endl;
484 out <<
"numExportIDs: " << getNumExportIDs() << endl;
488 if (vl <= Teuchos::VERB_MEDIUM) {
489 out <<
"permuteFromLIDs count: " << getPermuteFromLIDs().size() << endl
490 <<
"permuteToLIDs count: " << getPermuteToLIDs().size() << endl
491 <<
"remoteLIDs count: " << getRemoteLIDs().size() << endl
492 <<
"exportLIDs count: " << getExportLIDs().size() << endl
493 <<
"exportPIDs count: " << getExportPIDs() << endl;
496 RCP<const Map<LO, GO, NT> > tmap = getTargetMap();
497 RCP<const Map<LO, GO, NT> > smap = getSourceMap();
498 Teuchos::Array<GO> RemoteGIDs(getRemoteLIDs().size());
499 Teuchos::Array<int> RemotePIDs(getRemoteLIDs().size());
500 for (
size_t i = 0; i < (size_t)getRemoteLIDs().size(); i++)
501 RemoteGIDs[i] = tmap->getGlobalElement(getRemoteLIDs()[i]);
503 Teuchos::Array<int> ExportGIDs(getExportLIDs().size());
504 for (
size_t i = 0; i < (size_t)getExportLIDs().size(); i++)
505 ExportGIDs[i] = smap->getGlobalElement(getExportLIDs()[i]);
510 Teuchos::ArrayView<const int> ProcsFrom = D.
getProcsFrom();
511 Teuchos::ArrayView<const size_t> LengthsFrom = D.
getLengthsFrom();
512 for (
size_t i = 0, j = 0; i < NumReceives; ++i) {
513 const int pid = ProcsFrom[i];
514 for (
size_t k = 0; k < LengthsFrom[i]; ++k) {
520 out <<
"distor.NumRecvs : " << NumReceives << endl
521 <<
"distor.ProcsFrom : " << toString(ProcsFrom) << endl
522 <<
"distor.LengthsFrom: " << toString(LengthsFrom) << endl;
524 out <<
"distor.NumSends : " << D.
getNumSends() << endl
525 <<
"distor.ProcsTo : " << toString(D.
getProcsTo()) << endl
526 <<
"distor.LengthsTo : " << toString(D.
getLengthsTo()) << endl;
530 out <<
"permuteFromLIDs: " << toString(getPermuteFromLIDs()) << endl
531 <<
"permuteToLIDs: " << toString(getPermuteToLIDs()) << endl
532 <<
"remoteLIDs: " << toString(getRemoteLIDs()) << endl
533 <<
"remoteGIDs: " << toString(RemoteGIDs()) << endl
534 <<
"remotePIDs: " << toString(RemotePIDs()) << endl
535 <<
"exportLIDs: " << toString(getExportLIDs()) << endl
536 <<
"exportGIDs: " << toString(ExportGIDs()) << endl
537 <<
"exportPIDs: " << toString(getExportPIDs()) << endl;
541 return outString->str();
545template <
class LO,
class GO,
class NT>
546void expertSetRemoteLIDsContiguous(Transfer<LO, GO, NT> transfer,
bool contig) {
547 transfer.TransferData_->remoteLIDsContiguous_ = contig;
550template <
class LO,
class GO,
class NT>
551void expertSetExportLIDsContiguous(Transfer<LO, GO, NT> transfer,
bool contig) {
552 transfer.TransferData_->exportLIDsContiguous_ = contig;
Declaration of a function that prints strings from each process.
Struct that holds views of the contents of a CrsMatrix.
static bool verbose()
Whether Tpetra is in verbose mode.
Common base class of Import and Export.
size_t getNumPermuteIDs() const
Number of IDs to permute but not to communicate.
Kokkos::DualView< const LO *, device_type > getPermuteFromLIDs_dv() const
List of local IDs in the source Map that are permuted, as a const DualView (that is sync'd to both ho...
size_t getNumSameIDs() const
Number of initial identical IDs.
Kokkos::DualView< const LO *, device_type > getPermuteToLIDs_dv() const
List of local IDs in the target Map that are permuted, as a const DualView (that is sync'd to both ho...
Teuchos::RCP< const map_type > getTargetMap() const
The target Map used to construct this Export or Import.
Teuchos::ArrayView< const LO > getExportLIDs() const
List of entries in the source Map that will be sent to other processes.
size_t getNumExportIDs() const
Number of entries that must be sent by the calling process to other processes.
size_t getNumRemoteIDs() const
Number of entries not on the calling process.
Teuchos::ArrayView< const LO > getRemoteLIDs() const
List of entries in the target Map to receive from other processes.
Teuchos::ArrayView< const LO > getPermuteFromLIDs() const
List of local IDs in the source Map that are permuted.
bool verbose() const
Whether to print verbose debugging output.
bool isLocallyComplete() const
Is this Export or Import locally complete?
Teuchos::ArrayView< const LO > getPermuteToLIDs() const
List of local IDs in the target Map that are permuted.
Teuchos::ArrayView< const int > getExportPIDs() const
List of processes to which entries will be sent.
Teuchos::RCP< ImportExportData< LO, GO, NT > > TransferData_
All the data needed for executing the Export communication plan.
bool isLocallyFitted() const
Are source and target map locally fitted?
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object in a human-readable way to the given output stream.
Teuchos::FancyOStream & verboseOutputStream() const
Valid (nonnull) output stream for verbose output.
Kokkos::DualView< const LO *, device_type > getRemoteLIDs_dv() const
List of entries in the target Map to receive from other processes, as a const DualView (that is sync'...
::Tpetra::Distributor & getDistributor() const
The Distributor that this Export or Import object uses to move data.
void describeImpl(Teuchos::FancyOStream &out, const std::string &className, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Implementation of describe() for subclasses (Tpetra::Import and Tpetra::Export).
Teuchos::RCP< const map_type > getSourceMap() const
The source Map used to construct this Export or Import.
Kokkos::DualView< const LO *, device_type > getExportLIDs_dv() const
List of entries in the source Map that will be sent to other processes, as a const DualView (that is ...
Sets up and executes a communication plan for a Tpetra DistObject.
Teuchos::ArrayView< const int > getProcsTo() const
Ranks of the processes to which this process will send values.
size_t getNumReceives() const
The number of processes from which we will receive data.
bool hasSelfMessage() const
Whether the calling process will send or receive messages to itself.
Teuchos::ArrayView< const size_t > getLengthsTo() const
Number of values this process will send to each process.
Teuchos::ArrayView< const int > getProcsFrom() const
Ranks of the processes sending values to this process.
Teuchos::ArrayView< const size_t > getLengthsFrom() const
Number of values this process will receive from each process.
size_t getNumSends() const
The number of processes to which we will send data.
Implementation detail of Import and Export.
Implementation details of Tpetra.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator,...
Namespace Tpetra contains the class and methods constituting the Tpetra library.