source: XIOS/trunk/src/utils.hpp @ 593

Last change on this file since 593 was 584, checked in by mhnguyen, 9 years ago

Implementing new hash algorithm and fixing bug related to zoom

+) Replace boost hash with hash algorithm of Jenkins
+) Domain, if an attribute is non-empty for one client, it should also be non-empty for others inspite of zoom
+) Replace the way to find the number of client connecting to a server to make sure every server receive a message

Test
+) On Curie
+) test_client: passed and results are same like before
+) test_complete: passed, results are partially the same, the different part comes from added working operation

File size: 5.4 KB
Line 
1/*!
2   \file utils.hpp
3   \author Ha NGUYEN
4   \since 06 Oct 2014
5   \date 10 Feb 2015
6
7
8   \brief Some utils for Xios
9 */
10
11#ifndef __XIOS_UTILS_HPP__
12#define __XIOS_UTILS_HPP__
13
14#include <vector>
15#include "array_new.hpp"
16#include "exception.hpp"
17
18namespace xios
19{
20  template<typename K>
21  struct CArrayTraits {
22    typedef K ArrayType;
23  };
24
25  template<typename K>
26  struct CArrayBoolTraits : public CArrayTraits<K> {
27    typedef bool Type;
28  };
29
30  template<>
31  struct CArrayBoolTraits<CArray<bool,1> >
32  {
33    static inline void resizeArray(CArray<bool,1>& boolArray, const std::vector<int>& dimensionSize)
34    {
35      if (1 != dimensionSize.size())
36        ERROR("utils::CArrayBoolTraits",
37                <<"Dimension of resized array mismatch"<<endl
38                <<"Dimension of resized is 1 "<<endl
39                <<"Dimension of vetor resizing is "<< dimensionSize.size());
40      boolArray.resize(dimensionSize[0]);
41    }
42  };
43
44  template<>
45  struct CArrayBoolTraits<CArray<bool,2> >
46  {
47    static inline void resizeArray(CArray<bool,2>& boolArray, const std::vector<int>& dimensionSize)
48    {
49      if (2 != dimensionSize.size())
50        ERROR("utils::CArrayBoolTraits",
51                <<"Dimension of resized array mismatch"<<endl
52                <<"Dimension of resized is 2 "<<endl
53                <<"Dimension of vetor resizing is "<< dimensionSize.size());
54      boolArray.resize(dimensionSize[0], dimensionSize[1]);
55    }
56  };
57
58  template<>
59  struct CArrayBoolTraits<CArray<bool,3> >
60  {
61    static inline void resizeArray(CArray<bool,3>& boolArray, const std::vector<int>& dimensionSize)
62    {
63      if (3 != dimensionSize.size())
64        ERROR("utils::CArrayBoolTraits",
65                <<"Dimension of resized array mismatch"<<endl
66                <<"Dimension of resized is 3 "<<endl
67                <<"Dimension of vetor resizing is "<< dimensionSize.size());
68      boolArray.resize(dimensionSize[0], dimensionSize[1], dimensionSize[2]);
69    }
70  };
71
72  template<>
73  struct CArrayBoolTraits<CArray<bool,4> >
74  {
75    static inline void resizeArray(CArray<bool,4>& boolArray, const std::vector<int>& dimensionSize)
76    {
77      if (4 != dimensionSize.size())
78        ERROR("utils::CArrayBoolTraits",
79                <<"Dimension of resized array mismatch"<<endl
80                <<"Dimension of resized is 4 "<<endl
81                <<"Dimension of vetor resizing is "<< dimensionSize.size());
82      boolArray.resize(dimensionSize[0], dimensionSize[1], dimensionSize[2], dimensionSize[3]);
83    }
84  };
85
86  template<>
87  struct CArrayBoolTraits<CArray<bool,5> >
88  {
89    static inline void resizeArray(CArray<bool,5>& boolArray, const std::vector<int>& dimensionSize)
90    {
91      if (5 != dimensionSize.size())
92        ERROR("utils::CArrayBoolTraits",
93                <<"Dimension of resized array mismatch"<<endl
94                <<"Dimension of resized is 5 "<<endl
95                <<"Dimension of vetor resizing is "<< dimensionSize.size());
96      boolArray.resize(dimensionSize[0], dimensionSize[1],
97                       dimensionSize[2], dimensionSize[3], dimensionSize[4]);
98    }
99  };
100
101  template<>
102  struct CArrayBoolTraits<CArray<bool,6> >
103  {
104    static inline void resizeArray(CArray<bool,6>& boolArray, const std::vector<int>& dimensionSize)
105    {
106      if (6 != dimensionSize.size())
107        ERROR("utils::CArrayBoolTraits",
108                <<"Dimension of resized array mismatch"<<endl
109                <<"Dimension of resized is 6 "<<endl
110                <<"Dimension of vetor resizing is "<< dimensionSize.size());
111      boolArray.resize(dimensionSize[0], dimensionSize[1],
112                       dimensionSize[2], dimensionSize[3],
113                       dimensionSize[4], dimensionSize[5]);
114    }
115  };
116
117  template<>
118  struct CArrayBoolTraits<CArray<bool,7> >
119  {
120    static inline void resizeArray(CArray<bool,7>& boolArray, const std::vector<int>& dimensionSize)
121    {
122      if (7 != dimensionSize.size())
123        ERROR("utils::CArrayBoolTraits",
124                <<"Dimension of resized array mismatch"<<endl
125                <<"Dimension of resized is 7 "<<endl
126                <<"Dimension of vetor resizing is "<< dimensionSize.size());
127      boolArray.resize(dimensionSize[0], dimensionSize[1],
128                       dimensionSize[2], dimensionSize[3],
129                       dimensionSize[4], dimensionSize[5], dimensionSize[6]);
130    }
131  };
132
133  template <int v>
134  struct Int2Type
135  {
136  enum { value = v };
137  };
138
139  template<typename T>
140  union TypeToBytes {
141    T value;
142    unsigned char bytes[sizeof(value)];
143  };
144
145  template<typename T>
146  struct HashAlgorithm
147  {
148    /*!
149      Adapted version of one-at-a-time hash by Bob Jenkins,
150      which is an expanded version of his Dr. Dobbs article.
151    */
152    static inline size_t jenkins_hash(const T& value)
153    {
154      TypeToBytes<T> u;
155      (u.value) = value;
156
157      size_t hash = 0;
158      size_t length = sizeof(value);
159
160      for (size_t i = 0; i < length; ++i)
161      {
162          hash += u.bytes[i];
163          hash += (hash << 10);
164          hash ^= (hash >> 6);
165      }
166      hash += (hash << 3);
167      hash ^= (hash >> 11);
168      hash += (hash << 15);
169
170      return hash;
171    }
172  };
173
174  template<typename T, typename Algo = Int2Type<0> >
175  struct HashXIOS
176  {
177    std::size_t operator()(const T& val)
178    {
179      Algo al;
180      return hash_value(val, al);
181    }
182
183  private:
184    size_t hash_value(const T& val, Int2Type<0>)
185    {
186      return HashAlgorithm<T>::jenkins_hash(val);
187    }
188  };
189}
190
191#endif // __UTILS_HPP__
Note: See TracBrowser for help on using the repository browser.