10 #ifndef TPETRA_DIRECTORYIMPL_DEF_HPP
11 #define TPETRA_DIRECTORYIMPL_DEF_HPP
16 #include "Tpetra_Distributor.hpp"
17 #include "Tpetra_Map.hpp"
20 #include "Tpetra_Details_FixedHashTable.hpp"
21 #include "Teuchos_Comm.hpp"
26 #ifdef HAVE_TPETRACORE_MPI
28 #endif // HAVE_TPETRACORE_MPI
33 template<
class LO,
class GO,
class NT>
37 const Teuchos::ArrayView<const GO> &globalIDs,
38 const Teuchos::ArrayView<int> &nodeIDs,
39 const Teuchos::ArrayView<LO> &localIDs,
40 const bool computeLIDs)
const
44 TEUCHOS_TEST_FOR_EXCEPTION(nodeIDs.size() != globalIDs.size(),
45 std::invalid_argument, Teuchos::typeName(*
this) <<
"::getEntries(): "
46 "Output arrays do not have the right sizes. nodeIDs.size() = "
47 << nodeIDs.size() <<
" != globalIDs.size() = " << globalIDs.size()
49 TEUCHOS_TEST_FOR_EXCEPTION(
50 computeLIDs && localIDs.size() != globalIDs.size(),
51 std::invalid_argument, Teuchos::typeName(*
this) <<
"::getEntries(): "
52 "Output array do not have the right sizes. localIDs.size() = "
53 << localIDs.size() <<
" != globalIDs.size() = " << globalIDs.size()
61 std::fill (nodeIDs.begin(), nodeIDs.end(), -1);
63 std::fill (localIDs.begin(), localIDs.end(),
64 Teuchos::OrdinalTraits<LO>::invalid ());
67 return this->getEntriesImpl (map, globalIDs, nodeIDs, localIDs, computeLIDs);
71 template<
class LO,
class GO,
class NT>
74 numProcs_ (map.getComm ()->getSize ())
78 template<
class LO,
class GO,
class NT>
86 return (numProcs_ == 1);
90 template<
class LO,
class GO,
class NT>
94 std::ostringstream os;
95 os <<
"ReplicatedDirectory"
96 <<
"<" << Teuchos::TypeNameTraits<LO>::name ()
97 <<
", " << Teuchos::TypeNameTraits<GO>::name ()
98 <<
", " << Teuchos::TypeNameTraits<NT>::name () <<
">";
103 template<
class LO,
class GO,
class NT>
107 TEUCHOS_TEST_FOR_EXCEPTION(! map.isContiguous (), std::invalid_argument,
108 Teuchos::typeName (*
this) <<
" constructor: Map is not contiguous.");
109 TEUCHOS_TEST_FOR_EXCEPTION(! map.isUniform (), std::invalid_argument,
110 Teuchos::typeName (*
this) <<
" constructor: Map is not uniform.");
114 template<
class LO,
class GO,
class NT>
118 std::ostringstream os;
119 os <<
"ContiguousUniformDirectory"
120 <<
"<" << Teuchos::TypeNameTraits<LO>::name ()
121 <<
", " << Teuchos::TypeNameTraits<GO>::name ()
122 <<
", " << Teuchos::TypeNameTraits<NT>::name () <<
">";
127 template<
class LO,
class GO,
class NT>
131 const Teuchos::ArrayView<const GO> &globalIDs,
132 const Teuchos::ArrayView<int> &nodeIDs,
133 const Teuchos::ArrayView<LO> &localIDs,
134 const bool computeLIDs)
const
138 typedef typename Teuchos::ArrayView<const GO>::size_type size_type;
139 const LO invalidLid = Teuchos::OrdinalTraits<LO>::invalid ();
142 RCP<const Comm<int> > comm = map.
getComm ();
166 const size_type N_G =
168 const size_type P =
static_cast<size_type
> (comm->getSize ());
169 const size_type N_L = N_G / P;
170 const size_type R = N_G - N_L * P;
171 const size_type N_R = R * (N_L +
static_cast<size_type
> (1));
173 #ifdef HAVE_TPETRA_DEBUG
174 TEUCHOS_TEST_FOR_EXCEPTION(
175 N_G != P*N_L + R, std::logic_error,
176 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: "
177 "N_G = " << N_G <<
" != P*N_L + R = " << P <<
"*" << N_L <<
" + " << R
178 <<
" = " << P*N_L + R <<
". "
179 "Please report this bug to the Tpetra developers.");
180 #endif // HAVE_TPETRA_DEBUG
182 const size_type numGids = globalIDs.size ();
185 const GO ONE =
static_cast<GO
> (1);
188 for (size_type k = 0; k < numGids; ++k) {
189 const GO g_0 = globalIDs[k] - g_min;
195 if (g_0 + ONE < ONE || g_0 >= static_cast<GO> (N_G)) {
197 localIDs[k] = invalidLid;
200 else if (g_0 < static_cast<GO> (N_R)) {
202 nodeIDs[k] =
static_cast<int> (g_0 /
static_cast<GO
> (N_L + 1));
203 localIDs[k] =
static_cast<LO
> (g_0 %
static_cast<GO
> (N_L + 1));
205 else if (g_0 >= static_cast<GO> (N_R)) {
207 const GO g_R = g_0 -
static_cast<GO
> (N_R);
208 nodeIDs[k] =
static_cast<int> (R + g_R / N_L);
209 localIDs[k] =
static_cast<int> (g_R % N_L);
211 #ifdef HAVE_TPETRA_DEBUG
213 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
214 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: "
215 "should never get here. "
216 "Please report this bug to the Tpetra developers.");
218 #endif // HAVE_TPETRA_DEBUG
222 for (size_type k = 0; k < numGids; ++k) {
223 const GO g_0 = globalIDs[k] - g_min;
228 if (g_0 + ONE < ONE || g_0 >= static_cast<GO> (N_G)) {
232 else if (g_0 < static_cast<GO> (N_R)) {
234 nodeIDs[k] =
static_cast<int> (g_0 /
static_cast<GO
> (N_L + 1));
236 else if (g_0 >= static_cast<GO> (N_R)) {
238 const GO g_R = g_0 -
static_cast<GO
> (N_R);
239 nodeIDs[k] =
static_cast<int> (R + g_R / N_L);
241 #ifdef HAVE_TPETRA_DEBUG
243 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
244 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: "
245 "should never get here. "
246 "Please report this bug to the Tpetra developers.");
248 #endif // HAVE_TPETRA_DEBUG
254 template<
class LO,
class GO,
class NT>
259 using Teuchos::gatherAll;
262 RCP<const Teuchos::Comm<int> > comm = map.getComm ();
264 TEUCHOS_TEST_FOR_EXCEPTION(! map.isDistributed (), std::invalid_argument,
265 Teuchos::typeName (*
this) <<
" constructor: Map is not distributed.");
266 TEUCHOS_TEST_FOR_EXCEPTION(! map.isContiguous (), std::invalid_argument,
267 Teuchos::typeName (*
this) <<
" constructor: Map is not contiguous.");
269 const int numProcs = comm->getSize ();
273 allMinGIDs_ = arcp<GO> (numProcs + 1);
275 GO minMyGID = map.getMinGlobalIndex ();
286 #ifdef HAVE_TPETRACORE_MPI
287 MPI_Datatype rawMpiType = MPI_INT;
288 bool useRawMpi =
true;
289 if (
typeid (GO) ==
typeid (
int)) {
290 rawMpiType = MPI_INT;
291 }
else if (
typeid (GO) ==
typeid (
long)) {
292 rawMpiType = MPI_LONG;
297 using Teuchos::rcp_dynamic_cast;
298 using Teuchos::MpiComm;
299 RCP<const MpiComm<int> > mpiComm =
300 rcp_dynamic_cast<
const MpiComm<int> > (comm);
303 if (! comm.is_null ()) {
304 MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
306 MPI_Allgather (&minMyGID, 1, rawMpiType,
307 allMinGIDs_.getRawPtr (), 1, rawMpiType,
309 TEUCHOS_TEST_FOR_EXCEPTION(err != MPI_SUCCESS, std::runtime_error,
310 "Tpetra::DistributedContiguousDirectory: MPI_Allgather failed");
312 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.getRawPtr ());
315 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.getRawPtr ());
317 #else // NOT HAVE_TPETRACORE_MPI
318 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.getRawPtr ());
319 #endif // HAVE_TPETRACORE_MPI
326 allMinGIDs_[numProcs] = map.getMaxAllGlobalIndex ()
327 + Teuchos::OrdinalTraits<GO>::one ();
330 template<
class LO,
class GO,
class NT>
334 std::ostringstream os;
335 os <<
"DistributedContiguousDirectory"
336 <<
"<" << Teuchos::TypeNameTraits<LO>::name ()
337 <<
", " << Teuchos::TypeNameTraits<GO>::name ()
338 <<
", " << Teuchos::TypeNameTraits<NT>::name () <<
">";
342 template<
class LO,
class GO,
class NT>
346 const Teuchos::ArrayView<const GO> &globalIDs,
347 const Teuchos::ArrayView<int> &nodeIDs,
348 const Teuchos::ArrayView<LO> &localIDs,
349 const bool computeLIDs)
const
351 using Teuchos::Array;
352 using Teuchos::ArrayRCP;
353 using Teuchos::ArrayView;
359 RCP<const Teuchos::Comm<int> > comm = map.
getComm ();
360 const int myRank = comm->getRank ();
363 typename ArrayView<int>::iterator procIter = nodeIDs.begin();
364 typename ArrayView<LO>::iterator lidIter = localIDs.begin();
365 typename ArrayView<const GO>::iterator gidIter;
366 for (gidIter = globalIDs.begin(); gidIter != globalIDs.end(); ++gidIter) {
368 *procIter++ = myRank;
385 template<
class LO,
class GO,
class NT>
389 const Teuchos::ArrayView<const GO> &globalIDs,
390 const Teuchos::ArrayView<int> &nodeIDs,
391 const Teuchos::ArrayView<LO> &localIDs,
392 const bool computeLIDs)
const
394 using Teuchos::Array;
395 using Teuchos::ArrayRCP;
396 using Teuchos::ArrayView;
401 RCP<const Teuchos::Comm<int> > comm = map.
getComm ();
402 const int numProcs = comm->getSize ();
403 const LO LINVALID = Teuchos::OrdinalTraits<LO>::invalid();
404 const GO noGIDsOnProc = std::numeric_limits<GO>::max();
408 int firstProcWithGIDs;
409 for (firstProcWithGIDs = 0; firstProcWithGIDs < numProcs;
410 firstProcWithGIDs++) {
411 if (allMinGIDs_[firstProcWithGIDs] != noGIDsOnProc)
break;
417 if (firstProcWithGIDs == numProcs) {
420 std::fill(nodeIDs.begin(), nodeIDs.end(), -1);
422 std::fill(localIDs.begin(), localIDs.end(), LINVALID);
426 const GO one = as<GO> (1);
428 / as<global_size_t>(numProcs - firstProcWithGIDs));
431 typename ArrayView<int>::iterator procIter = nodeIDs.begin();
432 typename ArrayView<LO>::iterator lidIter = localIDs.begin();
433 typename ArrayView<const GO>::iterator gidIter;
434 for (gidIter = globalIDs.begin(); gidIter != globalIDs.end(); ++gidIter) {
443 const GO firstGuess = firstProcWithGIDs + GID / std::max(nOverP, one);
444 curRank = as<int>(std::min(firstGuess, as<GO>(numProcs - 1)));
448 while (allMinGIDs_[curRank] == noGIDsOnProc) curRank++;
451 while (curRank >= firstProcWithGIDs && GID < allMinGIDs_[curRank]) {
453 while (curRank >= firstProcWithGIDs &&
454 allMinGIDs_[curRank] == noGIDsOnProc) curRank--;
456 if (curRank < firstProcWithGIDs) {
460 else if (curRank == numProcs) {
466 int above = curRank + 1;
467 while (allMinGIDs_[above] == noGIDsOnProc) above++;
469 while (GID >= allMinGIDs_[above]) {
471 if (curRank == numProcs) {
477 while (allMinGIDs_[above] == noGIDsOnProc) above++;
483 LID = as<LO> (GID - allMinGIDs_[image]);
496 template<
class LO,
class GO,
class NT>
499 oneToOneResult_ (ONE_TO_ONE_NOT_CALLED_YET),
500 locallyOneToOne_ (true),
501 useHashTables_ (false)
506 template<
class LO,
class GO,
class NT>
507 DistributedNoncontiguousDirectory<LO, GO, NT>::
508 DistributedNoncontiguousDirectory (
const map_type& map,
509 const tie_break_type& tie_break) :
510 oneToOneResult_ (ONE_TO_ONE_NOT_CALLED_YET),
511 locallyOneToOne_ (true),
512 useHashTables_ (false)
514 initialize (map, Teuchos::ptrFromRef (tie_break));
517 template<
class LO,
class GO,
class NT>
519 DistributedNoncontiguousDirectory<LO, GO, NT>::
520 initialize (
const map_type& map,
521 Teuchos::Ptr<const tie_break_type> tie_break)
524 using Teuchos::Array;
525 using Teuchos::ArrayRCP;
526 using Teuchos::ArrayView;
530 using Teuchos::typeName;
531 using Teuchos::TypeNameTraits;
534 typedef Array<int>::size_type size_type;
544 TEUCHOS_TEST_FOR_EXCEPTION(
sizeof(
global_size_t) <
sizeof(GO),
545 std::logic_error, typeName (*
this) <<
": sizeof(Tpetra::"
546 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(Global"
547 "Ordinal = " << TypeNameTraits<LO>::name () <<
") = " <<
sizeof(GO)
549 TEUCHOS_TEST_FOR_EXCEPTION(
sizeof(
global_size_t) <
sizeof(
int),
550 std::logic_error, typeName (*
this) <<
": sizeof(Tpetra::"
551 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(int) = "
552 <<
sizeof(
int) <<
".");
553 TEUCHOS_TEST_FOR_EXCEPTION(
sizeof(
global_size_t) <
sizeof(LO),
554 std::logic_error, typeName (*
this) <<
": sizeof(Tpetra::"
555 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(Local"
556 "Ordinal = " << TypeNameTraits<LO>::name () <<
") = " <<
sizeof(LO)
559 RCP<const Teuchos::Comm<int> > comm = map.getComm ();
560 const LO LINVALID = Teuchos::OrdinalTraits<LO>::invalid ();
561 const GO minAllGID = map.getMinAllGlobalIndex ();
562 const GO maxAllGID = map.getMaxAllGlobalIndex ();
569 const global_size_t numGlobalEntries = maxAllGID - minAllGID + 1;
579 directoryMap_ = rcp (
new map_type (numGlobalEntries, minAllGID, comm,
580 GloballyDistributed));
582 const size_t dir_numMyEntries = directoryMap_->getLocalNumElements ();
606 const size_t inverseSparsityThreshold = 10;
608 (dir_numMyEntries >= inverseSparsityThreshold * map.getLocalNumElements());
613 const int myRank = comm->getRank ();
614 const size_t numMyEntries = map.getLocalNumElements ();
615 Array<int> sendImageIDs (numMyEntries);
616 ArrayView<const GO> myGlobalEntries = map.getLocalElementList ();
621 directoryMap_->getRemoteIndexList (myGlobalEntries, sendImageIDs);
622 TEUCHOS_TEST_FOR_EXCEPTION(
623 lookupStatus ==
IDNotPresent, std::logic_error, Teuchos::typeName(*
this)
624 <<
" constructor: the Directory Map could not find out where one or "
625 "more of my Map's indices should go. The input to getRemoteIndexList "
626 "is " << Teuchos::toString (myGlobalEntries) <<
", and the output is "
627 << Teuchos::toString (sendImageIDs ()) <<
". The input Map itself has "
628 "the following entries on the calling process " <<
629 map.getComm ()->getRank () <<
": " <<
630 Teuchos::toString (map.getLocalElementList ()) <<
", and has "
631 << map.getGlobalNumElements () <<
" total global indices in ["
632 << map.getMinAllGlobalIndex () <<
"," << map.getMaxAllGlobalIndex ()
633 <<
"]. The Directory Map has "
634 << directoryMap_->getGlobalNumElements () <<
" total global indices in "
635 "[" << directoryMap_->getMinAllGlobalIndex () <<
"," <<
636 directoryMap_->getMaxAllGlobalIndex () <<
"], and the calling process "
637 "has GIDs [" << directoryMap_->getMinGlobalIndex () <<
"," <<
638 directoryMap_->getMaxGlobalIndex () <<
"]. "
639 "This probably means there is a bug in Map or Directory. "
640 "Please report this bug to the Tpetra developers.");
647 Distributor distor (comm);
648 const size_t numReceives = distor.createFromSends (sendImageIDs);
662 const int packetSize = 3;
663 Kokkos::View<GO*, Kokkos::HostSpace> exportEntries(
"exportEntries", packetSize * numMyEntries);
665 size_t exportIndex = 0;
666 for (
size_t i = 0; i < numMyEntries; ++i) {
667 exportEntries[exportIndex++] = myGlobalEntries[i];
668 exportEntries[exportIndex++] = as<GO> (myRank);
669 exportEntries[exportIndex++] = as<GO> (i);
675 Kokkos::View<GO*, Kokkos::HostSpace> importElements(
"importElements", packetSize * distor.getTotalReceiveLength());
678 distor.doPostsAndWaits(exportEntries, packetSize, importElements);
685 if (useHashTables_) {
700 LO* tableKeysRaw = NULL;
701 LO* tableLidsRaw = NULL;
702 int* tablePidsRaw = NULL;
704 tableKeysRaw =
new LO [numReceives];
705 tableLidsRaw =
new LO [numReceives];
706 tablePidsRaw =
new int [numReceives];
708 if (tableKeysRaw != NULL) {
709 delete [] tableKeysRaw;
711 if (tableLidsRaw != NULL) {
712 delete [] tableLidsRaw;
714 if (tablePidsRaw != NULL) {
715 delete [] tablePidsRaw;
719 ArrayRCP<LO> tableKeys (tableKeysRaw, 0, numReceives,
true);
720 ArrayRCP<LO> tableLids (tableLidsRaw, 0, numReceives,
true);
721 ArrayRCP<int> tablePids (tablePidsRaw, 0, numReceives,
true);
723 if (tie_break.is_null ()) {
725 size_type importIndex = 0;
726 for (size_type i = 0; i < static_cast<size_type> (numReceives); ++i) {
727 const GO curGID = importElements[importIndex++];
728 const LO curLID = directoryMap_->getLocalElement (curGID);
729 TEUCHOS_TEST_FOR_EXCEPTION(
730 curLID == LINVALID, std::logic_error,
731 Teuchos::typeName(*
this) <<
" constructor: Incoming global index "
732 << curGID <<
" does not have a corresponding local index in the "
733 "Directory Map. Please report this bug to the Tpetra developers.");
734 tableKeys[i] = curLID;
735 tablePids[i] = importElements[importIndex++];
736 tableLids[i] = importElements[importIndex++];
742 rcp (
new lidToPidTable_type (tableKeys (), tablePids ()));
743 locallyOneToOne_ = ! (lidToPidTable_->hasDuplicateKeys ());
745 rcp (
new lidToLidTable_type (tableKeys (), tableLids ()));
754 typedef std::map<LO, std::vector<std::pair<int, LO> > > pair_table_type;
755 pair_table_type ownedPidLidPairs;
759 size_type importIndex = 0;
760 for (size_type i = 0; i < static_cast<size_type> (numReceives); ++i) {
761 const GO curGID = importElements[importIndex++];
762 const LO dirMapLid = directoryMap_->getLocalElement (curGID);
763 TEUCHOS_TEST_FOR_EXCEPTION(
764 dirMapLid == LINVALID, std::logic_error,
765 Teuchos::typeName(*
this) <<
" constructor: Incoming global index "
766 << curGID <<
" does not have a corresponding local index in the "
767 "Directory Map. Please report this bug to the Tpetra developers.");
768 tableKeys[i] = dirMapLid;
769 const int PID = importElements[importIndex++];
770 const int LID = importElements[importIndex++];
780 ownedPidLidPairs[dirMapLid].push_back (std::make_pair (PID, LID));
793 for (size_type i = 0; i < static_cast<size_type> (numReceives); ++i) {
794 const LO dirMapLid = tableKeys[i];
795 const std::vector<std::pair<int, LO> >& pidLidList =
796 ownedPidLidPairs[dirMapLid];
797 const size_t listLen = pidLidList.size();
798 if (listLen == 0)
continue;
799 const GO dirMapGid = directoryMap_->getGlobalElement (dirMapLid);
801 locallyOneToOne_ =
false;
810 const size_type index =
811 static_cast<size_type
> (tie_break->selectedIndex (dirMapGid,
813 tablePids[i] = pidLidList[index].first;
814 tableLids[i] = pidLidList[index].second;
819 rcp (
new lidToPidTable_type (tableKeys (), tablePids ()));
821 rcp (
new lidToLidTable_type (tableKeys (), tableLids ()));
825 if (tie_break.is_null ()) {
830 PIDs_ = arcp<int> (dir_numMyEntries);
831 std::fill (PIDs_.begin (), PIDs_.end (), -1);
832 LIDs_ = arcp<LO> (dir_numMyEntries);
833 std::fill (LIDs_.begin (), LIDs_.end (), LINVALID);
835 size_type importIndex = 0;
836 for (size_type i = 0; i < static_cast<size_type> (numReceives); ++i) {
837 const GO curGID = importElements[importIndex++];
838 const LO curLID = directoryMap_->getLocalElement (curGID);
839 TEUCHOS_TEST_FOR_EXCEPTION(curLID == LINVALID, std::logic_error,
840 Teuchos::typeName(*
this) <<
" constructor: Incoming global index "
841 << curGID <<
" does not have a corresponding local index in the "
842 "Directory Map. Please report this bug to the Tpetra developers.");
847 if (PIDs_[curLID] != -1) {
848 locallyOneToOne_ =
false;
850 PIDs_[curLID] = importElements[importIndex++];
851 LIDs_[curLID] = importElements[importIndex++];
855 PIDs_ = arcp<int> (dir_numMyEntries);
856 LIDs_ = arcp<LO> (dir_numMyEntries);
857 std::fill (PIDs_.begin (), PIDs_.end (), -1);
866 Array<std::vector<std::pair<int, LO> > > ownedPidLidPairs (dir_numMyEntries);
867 size_t importIndex = 0;
868 for (
size_t i = 0; i < numReceives; ++i) {
869 const GO GID = importElements[importIndex++];
870 const int PID = importElements[importIndex++];
871 const LO LID = importElements[importIndex++];
873 const LO dirMapLid = directoryMap_->getLocalElement (GID);
874 TEUCHOS_TEST_FOR_EXCEPTION(
875 dirMapLid == LINVALID, std::logic_error,
876 Teuchos::typeName(*
this) <<
" constructor: Incoming global index "
877 << GID <<
" does not have a corresponding local index in the "
878 "Directory Map. Please report this bug to the Tpetra developers.");
879 ownedPidLidPairs[dirMapLid].push_back (std::make_pair (PID, LID));
891 for (
size_t i = 0; i < dir_numMyEntries; ++i) {
892 const std::vector<std::pair<int, LO> >& pidLidList =
894 const size_t listLen = pidLidList.size();
895 if (listLen == 0)
continue;
897 const LO dirMapLid =
static_cast<LO
> (i);
898 const GO dirMapGid = directoryMap_->getGlobalElement (dirMapLid);
900 locallyOneToOne_ =
false;
909 const size_type index =
910 static_cast<size_type
> (tie_break->selectedIndex (dirMapGid,
912 PIDs_[i] = pidLidList[index].first;
913 LIDs_[i] = pidLidList[index].second;
919 template<
class LO,
class GO,
class NT>
923 std::ostringstream os;
924 os <<
"DistributedNoncontiguousDirectory"
925 <<
"<" << Teuchos::TypeNameTraits<LO>::name ()
926 <<
", " << Teuchos::TypeNameTraits<GO>::name ()
927 <<
", " << Teuchos::TypeNameTraits<NT>::name () <<
">";
931 template<
class LO,
class GO,
class NT>
935 const Teuchos::ArrayView<const GO> &globalIDs,
936 const Teuchos::ArrayView<int> &nodeIDs,
937 const Teuchos::ArrayView<LO> &localIDs,
938 const bool computeLIDs)
const
940 using Teuchos::Array;
941 using Teuchos::ArrayRCP;
942 using Teuchos::ArrayView;
945 using Teuchos::toString;
950 using size_type =
typename Array<GO>::size_type;
951 const char funcPrefix[] =
"Tpetra::"
952 "DistributedNoncontiguousDirectory::getEntriesImpl: ";
953 const char errSuffix[] =
954 " Please report this bug to the Tpetra developers.";
956 RCP<const Teuchos::Comm<int> > comm = map.getComm ();
959 const size_t maxNumToPrint = verbose ?
962 std::unique_ptr<std::string> procPrefix;
964 std::ostringstream os;
966 if (map.getComm ().is_null ()) {
970 os << map.getComm ()->getRank ();
973 procPrefix = std::unique_ptr<std::string>(
974 new std::string(os.str()));
975 os << funcPrefix <<
"{";
981 os <<
", computeLIDs: "
982 << (computeLIDs ?
"true" :
"false") <<
"}" << endl;
986 const size_t numEntries = globalIDs.size ();
987 const LO LINVALID = Teuchos::OrdinalTraits<LO>::invalid();
996 const int packetSize = computeLIDs ? 3 : 2;
1002 Array<int> dirImages (numEntries);
1004 std::ostringstream os;
1005 os << *procPrefix <<
"Call directoryMap_->getRemoteIndexList"
1009 res = directoryMap_->getRemoteIndexList (globalIDs, dirImages ());
1011 std::ostringstream os;
1012 os << *procPrefix <<
"Director Map getRemoteIndexList out ";
1019 size_t numMissing = 0;
1021 for (
size_t i=0; i < numEntries; ++i) {
1022 if (dirImages[i] == -1) {
1025 localIDs[i] = LINVALID;
1033 Array<int> sendImages;
1035 std::ostringstream os;
1036 os << *procPrefix <<
"Call Distributor::createFromRecvs"
1040 distor.
createFromRecvs (globalIDs, dirImages (), sendGIDs, sendImages);
1042 std::ostringstream os;
1043 os << *procPrefix <<
"Distributor::createFromRecvs result: "
1051 const size_type numSends = sendGIDs.size ();
1084 Kokkos::View<global_size_t*, Kokkos::HostSpace> exports(
"exports", packetSize * numSends);
1097 size_t exportsIndex = 0;
1099 if (useHashTables_) {
1101 std::ostringstream os;
1102 os << *procPrefix <<
"Pack exports (useHashTables_ true)"
1106 for (size_type gidIndex = 0; gidIndex < numSends; ++gidIndex) {
1107 const GO curGID = sendGIDs[gidIndex];
1109 exports[exportsIndex++] =
static_cast<global_size_t> (curGID);
1110 const LO curLID = directoryMap_->getLocalElement (curGID);
1111 TEUCHOS_TEST_FOR_EXCEPTION
1112 (curLID == LINVALID, std::logic_error, funcPrefix <<
1113 "Directory Map's global index " << curGID <<
" lacks "
1114 "a corresponding local index." << errSuffix);
1116 exports[exportsIndex++] =
1120 exports[exportsIndex++] =
1127 std::ostringstream os;
1128 os << *procPrefix <<
"Pack exports (useHashTables_ false)"
1132 for (size_type gidIndex = 0; gidIndex < numSends; ++gidIndex) {
1133 const GO curGID = sendGIDs[gidIndex];
1135 exports[exportsIndex++] =
static_cast<global_size_t> (curGID);
1136 const LO curLID = directoryMap_->getLocalElement (curGID);
1137 TEUCHOS_TEST_FOR_EXCEPTION
1138 (curLID == LINVALID, std::logic_error, funcPrefix <<
1139 "Directory Map's global index " << curGID <<
" lacks "
1140 "a corresponding local index." << errSuffix);
1142 exports[exportsIndex++] =
1146 exports[exportsIndex++] =
1152 TEUCHOS_TEST_FOR_EXCEPTION
1153 (exportsIndex > exports.size(), std::logic_error,
1154 funcPrefix <<
"On Process " << comm->getRank () <<
", "
1155 "exportsIndex = " << exportsIndex <<
" > exports.size() = "
1156 << exports.size () <<
"." << errSuffix);
1159 TEUCHOS_TEST_FOR_EXCEPTION
1160 (numEntries < numMissing, std::logic_error, funcPrefix <<
1161 "On Process " << comm->getRank () <<
", numEntries = " <<
1162 numEntries <<
" < numMissing = " << numMissing <<
"." <<
1169 const size_t numRecv = numEntries - numMissing;
1173 const size_t requiredImportLen = numRecv * packetSize;
1174 const int myRank = comm->getRank ();
1175 TEUCHOS_TEST_FOR_EXCEPTION(
1176 importLen < requiredImportLen, std::logic_error,
1177 "Tpetra::Details::DistributedNoncontiguousDirectory::getEntriesImpl: "
1178 "On Process " << myRank <<
": The 'imports' array must have length "
1179 "at least " << requiredImportLen <<
", but its actual length is " <<
1180 importLen <<
". numRecv: " << numRecv <<
", packetSize: " <<
1181 packetSize <<
", numEntries (# GIDs): " << numEntries <<
1182 ", numMissing: " << numMissing <<
": distor.getTotalReceiveLength(): "
1184 "Distributor description: " << distor.
description () <<
". "
1186 "Please report this bug to the Tpetra developers.");
1189 Kokkos::View<global_size_t*, Kokkos::HostSpace> imports(
"imports", packetSize * distor.
getTotalReceiveLength());
1194 std::ostringstream os;
1195 os << *procPrefix <<
"Call doPostsAndWaits: {"
1196 <<
"packetSize: " << packetSize <<
", ";
1203 std::ostringstream os;
1204 os << *procPrefix <<
"doPostsAndWaits result: ";
1210 Array<GO> sortedIDs (globalIDs);
1211 Array<GO> offset (numEntries);
1212 for (GO ii = 0; ii < static_cast<GO> (numEntries); ++ii) {
1215 sort2 (sortedIDs.begin(), sortedIDs.begin() + numEntries, offset.begin());
1217 std::ostringstream os;
1226 size_t importsIndex = 0;
1231 for (
size_t i = 0; i < numRecv; ++i) {
1232 const GO curGID =
static_cast<GO
> (imports[importsIndex++]);
1233 auto p1 = std::equal_range (sortedIDs.begin(),
1234 sortedIDs.end(), curGID);
1235 if (p1.first != p1.second) {
1236 const size_t j = p1.first - sortedIDs.begin();
1237 nodeIDs[offset[j]] =
1238 static_cast<int> (imports[importsIndex++]);
1240 localIDs[offset[j]] =
1241 static_cast<LO
> (imports[importsIndex++]);
1243 if (nodeIDs[offset[j]] == -1) {
1249 TEUCHOS_TEST_FOR_EXCEPTION
1250 (
size_t (importsIndex) >
size_t (imports.size ()),
1251 std::logic_error, funcPrefix <<
"On Process " <<
1252 comm->getRank () <<
": importsIndex = " << importsIndex
1253 <<
" > imports.size() = " << imports.size () <<
". "
1254 "numRecv: " << numRecv
1255 <<
", packetSize: " << packetSize <<
", "
1256 "numEntries (# GIDs): " << numEntries
1257 <<
", numMissing: " << numMissing
1258 <<
": distor.getTotalReceiveLength(): "
1261 std::ostringstream os;
1262 os << *procPrefix << funcPrefix <<
"Done!" << endl;
1269 template<
class LO,
class GO,
class NT>
1274 if (oneToOneResult_ == ONE_TO_ONE_NOT_CALLED_YET) {
1275 const int lcl121 = isLocallyOneToOne () ? 1 : 0;
1277 Teuchos::reduceAll<int, int> (comm, Teuchos::REDUCE_MIN, lcl121,
1278 Teuchos::outArg (gbl121));
1279 oneToOneResult_ = (gbl121 == 1) ? ONE_TO_ONE_TRUE : ONE_TO_ONE_FALSE;
1281 return (oneToOneResult_ == ONE_TO_ONE_TRUE);
1291 #define TPETRA_DIRECTORYIMPL_INSTANT(LO,GO,NODE) \
1292 namespace Details { \
1293 template class Directory< LO , GO , NODE >; \
1294 template class ReplicatedDirectory< LO , GO , NODE >; \
1295 template class ContiguousUniformDirectory< LO, GO, NODE >; \
1296 template class DistributedContiguousDirectory< LO , GO , NODE >; \
1297 template class DistributedNoncontiguousDirectory< LO , GO , NODE >; \
1300 #endif // TPETRA_DIRECTORYIMPL_DEF_HPP
void initialize(int *argc, char ***argv)
Initialize Tpetra.
std::string description() const override
A one-line human-readable description of this object.
Interface for breaking ties in ownership.
std::string description() const
Return a one-line description of this object.
LookupStatus
Return status of Map remote index lookup (getRemoteIndexList()).
Implementation of Directory for a distributed noncontiguous Map.
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
bool isOneToOne(const Teuchos::Comm< int > &comm) const override
Whether the Directory's input Map is (globally) one to one.
Implementation of Directory for a distributed contiguous Map.
void verbosePrintArray(std::ostream &out, const ArrayType &x, const char name[], const size_t maxNumToPrint)
Print min(x.size(), maxNumToPrint) entries of x.
LookupStatus getEntriesImpl(const map_type &map, const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const override
Find process IDs and (optionally) local IDs for the given global IDs.
bool isOneToOne(const Teuchos::Comm< int > &comm) const override
Whether the Directory's input Map is (globally) one to one.
size_t global_size_t
Global size_t object.
LookupStatus getEntriesImpl(const map_type &map, const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const override
Find process IDs and (optionally) local IDs for the given global IDs.
void sort2(const IT1 &first1, const IT1 &last1, const IT2 &first2, const bool stableSort=false)
Sort the first array, and apply the resulting permutation to the second array.
LookupStatus getEntries(const map_type &map, const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const
bool isNodeGlobalElement(global_ordinal_type globalIndex) const
Whether the given global index is owned by this Map on the calling process.
Sets up and executes a communication plan for a Tpetra DistObject.
static bool verbose()
Whether Tpetra is in verbose mode.
size_t getTotalReceiveLength() const
Total number of values this process will receive from other processes.
std::string description() const override
A one-line human-readable description of this object.
A parallel distribution of indices over processes.
ReplicatedDirectory()=default
Constructor (that takes no arguments).
std::enable_if<(Kokkos::is_view< ExpView >::value &&Kokkos::is_view< ImpView >::value)>::type doPostsAndWaits(const ExpView &exports, size_t numPackets, const ImpView &imports)
Execute the (forward) communication plan.
local_ordinal_type getLocalElement(global_ordinal_type globalIndex) const
The local index corresponding to the given global index.
Stand-alone utility functions and macros.
static size_t verbosePrintCountThreshold()
Number of entries below which arrays, lists, etc. will be printed in debug mode.
LookupStatus getEntriesImpl(const map_type &map, const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const override
Find process IDs and (optionally) local IDs for the given global IDs.
void createFromRecvs(const Teuchos::ArrayView< const Ordinal > &remoteIDs, const Teuchos::ArrayView< const int > &remoteProcIDs, Teuchos::Array< Ordinal > &exportIDs, Teuchos::Array< int > &exportProcIDs)
Set up Distributor using list of process ranks from which to receive.
std::string description() const override
A one-line human-readable description of this object.
global_ordinal_type getMinAllGlobalIndex() const
The minimum global index over all processes in the communicator.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
Description of Tpetra's behavior.