Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_Fad_Exp_VectorDynamicStorage.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_VECTORDYNAMICSTORAGE_HPP
31#define SACADO_FAD_EXP_VECTORDYNAMICSTORAGE_HPP
32
33#include <type_traits>
34#include <utility>
35
36#include "Sacado_Traits.hpp"
38
39namespace Sacado {
40
41 namespace Fad {
42 namespace Exp {
43
45 template <typename T, typename U = T>
47
48 public:
49
50 typedef typename std::remove_cv<T>::type value_type;
51 static constexpr bool is_statically_sized = false;
52 static constexpr int static_size = 0;
53 static constexpr bool is_view = false;
54
56 template <typename TT, typename UU = TT>
57 struct apply {
59 };
60
62 template <int N>
63 struct apply_N {
65 };
66
70 v_(), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_),
72 {}
73
77 v_(x), owns_mem(true), sz_(0), len_(0), stride_(1), val_(&v_),
79 {}
80
82
86 VectorDynamicStorage(const int sz, const T & x,
87 const DerivInit zero_out = InitDerivArray) :
88 v_(x), owns_mem(true), sz_(sz), len_(sz), stride_(1), val_(&v_) {
89 if (zero_out == InitDerivArray)
91 else
93 }
94
96
102 VectorDynamicStorage(const int sz, const int i, const value_type & x) :
104 dx_[i]=1.;
105 }
106
109 VectorDynamicStorage(const int sz, T* x, U* dx_p, const int stride,
110 bool zero_out) :
111 v_(), owns_mem(false), sz_(sz), len_(sz), stride_(stride),
112 val_(x), dx_(dx_p) {
113 if (zero_out)
114 zero();
115 }
116
124
125 // Move does not make sense for this storage since it is always tied to
126 // some preallocated data. Don't define move constructor so compiler will
127 // always fall-back to copy
128
132 if (owns_mem) {
133 if (len_ != 0)
135 }
136 }
137
141 if (this != &x) {
142 *val_ = *x.val_;
143 if (sz_ != x.sz_) {
144 sz_ = x.sz_;
145 if (x.sz_ > len_) {
146#if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
147 if (!owns_mem)
148 throw "Can\'t resize beyond original size when memory isn't owned!";
149#endif
150 if (len_ != 0)
152 len_ = x.sz_;
154 }
155 else
156 ds_array<U>::strided_copy(x.dx_, x.stride_, dx_, stride_, sz_);
157 }
158 else
159 ds_array<U>::strided_copy(x.dx_, x.stride_, dx_, stride_, sz_);
160 }
161 return *this;
162 }
163
164 // Move does not make sense for this storage since it is always tied to
165 // some preallocated data. Don't define move assignment so compiler will
166 // always fall-back to copy
167
170 int size() const { return sz_;}
171
174 int length() const { return len_; }
175
177
181 void resize(int sz) {
182 if (sz > len_) {
183#if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
184 if (!owns_mem)
185 throw "Can\'t resize beyond original size when memory isn't owned!";
186#endif
187 if (len_ != 0)
189 len_ = sz;
191 }
192 sz_ = sz;
193 }
194
196
201 void resizeAndZero(int sz) {
202 if (sz > len_) {
203#if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
204 if (!owns_mem)
205 throw "Can\'t resize beyond original size when memory isn't owned!";
206#endif
207 if (len_ != 0)
209 len_ = sz;
211 }
212 else if (sz > sz_)
214 sz_ = sz;
215 }
216
218
223 void expand(int sz) {
224 if (sz > len_) {
225#if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ ) && !defined(__HIP_DEVICE_COMPILE__)
226 if (!owns_mem)
227 throw "Can\'t resize beyond original size when memory isn't owned!";
228#endif
231 if (len_ > 0)
233 dx_ = dx_new;
234 len_ = sz;
235 }
236 else if (sz > sz_)
238 sz_ = sz;
239 }
240
246
249 void setMemory(int sz, T* x, U* dx_p, int stride) {
250
251 // Destroy old memory
252 if (owns_mem) {
253 if (len_ != 0)
255 }
256
257 // Set new values
258 owns_mem = false;
259 sz_ = sz;
260 len_ = sz;
261 stride_ = stride;
262 val_ = x;
263 dx_ = dx_p;
264 }
265
268 const T& val() const { return *val_; }
269
272 T& val() { return *val_; }
273
276 const U* dx() const { return dx_;}
277
280 U dx(int i) const { return sz_ ? dx_[i*stride_] : T(0.); }
281
284 U& fastAccessDx(int i) { return dx_[i*stride_];}
285
288 const U& fastAccessDx(int i) const { return dx_[i*stride_];}
289
290 private:
291
292 T v_;
293
294 private:
295
298
300 int sz_;
301
303 int len_;
304
307
310
313
314 }; // class VectorDynamicStorage
315
316 } // namespace Exp
317 } // namespace Fad
318
319} // namespace Sacado
320
321#endif // SACADO_FAD_VECTORDYNAMICSTORAGE_HPP
#define SACADO_INLINE_FUNCTION
expr expr expr bar false
expr true
#define T
Fad specializations for Teuchos::BLAS wrappers.
Derivative array storage class using dynamic memory allocation.
SACADO_INLINE_FUNCTION const U & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, const int i, const value_type &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION void resizeAndZero(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz.
SACADO_INLINE_FUNCTION U & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION const U * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const T &x)
Constructor with value.
SACADO_INLINE_FUNCTION U dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const VectorDynamicStorage &x)
Copy constructor.
SACADO_INLINE_FUNCTION VectorDynamicStorage & operator=(const VectorDynamicStorage &x)
Assignment.
SACADO_INLINE_FUNCTION VectorDynamicStorage(const int sz, T *x, U *dx_p, const int stride, bool zero_out)
Constructor with supplied memory.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize the derivative array to sz.
SACADO_INLINE_FUNCTION VectorDynamicStorage()
Default constructor.
SACADO_INLINE_FUNCTION int length() const
Returns array length.
SACADO_INLINE_FUNCTION void zero()
Zero out derivative array.
SACADO_INLINE_FUNCTION void setMemory(int sz, T *x, U *dx_p, int stride)
Set value/derivative array memory.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors.
@ InitDerivArray
Initialize the derivative array.
Turn DynamicStorage into a meta-function class usable with mpl::apply.
static SACADO_INLINE_FUNCTION T * get(int sz)
Get memory for new array of length sz.
static SACADO_INLINE_FUNCTION T * strided_get_and_fill(const T *src, int stride, int sz)
Get memory for new array of length sz and fill with entries from src.
static SACADO_INLINE_FUNCTION void strided_copy(const T *src, int src_stride, T *dest, int dest_stride, int sz)
Copy array from src to dest of length sz.
static SACADO_INLINE_FUNCTION T * get_and_fill(int sz)
Get memory for new array of length sz and fill with zeros.
static SACADO_INLINE_FUNCTION void destroy_and_release(T *m, int sz)
Destroy array elements and release memory.
static SACADO_INLINE_FUNCTION void strided_zero(T *dest, int stride, int sz)
Zero out array dest of length sz.
static SACADO_INLINE_FUNCTION void copy(const T *src, T *dest, int sz)
Copy array from src to dest of length sz.