Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_Fad_MemPoolStorage.hpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 #ifndef SACADO_FAD_MEMPOOLSTORAGE_HPP
33 #define SACADO_FAD_MEMPOOLSTORAGE_HPP
34 
35 #include <new>
36 #include <cstring>
37 
38 #include "Sacado_Traits.hpp"
39 #include "Sacado_Fad_MemPool.hpp"
40 
41 namespace Sacado {
42 
43  namespace Fad {
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 T value_type;
182 
184  template <typename S>
186  val_(x), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
187 
189 
192  MemPoolStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
193  val_(x), sz_(sz), len_(sz), myPool_(defaultPool_) {
194  if (zero_out == InitDerivArray)
196  else
198  }
199 
202  val_(x.val_), sz_(x.sz_), len_(x.sz_), myPool_(x.myPool_) {
204  }
205 
208  if (len_ != 0)
210  }
211 
214  if (this != &x) {
215  val_ = x.val_;
216  if (sz_ != x.sz_) {
217  sz_ = x.sz_;
218  if (x.sz_ > len_) {
219  if (len_ != 0)
221  len_ = x.sz_;
222  myPool_ = x.myPool_;
224  }
225  else
227  }
228  else
230  }
231  return *this;
232  }
233 
235  int size() const { return sz_;}
236 
238 
241  void resize(int sz) {
242  if (sz > len_) {
243  if (len_ != 0)
247  len_ = sz;
248  }
249  sz_ = sz;
250  }
251 
253 
257  void resizeAndZero(int sz) {
258  if (sz > len_) {
259  if (len_ != 0)
263  len_ = sz;
264  }
265  else if (sz > sz_)
267  sz_ = sz;
268  }
269 
271 
275  void expand(int sz) {
276  if (sz > len_) {
277  T* dx_new = mp_array<T>::get_and_fill(sz, myPool_);
278  mp_array<T>::copy(dx_, dx_new, sz_);
279  if (len_ > 0)
281  dx_ = dx_new;
282  len_ = sz;
283  }
284  else if (sz > sz_)
286  sz_ = sz;
287  }
288 
290  void zero() {
292  }
293 
295  const T& val() const { return val_; }
296 
298  T& val() { return val_; }
299 
301  const T* dx() const { return dx_;}
302 
304  T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
305 
307  T& fastAccessDx(int i) { return dx_[i];}
308 
310  const T& fastAccessDx(int i) const { return dx_[i];}
311 
312  private:
313 
316 
318  int sz_;
319 
321  int len_;
322 
324  T* dx_;
325 
326  public:
327 
330 
331  protected:
332 
335 
336  }; // class MemPoolStorage
337 
338  } // namespace Fad
339 
340 } // namespace Sacado
341 
342 #endif // SACADO_FAD_MEMPOOLSTORAGE_HPP
static void destroy_and_release(T *m, int sz, MemPool *pool)
Destroy array elements and release memory.
T dx(int i) const
Returns derivative component i with bounds checking.
#define SACADO_ENABLE_VALUE_CTOR_DECL
Sacado::Fad::MemPool * defaultPool_
void resizeAndZero(int sz)
Resize the derivative array to sz.
static T * get_and_fill(int sz, MemPool *pool)
Get memory for new array of length sz and fill with zeros.
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.
const T & val() const
Returns value.
expr true
void resize(int sz)
Resize the derivative array to sz.
void free(void *b)
Free an element.
static MemPool * defaultPool_
Default memory pool.
T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
static T * get_and_fill(int sz, MemPool *pool)
Get memory for new array of length sz and fill with zeros.
MemPoolStorage(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Default constructor.
#define T
Definition: Sacado_rad.hpp:573
MemPoolStorage & operator=(const MemPoolStorage &x)
Assignment.
Dynamic array allocation class that works for any type.
static T * get(int sz, MemPool *pool)
Get memory for new array of length sz.
static void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.
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.
void zero()
Zero out derivative array.
MemPoolStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
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.
int size() const
Returns number of derivative components.
static void destroy_and_release(T *m, int sz, MemPool *pool)
Destroy array elements and release memory.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
int len_
Derivative array length.
void * alloc()
Allocate a new element.
Initialize the derivative array.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
void expand(int sz)
Expand derivative array to size sz.
MemPoolStorage(const MemPoolStorage &x)
Copy constructor.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
const T * dx() const
Returns derivative array.