cloudy  trunk
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vectorize.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2017 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 
4 #ifndef VECTORIZE_H
5 #define VECTORIZE_H
6 
7 #include "vectorize_reduce.h"
8 #include "vectorize_exp.h"
9 #include "vectorize_log.h"
10 #include "vectorize_sqrt.h"
11 #include "vectorize_hyper.h"
12 
13 //
14 // The t_avx_pool class maintains a pool of scratch arrays that can be used by the vectorization
15 // routines. The memory is correctly aligned on an AVX boundary.
16 //
17 // To use the pool, declare a pointer variable as follows
18 //
19 // #include "vectorize.h"
20 // avx_ptr<realnum> a(n), b(nlo, nhi), c(nlo, nhi);
21 //
22 // You can index the resulting pointers as you would a normal array
23 //
24 // realnum c = a[0]*b[2];
25 //
26 // Indexing the arrays is valid for the following indices:
27 //
28 // a[i] : for 0 <= i < n
29 // b[i] : for nlo <= i < nhi
30 //
31 // In the case of the array b[], the element b[nlo] will be correctly aligned on an AVX boundary. To
32 // pass the scratch arrays to the vectorization routine, use the following:
33 //
34 // vexp( b.ptr0(), c.ptr0(), nlo, nhi );
35 //
36 // When the avx_ptr variable goes out of scope, the scratch array will be released automatically.
37 // An avx_ptr supports bounds checking when the BOUNDS_CHECK macro is defined at compile time
38 // (similar to multi_arr and flex_arr).
39 //
40 // The t_avx_pool class maintains a minimum pool of p_min_size arrays. The size of the pool can grow
41 // when needed. When a scratch array is released with an index number >= p_min_size, the memory will
42 // be freed immediately to conserve memory.
43 //
44 // The default scratch array can hold p_def_size doubles. In the default setup that should be larger
45 // than rfield.nflux_with_check for efficiency since an array of that size is plausible to be
46 // requested. If the frequency mesh is larger (e.g. due to the SET CONTINUUM RESOLUTION command) the
47 // following optimization will be used. The first time an array larger than p_def_size doubles is
48 // requested, it will have to allocated. When that array is subsequently released, it is swapped
49 // with an unused smaller array with index < p_min_size, and then the smaller array is freed. This
50 // gives near optimal performance even for large frequency grids, at the expense of a slight
51 // overhead when releasing the scratch array.
52 //
53 
55 {
56  vector<void*> p_ptr;
57  vector<size_t> p_size;
58  vector<bool> p_used;
59 
60  // for efficiency this number should be larger than rfield.nflux_with_check
61  // in the default setup -- adjust this constant if that is not the case.
62  static const size_t p_def_size = 8500*sizeof(double);
63  static const size_t p_min_size = 30;
64 
65  void p_alloc(size_t sz, bool lgUsed)
66  {
67  void *p_ptr_alloc;
68  if( posix_memalign(&p_ptr_alloc, CD_ALIGN, sz) != 0 )
69  throw bad_alloc();
70  p_ptr.push_back(p_ptr_alloc);
71  p_size.push_back(sz);
72  p_used.push_back(lgUsed);
73  }
74 
75 public:
77  {
78  for( size_t i=0; i < p_min_size; ++i )
79  p_alloc(p_def_size,false);
80  }
82  {
83  for( size_t i=0; i < p_ptr.size(); ++i )
84  free(p_ptr[i]);
85  }
86  void* avx_alloc(size_t sz)
87  {
88  void* p_ptr_alloc = NULL;
89  for( size_t i=0; i < p_ptr.size(); i++ )
90  {
91  if( !p_used[i] && sz <= p_size[i] )
92  {
93  p_ptr_alloc = p_ptr[i];
94  p_used[i] = true;
95  break;
96  }
97  }
98  if( p_ptr_alloc == NULL )
99  {
100  p_alloc(sz,true);
101  p_ptr_alloc = p_ptr.back();
102  }
103  return p_ptr_alloc;
104  }
105  void avx_free(void* p_ptr_alloc)
106  {
107  size_t i;
108  for( i=0; i < p_ptr.size(); i++ )
109  {
110  if( p_ptr[i] == p_ptr_alloc )
111  {
112  if( i >= p_min_size )
113  {
114  //
115  size_t j = p_min_size;
116  for( j=0; j < p_min_size; ++j )
117  {
118  if( !p_used[j] && p_size[j] < p_size[i] )
119  break;
120  }
121  if( j < p_min_size )
122  {
123  free(p_ptr[j]);
124  p_ptr[j] = p_ptr[i];
125  p_size[j] = p_size[i];
126  }
127  else
128  {
129  free(p_ptr[i]);
130  }
131  p_ptr[i] = NULL;
132  p_size[i] = 0;
133  }
134  p_used[i] = false;
135  break;
136  }
137  }
138  // clean up unused entries at the back
139  while( p_ptr.back() == NULL )
140  {
141  p_ptr.pop_back();
142  p_size.pop_back();
143  p_used.pop_back();
144  }
145  }
146 };
147 
148 extern t_avx_pool avx_pool;
149 
150 template<class T, bool lgBC=lgBOUNDSCHECKVAL>
151 class avx_ptr
152 {
153  long p_begin;
154  long p_end;
156  T* p_ptr;
157 
158  // make default constructor private since it shouldn't be used
160  {
161  p_ptr = NULL;
162  p_ptr_alloc = NULL;
163  p_begin = 0;
164  p_end = 0;
165  }
166  void p_alloc(long begin, long end)
167  {
168  size_t sz = size_t(max(end-begin,0)*sizeof(T));
169  if( sz == 0 )
170  {
171  p_ptr_alloc = NULL;
172  p_ptr = NULL;
173  p_begin = 0;
174  p_end = 0;
175  }
176  else
177  {
178  p_ptr_alloc = static_cast<T*>(avx_pool.avx_alloc(sz));
179  p_ptr = p_ptr_alloc - begin;
180  p_begin = begin;
181  p_end = end;
182  }
183  }
184 public:
185  typedef T& reference;
186  typedef const T& const_reference;
187 
188  explicit avx_ptr(long size)
189  {
190  p_alloc(0, size);
191  }
192  avx_ptr(long begin, long end)
193  {
194  p_alloc(begin, end);
195  }
197  {
198  if( p_ptr_alloc != NULL )
199  avx_pool.avx_free(p_ptr_alloc);
200  }
202  {
203  if( lgBC && ( i < p_begin || i >= p_end ) )
204  OUT_OF_RANGE( "avx_ptr::operator[]" );
205  return *(p_ptr+i);
206  }
208  {
209  if( lgBC && ( i < p_begin || i >= p_end ) )
210  OUT_OF_RANGE( "avx_ptr::operator[]" );
211  return *(p_ptr+i);
212  }
213  T* data()
214  {
215  return p_ptr_alloc;
216  }
217  const T* data() const
218  {
219  return p_ptr_alloc;
220  }
221  T* ptr0()
222  {
223  return p_ptr;
224  }
225  const T* ptr0() const
226  {
227  return p_ptr;
228  }
229 };
230 
231 //
232 // The allocator_avx class can be used to get a vector with internal data aligned on an AVX boundary
233 //
234 // Use the class as follows:
235 //
236 // #include "vectorize.h"
237 // vector< realnum, allocator_avx<realnum> > arr;
238 //
239 // When C++11 is in effect, you can also use the following shorthand:
240 //
241 // vector_avx<realnum> arr;
242 //
243 
244 template<class T>
246 
247 template<>
248 class allocator_avx<void>
249 {
250 public:
251  typedef size_t size_type;
252  typedef ptrdiff_t difference_type;
253  typedef void* pointer;
254  typedef const void* const_pointer;
255  typedef void value_type;
256 
257  template<class U>
258  struct rebind { typedef allocator_avx<U> other; };
259 
260 #if __cplusplus >= 201103L
261  typedef true_type propagate_on_container_move_assignment;
262 #endif
263 };
264 
265 template<class T>
266 class allocator_avx : public allocator<T>
267 {
268 public:
269  typedef size_t size_type;
270  typedef ptrdiff_t difference_type;
271  typedef T* pointer;
272  typedef const T* const_pointer;
273  typedef T& reference;
274  typedef const T& const_reference;
275  typedef T value_type;
276 
277  template<class U>
278  struct rebind
279  {
281  };
282 
283 #if __cplusplus >= 201103L
284  typedef true_type propagate_on_container_move_assignment;
285 #endif
286 
287  allocator_avx() throw() {}
288 
289  allocator_avx(const allocator_avx& a) throw() : allocator<T>(a) {}
290 
291  template<class U>
292  allocator_avx(const allocator_avx<U>&) throw() {}
293 
294  ~allocator_avx() throw() {}
295 
297  {
298  void* p;
299  if( posix_memalign(&p, CD_ALIGN, n*sizeof(T)) != 0 )
300  throw bad_alloc();
301  return pointer(p);
302  }
303 
304  void deallocate(pointer p, size_type) throw()
305  {
306  free(p);
307  }
308 };
309 
310 template<class T, class U>
311 inline bool operator== (const allocator_avx<T>&, const allocator_avx<U>&)
312 {
313  return true;
314 }
315 
316 template<class T>
317 inline bool operator== (const allocator_avx<T>&, const allocator_avx<T>&)
318 {
319  return true;
320 }
321 
322 template<class T, class U>
323 inline bool operator!= (const allocator_avx<T>&, const allocator_avx<U>&)
324 {
325  return false;
326 }
327 
328 template<class T>
329 inline bool operator!= (const allocator_avx<T>&, const allocator_avx<T>&)
330 {
331  return false;
332 }
333 
334 #if __cplusplus >= 201103L
335 
336 template<typename T>
337 using vector_avx = typename std::vector<T,allocator_avx<T>>;
338 
339 #endif
340 
341 #endif
T * p_ptr
Definition: vectorize.h:156
t_avx_pool()
Definition: vectorize.h:76
static const size_t p_min_size
Definition: vectorize.h:63
const T * const_pointer
Definition: vectorize.h:272
T * ptr0()
Definition: vectorize.h:221
void avx_free(void *p_ptr_alloc)
Definition: vectorize.h:105
ptrdiff_t difference_type
Definition: vectorize.h:270
allocator_avx< U > other
Definition: vectorize.h:258
allocator_avx(const allocator_avx< U > &)
Definition: vectorize.h:292
long p_end
Definition: vectorize.h:154
vector< size_t > p_size
Definition: vectorize.h:57
t_avx_pool avx_pool
Definition: vectorize.cpp:7
avx_ptr(long size)
Definition: vectorize.h:188
void p_alloc(size_t sz, bool lgUsed)
Definition: vectorize.h:65
ptrdiff_t difference_type
Definition: vectorize.h:252
const T * ptr0() const
Definition: vectorize.h:225
NORETURN void OUT_OF_RANGE(const char *str)
Definition: cddefines.h:647
bool operator!=(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:113
void * avx_alloc(size_t sz)
Definition: vectorize.h:86
allocator_avx< U > other
Definition: vectorize.h:280
void deallocate(pointer p, size_type)
Definition: vectorize.h:304
avx_ptr()
Definition: vectorize.h:159
reference operator[](long i)
Definition: vectorize.h:201
T * data()
Definition: vectorize.h:213
long max(int a, long b)
Definition: cddefines.h:821
~avx_ptr()
Definition: vectorize.h:196
void p_alloc(long begin, long end)
Definition: vectorize.h:166
const T & const_reference
Definition: vectorize.h:186
bool operator==(const count_ptr< T > &a, const count_ptr< T > &b)
Definition: count_ptr.h:108
~t_avx_pool()
Definition: vectorize.h:81
const void * const_pointer
Definition: vectorize.h:254
avx_ptr(long begin, long end)
Definition: vectorize.h:192
allocator_avx(const allocator_avx &a)
Definition: vectorize.h:289
const T & const_reference
Definition: vectorize.h:274
pointer allocate(size_type n, typename allocator_avx< void >::const_pointer=NULL)
Definition: vectorize.h:296
T & reference
Definition: vectorize.h:185
const T * data() const
Definition: vectorize.h:217
#define CD_ALIGN
Definition: cpu.h:156
vector< void * > p_ptr
Definition: vectorize.h:56
size_t size_type
Definition: vectorize.h:269
long p_begin
Definition: vectorize.h:153
vector< bool > p_used
Definition: vectorize.h:58
static const size_t p_def_size
Definition: vectorize.h:62
T * p_ptr_alloc
Definition: vectorize.h:155