Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_Exp_MemPoolStorage.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_FAD_EXP_MEMPOOLSTORAGE_HPP
31 #define SACADO_FAD_EXP_MEMPOOLSTORAGE_HPP
32 
33 #include <type_traits>
34 #include <new>
35 #include <cstring>
36 
37 #include "Sacado_Traits.hpp"
38 #include "Sacado_Fad_MemPool.hpp"
39 
40 namespace Sacado {
41 
42  namespace Fad {
43  namespace Exp {
44 
48  template <typename T, bool isScalar = IsScalarType<T>::value>
49  struct mp_array {
50 
52  static inline T* get(int sz, MemPool* pool) {
53  if (sz) {
54  T* m = static_cast<T*>(pool->alloc());
55  T* p = m;
56  for (int i=0; i<sz; ++i)
57  new (p++) T();
58  return m;
59  }
60  else
61  return NULL;
62  }
63 
65  static inline T* get_and_fill(int sz, MemPool* pool) {
66  if (sz) {
67  T* m = static_cast<T*>(pool->alloc());
68  T* p = m;
69  for (int i=0; i<sz; ++i)
70  new (p++) T(0.);
71  return m;
72  }
73  else
74  return NULL;
75  }
76 
81  static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
82  if (sz) {
83  T* m = static_cast<T*>(pool->alloc());
84  T* p = m;
85  for (int i=0; i<sz; ++i)
86  new (p++) T(*(src++));
87  return m;
88  }
89  else
90  return NULL;
91  }
92 
94  static inline void copy(const T* src, T* dest, int sz) {
95  for (int i=0; i<sz; ++i)
96  *(dest++) = *(src++);
97  }
98 
100  static inline void zero(T* dest, int sz) {
101  for (int i=0; i<sz; ++i)
102  *(dest++) = T(0.);
103  }
104 
106  static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
107  T* e = m+sz;
108  for (T* b = m; b!=e; b++)
109  b->~T();
110  pool->free((void*) m);
111  }
112  };
113 
118  template <typename T>
119  struct mp_array<T,true> {
120 
122  static inline T* get(int sz, MemPool* pool) {
123  if (sz) {
124  T* m = static_cast<T*>(pool->alloc());
125  return m;
126  }
127  else
128  return NULL;
129  }
130 
132  static inline T* get_and_fill(int sz, MemPool* pool) {
133  if (sz) {
134  T* m = static_cast<T*>(pool->alloc());
135  std::memset(m,0,sz*sizeof(T));
136  return m;
137  }
138  else
139  return NULL;
140  }
141 
146  static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
147  if (sz) {
148  T* m = static_cast<T*>(pool->alloc());
149  T* p = m;
150  for (int i=0; i<sz; ++i)
151  new (p++) T(*(src++));
152  return m;
153  }
154  else
155  return NULL;
156  }
157 
159  static inline void copy(const T* src, T* dest, int sz) {
160  std::memcpy(dest,src,sz*sizeof(T));
161  }
162 
164  static inline void zero(T* dest, int sz) {
165  if (sz > 0)
166  std::memset(dest,0,sz*sizeof(T));
167  }
168 
170  static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
171  pool->free((void*) m);
172  }
173  };
174 
176  template <typename T>
178 
179  public:
180 
181  typedef typename std::remove_cv<T>::type value_type;
182  static constexpr bool is_statically_sized = false;
183  static constexpr int static_size = 0;
184 
186  template <typename TT>
187  struct apply {
189  };
190 
192  template <int N>
193  struct apply_N {
195  };
196 
200  val_(), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
201 
203  MemPoolStorage(const T & x) :
204  val_(x), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
205 
207 
210  MemPoolStorage(const int sz, const T & x, const DerivInit zero_out) :
211  val_(x), sz_(sz), len_(sz), myPool_(defaultPool_) {
212  if (zero_out == InitDerivArray)
214  else
216  }
217 
220  val_(x.val_), sz_(x.sz_), len_(x.sz_), myPool_(x.myPool_) {
222  }
223 
226  if (len_ != 0)
228  }
229 
232  if (this != &x) {
233  val_ = x.val_;
234  if (sz_ != x.sz_) {
235  sz_ = x.sz_;
236  if (x.sz_ > len_) {
237  if (len_ != 0)
239  len_ = x.sz_;
240  myPool_ = x.myPool_;
242  }
243  else
245  }
246  else
248  }
249  return *this;
250  }
251 
253  static void setDefaultPool(MemPool* pool) {
254  defaultPool_ = pool;
255  }
256 
258  int size() const { return sz_;}
259 
262  int length() const { return len_; }
263 
265 
268  void resize(int sz) {
269  if (sz > len_) {
270  if (len_ != 0)
274  len_ = sz;
275  }
276  sz_ = sz;
277  }
278 
280 
284  void resizeAndZero(int sz) {
285  if (sz > len_) {
286  if (len_ != 0)
290  len_ = sz;
291  }
292  else if (sz > sz_)
294  sz_ = sz;
295  }
296 
298 
302  void expand(int sz) {
303  if (sz > len_) {
304  T* dx_new = mp_array<T>::get_and_fill(sz, myPool_);
305  mp_array<T>::copy(dx_, dx_new, sz_);
306  if (len_ > 0)
308  dx_ = dx_new;
309  len_ = sz;
310  }
311  else if (sz > sz_)
313  sz_ = sz;
314  }
315 
317  void zero() {
319  }
320 
322  const T& val() const { return val_; }
323 
325  T& val() { return val_; }
326 
328  const T* dx() const { return dx_;}
329 
331  T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
332 
334  T& fastAccessDx(int i) { return dx_[i];}
335 
337  const T& fastAccessDx(int i) const { return dx_[i];}
338 
339  private:
340 
343 
345  int sz_;
346 
348  int len_;
349 
351  T* dx_;
352 
353  public:
354 
357 
358  protected:
359 
362 
363  }; // class MemPoolStorage
364 
365  } // namespace Exp
366  } // namespace Fad
367 
368 } // namespace Sacado
369 
370 #endif // SACADO_FAD_EXP_MEMPOOLSTORAGE_HPP
KOKKOS_INLINE_FUNCTION MemPoolStorage()
Default constructor.
void zero()
Zero out derivative array.
static MemPool * defaultPool_
Default memory pool.
MemPoolStorage & operator=(const MemPoolStorage &x)
Assignment.
MemPoolStorage(const MemPoolStorage &x)
Copy constructor.
int size() const
Returns number of derivative components.
static T * get_and_fill(const T *src, int sz, MemPool *pool)
Get memory for new array of length sz and fill with entries from src.
static T * get_and_fill(int sz, MemPool *pool)
Get memory for new array of length sz and fill with zeros.
static void destroy_and_release(T *m, int sz, MemPool *pool)
Destroy array elements and release memory.
expr true
MemPoolStorage(const int sz, const T &x, const DerivInit zero_out)
Constructor with size sz.
void free(void *b)
Free an element.
T dx(int i) const
Returns derivative component i with bounds checking.
MemPoolStorage(const T &x)
Constructor with value.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
static T * get_and_fill(const T *src, int sz, MemPool *pool)
Get memory for new array of length sz and fill with entries from src.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
Dynamic array allocation class that works for any type.
static T * get(int sz, MemPool *pool)
Get memory for new array of length sz.
const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION int length() const
Returns array length.
T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
void expand(int sz)
Expand derivative array to size sz.
static void destroy_and_release(T *m, int sz, MemPool *pool)
Destroy array elements and release memory.
void resizeAndZero(int sz)
Resize the derivative array to sz.
void resize(int sz)
Resize the derivative array to sz.
void * alloc()
Allocate a new element.
Initialize the derivative array.
Turn MemPoolStorage into a meta-function class usable with mpl::apply.
static T * get_and_fill(int sz, MemPool *pool)
Get memory for new array of length sz and fill with zeros.
static void setDefaultPool(MemPool *pool)
Set the default memory pool for new objects.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
Derivative array storage class using dynamic memory allocation.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
const T * dx() const
Returns derivative array.