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_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 "Sacado_ConfigDefs.h"
36 
37 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
38 
40 
41 namespace Sacado {
42  namespace Fad {
43 
44  template <typename T>
45  using MemPoolStorage = Exp::MemPoolStorage<T>;
46 
47  }
48 }
49 
50 #else
51 
52 #include <new>
53 #include <cstring>
54 
55 #include "Sacado_Traits.hpp"
56 #include "Sacado_Fad_MemPool.hpp"
57 
58 namespace Sacado {
59 
60  namespace Fad {
61 
65  template <typename T, bool isScalar = IsScalarType<T>::value>
66  struct mp_array {
67 
69  static inline T* get(int sz, MemPool* pool) {
70  if (sz) {
71  T* m = static_cast<T*>(pool->alloc());
72  T* p = m;
73  for (int i=0; i<sz; ++i)
74  new (p++) T();
75  return m;
76  }
77  else
78  return NULL;
79  }
80 
82  static inline T* get_and_fill(int sz, MemPool* pool) {
83  if (sz) {
84  T* m = static_cast<T*>(pool->alloc());
85  T* p = m;
86  for (int i=0; i<sz; ++i)
87  new (p++) T(0.);
88  return m;
89  }
90  else
91  return NULL;
92  }
93 
98  static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
99  if (sz) {
100  T* m = static_cast<T*>(pool->alloc());
101  T* p = m;
102  for (int i=0; i<sz; ++i)
103  new (p++) T(*(src++));
104  return m;
105  }
106  else
107  return NULL;
108  }
109 
111  static inline void copy(const T* src, T* dest, int sz) {
112  for (int i=0; i<sz; ++i)
113  *(dest++) = *(src++);
114  }
115 
117  static inline void zero(T* dest, int sz) {
118  for (int i=0; i<sz; ++i)
119  *(dest++) = T(0.);
120  }
121 
123  static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
124  T* e = m+sz;
125  for (T* b = m; b!=e; b++)
126  b->~T();
127  pool->free((void*) m);
128  }
129  };
130 
135  template <typename T>
136  struct mp_array<T,true> {
137 
139  static inline T* get(int sz, MemPool* pool) {
140  if (sz) {
141  T* m = static_cast<T*>(pool->alloc());
142  return m;
143  }
144  else
145  return NULL;
146  }
147 
149  static inline T* get_and_fill(int sz, MemPool* pool) {
150  if (sz) {
151  T* m = static_cast<T*>(pool->alloc());
152  std::memset(m,0,sz*sizeof(T));
153  return m;
154  }
155  else
156  return NULL;
157  }
158 
163  static inline T* get_and_fill(const T* src, int sz, MemPool* pool) {
164  if (sz) {
165  T* m = static_cast<T*>(pool->alloc());
166  T* p = m;
167  for (int i=0; i<sz; ++i)
168  new (p++) T(*(src++));
169  return m;
170  }
171  else
172  return NULL;
173  }
174 
176  static inline void copy(const T* src, T* dest, int sz) {
177  std::memcpy(dest,src,sz*sizeof(T));
178  }
179 
181  static inline void zero(T* dest, int sz) {
182  if (sz > 0)
183  std::memset(dest,0,sz*sizeof(T));
184  }
185 
187  static inline void destroy_and_release(T* m, int sz, MemPool* pool) {
188  pool->free((void*) m);
189  }
190  };
191 
193  template <typename T>
195 
196  public:
197 
198  typedef T value_type;
199 
201  template <typename S>
203  val_(x), sz_(0), len_(0), dx_(NULL), myPool_(defaultPool_) {}
204 
206 
209  MemPoolStorage(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) :
210  val_(x), sz_(sz), len_(sz), myPool_(defaultPool_) {
211  if (zero_out == InitDerivArray)
213  else
215  }
216 
219  val_(x.val_), sz_(x.sz_), len_(x.sz_), myPool_(x.myPool_) {
221  }
222 
225  if (len_ != 0)
227  }
228 
231  if (this != &x) {
232  val_ = x.val_;
233  if (sz_ != x.sz_) {
234  sz_ = x.sz_;
235  if (x.sz_ > len_) {
236  if (len_ != 0)
238  len_ = x.sz_;
239  myPool_ = x.myPool_;
241  }
242  else
244  }
245  else
247  }
248  return *this;
249  }
250 
252  int size() const { return sz_;}
253 
255 
258  void resize(int sz) {
259  if (sz > len_) {
260  if (len_ != 0)
264  len_ = sz;
265  }
266  sz_ = sz;
267  }
268 
270 
274  void resizeAndZero(int sz) {
275  if (sz > len_) {
276  if (len_ != 0)
280  len_ = sz;
281  }
282  else if (sz > sz_)
284  sz_ = sz;
285  }
286 
288 
292  void expand(int sz) {
293  if (sz > len_) {
294  T* dx_new = mp_array<T>::get_and_fill(sz, myPool_);
295  mp_array<T>::copy(dx_, dx_new, sz_);
296  if (len_ > 0)
298  dx_ = dx_new;
299  len_ = sz;
300  }
301  else if (sz > sz_)
303  sz_ = sz;
304  }
305 
307  void zero() {
309  }
310 
312  const T& val() const { return val_; }
313 
315  T& val() { return val_; }
316 
318  const T* dx() const { return dx_;}
319 
321  T dx(int i) const { return sz_ ? dx_[i] : T(0.); }
322 
324  T& fastAccessDx(int i) { return dx_[i];}
325 
327  const T& fastAccessDx(int i) const { return dx_[i];}
328 
329  private:
330 
333 
335  int sz_;
336 
338  int len_;
339 
341  T* dx_;
342 
343  public:
344 
347 
348  protected:
349 
352 
353  }; // class MemPoolStorage
354 
355  } // namespace Fad
356 
357 } // namespace Sacado
358 
359 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
360 
361 #endif // SACADO_FAD_MEMPOOLSTORAGE_HPP
static void destroy_and_release(T *m, int sz, MemPool *pool)
Destroy array elements and release memory.
#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.
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.
const T & val() const
Returns value.
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.
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 * dx() const
Returns derivative array.
int len_
Derivative array length.
const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
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.
int size() const
Returns number of derivative components.
static void zero(T *dest, int sz)
Zero out array dest of length sz.
T dx(int i) const
Returns derivative component i with bounds checking.