Tpetra Matrix/Vector Services  Version of the Day
Tpetra_HashTable_def.hpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Tpetra: Templated Linear Algebra Services Package
6 // Copyright (2008) 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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 */
43 
44 #ifndef TPETRA_HASHTABLE_DEF_HPP_
45 #define TPETRA_HASHTABLE_DEF_HPP_
46 
47 #ifdef DOXYGEN_USE_ONLY
48  #include "Tpetra_HashTable_decl.hpp"
49 #endif
50 
51 
52 #include "MurmurHash3.hpp"
53 
54 
55 namespace Tpetra
56 {
57 
58 namespace Details
59 {
60 
61 template<typename KeyType, typename ValueType>
62 int
63 HashTable<KeyType, ValueType>::hashFunc( const KeyType key ) {
64 #ifdef TPETRA_USE_MURMUR_HASH
65  uint32_t k;
66  MurmurHash3_x86_32((void *)&key, sizeof(KeyType),
67  1, (void *)&k);
68  return (int) (k%Size_);
69 #else
70  // Epetra's version of hash, using it as default as we have observed
71  // this is much faster than murmur hash. However, this is not a good
72  // hash function for all data sets. For our typical use case, this is good.
73  // Use murmur if the maps are sparse.
74  int intkey = (int) ((key & 0x000000007fffffffLL) +
75  ((key & 0x7fffffff80000000LL) >> 31));
76  return (int) ((Seed_ ^ intkey)%Size_);
77 #endif
78 }
79 
80 template<typename KeyType, typename ValueType>
81 int
82 HashTable<KeyType, ValueType>::getRecommendedSize( const int size ) {
83  // A large list of prime numbers.
84  // Based on a recommendation by Andres Valloud in hash forums.
85  // There are only enough primes here so that between any number N and 2*N,
86  // there will be at least about 8 to choose from (except the first 10).
87  // This is a balance between a small list of primes, and getting a
88  // collection size that doesn't waste too much space. In addition,
89  // the primes in this table were chosen so that they do not divide
90  // 256^k +- a, for 1<=k<=8, and -32<=a<=32. This is so that
91  // using them as a modulo will not have a tendency to just throw away
92  // the most significant bits of the object's hash. The primes (except the
93  // first ten) or not close to any power of two to avoid aliasing
94  // between hash functions based on bit manipulation and the moduli.
95  int primes [ ] = {
96  3, 7, 13, 23, 53, 97, 193, 389, 769, 1543,
97  2237, 2423, 2617, 2797, 2999, 3167, 3359, 3539,
98  3727, 3911, 4441 , 4787 , 5119 , 5471 , 5801 , 6143 , 6521 , 6827
99  , 7177 , 7517 , 7853 , 8887 , 9587 , 10243 , 10937 , 11617 , 12289
100  , 12967 , 13649 , 14341 , 15013 , 15727
101  , 17749 , 19121 , 20479 , 21859 , 23209 , 24593 , 25939 , 27329
102  , 28669 , 30047 , 31469 , 35507 , 38231 , 40961 , 43711 , 46439
103  , 49157 , 51893 , 54617 , 57347 , 60077 , 62801 , 70583 , 75619
104  , 80669 , 85703 , 90749 , 95783 , 100823 , 105871 , 110909 , 115963
105  , 120997 , 126031 , 141157 , 151237 , 161323 , 171401 , 181499 , 191579
106  , 201653 , 211741 , 221813 , 231893 , 241979 , 252079
107  , 282311 , 302483 , 322649 , 342803 , 362969 , 383143 , 403301 , 423457
108  , 443629 , 463787 , 483953 , 504121 , 564617 , 604949 , 645313 , 685609
109  , 725939 , 766273 , 806609 , 846931 , 887261 , 927587 , 967919 , 1008239
110  , 1123477 , 1198397 , 1273289 , 1348177 , 1423067 , 1497983 , 1572869
111  , 1647761 , 1722667 , 1797581 , 1872461 , 1947359 , 2022253
112  , 2246953 , 2396759 , 2546543 , 2696363 , 2846161 , 2995973 , 3145739
113  , 3295541 , 3445357 , 3595117 , 3744941 , 3894707 , 4044503
114  , 4493921 , 4793501 , 5093089 , 5392679 , 5692279 , 5991883 , 6291469
115  , 6591059 , 6890641 , 7190243 , 7489829 , 7789447 , 8089033
116  , 8987807 , 9586981 , 10186177 , 10785371 , 11384539 , 11983729
117  , 12582917 , 13182109 , 13781291 , 14380469 , 14979667 , 15578861
118  , 16178053 , 17895707 , 19014187 , 20132683 , 21251141 , 22369661
119  , 23488103 , 24606583 , 25725083 , 26843549 , 27962027 , 29080529
120  , 30198989 , 31317469 , 32435981 , 35791397 , 38028379 , 40265327
121  , 42502283 , 44739259 , 46976221 , 49213237 , 51450131 , 53687099
122  , 55924061 , 58161041 , 60397993 , 62634959 , 64871921
123  , 71582857 , 76056727 , 80530643 , 85004567 , 89478503 , 93952427
124  , 98426347 , 102900263 , 107374217 , 111848111 , 116322053 , 120795971
125  , 125269877 , 129743807 , 143165587 , 152113427 , 161061283 , 170009141
126  , 178956983 , 187904819 , 196852693 , 205800547 , 214748383 , 223696237
127  , 232644089 , 241591943 , 250539763 , 259487603 , 268435399 };
128 
129  int hsize = primes[220] ;
130  for (int i = 0 ; i < 221 ; i++)
131  {
132  if (size <= primes[i])
133  {
134  hsize = primes[i] ;
135  break ;
136  }
137  }
138 
139  return hsize ;
140 }
141 
142 template<typename KeyType, typename ValueType>
144 HashTable( const int size, const unsigned int seed )
145  : Container_(NULL),
146  Seed_(seed)
147 {
148  TEUCHOS_TEST_FOR_EXCEPTION(size < 0, std::runtime_error,
149  "HashTable : ERROR, size cannot be less than zero");
150 
151  Size_ = getRecommendedSize(size);
152  Container_ = new Node * [Size_];
153  for( KeyType i = 0; i < Size_; ++i ) Container_[i] = NULL;
154 #ifdef HAVE_TEUCHOS_DEBUG
155  maxc_ = 0;
156  nc_ = 0;
157 #endif
158 }
159 
160 template<typename KeyType, typename ValueType>
162 HashTable( const HashTable & obj )
163  : Container_(NULL),
164  Size_(obj.Size_),
165  Seed_(obj.Seed_)
166 {
167 #ifdef HAVE_TEUCHOS_DEBUG
168  maxc_ = 0;
169  nc_ = 0;
170 #endif
171  Container_ = new Node * [Size_];
172  for( KeyType i = 0; i < Size_; ++i ) Container_[i] = NULL;
173  for( KeyType i = 0; i < Size_; ++i ) {
174  Node * ptr = obj.Container_[i];
175  while( ptr ) { add( ptr->Key, ptr->Value ); ptr = ptr->Ptr; }
176  }
177 }
178 
179 template<typename KeyType, typename ValueType>
181  Node * ptr1;
182  Node * ptr2;
183  for( KeyType i = 0; i < Size_; ++i ) {
184  ptr1 = Container_[i];
185  while( ptr1 ) { ptr2 = ptr1; ptr1 = ptr1->Ptr; delete ptr2; }
186  }
187 
188  delete [] Container_;
189 }
190 
191 template<typename KeyType, typename ValueType>
192 void
194 add( const KeyType key, const ValueType value ) {
195  int v = hashFunc(key);
196  Node * n1 = Container_[v];
197  Container_[v] = new Node(key,value,n1);
198 }
199 
200 template<typename KeyType, typename ValueType>
201 ValueType
203 get( const KeyType key ) {
204  Node * n = Container_[ hashFunc(key) ];
205 
206 #ifdef HAVE_TEUCHOS_DEBUG
207  int k = 0;
208 #endif
209 
210  while( n && (n->Key != key) ){
211  n = n->Ptr;
212 #ifdef HAVE_TEUCHOS_DEBUG
213  ((k+1 > maxc_) ? maxc_ = k+1 : 0) ;
214  k++;
215 #endif
216  }
217 
218 #ifdef HAVE_TEUCHOS_DEBUG
219  if (k != 0) nc_++;
220 #endif
221  if( n ) return n->Value;
222  else return -1;
223 }
224 
225 template <typename KeyType, typename ValueType>
227  std::ostringstream oss;
228  oss << "HashTable<"
231  << "{ Size_: " << Size_ << " }";
232  return oss.str();
233 }
234 
235 template <typename KeyType, typename ValueType>
238  const Teuchos::EVerbosityLevel verbLevel) const {
239  using std::endl;
240  using std::setw;
241  using Teuchos::OSTab;
242  using Teuchos::rcpFromRef;
244  using Teuchos::VERB_DEFAULT;
245  using Teuchos::VERB_NONE;
246  using Teuchos::VERB_LOW;
247  using Teuchos::VERB_EXTREME;
248 
249  Teuchos::EVerbosityLevel vl = verbLevel;
250  if (vl == VERB_DEFAULT) vl = VERB_LOW;
251 
252  if (vl == VERB_NONE) {
253  // do nothing
254  }
255  else if (vl == VERB_LOW) {
256  out << this->description() << endl;
257  }
258  else { // MEDIUM, HIGH or EXTREME
259  out << "HashTable: {" << endl;
260  {
261  OSTab tab1 (rcpFromRef (out));
262 
263  const std::string label = this->getObjectLabel ();
264  if (label != "") {
265  out << "label: " << label << endl;
266  }
267  out << "Template parameters: {" << endl;
268  {
269  OSTab tab2 (rcpFromRef (out));
270  out << "KeyType: " << TypeNameTraits<KeyType>::name () << endl
271  << "ValueType" << TypeNameTraits<ValueType>::name () << endl;
272  }
273  out << "}" << endl << "Table parameters: {" << endl;
274  {
275  OSTab tab2 (rcpFromRef (out));
276  out << "Size_: " << Size_ << endl;
277  }
278  out << "}" << endl;
279 #ifdef HAVE_TEUCHOS_DEBUG
280  out << "Debug info: {" << endl;
281  {
282  OSTab tab2 (rcpFromRef (out));
283  out << "Maximum number of collisions for any key: " << maxc_ << endl
284  << "Total number of collisions: " << nc_ << endl;
285  }
286  out << "}" << endl;
287 #endif // HAVE_TEUCHOS_DEBUG
288 
289  if (vl >= VERB_EXTREME) {
290  out << "Contents: ";
291  if (Container_ == NULL || Size_ == 0) {
292  out << "[]" << endl;
293  } else {
294  out << "[" << endl;
295  {
296  OSTab tab2 (rcpFromRef (out));
297  for (KeyType i = 0; i < Size_; ++i) {
298  Node* curNode = Container_[i];
299  if (curNode == NULL) {
300  out << "NULL";
301  } else { // curNode != NULL
302  // Print all the buckets at the current table position i.
303  out << "[";
304  // Print the first bucket.
305  out << "[" << curNode->Key << "," << curNode->Value << "]";
306  curNode = curNode->Ptr;
307  // Print the remaining buckets, if there are any.
308  while (curNode != NULL) {
309  out << ", [" << curNode->Key << "," << curNode->Value << "]";
310  curNode = curNode->Ptr;
311  }
312  out << "]" << endl;
313  } // if curNode == or != NULL
314  if (i + 1 < Size_) {
315  out << ", ";
316  }
317  } // for each table position i
318  }
319  out << "]" << endl;
320  } // The table contains entries
321  } // vl >= VERB_EXTREME
322  }
323  out << "}" << endl;
324  } // if vl > VERB_LOW
325 }
326 
327 } // namespace Details
328 } // namespace Tpetra
329 
330 // Macro that explicitly instantiates HashTable for the given local
331 // ordinal (LO) and global ordinal (GO) types. Note that HashTable's
332 // template parameters occur in the opposite order of most Tpetra
333 // classes. This is because HashTable performs global-to-local
334 // lookup, and the convention in templated C++ lookup tables (such as
335 // std::map) is <KeyType, ValueType>.
336 //
337 // This macro must be explanded within the Tpetra::Details namespace.
338 #define TPETRA_HASHTABLE_INSTANT_DEFAULTNODE(LO,GO) \
339  template class HashTable< GO , LO >; \
340 
341 #endif
Namespace Tpetra contains the class and methods constituting the Tpetra library.
virtual std::string getObjectLabel() const
basic_OSTab< char > OSTab
HashTable(const int size, const unsigned int seed=(2654435761U))
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Implementation details of Tpetra.
ValueType get(const KeyType key)
Get the value corresponding to the given key.
void add(const KeyType key, const ValueType value)
Add a key and its value to the hash table.
std::string description() const
Implementation of Teuchos::Describable.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print this object with the given verbosity to the output stream.
static std::string name()