Zoltan2  Version of the Day
Zoltan2_IdentifierTraits.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 // @HEADER
46 // ***********************************************************************
47 // copyright
48 //
49 // ***********************************************************************
50 // @HEADER
51 
56 #ifndef _ZOLTAN2_IDENTIFIERTRAITS
57 #define _ZOLTAN2_IDENTIFIERTRAITS
58 
59 #include <Zoltan2_Standards.hpp>
60 #include <Zoltan2_AlltoAll.hpp>
61 
62 #include <Teuchos_SerializationTraits.hpp>
63 #include <Teuchos_HashUtils.hpp>
64 #include <Teuchos_ReductionOp.hpp>
65 
66 #include <utility>
67 #include <iostream>
68 #include <sstream>
69 #include <string>
70 #include <limits>
71 #include <cstdlib>
72 
73 using Teuchos::SerializationTraits;
74 
75 namespace Zoltan2
76 {
77 extern int getHashCode(const unsigned char *a, size_t len);
78 
81 template <typename T>
82  std::pair<T, T> z2LocalMinMax(const T *val, size_t n)
83 {
84  T max = numeric_limits<T>::min();
85  T min = numeric_limits<T>::max();
86 
87  for (size_t i=0; i < n; i++){
88  if (val[i] < min) min = val[i];
89  if (val[i] > max) max = val[i];
90  }
91  return std::pair<T,T>(min,max);
92 }
93 
94 
100 template <typename T> class Zoltan2_MinMaxOperation :
101  public Teuchos::ValueTypeReductionOp<int, char>
102 {
103 public:
104  void reduce(
105  const int count, const char inBuffer[], char inoutBuffer[]) const
106  {
107  const T *in = reinterpret_cast<const T*>(inBuffer);
108  T *inout = reinterpret_cast<T*>(inoutBuffer);
109 
110  if (in[0] < inout[0]) inout[0] = in[0];
111  if (in[1] > inout[1]) inout[1] = in[1];
112  }
113 };
114 
117 template <typename T>
118  void z2GlobalMinMax(const Comm<int> &comm, bool noLocalValues,
119  T localMin, T localMax, T &globalMin, T &globalMax)
120 {
121  if (noLocalValues){
122  localMin = numeric_limits<T>::max();
123  localMax = numeric_limits<T>::min();
124  }
125 
126  if (comm.getSize() == 1){
127  globalMin = localMin;
128  globalMax = localMax;
129  return;
130  }
131 
132  T localValues[2] = {localMin, localMax};
133  T globalValues[2];
134 
135  char *lv = reinterpret_cast<char *>(&localValues[0]);
136  char *gv = reinterpret_cast<char *>(&globalValues[0]);
137 
138  Zoltan2_MinMaxOperation<T> reductionOp;
139  Teuchos::reduceAll<int, char>(comm, reductionOp, 2*sizeof(T), lv, gv);
140 
141  globalMin = globalValues[0];
142  globalMax = globalValues[1];
143 }
144 
147 template <typename T>
148  bool z2AreConsecutive(const T *val, size_t n)
149 {
150  if (n == 0) return true;
151 
152  if (val[n-1] - val[0] + 1 != T(n))
153  return false;
154 
155  T nextval = val[0]+1;
156  for (size_t i=1; i < n-1; i++){
157  if (val[i] != nextval)
158  return false;
159  nextval++;
160  }
161  return true;
162 }
163 
166 template <typename T>
167  std::string stringifyOrdinal(T ordinal)
168 {
169  std::ostringstream oss;
170  oss << ordinal;
171  return oss.str();
172 }
173 
176 template<typename T>
178 {
179 static inline void noop() {
180  T::UsingInvalidGlobalIdentifierDataType();
181 }
182 static inline T invalid() {
183  return T::UsingInvalidGlobalIdentifierDataType();
184 }
185 };
186 
187 
239 template<typename T>
241 
247  static int hashCode(const T id) {
249  }
250 
255  static inline bool hasUniqueKey(){
257 
265  static inline double key(const T c){return static_cast<double>(c); }
266 
271  static inline std::string name() {
273  }
274 
280  static std::string stringify(T val) {
282  }
283 
290  static inline bool isGlobalOrdinal() {
292  }
293 
303  static inline T difference(const T a, const T b) {
305  }
306 
312  static inline bool is_valid_id_type() { return false; }
313 
323  static void minMax(const T *values, size_t numValues, T &min, T&max) {
325  }
326 
340  static void globalMinMax(const Comm<int> &comm, bool flag,
341  T localMin, T localMax, T &globalMin, T &globalMax){
343  }
344 
355  static bool areConsecutive(const T *val, size_t n){
357  }
358 
359 };
360 
361 #ifndef DOXYGEN_SHOULD_SKIP_THIS
362 
363 template<>
364 struct IdentifierTraits<char> {
365  typedef char T;
366  static inline int hashCode(const T c) {return static_cast<int>(c);}
367  static inline bool hasUniqueKey() { return true;}
368  static inline double key(const T c){return static_cast<double>(c); }
369  static inline std::string name() {return("char");}
370  static std::string stringify(T val) {return stringifyOrdinal(val);}
371  static inline bool isGlobalOrdinal() {return true; }
372 
373  static inline T difference(const T a, const T b) { return (b-a); }
374  static inline bool is_valid_id_type() {return true; }
375  static void minMax(const T *values, size_t n, T &min, T &max) {
376  std::pair<T, T> ext = z2LocalMinMax(values, n);
377  min = ext.first;
378  max = ext.second;
379  }
380  static void globalMinMax(const Comm<int> &comm, bool flag,
381  T localMin, T localMax, T &globalMin, T &globalMax){
382  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
383  static bool areConsecutive(const T *val, size_t n){
384  return z2AreConsecutive(val, n); }
385 };
386 
387 template<>
388 struct IdentifierTraits<unsigned char> {
389  typedef unsigned char T;
390  static inline int hashCode(const T c) {return static_cast<int>(c);}
391  static inline bool hasUniqueKey() { return true;}
392  static inline double key(const T c){return static_cast<double>(c); }
393  static inline std::string name() {return("unsigned char");}
394  static std::string stringify(T val) {return stringifyOrdinal(val);}
395  static inline bool isGlobalOrdinal() {return true; }
396 
397  static inline T difference(const T a, const T b) { return (b-a); }
398  static inline bool is_valid_id_type() {return true; }
399  static void minMax(const T *values, size_t n, T &min, T &max) {
400  std::pair<T, T> ext = z2LocalMinMax(values, n);
401  min = ext.first;
402  max = ext.second;
403  }
404  static void globalMinMax(const Comm<int> &comm, bool flag,
405  T localMin, T localMax, T &globalMin, T &globalMax){
406  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
407  static bool areConsecutive(const T *val, size_t n){
408  return z2AreConsecutive(val, n); }
409 };
410 
411 template<>
412 struct IdentifierTraits<short> {
413  typedef short T;
414  static inline int hashCode(const T a) {return static_cast<int>(a);}
415  static inline bool hasUniqueKey() { return true;}
416  static inline double key(const T a){return static_cast<double>(a); }
417  static inline std::string name() {return("short");}
418  static std::string stringify(T val) {return stringifyOrdinal(val);}
419  static inline bool isGlobalOrdinal() {return true; }
420 
421  static inline T difference(const T a, const T b) { return (b-a); }
422  static inline bool is_valid_id_type() {return true; }
423  static void minMax(const T *values, size_t n, T &min, T &max) {
424  std::pair<T, T> ext = z2LocalMinMax(values, n);
425  min = ext.first;
426  max = ext.second;
427  }
428  static void globalMinMax(const Comm<int> &comm, bool flag,
429  T localMin, T localMax, T &globalMin, T &globalMax){
430  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
431  static bool areConsecutive(const T *val, size_t n){
432  return z2AreConsecutive(val, n); }
433 };
434 
435 template<>
436 struct IdentifierTraits<unsigned short> {
437  typedef unsigned short T;
438  static inline int hashCode(const T a) {return static_cast<int>(a);}
439  static inline bool hasUniqueKey() { return true;}
440  static inline double key(const T a){return static_cast<double>(a); }
441  static inline std::string name() {return("unsigned short");}
442  static std::string stringify(T val) {return stringifyOrdinal(val);}
443  static inline bool isGlobalOrdinal() {return true; }
444 
445  static inline T difference(const T a, const T b) { return (b-a); }
446  static inline bool is_valid_id_type() {return true; }
447  static void minMax(const T *values, size_t n, T &min, T &max) {
448  std::pair<T, T> ext = z2LocalMinMax(values, n);
449  min = ext.first;
450  max = ext.second;
451  }
452  static void globalMinMax(const Comm<int> &comm, bool flag,
453  T localMin, T localMax, T &globalMin, T &globalMax){
454  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
455  static bool areConsecutive(const T *val, size_t n){
456  return z2AreConsecutive(val, n); }
457 };
458 
459 template<>
460 struct IdentifierTraits<int> {
461  typedef int T;
462  static inline int hashCode(const T a) {return static_cast<int>(a);}
463  static inline bool hasUniqueKey() { return true;}
464  static inline double key(const T a){return static_cast<double>(a); }
465  static inline std::string name() {return("int");}
466  static std::string stringify(T val) {return stringifyOrdinal(val);}
467  static inline bool isGlobalOrdinal() {return true; }
468 
469  static inline T difference(const T a, const T b) { return (b-a); }
470  static inline bool is_valid_id_type() {return true; }
471  static void minMax(const T *values, size_t n, T &min, T &max) {
472  std::pair<T, T> ext = z2LocalMinMax(values, n);
473  min = ext.first;
474  max = ext.second;
475  }
476  static void globalMinMax(const Comm<int> &comm, bool flag,
477  T localMin, T localMax, T &globalMin, T &globalMax){
478  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
479  static bool areConsecutive(const T *val, size_t n){
480  return z2AreConsecutive(val, n); }
481 };
482 
483 template<>
484 struct IdentifierTraits<unsigned int> {
485  typedef unsigned int T;
486  static inline int hashCode(const T a) {return static_cast<int>(a);}
487  static inline bool hasUniqueKey() { return true;}
488  static inline double key(const T a){return static_cast<double>(a); }
489  static inline std::string name() {return("unsigned int");}
490  static std::string stringify(T val) {return stringifyOrdinal(val);}
491  static inline bool isGlobalOrdinal() {return true; }
492 
493  static inline T difference(const T a, const T b) { return (b-a); }
494  static inline bool is_valid_id_type() {return true; }
495  static void minMax(const T *values, size_t n, T &min, T &max) {
496  std::pair<T, T> ext = z2LocalMinMax(values, n);
497  min = ext.first;
498  max = ext.second;
499  }
500  static void globalMinMax(const Comm<int> &comm, bool flag,
501  T localMin, T localMax, T &globalMin, T &globalMax){
502  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
503  static bool areConsecutive(const T *val, size_t n){
504  return z2AreConsecutive(val, n); }
505 };
506 
507 
508 template<>
509 struct IdentifierTraits<long> {
510  typedef long T;
511  static inline int hashCode(const T a) {
512  return getHashCode(
513  reinterpret_cast<const unsigned char *>(&a), sizeof(T));}
514  static inline bool hasUniqueKey() { return true;}
515  static inline double key(const T a){return static_cast<double>(a); }
516  static inline std::string name() {return("long");}
517  static std::string stringify(T val) {return stringifyOrdinal(val);}
518  static inline bool isGlobalOrdinal() {return true; }
519  static inline T difference(const T a, const T b) { return (b-a); }
520  static inline bool is_valid_id_type() {return true; }
521  static void minMax(const T *values, size_t n, T &min, T &max) {
522  std::pair<T, T> ext = z2LocalMinMax(values, n);
523  min = ext.first;
524  max = ext.second;
525  }
526  static void globalMinMax(const Comm<int> &comm, bool flag,
527  T localMin, T localMax, T &globalMin, T &globalMax){
528  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
529  static bool areConsecutive(const T *val, size_t n){
530  return z2AreConsecutive(val, n); }
531 };
532 
533 template<>
534 struct IdentifierTraits<unsigned long> {
535  typedef unsigned long T;
536  static inline int hashCode(const T a) {
537  return getHashCode(
538  reinterpret_cast<const unsigned char *>(&a), sizeof(T));}
539  static inline bool hasUniqueKey() { return true;}
540  static inline double key(const T a){return static_cast<double>(a);}
541  static inline std::string name() {return("unsigned long");}
542  static std::string stringify(T val) {return stringifyOrdinal(val);}
543  static inline bool isGlobalOrdinal() {return true; }
544  static inline T difference(const T a, const T b) { return (b-a); }
545  static inline bool is_valid_id_type() {return true; }
546  static void minMax(const T *values, size_t n, T &min, T &max) {
547  std::pair<T, T> ext = z2LocalMinMax(values, n);
548  min = ext.first;
549  max = ext.second;
550  }
551  static void globalMinMax(const Comm<int> &comm, bool flag,
552  T localMin, T localMax, T &globalMin, T &globalMax){
553  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
554  static bool areConsecutive(const T *val, size_t n){
555  return z2AreConsecutive(val, n); }
556 };
557 
558 #ifdef HAVE_ZOLTAN2_LONG_LONG
559 
560 template<>
561 struct IdentifierTraits<long long> {
562  typedef long long T;
563  static inline int hashCode(const T a) {
564  return getHashCode(
565  reinterpret_cast<const unsigned char *>(&a), sizeof(T));}
566  static inline bool hasUniqueKey() { return true;}
567  static inline double key(const T a){return static_cast<double>(a); }
568  static inline std::string name() {return("long long");}
569  static std::string stringify(T val) {return stringifyOrdinal(val);}
570  static inline bool isGlobalOrdinal() {return true; }
571  static inline T difference(const T a, const T b) { return (b-a); }
572  static inline bool is_valid_id_type() {return true; }
573  static void minMax(const T *values, size_t n, T &min, T &max) {
574  std::pair<T, T> ext = z2LocalMinMax(values, n);
575  min = ext.first;
576  max = ext.second;
577  }
578  static void globalMinMax(const Comm<int> &comm, bool flag,
579  T localMin, T localMax, T &globalMin, T &globalMax){
580  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
581  static bool areConsecutive(const T *val, size_t n){
582  return z2AreConsecutive(val, n); }
583 };
584 
585 template<>
586 struct IdentifierTraits<unsigned long long> {
587  typedef unsigned long long T;
588  static inline int hashCode(const T a) {
589  return getHashCode(
590  reinterpret_cast<const unsigned char *>(&a), sizeof(T));}
591  static inline bool hasUniqueKey() { return true;}
592  static inline double key(const T a){return static_cast<double>(a);}
593  static inline std::string name() {return("unsigned long long");}
594  static std::string stringify(T val) {return stringifyOrdinal(val);}
595  static inline bool isGlobalOrdinal() {return true; }
596  static inline T difference(const T a, const T b) { return (b-a); }
597  static inline bool is_valid_id_type() {return true; }
598  static void minMax(const T *values, size_t n, T &min, T &max) {
599  std::pair<T, T> ext = z2LocalMinMax(values, n);
600  min = ext.first;
601  max = ext.second;
602  }
603  static void globalMinMax(const Comm<int> &comm, bool flag,
604  T localMin, T localMax, T &globalMin, T &globalMax){
605  z2GlobalMinMax(comm, flag, localMin, localMax, globalMin, globalMax);}
606  static bool areConsecutive(const T *val, size_t n){
607  return z2AreConsecutive(val, n); }
608 };
609 
610 #endif
611 
612 template<>
613 struct IdentifierTraits<string> {
614  typedef string T;
615  static inline int hashCode(const T a) {
616  return getHashCode(
617  reinterpret_cast<const unsigned char *>(a.c_str()), a.size());}
618  static inline bool hasUniqueKey() { return false;}
619  static inline double key(const T a){ throw std::logic_error("invalid call");}
620  static inline std::string name() {return("string");}
621  static std::string stringify(T val) {return val;}
622  static inline bool isGlobalOrdinal() {return false; }
623  static inline T difference(const T a, const T b) {
624  throw std::logic_error("invalid call");}
625  static inline bool is_valid_id_type() {return true; }
626  static void minMax(const T *values, size_t n, T &min, T &max) {
627  throw std::logic_error("invalid call");}
628  static void globalMinMax(const Comm<int> &comm, bool flag,
629  T localMin, T localMax, T &globalMin, T &globalMax){
630  throw std::logic_error("invalid call");}
631  static bool areConsecutive(const T *val, size_t n){
632  throw std::logic_error("invalid call");}
633 };
634 
635 template<typename T1, typename T2>
636 struct IdentifierTraits<std::pair<T1, T2> > {
637  typedef std::pair<T1, T2> pair_t;
638  typedef typename std::pair<pair_t, pair_t> pairPair_t;
639 
640  static inline int hashCode(const pair_t p) {
641  return IdentifierTraits<T1>::hashCode(p.first) +
643  }
644 
645  static inline bool hasUniqueKey() {
646  if ((sizeof(T1)*2 <= sizeof(double))&&(sizeof(T2)*2 <= sizeof(double)))
647  return true;
648  else
649  return false;
650  }
651 
652  static inline double key(const pair_t p) {
653  size_t nbytes = sizeof(double) / 2;
654  if ((sizeof(T1) > nbytes)||(sizeof(T2) > nbytes))
655  throw std::logic_error("invalid call");
656  else{
657  double keyVal=0;
658  char *cx = reinterpret_cast<char *>(&keyVal);
659  char *cy = cx + nbytes;
660  T1 *xpos = reinterpret_cast<T1 *>(cx + nbytes-sizeof(T1));
661  T2 *ypos = reinterpret_cast<T2 *>(cy + nbytes-sizeof(T2));
662  *xpos = p.first;
663  *ypos = p.second;
664  return keyVal;
665  }
666  }
667 
668  static inline std::string name() {
669  std::string s("std::pair<");
671  s += std::string(", ");
673  s += std::string(">");
674  return s;
675  }
676 
677  static std::string stringify(pair_t val) {
678  std::ostringstream oss;
679  oss << "pair<" << IdentifierTraits<T1>::name();
680  oss << "," << IdentifierTraits<T2>::name();
681  oss << ">(" << val.first << "," << val.second << ")";
682  return oss.str();
683  }
684 
685  static inline bool isGlobalOrdinal() {return false; }
686 
687  static inline pair_t difference( const pair_t a, const pair_t b) {
688  throw std::logic_error("invalid call");
689  return pair_t();}
690 
691  static inline bool is_valid_id_type() {
692  return (sizeof(T1)+sizeof(T2) <= sizeof(double)); }
693 
694  static void minMax(const pair_t *values, size_t n, pair_t &min, pair_t &max) {
695  throw std::logic_error("invalid call");}
696 
697  static void globalMinMax(const Comm<int> &comm, bool flag,
698  pair_t localMin, pair_t localMax,
699  pair_t &globalMin, pair_t &globalMax){
700  throw std::logic_error("invalid call");}
701 
702  static bool areConsecutive(const pair_t *val, size_t n){
703  throw std::logic_error("invalid call");
704  return false; }
705 };
706 
708 // A helper function. If T is an ordinal, are the values
709 // globally consecutive? If so return true, otherwise
710 // return false.
711 //
712 // On return, globalLen is set to the sum of the local lengths.
713 //
714 // If T is an ordinal, but the list is not globally consecutive,
715 // on return dist[0] is set to the global minimum of
716 // the values and dist[1] to the global maximum.
717 //
718 // If T is an ordinal and the list is globally consecutive,
719 // on return dist[p] is set to val[0] on process p. dist[nprocs]
720 // is set to one past the global maximum value.
722 
723 template <typename T>
724  bool globallyConsecutiveOrdinals(
725  const Comm<int> &comm, const Environment &env,
726  const T* val, size_t len,
727  ArrayRCP<T> &dist, size_t &globalLen)
728 {
729  // Non-ordinals can not be consecutive.
730 
732  return false;
733 
734  // return values
735 
736  T *distBuf= NULL;
737  T *minMaxBuf= NULL;
738  globalLen = 0;
739 
740  // Locally consecutive?
741 
742  int nprocs = comm.getSize();
743  bool locallyConsecutive = IdentifierTraits<T>::areConsecutive(val, len);
744  bool globallyConsecutive = false;
745 
746  if (nprocs == 1){
747  if (locallyConsecutive){
748  distBuf = new T [2];
749  if (len > 0){
750  distBuf[0] = val[0];
751  distBuf[1] = val[len-1] + 1;
752  }
753  else{
754  distBuf[0] = 0;
755  distBuf[1] = 0;
756  }
757  }
758  else{
759  std::pair<T, T> extrema = z2LocalMinMax(val, len);
760  minMaxBuf = new T [2];
761  minMaxBuf[0] = extrema.first;
762  minMaxBuf[1] = extrema.second;
763  }
764 
765  globalLen = len;
766  globallyConsecutive = locallyConsecutive;
767  }
768  else{ // nprocs > 1
769  try{
770  reduceAll<int, size_t>(comm, Teuchos::REDUCE_SUM, 1, &len, &globalLen);
771  }
773 
774  T v0, v1, gMin, gMax;
775 
776  if (len > 0){
777  v0 = val[0];
778  v1 = val[len-1];
779  }
780  else {
781  v0 = numeric_limits<T>::max();
782  v1 = numeric_limits<T>::min();
783  }
784 
785  try{
786  IdentifierTraits<T>::globalMinMax(comm, len==0, v0, v1, gMin, gMax);
787  }
789 
790  int lFlag = (locallyConsecutive ? 1 : 0);
791  int gFlag = 0;
792 
793  try{
794  reduceAll<int, int>(comm, Teuchos::REDUCE_MIN, 1, &lFlag, &gFlag);
795  }
797 
798  if (gFlag == 1){ // all processes have consecutive values
799 
800  size_t g0 = static_cast<size_t>(gMin);
801  size_t g1 = static_cast<size_t>(gMax);
802 
803  if (g1 - g0 + 1 == globalLen){
804  size_t sentinel = g1 + 1; // invalid id
805  size_t sendVal = sentinel;
806  if (len > 0)
807  sendVal = static_cast<size_t>(v0);
808 
809  size_t *recvBuf = new size_t[nprocs];
810 
811  try {
812  Teuchos::gatherAll<int, size_t>(comm, 1, &sendVal, nprocs, recvBuf);
813  }
815 
816  int numNoIds = 0; // number of procs with no ids
817  for (int i=0; i < nprocs; i++)
818  if (recvBuf[i] == sentinel)
819  numNoIds++;
820 
821  globallyConsecutive = true;
822 
823  if (numNoIds == 0){
824  for (int i=1; globallyConsecutive && i < nprocs; i++)
825  if (recvBuf[i] < recvBuf[i-1])
826  globallyConsecutive = false;
827 
828  if (globallyConsecutive){
829  distBuf = new T [nprocs+1];
830  for (int i=0; i < nprocs; i++)
831  distBuf[i] = static_cast<T>(recvBuf[i]);
832  distBuf[nprocs] = static_cast<T>(sentinel);
833  }
834  }
835  else{
836  Array<int> index;
837  for (int i=0; i < nprocs; i++)
838  if (recvBuf[i] != sentinel)
839  index.push_back(i);
840 
841  for (int i=1; i < index.size(); i++){
842  if (recvBuf[index[i]] < recvBuf[index[i-1]])
843  globallyConsecutive = false;
844  }
845 
846  if (globallyConsecutive){
847  distBuf = new T [nprocs+1];
848  for (int i=0; i < nprocs+1; i++)
849  distBuf[i] = static_cast<T>(sentinel);
850 
851  for (int i=0; i < index.size(); i++)
852  distBuf[index[i]] = static_cast<T>(recvBuf[index[i]]);
853 
854  T useValue = static_cast<T>(sentinel);
855  for (int i = nprocs-1; i >= 0; i--){
856  if (distBuf[i] == static_cast<T>(sentinel))
857  distBuf[i] = useValue;
858  else
859  useValue = distBuf[i];
860  }
861  }
862  }
863  delete [] recvBuf;
864  }
865  } // all processes have locally consecutive values
866 
867  if (!globallyConsecutive){
868  minMaxBuf = new T [2];
869  minMaxBuf[0] = gMin;
870  minMaxBuf[1] = gMax;
871  }
872  } // nprocs > 1
873 
874  if (minMaxBuf)
875  dist = arcp(minMaxBuf, 0, 2, true);
876  else if (distBuf)
877  dist = arcp(distBuf, 0, nprocs+1, true);
878  else
879  throw std::logic_error("no buffer"); // should never get here
880 
881  return globallyConsecutive;
882 }
883 
884 #endif // DOXYGEN_SHOULD_SKIP_THIS
885 
886 } // namespace Z2
887 
888 #endif // _ZOLTAN2_IDENTIFIERTRAITS
static void globalMinMax(const Comm< int > &comm, bool flag, T localMin, T localMax, T &globalMin, T &globalMax)
Find global minimum and maximum.
static void minMax(const T *values, size_t numValues, T &min, T &max)
Return the minimum and maximum of a list of values.
static std::string name()
The name of the identifier data type.
#define Z2_FORWARD_EXCEPTIONS
Forward an exception back through call stack.
static int hashCode(const T id)
Compute an integer hash code for the user&#39;s global ID.
static std::string stringify(T val)
A string displaying the value.
The functions to be defined for users&#39; global ID types.
static double key(const T c)
Return a double which is a unique key for the value.
void reduce(const int count, const char inBuffer[], char inoutBuffer[]) const
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static bool hasUniqueKey()
Determine if the data type supports unique hash keys.
static bool areConsecutive(const T *val, size_t n)
Determine if the values are locally increasing consecutive.
void z2GlobalMinMax(const Comm< int > &comm, bool noLocalValues, T localMin, T localMax, T &globalMin, T &globalMax)
helper function to find global min and max of array of user Ids
bool z2AreConsecutive(const T *val, size_t n)
helper function to determine if list of user Ids are consecutive
std::string stringifyOrdinal(T ordinal)
helper function write a user ID to a string
static bool is_valid_id_type()
Determine if the data type is one for which IdentifierTraits are defined.
Gathering definitions used in software development.
int getHashCode(const unsigned char *a, size_t len)
helper to hash values larger than int to an int. Hash values do not need to be unique, but there should be as few overlaps as possible.
std::pair< T, T > z2LocalMinMax(const T *val, size_t n)
helper function to find min and max of array of user Ids
static bool isGlobalOrdinal()
Determine whether the data type can be a Teuchos Ordinal.
#define Z2_THROW_OUTSIDE_ERROR(env)
Throw an error returned from outside the Zoltan2 library.
Structure to catch invalid user ID types.
AlltoAll communication methods.
static T difference(const T a, const T b)
Compute b - a, if possible.