50 #ifndef _ZOLTAN2_IDENTIFIERMAP_HPP_
51 #define _ZOLTAN2_IDENTIFIERMAP_HPP_
63 #include <Teuchos_as.hpp>
64 #include <Teuchos_CommHelpers.hpp>
105 template<
typename User>
126 const RCP<
const Comm<int> > &comm,
127 const ArrayRCP<const gid_t> &gids,
128 bool gidsMustBeConsecutive=
false);
192 ArrayView<gno_t> gno,
217 ArrayView<gno_t> gno,
240 ArrayView<gno_t> out_gno,
241 ArrayView<int> out_proc)
const;
262 ArrayView<gid_t> out_gid,
263 ArrayView<int> out_proc)
const;
268 const RCP<const Environment> env_;
272 const RCP<const Comm<int> > comm_;
276 const ArrayRCP<const gid_t> myGids_;
284 ArrayRCP<gno_t> gnoDist_;
289 RCP<GidLookupHelper<gid_t, lno_t > > gidLookup_;
292 size_t localNumberOfIds_;
299 bool userGidsAreTeuchosOrdinal_;
300 bool userGidsAreConsecutive_;
301 bool userGidsAreZoltan2Gnos_;
302 bool zoltan2GnosAreConsecutive_;
304 bool consecutiveGidsAreRequired_;
316 template<
typename User>
318 const RCP<
const Comm<int> > &comm,
319 const ArrayRCP<const gid_t> &gids,
bool idsMustBeConsecutive) :
320 env_(env), comm_(comm), myGids_(gids), gnoDist_(), gidLookup_(),
321 globalNumberOfIds_(0), localNumberOfIds_(0),
322 myRank_(comm_->getRank()), numProcs_(comm_->getSize()),
323 userGidsAreTeuchosOrdinal_(false), userGidsAreConsecutive_(false),
324 userGidsAreZoltan2Gnos_(false), zoltan2GnosAreConsecutive_(false),
325 consecutiveGidsAreRequired_(idsMustBeConsecutive),
326 minGlobalGno_(0), maxGlobalGno_(0)
328 env_->memory(
"Initial memory in use");
332 env_->memory(
"After user IDs mapped");
337 template<
typename User>
340 return userGidsAreZoltan2Gnos_;
343 template <
typename User>
350 template<
typename User>
353 return zoltan2GnosAreConsecutive_;
356 template<
typename User>
359 return consecutiveGidsAreRequired_;
362 template<
typename User>
365 return minGlobalGno_;
368 template<
typename User>
371 return maxGlobalGno_;
374 template<
typename User>
376 ArrayView<gid_t> gid,
377 ArrayView<gno_t> gno,
380 typename ArrayView<gid_t>::size_type inLen=gid.size();
386 env_->localInputAssertion(__FILE__, __LINE__,
"invalid TranslationType",
390 env_->localInputAssertion(__FILE__, __LINE__,
391 "Destination array is too small",
396 if (userGidsAreZoltan2Gnos_){
398 for (
typename ArrayView<gid_t>::size_type i=0; i < inLen; i++)
399 gid[i] = static_cast<gid_t>(gno[i]);
401 for (
typename ArrayView<gid_t>::size_type i=0; i < inLen; i++)
402 gno[i] = static_cast<gno_t>(gid[i]);
405 gno_t firstGno = gnoDist_[myRank_];
406 gno_t endGno = gnoDist_[myRank_ + 1];
409 for (
typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
411 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global number",
414 gid[i] = myGids_[gno[i] - firstGno];
419 if (userGidsAreConsecutive_){
420 for (
typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
423 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global id",
428 for (
typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
430 idx = gidLookup_->lookup(gid[i]);
432 catch (
const std::exception &e) {
433 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global id",
437 gno[i] = firstGno + idx;
445 template<
typename User>
447 ArrayView<lno_t> lno,
448 ArrayView<gno_t> gno,
451 typename ArrayView<lno_t>::size_type inLen=lno.size();
456 env_->localInputAssertion(__FILE__, __LINE__,
"invalid TranslationType",
460 env_->localInputAssertion(__FILE__, __LINE__,
461 "Destination array is too small",
466 gno_t firstGno(0), endGno(0);
467 if (gnoDist_.size() > 0){
468 firstGno = gnoDist_[myRank_];
469 endGno = gnoDist_[myRank_+1];
473 if (gnoDist_.size() > 0) {
474 for (
typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
475 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global number",
477 lno[i] = gno[i] - firstGno;
481 if (userGidsAreConsecutive_){
482 for (
typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
485 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global number",
486 (lno[i] >= 0) && (lno[i] <
lno_t(localNumberOfIds_)),
491 for (
typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
494 lno[i] = gidLookup_->lookup(keyArg);
496 catch (
const std::exception &e) {
497 env_->localInputAssertion(__FILE__, __LINE__,
505 for (
typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
508 if (gnoDist_.size() > 0)
509 gno[i] = firstGno + idx;
511 gno[i] =
static_cast<gno_t>(myGids_[idx]);
516 template<
typename User>
518 ArrayView<const gid_t> in_gid,
519 ArrayView<gno_t> out_gno,
520 ArrayView<int> out_proc)
const
522 typename ArrayView<const gid_t>::size_type inLen = in_gid.size();
525 const gid_t *gids = in_gid.getRawPtr();
526 gno_t *gnos = out_gno.getRawPtr();
527 int *pids = out_proc.getRawPtr();
529 bool needGnos =
false;
531 if (out_gno.size() > 0){
532 env_->localInputAssertion(__FILE__, __LINE__,
"array too small",
533 out_gno.size()>=
static_cast<typename ArrayView<gno_t>::size_type
>(inLen),
536 if (userGidsAreZoltan2Gnos_){
538 for (
typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++)
539 gnos[i] = static_cast<gno_t>(gids[i]);
545 bool needProcs =
false;
547 if (out_proc.size() > 0){
548 env_->localInputAssertion(__FILE__, __LINE__,
"array too small",
549 out_proc.size()>=
static_cast<typename ArrayView<int>::size_type
>(inLen),
552 if (userGidsAreZoltan2Gnos_ && (gnoDist_.size() > 0)){
557 gno_t *gnoDist = gnoDist_.getRawPtr();
558 gno_t *gnoEnd = gnoDist + numProcs_ + 1;
560 for (
typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++){
563 gno_t *ub = std::upper_bound(gnoDist, gnoEnd, gno);
565 if (ub == gnoDist || ub == gnoEnd)
568 pids[i] = (ub - gnoDist - 1);
575 int haveWork = ((needProcs || needGnos) ? 1 : 0);
576 int someHaveWork = 0;
578 Teuchos::reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
579 &haveWork, &someHaveWork);
592 Array<gid_t> gidOutBuf;
593 Array<gno_t> gnoOutBuf;
594 Array<int> countOutBuf(numProcs_, 0);
596 ArrayRCP<gid_t> gidInBuf;
597 ArrayRCP<gno_t> gnoInBuf;
598 Array<int> countInBuf(numProcs_, 0);
600 Array<gno_t> offsetBuf(numProcs_ + 1, 0);
602 if (localNumberOfIds_ > 0){
605 gidOutBuf.resize(localNumberOfIds_);
608 env_->localMemoryAssertion(__FILE__, __LINE__, localNumberOfIds_,
false);
611 for (
size_t i=0; i < localNumberOfIds_; i++){
613 countOutBuf[hashProc]++;
616 for (
int p=1; p <= numProcs_; p++){
617 offsetBuf[p] = offsetBuf[p-1] + countOutBuf[p-1];
623 gnoOutBuf.resize(localNumberOfIds_, 0);
626 for (
size_t i=0; i < localNumberOfIds_; i++){
628 gno_t offset = offsetBuf[hashProc];
629 gidOutBuf[offset] = myGids_[i];
631 gnoOutBuf[offset] = gnoDist_[myRank_] + i;
632 offsetBuf[hashProc] = offset + 1;
638 ArrayView<const gid_t> gidView = gidOutBuf();
639 ArrayView<const int> countView = countOutBuf();
641 AlltoAllv<gid_t>(*comm_, *env_, gidView, countView, gidInBuf, countInBuf());
648 ArrayView<const gno_t> gnoView = gnoOutBuf();
650 AlltoAllv<gno_t>(*comm_, *env_, gnoView, countView, gnoInBuf,
665 RCP<lookup_t> lookupMine = rcp(
new lookup_t(env_, gidInBuf));
669 std::vector<gno_t> firstIndex;
670 std::vector<int> sendProc;
671 gno_t indexTotal = 0;
674 for (
int p=0; p < numProcs_; p++){
675 int bufLen = countInBuf[p];
677 firstIndex.push_back(indexTotal);
678 sendProc.push_back(p);
679 indexTotal += bufLen;
682 firstIndex.push_back(indexTotal);
697 ArrayRCP<const gid_t> in_gidArray =
698 arcp(in_gid.getRawPtr(), 0, inLen,
false);
699 RCP<lookup_t> lookupRequested = rcp(
new lookup_t(env_, in_gidArray));
701 size_t numberOfUniqueGids = lookupRequested->size();
703 std::map<gid_t, lno_t> answerMap;
705 ArrayRCP<lno_t> uniqueIndices;
706 lookupRequested->getIndices(uniqueIndices);
708 countOutBuf.resize(numProcs_, 0);
710 if (numberOfUniqueGids > 0){
712 gidOutBuf.resize(numberOfUniqueGids);
715 env_->localMemoryAssertion(__FILE__, __LINE__, numberOfUniqueGids,
719 for (
size_t i=0; i < numberOfUniqueGids; i++){
720 gid_t gid = in_gid[uniqueIndices[i]];
722 countOutBuf[hashProc]++;
727 for (
int p=0; p < numProcs_; p++){
728 offsetBuf[p+1] = offsetBuf[p] + countOutBuf[p];
731 for (
size_t i=0; i < numberOfUniqueGids; i++){
732 gid_t gid = in_gid[uniqueIndices[i]];
734 gno_t loc = offsetBuf[hashProc];
736 answerMap[gid] = loc;
738 gidOutBuf[loc] = gid;
739 offsetBuf[hashProc] = loc + 1;
744 AlltoAllv<gid_t>(*comm_, *env_, gidOutBuf(), countOutBuf(),
745 gidInBuf, countInBuf());
756 for (
int p=0; p < numProcs_; p++){
757 countOutBuf[p] = countInBuf[p];
758 total += countOutBuf[p];
761 ArrayRCP<int> procOutBuf;
762 ArrayRCP<int> procInBuf;
765 int *tmp =
new int [total];
766 env_->localMemoryAssertion(__FILE__, __LINE__, total, tmp);
767 procOutBuf = arcp(tmp, 0, total,
true);
773 gnoOutBuf.resize(total, 0);
776 env_->localMemoryAssertion(__FILE__, __LINE__, total,
false);
783 typename std::vector<gno_t>::iterator indexFound;
785 for (
int p=0; p < numProcs_; p++){
786 for (
int i=0; i < countInBuf[p]; i++, total++){
792 index = lookupMine->lookup(gidInBuf[total]);
794 catch (std::exception &e){
798 env_->localBugAssertion(__FILE__, __LINE__,
"gidToIndex table",
805 upper_bound(firstIndex.begin(), firstIndex.end(), index);
806 int sendingProc = indexFound - firstIndex.begin() - 1;
807 procOutBuf[total] = sendProc[sendingProc];
811 gnoOutBuf[total] = gnoInBuf[index];
816 procOutBuf[total] = -1;
824 lookupMine.~RCP<lookup_t>();
828 AlltoAllv<int>(*comm_, *env_, procOutBuf.view(0, total), countOutBuf(),
829 procInBuf, countInBuf());
838 AlltoAllv<gno_t>(*comm_, *env_, gnoOutBuf(), countOutBuf(),
839 gnoInBuf, countInBuf());
852 for (
typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++){
853 lno_t loc = answerMap[in_gid[i]];
855 out_proc[i] = procInBuf[loc];
857 out_gno[i] = gnoInBuf[loc];
861 template<
typename User>
863 ArrayView<const gno_t> in_gno,
864 ArrayView<gid_t> out_gid,
865 ArrayView<int> out_proc)
const
867 typename ArrayView<const gno_t>::size_type inLen = in_gno.size();
869 if (userGidsAreZoltan2Gnos_){
873 const gno_t *gnos = in_gno.getRawPtr();
874 ArrayView<const gid_t> gids(static_cast<const gid_t *>(gnos), inLen);
875 ArrayView<gno_t> noGnos;
878 gidGlobalTranslate(gids, noGnos, out_proc);
882 if (out_gid.size() ==
883 static_cast<typename ArrayView<gid_t>::size_type
>(inLen))
884 for (
typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++)
885 out_gid[i] = gids[i];
890 bool needProcs = out_proc.size() > 0;
891 bool needGids = out_gid.size() > 0;
893 int haveWork = ((needProcs || needGids) ? 1 : 0);
894 int someHaveWork = 0;
896 Teuchos::reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
897 &haveWork, &someHaveWork);
902 env_->localInputAssertion(__FILE__, __LINE__,
"array too small",
904 out_proc.size()>=
static_cast<typename ArrayView<int>::size_type
>(inLen))
907 out_gid.size()>=
static_cast<typename ArrayView<gid_t>::size_type
>(inLen)),
912 ArrayRCP<int> procArray;
914 if (out_proc.size() ==
static_cast<typename ArrayView<int>::size_type
>(inLen))
915 procArray = arcp(out_proc.getRawPtr(), 0, inLen,
false);
917 int *tmp =
new int [inLen];
918 env_->localMemoryAssertion(__FILE__, __LINE__, inLen, tmp);
919 procArray = arcp(tmp, 0, inLen,
true);
926 gno_t *gnos = gnoDist_.getRawPtr();
927 gno_t *
final = gnos + numProcs_ + 1;
928 bool remoteGno =
false;
929 int *pids = procArray.getRawPtr();
931 for (
typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
933 gno_t *ub = std::upper_bound(gnos,
final, in_gno[i]);
935 pids[i] = (ub - gnos - 1);
937 env_->localInputAssertion(__FILE__, __LINE__,
"invalid global number",
940 if (!remoteGno && (pids[i] != myRank_))
951 gidTranslate(out_gid,
952 ArrayView<gno_t>(const_cast<gno_t *>(in_gno.getRawPtr()),inLen),
968 ArrayRCP<gno_t> gnoOutBuf;
969 ArrayRCP<gno_t> offsetBuf;
970 gno_t *tmpGno = NULL;
973 tmpGno =
new gno_t [inLen];
974 env_->localMemoryAssertion(__FILE__, __LINE__, inLen, tmpGno);
975 gnoOutBuf = arcp(tmpGno, 0, inLen,
true);
978 int *tmpCount =
new int [numProcs_];
979 env_->localMemoryAssertion(__FILE__, __LINE__, numProcs_, tmpCount);
980 memset(tmpCount, 0,
sizeof(
int) * numProcs_);
981 ArrayRCP<int> countOutBuf(tmpCount, 0, numProcs_,
true);
984 env_->localMemoryAssertion(__FILE__, __LINE__, numProcs_+1, tmpOff);
985 offsetBuf = arcp(tmpOff, 0, numProcs_+1,
true);
987 for (
typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++)
991 for (
int i=0; i < numProcs_; i++)
992 tmpOff[i+1] = tmpOff[i] + tmpCount[i];
994 for (
typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
996 gno_t off = tmpOff[p];
997 tmpGno[off] = in_gno[i];
1004 for (
int i=0; i < numProcs_; i++)
1005 tmpOff[i+1] = tmpOff[i] + tmpCount[i];
1007 ArrayRCP<gno_t> gnoInBuf;
1008 Array<int> countInBuf(numProcs_, 0);
1011 AlltoAllv<gno_t>(*comm_, *env_,
1012 gnoOutBuf.view(0, inLen), countOutBuf.view(0, numProcs_),
1013 gnoInBuf, countInBuf());
1018 countOutBuf.clear();
1020 gno_t numRequests = 0;
1021 for (
int i=0; i < numProcs_; i++)
1022 numRequests += countInBuf[i];
1024 ArrayRCP<gid_t> gidQueryBuf;
1028 env_->localMemoryAssertion(__FILE__, __LINE__, numRequests, tmpGid);
1029 gidQueryBuf = arcp(tmpGid, 0, numRequests);
1033 gidTranslate(gidQueryBuf.view(0, numRequests),
1034 gnoInBuf.view(0, numRequests),
1039 ArrayRCP<gid_t> gidInBuf;
1040 Array<lno_t> newCountInBuf(numProcs_, 0);
1043 AlltoAllv<gid_t>(*comm_, *env_,
1044 gidQueryBuf.view(0, numRequests), countInBuf(),
1045 gidInBuf, newCountInBuf());
1049 gidQueryBuf.clear();
1053 gid_t *gidInfo = gidInBuf.getRawPtr();
1055 for (
typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
1057 gno_t off = tmpOff[p];
1058 out_gid[i] = gidInfo[off];
1063 template<
typename User>
1066 env_->globalInputAssertion(__FILE__, __LINE__,
1067 "application global ID type is not supported yet",
1072 userGidsAreTeuchosOrdinal_ =
true;
1074 localNumberOfIds_ = myGids_.size();
1075 const gid_t *gidPtr = myGids_.get();
1076 ArrayRCP<gid_t> tmpDist;
1077 gid_t mingid=0, maxgid=0;
1079 userGidsAreConsecutive_ = globallyConsecutiveOrdinals<gid_t>(
1080 *comm_, *env_, gidPtr, localNumberOfIds_,
1081 tmpDist, globalNumberOfIds_);
1083 bool baseZeroConsecutiveIds =
false;
1085 if (userGidsAreTeuchosOrdinal_){
1086 if (!userGidsAreConsecutive_){
1087 mingid = tmpDist[0];
1088 maxgid = tmpDist[1];
1092 if (
sizeof(gid_t) ==
sizeof(
gno_t)) {
1093 gnoDist_ = arcp_reinterpret_cast<
gno_t>(tmpDist);
1096 gnoDist_.resize(numProcs_ + 1);
1097 for (
lno_t i=0; i <= numProcs_; i++){
1098 gnoDist_[i] =
static_cast<gno_t>(tmpDist[i]);
1102 if (gnoDist_[0] == 0)
1103 baseZeroConsecutiveIds =
true;
1110 typedef GidLookupHelper<gid_t, lno_t> lookup_t;
1112 if (!userGidsAreConsecutive_){
1114 gidLookup_ = rcp(
new lookup_t(env_, myGids_));
1119 userGidsAreZoltan2Gnos_ =
false;
1121 if (userGidsAreTeuchosOrdinal_ &&
1122 ((consecutiveGidsAreRequired_ && baseZeroConsecutiveIds) ||
1123 !consecutiveGidsAreRequired_))
1126 userGidsAreZoltan2Gnos_ =
true;
1129 if (userGidsAreZoltan2Gnos_){
1131 zoltan2GnosAreConsecutive_ = userGidsAreConsecutive_;
1133 if (userGidsAreConsecutive_){
1134 minGlobalGno_ = gnoDist_[0];
1135 maxGlobalGno_ = gnoDist_[numProcs_]-1;
1138 minGlobalGno_ =
static_cast<gno_t>(mingid);
1139 maxGlobalGno_ =
static_cast<gno_t>(maxgid);
1144 zoltan2GnosAreConsecutive_ =
true;
1147 gnoDist_.resize(numProcs_ + 1, 0);
1151 gno_t myNum =
static_cast<gno_t>(localNumberOfIds_);
1154 gno_t *p = gnoDist_.getRawPtr();
1155 Teuchos::gatherAll<int, gno_t>(*comm_, 1, &myNum, numProcs_, p+1);
1159 for (
int i=2; i <= numProcs_; i++){
1160 gnoDist_[i] += gnoDist_[i-1];
1163 minGlobalGno_ = gnoDist_[0];
1164 maxGlobalGno_ = gnoDist_[numProcs_]-1;
An IdentifierMap manages a global space of object identifiers.
bool consecutiveGnosAreRequired() const
Return true if consecutive Gids are required.
bool gnosAreConsecutive() const
Return true if our internal global numbers are consecutive.
Defines GidLookupHelper class.
InputTraits< User >::gid_t gid_t
void gidGlobalTranslate(ArrayView< const gid_t > in_gid, ArrayView< gno_t > out_gno, ArrayView< int > out_proc) const
Map application global IDs to internal global numbers and/or process owners.
fast typical checks for valid arguments
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
TranslationType
Identifier translations available from IdentifierMap.
convert internal ids to user ids
IdentifierMap & operator=(const IdentifierMap &id)
Assignment operator.
static int hashCode(const T id)
Compute an integer hash code for the user's global ID.
Defines basic traits for user global identifiers.
InputTraits< User >::lno_t lno_t
Constructor - Must be called by all processes.
The functions to be defined for users' global ID types.
convert user ids to internal ids
void getGnoRange(gno_t &min, gno_t &max) const
Return the minimum and maximum values of the internal global numbers.
void gidTranslate(ArrayView< gid_t > gid, ArrayView< gno_t > gno, TranslationType tt) const
Map the application global IDs to internal global numbers or vice versa.
gno_t getMinimumGlobalId() const
Return the minimum Zoltan2 global Id across all processes.
void lnoTranslate(ArrayView< lno_t > lno, ArrayView< gno_t > gno, TranslationType tt) const
Map application indices to internal global numbers or vice versa.
gno_t getMaximumGlobalId() const
Return the maximum Zoltan2 global Id across all processes.
gno_t getGlobalNumberOfIds() const
Return the global number of identifiers.
bool gnosAreGids() const
Return true if we are using the application global IDs for our internal global numbers.
IdentifierMap(const RCP< const Environment > &env, const RCP< const Comm< int > > &comm, const ArrayRCP< const gid_t > &gids, bool gidsMustBeConsecutive=false)
~IdentifierMap()
Destructor.
gno_t getLocalNumberOfIds() const
Return the local number of identifiers.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
A class to do GID lookups.
void gnoGlobalTranslate(ArrayView< const gno_t > in_gno, ArrayView< gid_t > out_gid, ArrayView< int > out_proc) const
Map library internal global numbers to application GIDs, and their process owners.
AlltoAll communication methods.
InputTraits< User >::gno_t gno_t
static T difference(const T a, const T b)
Compute b - a, if possible.