Zoltan2  Version of the Day
Zoltan2_IdentifierMap.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #ifndef _ZOLTAN2_IDENTIFIERMAP_HPP_
51 #define _ZOLTAN2_IDENTIFIERMAP_HPP_
52 
54 #include <Zoltan2_InputTraits.hpp>
55 #include <Zoltan2_AlltoAll.hpp>
57 
58 #include <vector>
59 #include <algorithm>
60 #include <map>
61 #include <iostream>
62 
63 #include <Teuchos_as.hpp>
64 #include <Teuchos_CommHelpers.hpp>
65 
66 namespace Zoltan2
67 {
74 };
75 
101 // Declarations
104 
105 template<typename User>
107 
108 public:
109 
121  typedef typename InputTraits<User>::lno_t lno_t;
122  typedef typename InputTraits<User>::gno_t gno_t;
123  typedef typename InputTraits<User>::gid_t gid_t;
124 
125  explicit IdentifierMap( const RCP<const Environment > &env,
126  const RCP<const Comm<int> > &comm,
127  const ArrayRCP<const gid_t> &gids,
128  bool gidsMustBeConsecutive=false);
129 
132 
134  IdentifierMap(const IdentifierMap &id);
135 
138 
142  bool gnosAreGids() const;
143 
146  gno_t getGlobalNumberOfIds() const { return globalNumberOfIds_;}
147 
150  gno_t getLocalNumberOfIds() const { return localNumberOfIds_;}
151 
155  void getGnoRange(gno_t &min, gno_t &max) const;
156 
159  bool gnosAreConsecutive() const;
160 
163  bool consecutiveGnosAreRequired() const;
164 
167  gno_t getMinimumGlobalId() const;
168 
171  gno_t getMaximumGlobalId() const;
172 
191  void gidTranslate(ArrayView<gid_t> gid,
192  ArrayView<gno_t> gno,
193  TranslationType tt) const;
194 
216  void lnoTranslate(ArrayView<lno_t> lno,
217  ArrayView<gno_t> gno,
218  TranslationType tt) const;
219 
239  void gidGlobalTranslate( ArrayView<const gid_t> in_gid,
240  ArrayView<gno_t> out_gno,
241  ArrayView<int> out_proc) const;
242 
261  void gnoGlobalTranslate( ArrayView<const gno_t> in_gno,
262  ArrayView<gid_t> out_gid,
263  ArrayView<int> out_proc) const;
264 private:
265 
266  // Problem parameters, library configuration.
267 
268  const RCP<const Environment> env_;
269 
270  // Problem communicator
271 
272  const RCP<const Comm<int> > comm_;
273 
274  // Application global IDs
275 
276  const ArrayRCP<const gid_t> myGids_;
277 
278  // Zoltan2 gno_ts will be consecutive if the application gid_ts
279  // were mapped to gno_ts, or if the application gid_ts happen
280  // to be consecutive ordinals. In either case, gnoDist_[p]
281  // is the first gno_t on process p. gnoDist_[numProcs_] is the
282  // global total of gno_ts.
283 
284  ArrayRCP<gno_t> gnoDist_;
285 
286  // If application gids are not consecutive ordinals, the gidLookup_
287  // object allows lookup of the location of the gid_t in the myGids_ list.
288 
289  RCP<GidLookupHelper<gid_t, lno_t > > gidLookup_;
290 
291  global_size_t globalNumberOfIds_;
292  size_t localNumberOfIds_;
293  int myRank_;
294  int numProcs_;
295 
296  // By "Consecutive" we mean globally consecutive increasing
297  // with process rank.
298 
299  bool userGidsAreTeuchosOrdinal_;
300  bool userGidsAreConsecutive_;
301  bool userGidsAreZoltan2Gnos_;
302  bool zoltan2GnosAreConsecutive_;
303 
304  bool consecutiveGidsAreRequired_;
305 
306  gno_t minGlobalGno_;
307  gno_t maxGlobalGno_;
308 
309  void setupMap();
310 };
311 
313 // Definitions
315 
316 template<typename User>
317  IdentifierMap<User>::IdentifierMap( const RCP<const Environment> &env,
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)
327 {
328  env_->memory("Initial memory in use");
329 
330  setupMap();
331 
332  env_->memory("After user IDs mapped");
333 }
334 
335 // TODO many of these should be inline
336 
337 template< typename User>
339 {
340  return userGidsAreZoltan2Gnos_;
341 }
342 
343 template <typename User>
345 {
346  min = minGlobalGno_;
347  max = maxGlobalGno_;
348 }
349 
350 template< typename User>
352 {
353  return zoltan2GnosAreConsecutive_;
354 }
355 
356 template< typename User>
358 {
359  return consecutiveGidsAreRequired_;
360 }
361 
362 template< typename User>
364 {
365  return minGlobalGno_;
366 }
367 
368 template< typename User>
370 {
371  return maxGlobalGno_;
372 }
373 
374 template< typename User>
376  ArrayView<gid_t> gid,
377  ArrayView<gno_t> gno,
378  TranslationType tt) const
379 {
380  typename ArrayView<gid_t>::size_type inLen=gid.size();
381 
382  if (inLen == 0){
383  return;
384  }
385 
386  env_->localInputAssertion(__FILE__, __LINE__, "invalid TranslationType",
389 
390  env_->localInputAssertion(__FILE__, __LINE__,
391  "Destination array is too small",
392  ((tt==TRANSLATE_LIB_TO_APP) && (gid.size() >= gno.size())) ||
393  ((tt==TRANSLATE_APP_TO_LIB) && (gno.size() >= gid.size())),
395 
396  if (userGidsAreZoltan2Gnos_){ // our gnos are the app gids
397  if (tt == TRANSLATE_LIB_TO_APP)
398  for (typename ArrayView<gid_t>::size_type i=0; i < inLen; i++)
399  gid[i] = static_cast<gid_t>(gno[i]);
400  else
401  for (typename ArrayView<gid_t>::size_type i=0; i < inLen; i++)
402  gno[i] = static_cast<gno_t>(gid[i]);
403  }
404  else{ // we mapped gids to consecutive gnos
405  gno_t firstGno = gnoDist_[myRank_];
406  gno_t endGno = gnoDist_[myRank_ + 1];
407 
408  if (tt == TRANSLATE_LIB_TO_APP){
409  for (typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
410 
411  env_->localInputAssertion(__FILE__, __LINE__, "invalid global number",
412  (gno[i] >= firstGno) && (gno[i] < endGno), BASIC_ASSERTION);
413 
414  gid[i] = myGids_[gno[i] - firstGno];
415  }
416  }
417  else{
418  lno_t idx=0;
419  if (userGidsAreConsecutive_){
420  for (typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
421  gno[i] = firstGno + IdentifierTraits<gid_t>::difference(
422  myGids_[0], gid[i]);
423  env_->localInputAssertion(__FILE__, __LINE__, "invalid global id",
424  (gno[i] >= firstGno) && (gno[i] < endGno), BASIC_ASSERTION);
425  }
426  }
427  else{
428  for (typename ArrayView<gid_t>::size_type i=0; i < inLen; i++){
429  try{
430  idx = gidLookup_->lookup(gid[i]);
431  }
432  catch (const std::exception &e) {
433  env_->localInputAssertion(__FILE__, __LINE__, "invalid global id",
434  false, BASIC_ASSERTION);
435  }
436 
437  gno[i] = firstGno + idx;
438  }
439  }
440  }
441  }
442  return;
443 }
444 
445 template< typename User>
447  ArrayView<lno_t> lno,
448  ArrayView<gno_t> gno,
449  TranslationType tt) const
450 {
451  typename ArrayView<lno_t>::size_type inLen=lno.size();
452 
453  if (inLen == 0){
454  return;
455  }
456  env_->localInputAssertion(__FILE__, __LINE__, "invalid TranslationType",
459 
460  env_->localInputAssertion(__FILE__, __LINE__,
461  "Destination array is too small",
462  ((tt==TRANSLATE_LIB_TO_APP) && (lno.size() >= gno.size())) ||
463  ((tt==TRANSLATE_APP_TO_LIB) && (gno.size() >= lno.size())),
465 
466  gno_t firstGno(0), endGno(0);
467  if (gnoDist_.size() > 0){
468  firstGno = gnoDist_[myRank_];
469  endGno = gnoDist_[myRank_+1];
470  }
471 
472  if (tt == TRANSLATE_LIB_TO_APP){
473  if (gnoDist_.size() > 0) { // gnos are consecutive
474  for (typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
475  env_->localInputAssertion(__FILE__, __LINE__, "invalid global number",
476  (gno[i] >= firstGno) && (gno[i] < endGno), BASIC_ASSERTION);
477  lno[i] = gno[i] - firstGno;
478  }
479  }
480  else { // gnos must be the app gids
481  if (userGidsAreConsecutive_){
482  for (typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
483  gid_t tmp = static_cast<gid_t>(gno[i]);
484  lno[i] = IdentifierTraits<gid_t>::difference(myGids_[0], tmp);
485  env_->localInputAssertion(__FILE__, __LINE__, "invalid global number",
486  (lno[i] >= 0) && (lno[i] < lno_t(localNumberOfIds_)),
488  }
489  }
490  else{
491  for (typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
492  try{
493  gid_t keyArg = static_cast<gid_t>(gno[i]);
494  lno[i] = gidLookup_->lookup(keyArg);
495  }
496  catch (const std::exception &e) {
497  env_->localInputAssertion(__FILE__, __LINE__,
498  "invalid global number", false, BASIC_ASSERTION);
499  }
500  }
501  }
502  }
503  }
504  else{ // TRANSLATE_APP_TO_LIB
505  for (typename ArrayView<lno_t>::size_type i=0; i < inLen; i++){
506  lno_t idx = lno[i];
507 
508  if (gnoDist_.size() > 0) // gnos are consecutive
509  gno[i] = firstGno + idx;
510  else // gnos must be the app gids
511  gno[i] = static_cast<gno_t>(myGids_[idx]);
512  }
513  }
514 }
515 
516 template< typename User>
518  ArrayView<const gid_t> in_gid,
519  ArrayView<gno_t> out_gno,
520  ArrayView<int> out_proc) const
521 {
522  typename ArrayView<const gid_t>::size_type inLen = in_gid.size();
523 
524  // It's faster in loops to user raw pointers.
525  const gid_t *gids = in_gid.getRawPtr();
526  gno_t *gnos = out_gno.getRawPtr();
527  int *pids = out_proc.getRawPtr();
528 
529  bool needGnos = false;
530 
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),
535 
536  if (userGidsAreZoltan2Gnos_){
537  // Global numbers are the application global IDs
538  for (typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++)
539  gnos[i] = static_cast<gno_t>(gids[i]);
540  }
541  else
542  needGnos = true;
543  }
544 
545  bool needProcs = false;
546 
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),
551 
552  if (userGidsAreZoltan2Gnos_ && (gnoDist_.size() > 0)){
553 
554  // The gids are the gnos, which are consecutive
555  // increasing with rank.
556 
557  gno_t *gnoDist = gnoDist_.getRawPtr();
558  gno_t *gnoEnd = gnoDist + numProcs_ + 1;
559 
560  for (typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++){
561  gno_t gno = static_cast<gno_t>(gids[i]);
562  // pointer to first entry greater than gno
563  gno_t *ub = std::upper_bound(gnoDist, gnoEnd, gno);
564 
565  if (ub == gnoDist || ub == gnoEnd)
566  pids[i] = -1; // globally not one of our gids
567  else
568  pids[i] = (ub - gnoDist - 1);
569  }
570  }
571  else
572  needProcs = true;
573  }
574 
575  int haveWork = ((needProcs || needGnos) ? 1 : 0);
576  int someHaveWork = 0;
577 
578  Teuchos::reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
579  &haveWork, &someHaveWork);
580 
581  if (!someHaveWork)
582  return;
583 
585  // First: Hash each of my local gids to a process that will answer
586  // for it. Send my gids (and the Gnos if they are different)
587  // to their assigned processes. Build a lookup object for
588  // the gids that were assigned to me, so I can reply with
589  // with the process owning them (and their Gnos if they are different).
591 
592  Array<gid_t> gidOutBuf;
593  Array<gno_t> gnoOutBuf;
594  Array<int> countOutBuf(numProcs_, 0);
595 
596  ArrayRCP<gid_t> gidInBuf;
597  ArrayRCP<gno_t> gnoInBuf;
598  Array<int> countInBuf(numProcs_, 0);
599 
600  Array<gno_t> offsetBuf(numProcs_ + 1, 0);
601 
602  if (localNumberOfIds_ > 0){
603 
604  try{
605  gidOutBuf.resize(localNumberOfIds_);
606  }
607  catch(...){
608  env_->localMemoryAssertion(__FILE__, __LINE__, localNumberOfIds_, false);
609  }
610 
611  for (size_t i=0; i < localNumberOfIds_; i++){
612  int hashProc = IdentifierTraits<gid_t>::hashCode(myGids_[i]) % numProcs_;
613  countOutBuf[hashProc]++;
614  }
615 
616  for (int p=1; p <= numProcs_; p++){
617  offsetBuf[p] = offsetBuf[p-1] + countOutBuf[p-1];
618  }
619 
620  if (needGnos){
621  // The gnos are not the gids, which also implies that
622  // gnos are consecutive numbers given by gnoDist_.
623  gnoOutBuf.resize(localNumberOfIds_, 0);
624  }
625 
626  for (size_t i=0; i < localNumberOfIds_; i++){
627  int hashProc = IdentifierTraits<gid_t>::hashCode(myGids_[i]) % numProcs_;
628  gno_t offset = offsetBuf[hashProc];
629  gidOutBuf[offset] = myGids_[i];
630  if (needGnos)
631  gnoOutBuf[offset] = gnoDist_[myRank_] + i;
632  offsetBuf[hashProc] = offset + 1;
633  }
634  }
635 
636  // Z2::AlltoAllv comment: Buffers are in process rank contiguous order.
637 
638  ArrayView<const gid_t> gidView = gidOutBuf();
639  ArrayView<const int> countView = countOutBuf();
640  try{
641  AlltoAllv<gid_t>(*comm_, *env_, gidView, countView, gidInBuf, countInBuf());
642  }
644 
645  gidOutBuf.clear();
646 
647  if (needGnos){
648  ArrayView<const gno_t> gnoView = gnoOutBuf();
649  try{
650  AlltoAllv<gno_t>(*comm_, *env_, gnoView, countView, gnoInBuf,
651  countInBuf());
652  }
654  }
655 
656  gnoOutBuf.clear();
657  countOutBuf.clear();
658 
659  // Save the information that was hashed to me so I can do lookups.
660  // This is a lookup from gid to its position in gidInBuf.
661  // (The list of gids hashed to me is unique.)
662 
663  typedef GidLookupHelper<gid_t, lno_t> lookup_t;
664 
665  RCP<lookup_t> lookupMine = rcp(new lookup_t(env_, gidInBuf));
666 
667  // Use a vector to find process that sent gid.
668 
669  std::vector<gno_t> firstIndex;
670  std::vector<int> sendProc;
671  gno_t indexTotal = 0;
672 
673  if (needProcs){
674  for (int p=0; p < numProcs_; p++){
675  int bufLen = countInBuf[p];
676  if (bufLen > 0){
677  firstIndex.push_back(indexTotal);
678  sendProc.push_back(p);
679  indexTotal += bufLen;
680  }
681  }
682  firstIndex.push_back(indexTotal);
683  }
684 
686  // Send a request for information to the "answer process" for each
687  // of the unique gids in in_gid.
688  //
689  // We may be called by a model that wants to find out what
690  // process owns neighbors or columns. Such a list of gids is
691  // likely to have many duplicates.
692  //
693  // It is possible that some of the gids do not belong to any process.
694  // (This happens in graph subsetting.)
696 
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));
700 
701  size_t numberOfUniqueGids = lookupRequested->size();
702 
703  std::map<gid_t, lno_t> answerMap;// map from gid to position of answer
704 
705  ArrayRCP<lno_t> uniqueIndices;
706  lookupRequested->getIndices(uniqueIndices);
707 
708  countOutBuf.resize(numProcs_, 0);
709 
710  if (numberOfUniqueGids > 0){
711  try{
712  gidOutBuf.resize(numberOfUniqueGids);
713  }
714  catch(...){
715  env_->localMemoryAssertion(__FILE__, __LINE__, numberOfUniqueGids,
716  false);
717  }
718 
719  for (size_t i=0; i < numberOfUniqueGids; i++){
720  gid_t gid = in_gid[uniqueIndices[i]];
721  int hashProc = IdentifierTraits<gid_t>::hashCode(gid) % numProcs_;
722  countOutBuf[hashProc]++;
723  }
724 
725  offsetBuf[0] = 0;
726 
727  for (int p=0; p < numProcs_; p++){
728  offsetBuf[p+1] = offsetBuf[p] + countOutBuf[p];
729  }
730 
731  for (size_t i=0; i < numberOfUniqueGids; i++){
732  gid_t gid = in_gid[uniqueIndices[i]];
733  int hashProc = IdentifierTraits<gid_t>::hashCode(gid) % numProcs_;
734  gno_t loc = offsetBuf[hashProc];
735 
736  answerMap[gid] = loc;
737 
738  gidOutBuf[loc] = gid;
739  offsetBuf[hashProc] = loc + 1;
740  }
741  }
742 
743  try{
744  AlltoAllv<gid_t>(*comm_, *env_, gidOutBuf(), countOutBuf(),
745  gidInBuf, countInBuf());
746  }
748 
749  gidOutBuf.clear();
750 
752  // Create and send answers to the processes that made requests of me.
754 
755  gno_t total = 0;
756  for (int p=0; p < numProcs_; p++){
757  countOutBuf[p] = countInBuf[p];
758  total += countOutBuf[p];
759  }
760 
761  ArrayRCP<int> procOutBuf;
762  ArrayRCP<int> procInBuf;
763 
764  if (needProcs){
765  int *tmp = new int [total];
766  env_->localMemoryAssertion(__FILE__, __LINE__, total, tmp);
767  procOutBuf = arcp(tmp, 0, total, true);
768 
769  }
770 
771  if (needGnos){
772  try{
773  gnoOutBuf.resize(total, 0);
774  }
775  catch(...){
776  env_->localMemoryAssertion(__FILE__, __LINE__, total, false);
777  }
778  }
779 
780  if (total > 0){
781 
782  total=0;
783  typename std::vector<gno_t>::iterator indexFound;
784 
785  for (int p=0; p < numProcs_; p++){
786  for (int i=0; i < countInBuf[p]; i++, total++){
787 
788  lno_t index(0);
789  bool badGid = false;
790 
791  try{
792  index = lookupMine->lookup(gidInBuf[total]);
793  }
794  catch (std::exception &e){
795  badGid = true;
796  }
797 
798  env_->localBugAssertion(__FILE__, __LINE__, "gidToIndex table",
799  badGid || ((index >= 0)&&(index<=indexTotal)), BASIC_ASSERTION);
800 
801  if (!badGid){
802 
803  if (needProcs){
804  indexFound =
805  upper_bound(firstIndex.begin(), firstIndex.end(), index);
806  int sendingProc = indexFound - firstIndex.begin() - 1;
807  procOutBuf[total] = sendProc[sendingProc];
808  }
809 
810  if (needGnos){
811  gnoOutBuf[total] = gnoInBuf[index];
812  }
813  }
814  else if (needProcs){
815  // globally not one of our gids, can happen in subsetting
816  procOutBuf[total] = -1;
817  }
818  }
819  }
820  }
821 
822  gidInBuf.clear();
823  gnoInBuf.clear();
824  lookupMine.~RCP<lookup_t>();
825 
826  if (needProcs){
827  try{
828  AlltoAllv<int>(*comm_, *env_, procOutBuf.view(0, total), countOutBuf(),
829  procInBuf, countInBuf());
830  }
832 
833  procOutBuf.clear();
834  }
835 
836  if (needGnos){
837  try{
838  AlltoAllv<gno_t>(*comm_, *env_, gnoOutBuf(), countOutBuf(),
839  gnoInBuf, countInBuf());
840  }
842 
843  gnoOutBuf.clear();
844  }
845 
846  countOutBuf.clear();
847 
849  // Done. Process the replies to my queries
851 
852  for (typename ArrayView<const gid_t>::size_type i=0; i < inLen; i++){
853  lno_t loc = answerMap[in_gid[i]];
854  if (needProcs)
855  out_proc[i] = procInBuf[loc];
856  if (needGnos)
857  out_gno[i] = gnoInBuf[loc];
858  }
859 }
860 
861 template< typename User>
863  ArrayView<const gno_t> in_gno,
864  ArrayView<gid_t> out_gid,
865  ArrayView<int> out_proc) const
866 {
867  typename ArrayView<const gno_t>::size_type inLen = in_gno.size();
868 
869  if (userGidsAreZoltan2Gnos_){
870 
871  // Easy case - use gidGlobalTranslate.
872 
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;
876 
877  try{
878  gidGlobalTranslate(gids, noGnos, out_proc);
879  }
881 
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];
886 
887  return;
888  }
889 
890  bool needProcs = out_proc.size() > 0;
891  bool needGids = out_gid.size() > 0;
892 
893  int haveWork = ((needProcs || needGids) ? 1 : 0);
894  int someHaveWork = 0;
895 
896  Teuchos::reduceAll<int, int>(*comm_, Teuchos::REDUCE_MAX, 1,
897  &haveWork, &someHaveWork);
898 
899  if (!someHaveWork)
900  return;
901 
902  env_->localInputAssertion(__FILE__, __LINE__, "array too small",
903  (!needProcs ||
904  out_proc.size()>=static_cast<typename ArrayView<int>::size_type>(inLen))
905  &&
906  (!needGids ||
907  out_gid.size()>=static_cast<typename ArrayView<gid_t>::size_type>(inLen)),
909 
910  // Get process owning each gno. We need it to get the gid.
911 
912  ArrayRCP<int> procArray;
913 
914  if (out_proc.size() == static_cast<typename ArrayView<int>::size_type>(inLen))
915  procArray = arcp(out_proc.getRawPtr(), 0, inLen, false);
916  else{
917  int *tmp = new int [inLen];
918  env_->localMemoryAssertion(__FILE__, __LINE__, inLen, tmp);
919  procArray = arcp(tmp, 0, inLen, true);
920  }
921 
922  // The fact that global Ids and global Numbers are different
923  // means that the user's global Ids were mapped to consecutive increasing
924  // global numbers, so we know the process owning each global number.
925 
926  gno_t *gnos = gnoDist_.getRawPtr();
927  gno_t *final = gnos + numProcs_ + 1;
928  bool remoteGno = false;
929  int *pids = procArray.getRawPtr(); // in loops it's faster to use raw ptr
930 
931  for (typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
932 
933  gno_t *ub = std::upper_bound(gnos, final, in_gno[i]);
934 
935  pids[i] = (ub - gnos - 1);
936 
937  env_->localInputAssertion(__FILE__, __LINE__, "invalid global number",
938  (pids[i] >= 0) && (pids[i] < env_->numProcs_), BASIC_ASSERTION);
939 
940  if (!remoteGno && (pids[i] != myRank_))
941  remoteGno = true;
942  }
943 
944  if (!needGids)
945  return;
946 
947  if (!remoteGno){
948 
949  // Make a local call to get the gids
950  try{
951  gidTranslate(out_gid,
952  ArrayView<gno_t>(const_cast<gno_t *>(in_gno.getRawPtr()),inLen),
954 
955  }
957 
958  return;
959  }
960 
961  // Get the global ID from the owner.
962  // TODO: In gidGlobalTranslate, we go to much trouble to
963  // avoid duplicate requests. (Because that may be used
964  // to identify owners of neighbors, which would likely
965  // include many duplicates.) Any reason to go to this
966  // trouble here? Could the list of gnos have many duplicates?
967 
968  ArrayRCP<gno_t> gnoOutBuf;
969  ArrayRCP<gno_t> offsetBuf;
970  gno_t *tmpGno = NULL;
971 
972  if (inLen){
973  tmpGno = new gno_t [inLen];
974  env_->localMemoryAssertion(__FILE__, __LINE__, inLen, tmpGno);
975  gnoOutBuf = arcp(tmpGno, 0, inLen, true);
976  }
977 
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);
982 
983  gno_t *tmpOff = new gno_t [numProcs_+1];
984  env_->localMemoryAssertion(__FILE__, __LINE__, numProcs_+1, tmpOff);
985  offsetBuf = arcp(tmpOff, 0, numProcs_+1, true);
986 
987  for (typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++)
988  tmpCount[pids[i]]++;
989 
990  offsetBuf[0] = 0;
991  for (int i=0; i < numProcs_; i++)
992  tmpOff[i+1] = tmpOff[i] + tmpCount[i];
993 
994  for (typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
995  int p = pids[i];
996  gno_t off = tmpOff[p];
997  tmpGno[off] = in_gno[i];
998  tmpOff[p]++;
999  }
1000 
1001  // Reset offsetBuf so we can find replies later.
1002 
1003  tmpOff[0] = 0;
1004  for (int i=0; i < numProcs_; i++)
1005  tmpOff[i+1] = tmpOff[i] + tmpCount[i];
1006 
1007  ArrayRCP<gno_t> gnoInBuf;
1008  Array<int> countInBuf(numProcs_, 0);
1009 
1010  try{
1011  AlltoAllv<gno_t>(*comm_, *env_,
1012  gnoOutBuf.view(0, inLen), countOutBuf.view(0, numProcs_),
1013  gnoInBuf, countInBuf());
1014  }
1016 
1017  gnoOutBuf.clear();
1018  countOutBuf.clear();
1019 
1020  gno_t numRequests = 0;
1021  for (int i=0; i < numProcs_; i++)
1022  numRequests += countInBuf[i];
1023 
1024  ArrayRCP<gid_t> gidQueryBuf;
1025 
1026  if (numRequests){
1027  gid_t *tmpGid = new gid_t [numRequests];
1028  env_->localMemoryAssertion(__FILE__, __LINE__, numRequests, tmpGid);
1029  gidQueryBuf = arcp(tmpGid, 0, numRequests);
1030  }
1031 
1032  try{
1033  gidTranslate(gidQueryBuf.view(0, numRequests),
1034  gnoInBuf.view(0, numRequests),
1036  }
1038 
1039  ArrayRCP<gid_t> gidInBuf;
1040  Array<lno_t> newCountInBuf(numProcs_, 0);
1041 
1042  try{
1043  AlltoAllv<gid_t>(*comm_, *env_,
1044  gidQueryBuf.view(0, numRequests), countInBuf(),
1045  gidInBuf, newCountInBuf());
1046  }
1048 
1049  gidQueryBuf.clear();
1050 
1051  // copy the replies into the output gid list
1052 
1053  gid_t *gidInfo = gidInBuf.getRawPtr(); // faster in a loop
1054 
1055  for (typename ArrayView<const gno_t>::size_type i=0; i < inLen; i++){
1056  int p = pids[i];
1057  gno_t off = tmpOff[p];
1058  out_gid[i] = gidInfo[off];
1059  tmpOff[p]++;
1060  }
1061 }
1062 
1063 template< typename User>
1065 {
1066  env_->globalInputAssertion(__FILE__, __LINE__,
1067  "application global ID type is not supported yet",
1069  comm_);
1070 
1072  userGidsAreTeuchosOrdinal_ = true;
1073 
1074  localNumberOfIds_ = myGids_.size();
1075  const gid_t *gidPtr = myGids_.get();
1076  ArrayRCP<gid_t> tmpDist;
1077  gid_t mingid=0, maxgid=0;
1078 
1079  userGidsAreConsecutive_ = globallyConsecutiveOrdinals<gid_t>(
1080  *comm_, *env_, gidPtr, localNumberOfIds_, // input
1081  tmpDist, globalNumberOfIds_); // output
1082 
1083  bool baseZeroConsecutiveIds = false;
1084 
1085  if (userGidsAreTeuchosOrdinal_){
1086  if (!userGidsAreConsecutive_){
1087  mingid = tmpDist[0];
1088  maxgid = tmpDist[1];
1089  }
1090  else{
1091  // A gno_t is large enough to hold gid_ts, but may be a different type.
1092  if (sizeof(gid_t) == sizeof(gno_t)) {
1093  gnoDist_ = arcp_reinterpret_cast<gno_t>(tmpDist);
1094  }
1095  else{
1096  gnoDist_.resize(numProcs_ + 1);
1097  for (lno_t i=0; i <= numProcs_; i++){
1098  gnoDist_[i] = static_cast<gno_t>(tmpDist[i]);
1099  }
1100  }
1101 
1102  if (gnoDist_[0] == 0)
1103  baseZeroConsecutiveIds = true;
1104  }
1105  }
1106 
1107  // If user global IDs are not consecutive ordinals, create a lookup table
1108  // mapping the global IDs to their location in myGids_.
1109 
1110  typedef GidLookupHelper<gid_t, lno_t> lookup_t;
1111 
1112  if (!userGidsAreConsecutive_){
1113  try{
1114  gidLookup_ = rcp(new lookup_t(env_, myGids_));
1115  }
1117  }
1118 
1119  userGidsAreZoltan2Gnos_ = false;
1120 
1121  if (userGidsAreTeuchosOrdinal_ &&
1122  ((consecutiveGidsAreRequired_ && baseZeroConsecutiveIds) ||
1123  !consecutiveGidsAreRequired_))
1124  {
1125  // We can use the application's global IDs
1126  userGidsAreZoltan2Gnos_ = true;
1127  }
1128 
1129  if (userGidsAreZoltan2Gnos_){
1130 
1131  zoltan2GnosAreConsecutive_ = userGidsAreConsecutive_;
1132 
1133  if (userGidsAreConsecutive_){
1134  minGlobalGno_ = gnoDist_[0];
1135  maxGlobalGno_ = gnoDist_[numProcs_]-1;
1136  }
1137  else{
1138  minGlobalGno_ = static_cast<gno_t>(mingid);
1139  maxGlobalGno_ = static_cast<gno_t>(maxgid);
1140  }
1141  } else{
1142  // We map application gids to consecutive global numbers starting with 0.
1143 
1144  zoltan2GnosAreConsecutive_ = true;
1145 
1146  try{
1147  gnoDist_.resize(numProcs_ + 1, 0);
1148  }
1149  Z2_THROW_OUTSIDE_ERROR(*env_);
1150 
1151  gno_t myNum = static_cast<gno_t>(localNumberOfIds_);
1152 
1153  try{
1154  gno_t *p = gnoDist_.getRawPtr();
1155  Teuchos::gatherAll<int, gno_t>(*comm_, 1, &myNum, numProcs_, p+1);
1156  }
1157  Z2_THROW_OUTSIDE_ERROR(*env_);
1158 
1159  for (int i=2; i <= numProcs_; i++){
1160  gnoDist_[i] += gnoDist_[i-1];
1161  }
1162 
1163  minGlobalGno_ = gnoDist_[0];
1164  maxGlobalGno_ = gnoDist_[numProcs_]-1;
1165  }
1166 }
1167 
1168 } // end namespace Zoltan2
1169 
1170 #endif /* _ZOLTAN2_IDENTIFIERMAP_HPP_ */
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
size_t global_size_t
IdentifierMap & operator=(const IdentifierMap &id)
Assignment operator.
static int hashCode(const T id)
Compute an integer hash code for the user&#39;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&#39; 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.
default_lno_t lno_t
The ordinal type (e.g., int, long, int64_t) that represents local counts and local indices...
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.
default_gno_t gid_t
The data type that the user uses for global Identifiers.
Traits for application input objects.
default_gno_t gno_t
The ordinal type (e.g., int, long, int64_t) that can represent global counts and 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)
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.