56 #ifndef _ZOLTAN2_IDENTIFIERTRAITS
57 #define _ZOLTAN2_IDENTIFIERTRAITS
62 #include <Teuchos_SerializationTraits.hpp>
63 #include <Teuchos_HashUtils.hpp>
64 #include <Teuchos_ReductionOp.hpp>
73 using Teuchos::SerializationTraits;
77 extern int getHashCode(
const unsigned char *a,
size_t len);
84 T max = numeric_limits<T>::min();
85 T min = numeric_limits<T>::max();
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];
91 return std::pair<T,T>(min,max);
101 public Teuchos::ValueTypeReductionOp<int, char>
105 const int count,
const char inBuffer[],
char inoutBuffer[])
const
107 const T *in =
reinterpret_cast<const T*
>(inBuffer);
108 T *inout =
reinterpret_cast<T*
>(inoutBuffer);
110 if (in[0] < inout[0]) inout[0] = in[0];
111 if (in[1] > inout[1]) inout[1] = in[1];
117 template <
typename T>
119 T localMin, T localMax, T &globalMin, T &globalMax)
122 localMin = numeric_limits<T>::max();
123 localMax = numeric_limits<T>::min();
126 if (comm.getSize() == 1){
127 globalMin = localMin;
128 globalMax = localMax;
132 T localValues[2] = {localMin, localMax};
135 char *lv =
reinterpret_cast<char *
>(&localValues[0]);
136 char *gv =
reinterpret_cast<char *
>(&globalValues[0]);
139 Teuchos::reduceAll<int, char>(comm, reductionOp, 2*
sizeof(T), lv, gv);
141 globalMin = globalValues[0];
142 globalMax = globalValues[1];
147 template <
typename T>
150 if (n == 0)
return true;
152 if (val[n-1] - val[0] + 1 != T(n))
155 T nextval = val[0]+1;
156 for (
size_t i=1; i < n-1; i++){
157 if (val[i] != nextval)
166 template <
typename T>
169 std::ostringstream oss;
180 T::UsingInvalidGlobalIdentifierDataType();
183 return T::UsingInvalidGlobalIdentifierDataType();
265 static inline double key(
const T c){
return static_cast<double>(c); }
271 static inline std::string
name() {
323 static void minMax(
const T *values,
size_t numValues, T &min, T&max) {
341 T localMin, T localMax, T &globalMin, T &globalMax){
361 #ifndef DOXYGEN_SHOULD_SKIP_THIS
364 struct IdentifierTraits<char> {
366 static inline int hashCode(
const T c) {
return static_cast<int>(c);}
368 static inline double key(
const T c){
return static_cast<double>(c); }
369 static inline std::string
name() {
return(
"char");}
373 static inline T
difference(
const T a,
const T b) {
return (b-a); }
375 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
388 struct IdentifierTraits<unsigned char> {
389 typedef unsigned char T;
390 static inline int hashCode(
const T c) {
return static_cast<int>(c);}
392 static inline double key(
const T c){
return static_cast<double>(c); }
393 static inline std::string
name() {
return(
"unsigned char");}
397 static inline T
difference(
const T a,
const T b) {
return (b-a); }
399 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
412 struct IdentifierTraits<short> {
414 static inline int hashCode(
const T a) {
return static_cast<int>(a);}
416 static inline double key(
const T a){
return static_cast<double>(a); }
417 static inline std::string
name() {
return(
"short");}
421 static inline T
difference(
const T a,
const T b) {
return (b-a); }
423 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
436 struct IdentifierTraits<unsigned short> {
437 typedef unsigned short T;
438 static inline int hashCode(
const T a) {
return static_cast<int>(a);}
440 static inline double key(
const T a){
return static_cast<double>(a); }
441 static inline std::string
name() {
return(
"unsigned short");}
445 static inline T
difference(
const T a,
const T b) {
return (b-a); }
447 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
460 struct IdentifierTraits<int> {
462 static inline int hashCode(
const T a) {
return static_cast<int>(a);}
464 static inline double key(
const T a){
return static_cast<double>(a); }
465 static inline std::string
name() {
return(
"int");}
469 static inline T
difference(
const T a,
const T b) {
return (b-a); }
471 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
484 struct IdentifierTraits<unsigned int> {
485 typedef unsigned int T;
486 static inline int hashCode(
const T a) {
return static_cast<int>(a);}
488 static inline double key(
const T a){
return static_cast<double>(a); }
489 static inline std::string
name() {
return(
"unsigned int");}
493 static inline T
difference(
const T a,
const T b) {
return (b-a); }
495 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
509 struct IdentifierTraits<long> {
511 static inline int hashCode(
const T a) {
513 reinterpret_cast<const unsigned char *>(&a),
sizeof(T));}
515 static inline double key(
const T a){
return static_cast<double>(a); }
516 static inline std::string
name() {
return(
"long");}
519 static inline T
difference(
const T a,
const T b) {
return (b-a); }
521 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
534 struct IdentifierTraits<unsigned long> {
535 typedef unsigned long T;
536 static inline int hashCode(
const T a) {
538 reinterpret_cast<const unsigned char *>(&a),
sizeof(T));}
540 static inline double key(
const T a){
return static_cast<double>(a);}
541 static inline std::string
name() {
return(
"unsigned long");}
544 static inline T
difference(
const T a,
const T b) {
return (b-a); }
546 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
558 #ifdef HAVE_ZOLTAN2_LONG_LONG
561 struct IdentifierTraits<long long> {
563 static inline int hashCode(
const T a) {
565 reinterpret_cast<const unsigned char *>(&a),
sizeof(T));}
567 static inline double key(
const T a){
return static_cast<double>(a); }
568 static inline std::string
name() {
return(
"long long");}
571 static inline T
difference(
const T a,
const T b) {
return (b-a); }
573 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
586 struct IdentifierTraits<unsigned long long> {
587 typedef unsigned long long T;
588 static inline int hashCode(
const T a) {
590 reinterpret_cast<const unsigned char *>(&a),
sizeof(T));}
592 static inline double key(
const T a){
return static_cast<double>(a);}
593 static inline std::string
name() {
return(
"unsigned long long");}
596 static inline T
difference(
const T a,
const T b) {
return (b-a); }
598 static void minMax(
const T *values,
size_t n, T &min, T &max) {
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);}
613 struct IdentifierTraits<string> {
615 static inline int hashCode(
const T a) {
617 reinterpret_cast<const unsigned char *>(a.c_str()), a.size());}
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;}
623 static inline T
difference(
const T a,
const T b) {
624 throw std::logic_error(
"invalid call");}
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");}
632 throw std::logic_error(
"invalid call");}
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;
640 static inline int hashCode(
const pair_t p) {
646 if ((
sizeof(T1)*2 <=
sizeof(
double))&&(
sizeof(T2)*2 <=
sizeof(
double)))
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");
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));
668 static inline std::string
name() {
669 std::string s(
"std::pair<");
671 s += std::string(
", ");
673 s += std::string(
">");
677 static std::string
stringify(pair_t val) {
678 std::ostringstream oss;
681 oss <<
">(" << val.first <<
"," << val.second <<
")";
687 static inline pair_t
difference(
const pair_t a,
const pair_t b) {
688 throw std::logic_error(
"invalid call");
692 return (
sizeof(T1)+
sizeof(T2) <=
sizeof(
double)); }
694 static void minMax(
const pair_t *values,
size_t n, pair_t &min, pair_t &max) {
695 throw std::logic_error(
"invalid call");}
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");}
703 throw std::logic_error(
"invalid call");
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)
731 if (!IdentifierTraits<T>::isGlobalOrdinal())
742 int nprocs = comm.getSize();
744 bool globallyConsecutive =
false;
747 if (locallyConsecutive){
751 distBuf[1] = val[len-1] + 1;
760 minMaxBuf =
new T [2];
761 minMaxBuf[0] = extrema.first;
762 minMaxBuf[1] = extrema.second;
766 globallyConsecutive = locallyConsecutive;
770 reduceAll<int, size_t>(comm, Teuchos::REDUCE_SUM, 1, &len, &globalLen);
774 T v0, v1, gMin, gMax;
781 v0 = numeric_limits<T>::max();
782 v1 = numeric_limits<T>::min();
790 int lFlag = (locallyConsecutive ? 1 : 0);
794 reduceAll<int, int>(comm, Teuchos::REDUCE_MIN, 1, &lFlag, &gFlag);
800 size_t g0 =
static_cast<size_t>(gMin);
801 size_t g1 =
static_cast<size_t>(gMax);
803 if (g1 - g0 + 1 == globalLen){
804 size_t sentinel = g1 + 1;
805 size_t sendVal = sentinel;
807 sendVal =
static_cast<size_t>(v0);
809 size_t *recvBuf =
new size_t[nprocs];
812 Teuchos::gatherAll<int, size_t>(comm, 1, &sendVal, nprocs, recvBuf);
817 for (
int i=0; i < nprocs; i++)
818 if (recvBuf[i] == sentinel)
821 globallyConsecutive =
true;
824 for (
int i=1; globallyConsecutive && i < nprocs; i++)
825 if (recvBuf[i] < recvBuf[i-1])
826 globallyConsecutive =
false;
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);
837 for (
int i=0; i < nprocs; i++)
838 if (recvBuf[i] != sentinel)
841 for (
int i=1; i < index.size(); i++){
842 if (recvBuf[index[i]] < recvBuf[index[i-1]])
843 globallyConsecutive =
false;
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);
851 for (
int i=0; i < index.size(); i++)
852 distBuf[index[i]] = static_cast<T>(recvBuf[index[i]]);
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;
859 useValue = distBuf[i];
867 if (!globallyConsecutive){
868 minMaxBuf =
new T [2];
875 dist = arcp(minMaxBuf, 0, 2,
true);
877 dist = arcp(distBuf, 0, nprocs+1,
true);
879 throw std::logic_error(
"no buffer");
881 return globallyConsecutive;
884 #endif // DOXYGEN_SHOULD_SKIP_THIS
888 #endif // _ZOLTAN2_IDENTIFIERTRAITS
Teuchos reduction operation.
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's global ID.
static std::string stringify(T val)
A string displaying the value.
The functions to be defined for users' 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
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.