42 #ifndef __Tpetra_DirectoryImpl_def_hpp 43 #define __Tpetra_DirectoryImpl_def_hpp 45 #ifdef DOXYGEN_USE_ONLY 46 # include <Tpetra_DirectoryImpl_decl.hpp> 48 #include <Tpetra_Distributor.hpp> 49 #include <Tpetra_Map.hpp> 51 #ifdef HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 52 # include <Tpetra_Details_FixedHashTable.hpp> 53 #endif // HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 65 template<
class LO,
class GO,
class NT>
71 template<
class LO,
class GO,
class NT>
77 const bool computeLIDs)
const 83 "Output arrays do not have the right sizes. nodeIDs.size() = " 84 << nodeIDs.
size() <<
" != globalIDs.size() = " << globalIDs.
size()
87 computeLIDs && localIDs.
size() != globalIDs.
size(),
89 "Output array do not have the right sizes. localIDs.size() = " 90 << localIDs.
size() <<
" != globalIDs.size() = " << globalIDs.
size()
98 std::fill (nodeIDs.
begin(), nodeIDs.
end(), -1);
100 std::fill (localIDs.
begin(), localIDs.
end(),
104 return this->
getEntriesImpl (globalIDs, nodeIDs, localIDs, computeLIDs);
107 template<
class LO,
class GO,
class NT>
113 template<
class LO,
class GO,
class NT>
117 std::ostringstream os;
118 os <<
"ReplicatedDirectory" 126 template<
class LO,
class GO,
class NT>
138 template<
class LO,
class GO,
class NT>
142 std::ostringstream os;
143 os <<
"ContiguousUniformDirectory" 151 template<
class LO,
class GO,
class NT>
157 const bool computeLIDs)
const 192 const size_type P = as<size_type> (comm->getSize ());
193 const size_type N_L = N_G / P;
194 const size_type R = N_G - N_L * P;
195 const size_type N_R = R * (N_L + 1);
197 #ifdef HAVE_TPETRA_DEBUG 199 N_G != P*N_L + R, std::logic_error,
200 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: " 201 "N_G = " << N_G <<
" != P*N_L + R = " << P <<
"*" << N_L <<
" + " << R
202 <<
" = " << P*N_L + R <<
". " 203 "Please report this bug to the Tpetra developers.");
204 #endif // HAVE_TPETRA_DEBUG 206 const size_type numGids = globalIDs.
size ();
208 for (size_type k = 0; k < numGids; ++k) {
209 const GO g_0 = globalIDs[k] - g_min;
214 if (g_0 + 1 < 1 || g_0 >= N_G) {
216 localIDs[k] = invalidLid;
219 else if (g_0 < as<GO> (N_R)) {
221 nodeIDs[k] = as<int> (g_0 / (N_L + 1));
222 localIDs[k] = g_0 % (N_L + 1);
224 else if (g_0 >= as<GO> (N_R)) {
226 const GO g_R = g_0 - N_R;
227 nodeIDs[k] = as<int> (R + g_R / N_L);
228 localIDs[k] = as<int> (g_R % N_L);
230 #ifdef HAVE_TPETRA_DEBUG 232 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
233 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: " 234 "should never get here. " 235 "Please report this bug to the Tpetra developers.");
237 #endif // HAVE_TPETRA_DEBUG 241 for (size_type k = 0; k < numGids; ++k) {
242 const GO g_0 = globalIDs[k] - g_min;
247 if (g_0 + 1 < 1 || g_0 >= N_G) {
251 else if (g_0 < as<GO> (N_R)) {
253 nodeIDs[k] = as<int> (g_0 / (N_L + 1));
255 else if (g_0 >= as<GO> (N_R)) {
257 const GO g_R = g_0 - N_R;
258 nodeIDs[k] = as<int> (R + g_R / N_L);
260 #ifdef HAVE_TPETRA_DEBUG 262 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
263 "Tpetra::ContiguousUniformDirectory::getEntriesImpl: " 264 "should never get here. " 265 "Please report this bug to the Tpetra developers.");
267 #endif // HAVE_TPETRA_DEBUG 274 template<
class LO,
class GO,
class NT>
279 using Teuchos::gatherAll;
289 const int numProcs = comm->getSize ();
293 allMinGIDs_ = arcp<GO> (numProcs + 1);
295 GO minMyGID = map->getMinGlobalIndex ();
307 MPI_Datatype rawMpiType = MPI_INT;
308 bool useRawMpi =
true;
309 if (
typeid (GO) ==
typeid (
int)) {
310 rawMpiType = MPI_INT;
311 }
else if (
typeid (GO) ==
typeid (
long)) {
312 rawMpiType = MPI_LONG;
317 using Teuchos::rcp_dynamic_cast;
320 rcp_dynamic_cast<
const MpiComm<int> > (comm);
324 MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
326 MPI_Allgather (&minMyGID, 1, rawMpiType,
327 allMinGIDs_.getRawPtr (), 1, rawMpiType,
330 "Tpetra::DistributedContiguousDirectory: MPI_Allgather failed");
332 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.
getRawPtr ());
335 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.
getRawPtr ());
337 #else // NOT HAVE_MPI 338 gatherAll<int, GO> (*comm, 1, &minMyGID, numProcs, allMinGIDs_.
getRawPtr ());
346 allMinGIDs_[numProcs] = map->getMaxAllGlobalIndex ()
350 template<
class LO,
class GO,
class NT>
354 std::ostringstream os;
355 os <<
"DistributedContiguousDirectory" 362 template<
class LO,
class GO,
class NT>
368 const bool computeLIDs)
const 380 const int myRank = comm->getRank ();
386 for (gidIter = globalIDs.
begin(); gidIter != globalIDs.
end(); ++gidIter) {
388 *procIter++ = myRank;
405 template<
class LO,
class GO,
class NT>
411 const bool computeLIDs)
const 425 const int numProcs = comm->getSize ();
432 for (gidIter = globalIDs.
begin(); gidIter != globalIDs.
end(); ++gidIter) {
442 const GO one = as<GO> (1);
443 const GO two = as<GO> (2);
444 const GO nOverP_GID = as<GO> (nOverP);
445 const GO lowerBound = GID / std::max(nOverP_GID, one) + two;
446 curRank = as<int>(std::min(lowerBound, as<GO>(numProcs - 1)));
449 while (curRank >= 0 && curRank < numProcs) {
450 if (allMinGIDs_[curRank] <= GID) {
451 if (GID < allMinGIDs_[curRank + 1]) {
465 LID = as<LO> (GID - allMinGIDs_[image]);
478 template<
class LO,
class GO,
class NT>
482 useHashTables_ (
false)
501 std::logic_error,
typeName (*
this) <<
": sizeof(Tpetra::" 502 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(Global" 503 "Ordinal = " << TypeNameTraits<LO>::name () <<
") = " <<
sizeof(GO)
506 std::logic_error,
typeName (*
this) <<
": sizeof(Tpetra::" 507 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(int) = " 508 <<
sizeof(
int) <<
".");
510 std::logic_error,
typeName (*
this) <<
": sizeof(Tpetra::" 511 "global_size_t) = " <<
sizeof(
global_size_t) <<
" < sizeof(Local" 512 "Ordinal = " << TypeNameTraits<LO>::name () <<
") = " <<
sizeof(LO)
517 const GO minAllGID = map->getMinAllGlobalIndex ();
518 const GO maxAllGID = map->getMaxAllGlobalIndex ();
525 const global_size_t numGlobalEntries = maxAllGID - minAllGID + 1;
535 directoryMap_ =
rcp (
new map_type (numGlobalEntries, minAllGID, comm,
538 const size_t dir_numMyEntries = directoryMap_->getNodeNumElements ();
554 #ifdef HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 562 const size_t inverseSparsityThreshold = 10;
564 dir_numMyEntries >= inverseSparsityThreshold * map->getNodeNumElements ();
565 #endif // HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 570 const int myRank = comm->getRank ();
571 const size_t numMyEntries = map->getNodeNumElements ();
578 directoryMap_->getRemoteIndexList (myGlobalEntries, sendImageIDs) ==
IDNotPresent,
580 "Directory Map could not find out where one or more of my Map's " 581 "elements should go. This probably means there is a bug in Map or " 582 "Directory. Please report this bug to the Tpetra developers.");
590 const size_t numReceives = distor.createFromSends (sendImageIDs);
599 const int packetSize = 3;
600 Array<GO> exportEntries (packetSize * numMyEntries);
603 for (
size_t i=0; i < numMyEntries; ++i) {
604 *iter++ = myGlobalEntries[i];
605 *iter++ = as<GO> (myRank);
606 *iter++ = as<GO> (i);
612 Array<GO> importElements (packetSize * distor.getTotalReceiveLength ());
615 distor.doPostsAndWaits (exportEntries ().
getConst (), packetSize, importElements ());
617 #ifdef HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 623 if (useHashTables_) {
638 LO* tableKeysRaw = NULL;
639 LO* tableLidsRaw = NULL;
640 int* tablePidsRaw = NULL;
642 tableKeysRaw =
new LO [numReceives];
643 tableLidsRaw =
new LO [numReceives];
644 tablePidsRaw =
new int [numReceives];
646 if (tableKeysRaw != NULL) {
647 delete [] tableKeysRaw;
649 if (tableLidsRaw != NULL) {
650 delete [] tableLidsRaw;
652 if (tablePidsRaw != NULL) {
653 delete [] tablePidsRaw;
657 ArrayRCP<LO> tableKeys (tableKeysRaw, 0, numReceives,
true);
658 ArrayRCP<LO> tableLids (tableLidsRaw, 0, numReceives,
true);
659 ArrayRCP<int> tablePids (tablePidsRaw, 0, numReceives,
true);
663 for (
size_t i = 0; i < numReceives; ++i) {
664 const GO curGID = *iter++;
665 const LO curLID = directoryMap_->getLocalElement (curGID);
668 << curGID <<
" does not have a corresponding local index in the " 669 "Directory Map. Please report this bug to the Tpetra developers.");
670 tableKeys[i] = curLID;
671 tablePids[i] = *iter++;
672 tableLids[i] = *iter++;
687 PIDs_ = arcp<int> (dir_numMyEntries);
688 std::fill (PIDs_.begin (), PIDs_.end (), -1);
689 LIDs_ = arcp<LO> (dir_numMyEntries);
690 std::fill (LIDs_.begin (), LIDs_.end (), LINVALID);
693 for (
size_t i = 0; i < numReceives; ++i) {
694 const GO curGID = *iter++;
695 const LO curLID = directoryMap_->getLocalElement (curGID);
698 << curGID <<
" does not have a corresponding local index in the " 699 "Directory Map. Please report this bug to the Tpetra developers.");
700 PIDs_[curLID] = *iter++;
701 LIDs_[curLID] = *iter++;
704 #else // NOT HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 705 if (! useHashTables_) {
710 PIDs_ = arcp<int> (dir_numMyEntries);
711 std::fill (PIDs_.begin (), PIDs_.end (), -1);
712 LIDs_ = arcp<LO> (dir_numMyEntries);
713 std::fill (LIDs_.begin (), LIDs_.end (), LINVALID);
716 for (
size_t i = 0; i < numReceives; ++i) {
717 const GO curGID = *iter++;
718 const LO curLID = directoryMap_->getLocalElement (curGID);
721 << curGID <<
" does not have a corresponding local index in the " 722 "Directory Map. Please report this bug to the Tpetra developers.");
723 PIDs_[curLID] = *iter++;
724 LIDs_[curLID] = *iter++;
729 #endif // HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 732 template<
class LO,
class GO,
class NT>
736 std::ostringstream os;
737 os <<
"DistributedNoncontiguousDirectory" 744 template<
class LO,
class GO,
class NT>
750 const bool computeLIDs)
const 763 const size_t numEntries = globalIDs.
size ();
771 const int packetSize = computeLIDs ? 3 : 2;
778 res = directoryMap_->getRemoteIndexList (globalIDs, dirImages ());
780 size_t numMissing = 0;
782 for (
size_t i=0; i < numEntries; ++i) {
783 if (dirImages[i] == -1) {
786 localIDs[i] = LINVALID;
795 distor.
createFromRecvs (globalIDs, dirImages (), sendGIDs, sendImages);
796 const size_t numSends = sendGIDs.
size ();
839 #ifdef HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 840 if (useHashTables_) {
841 for ( ; gidIter != sendGIDs.
end(); ++gidIter) {
842 const GO curGID = *gidIter;
845 const LO curLID = directoryMap_->getLocalElement (curGID);
848 "Map's global index " << curGID <<
" does not have a corresponding " 849 "local index. Please report this bug to the Tpetra developers.");
851 *exportsIter++ =
static_cast<global_size_t> (lidToPidTable_->get (curLID));
854 *exportsIter++ =
static_cast<global_size_t> (lidToLidTable_->get (curLID));
858 for ( ; gidIter != sendGIDs.
end(); ++gidIter) {
859 const GO curGID = *gidIter;
862 const LO curLID = directoryMap_->getLocalElement (curGID);
865 "Map's global index " << curGID <<
" does not have a corresponding " 866 "local index. Please report this bug to the Tpetra developers.");
875 #else // NOT HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 876 for ( ; gidIter != sendGIDs.
end (); ++gidIter) {
877 const GO curGID = *gidIter;
880 const LO curLID = directoryMap_->getLocalElement (curGID);
883 "Map's global index " << curGID <<
" does not have a corresponding " 884 "local index. Please report this bug to the Tpetra developers.");
892 #endif // HAVE_TPETRA_DIRECTORY_SPARSE_MAP_FIX 903 const size_t numRecv = numEntries - numMissing;
909 oo != offset.
end(); ++oo, ++ii) {
916 for (
size_t i = 0; i < numRecv; ++i) {
918 const GO curGID =
static_cast<GO
> (*ptr++);
919 std::pair<IT, IT> p1 = std::equal_range (sortedIDs.
begin(), sortedIDs.
end(), curGID);
920 if (p1.first != p1.second) {
921 const size_t j = p1.first - sortedIDs.
begin();
923 nodeIDs[offset[j]] =
static_cast<int> (*ptr++);
926 localIDs[offset[j]] =
static_cast<LO
> (*ptr++);
928 if (nodeIDs[offset[j]] == -1) {
943 #define TPETRA_DIRECTORY_IMPL_INSTANT(LO,GO,NODE) \ 944 template class Directory< LO , GO , NODE >; \ 945 template class ReplicatedDirectory< LO , GO , NODE >; \ 946 template class DistributedContiguousDirectory< LO , GO , NODE >; \ 947 template class DistributedNoncontiguousDirectory< LO , GO , NODE >; \ 950 #endif // __Tpetra_DirectoryImpl_def_hpp Namespace Tpetra contains the class and methods constituting the Tpetra library.
LookupStatus getEntries(const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const
std::string description() const
A one-line human-readable description of this object.
bool isNodeGlobalElement(GlobalOrdinal globalIndex) const
Whether the given global index is valid for this Map on this process.
const Teuchos::RCP< const Teuchos::Comm< int > > & getComm() const
Accessors for the Teuchos::Comm and Kokkos Node objects.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Implementation of Directory for a distributed noncontiguous Map.
Implementation of Directory for a distributed contiguous Map.
GlobalOrdinal getMinAllGlobalIndex() const
The minimum global index over all processes in the communicator.
Implementation details of Tpetra.
size_t global_size_t
Global size_t object.
void doPostsAndWaits(const ArrayView< const Packet > &exports, size_t numPackets, const ArrayView< Packet > &imports)
Execute the (forward) communication plan.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
LookupStatus getEntriesImpl(const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const
Find process IDs and (optionally) local IDs for the given global IDs.
Sets up and executes a communication plan for a Tpetra DistObject.
size_t getTotalReceiveLength() const
Total number of values this process will receive from other processes.
Directory()
Empty constructor for post-constructor initialization.
LookupStatus getEntriesImpl(const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const
Find process IDs and (optionally) local IDs for the given global IDs.
LookupStatus getEntriesImpl(const Teuchos::ArrayView< const GlobalOrdinal > &globalIDs, const Teuchos::ArrayView< int > &nodeIDs, const Teuchos::ArrayView< LocalOrdinal > &localIDs, const bool computeLIDs) const
Find process IDs and (optionally) local IDs for the given global IDs.
std::vector< T >::const_iterator const_iterator
std::string description() const
A one-line human-readable description of this object.
LocalOrdinal getLocalElement(GlobalOrdinal globalIndex) const
The local index corresponding to the given global index.
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.
Computes the local ID and process ID corresponding to given global IDs.
std::string description() const
A one-line human-readable description of this object.
TypeTo as(const TypeFrom &t)
Teuchos::RCP< const map_type > getMap() const
Get the Map with which this object was created.
std::vector< T >::iterator iterator
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.
global_size_t getGlobalNumElements() const
The number of elements in this Map.
static std::string name()
std::string typeName(const T &t)
ReplicatedDirectory(const Teuchos::RCP< const map_type > &map)
Constructor.