Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_DynamicStorage.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef STOKHOS_DYNAMIC_STORAGE_HPP
11 #define STOKHOS_DYNAMIC_STORAGE_HPP
12 
14 
15 #include "Kokkos_Macros.hpp"
16 
17 #include "Sacado_Traits.hpp"
18 #include "Stokhos_KokkosTraits.hpp"
19 #include <sstream>
20 
21 namespace Stokhos {
22 
23  template <typename ordinal_t, typename value_t, typename device_t>
25  public:
26 
27  static const bool is_static = false;
28  static const int static_size = 0;
29  static const bool supports_reset = false;
30 
31  typedef ordinal_t ordinal_type;
32  typedef value_t value_type;
34  typedef typename device_t::memory_space memory_space;
36  typedef volatile value_type& volatile_reference;
37  typedef const value_type& const_reference;
38  typedef const volatile value_type& const_volatile_reference;
39  typedef value_type* pointer;
40  typedef volatile value_type* volatile_pointer;
41  typedef const value_type* const_pointer;
42  typedef const volatile value_type* const_volatile_pointer;
44 
46  template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
47  struct apply {
49  };
50 
51  template <int N>
52  struct apply_N {
54  };
55 
57  KOKKOS_INLINE_FUNCTION
59  const value_type& x = value_type(0.0)) :
60  sz_(sz), is_view_(false) {
61  if (sz_ == 0) {
62  sz_ = 1;
63  coeff_0_ = x;
64  coeff_ = &coeff_0_;
65  is_constant_ = true;
66  }
67  else if (sz_ == 1) {
68  coeff_0_ = x;
69  coeff_ = &coeff_0_;
70  is_constant_ = true;
71  }
72  else {
74  is_constant_ = false;
75  }
76  }
77 
79  KOKKOS_INLINE_FUNCTION
80  DynamicStorage(const ordinal_type& sz, const value_type* x) :
81  sz_(sz), is_view_(false) {
82  if (sz_ == 0) {
83  sz_ = 1;
84  coeff_0_ = 0.0;
85  coeff_ = &coeff_0_;
86  is_constant_ = true;
87  }
88  else if (sz_ == 1) {
89  coeff_0_ = *x;
90  coeff_ = &coeff_0_;
91  is_constant_ = true;
92  }
93  else {
95  is_constant_ = false;
96  }
97  }
98 
100  // This might be problematic if sz == 1 and owned == true
101  KOKKOS_INLINE_FUNCTION
102  DynamicStorage(const ordinal_type& sz, pointer v, bool owned) :
103  coeff_(v), sz_(sz), is_view_(!owned), is_constant_(false) {}
104 
106  KOKKOS_INLINE_FUNCTION
108  sz_(s.sz_), is_view_(false) {
109  if (sz_ > 1) {
111  is_constant_ = false;
112  }
113  else {
114  coeff_0_ = s.coeff_[0];
115  coeff_ = &coeff_0_;
116  is_constant_ = true;
117  }
118  }
119 
121  KOKKOS_INLINE_FUNCTION
122  DynamicStorage(const volatile DynamicStorage& s) :
123  sz_(s.sz_), is_view_(false) {
124  if (sz_ > 1) {
126  is_constant_ = false;
127  }
128  else {
129  coeff_0_ = s.coeff_[0];
130  coeff_ = &coeff_0_;
131  is_constant_ = true;
132  }
133  }
134 
136  KOKKOS_INLINE_FUNCTION
139  }
140 
142  // To do: add error check if is_view_ == true && s.sz_ > sz_
143  KOKKOS_INLINE_FUNCTION
145  if (&s != this) {
146  // Only reallocate if we own the array and the sizes
147  // differ
148  if (!is_view_ && s.sz_ != sz_) {
149  if (!is_constant_)
151  if (s.sz_ > 1) {
153  is_constant_ = false;
154  }
155  else {
156  coeff_0_ = s.coeff_[0];
157  coeff_ = &coeff_0_;
158  is_constant_ = true;
159  }
160  sz_ = s.sz_;
161  }
162  else {
163  // This should work if is_view_ or s.sz_ == sz_, regardless
164  // of whether s.sz_ or sz_ is 1
165  ds::copy(s.coeff_, coeff_, s.sz_);
166  }
167  }
168  return *this;
169  }
170 
172  // To do: add error check if is_view_ == true && s.sz_ > sz_
173  KOKKOS_INLINE_FUNCTION
175  if (&s != this) {
176  // Only reallocate if we own the array and the sizes
177  // differ
178  if (!is_view_ && s.sz_ != sz_) {
179  if (!is_constant_)
181  if (s.sz_ > 1) {
183  is_constant_ = false;
184  }
185  else {
186  coeff_0_ = s.coeff_[0];
187  coeff_ = &coeff_0_;
188  is_constant_ = true;
189  }
190  sz_ = s.sz_;
191  }
192  else {
193  // This should work if is_view_ or s.sz_ == sz_, regardless
194  // of whether s.sz_ or sz_ is 1
195  ds::copy(s.coeff_, coeff_, s.sz_);
196  }
197  }
198  return *this;
199  }
200 
202  // To do: add error check if is_view_ == true && s.sz_ > sz_
203  KOKKOS_INLINE_FUNCTION
204  /*volatile*/ DynamicStorage& operator=(const DynamicStorage& s) volatile {
205  if (&s != this) {
206  // Only reallocate if we own the array and the sizes
207  // differ
208  if (!is_view_ && s.sz_ != sz_) {
209  if (!is_constant_)
211  if (s.sz_ > 1) {
213  is_constant_ = false;
214  }
215  else {
216  coeff_0_ = s.coeff_[0];
217  coeff_ = const_cast<value_type*>(&coeff_0_);
218  is_constant_ = true;
219  }
220  sz_ = s.sz_;
221  }
222  else {
223  // This should work if is_view_ or s.sz_ == sz_, regardless
224  // of whether s.sz_ or sz_ is 1
225  ds::copy(s.coeff_, coeff_, s.sz_);
226  }
227  }
228  return const_cast<DynamicStorage&>(*this);
229  }
230 
232  // To do: add error check if is_view_ == true && s.sz_ > sz_
233  KOKKOS_INLINE_FUNCTION
234  /*volatile*/ DynamicStorage&
235  operator=(const volatile DynamicStorage& s) volatile {
236  if (&s != this) {
237  // Only reallocate if we own the array and the sizes
238  // differ
239  if (!is_view_ && s.sz_ != sz_) {
240  if (!is_constant_)
242  if (s.sz_ > 1) {
244  is_constant_ = false;
245  }
246  else {
247  coeff_0_ = s.coeff_[0];
248  coeff_ = &coeff_0_;
249  is_constant_ = true;
250  }
251  sz_ = s.sz_;
252  }
253  else {
254  // This should work if is_view_ or s.sz_ == sz_, regardless
255  // of whether s.sz_ or sz_ is 1
256  ds::copy(s.coeff_, coeff_, s.sz_);
257  }
258  }
259  return const_cast<DynamicStorage&>(*this);
260  }
261 
263  KOKKOS_INLINE_FUNCTION
265  ds::fill(coeff_, sz_, v);
266  }
267 
269  KOKKOS_INLINE_FUNCTION
270  void init(const_reference v) volatile {
271  ds::fill(coeff_, sz_, v);
272  }
273 
275  KOKKOS_INLINE_FUNCTION
276  void init(const_pointer v, const ordinal_type& sz = 0) {
277  if (sz == 0)
278  ds::copy(v, coeff_, sz_);
279  else
280  ds::copy(v, coeff_, sz);
281  }
282 
284  KOKKOS_INLINE_FUNCTION
285  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
286  if (sz == 0)
287  ds::copy(v, coeff_, sz_);
288  else
289  ds::copy(v, coeff_, sz);
290  }
291 
293  KOKKOS_INLINE_FUNCTION
294  void load(pointer v) {
295  ds::copy(v, coeff_, sz_);
296  }
297 
299  KOKKOS_INLINE_FUNCTION
300  void load(pointer v) volatile {
301  ds::copy(v, coeff_, sz_);
302  }
303 
305  KOKKOS_INLINE_FUNCTION
306  void resize(const ordinal_type& sz) {
307  if (!is_view_ && sz != sz_) {
308  if (sz > 1) {
309  value_type *coeff_new = ds::get_and_fill(sz);
310  if (sz > sz_)
311  ds::copy(coeff_, coeff_new, sz_);
312  else
313  ds::copy(coeff_, coeff_new, sz);
314  if (!is_view_ && !is_constant_)
316  coeff_ = coeff_new;
317  is_constant_ = false;
318  }
319  else {
320  coeff_0_ = coeff_[0];
321  if (!is_constant_)
323  coeff_ = &coeff_0_;
324  is_constant_ = true;
325  }
326  sz_ = sz;
327  }
328  }
329 
331  KOKKOS_INLINE_FUNCTION
332  void resize(const ordinal_type& sz) volatile {
333  if (!is_view_ && sz != sz_) {
334  if (sz > 1) {
335  value_type *coeff_new = ds::get_and_fill(sz);
336  if (sz > sz_)
337  ds::copy(coeff_, coeff_new, sz_);
338  else
339  ds::copy(coeff_, coeff_new, sz);
340  if (!is_view_ && !is_constant_)
342  coeff_ = coeff_new;
343  is_constant_ = false;
344  }
345  else {
346  coeff_0_ = coeff_[0];
347  if (!is_constant_)
349  coeff_ = const_cast<value_type*>(&coeff_0_);
350  is_constant_ = true;
351  }
352  sz_ = sz;
353  }
354  }
355 
357  KOKKOS_INLINE_FUNCTION
358  void shallowReset(pointer v, const ordinal_type& sz,
359  const ordinal_type& stride, bool owned) {
360  if (!is_view_ && !is_constant_)
362  coeff_ = v;
363  sz_ = sz;
364  is_view_ = !owned;
365  }
366 
368  KOKKOS_INLINE_FUNCTION
369  void shallowReset(pointer v, const ordinal_type& sz,
370  const ordinal_type& stride, bool owned) volatile {
371  if (!is_view_ && !is_constant_)
373  coeff_ = v;
374  sz_ = sz;
375  is_view_ = !owned;
376  }
377 
379  KOKKOS_INLINE_FUNCTION
380  ordinal_type size() const { return sz_; }
381 
383  KOKKOS_INLINE_FUNCTION
384  ordinal_type size() const volatile { return sz_; }
385 
387  KOKKOS_INLINE_FUNCTION
388  bool is_view() const { return is_view_; }
389 
391  KOKKOS_INLINE_FUNCTION
392  bool is_view() const volatile { return is_view_; }
393 
395  KOKKOS_INLINE_FUNCTION
397  return coeff_[i];
398  }
399 
401  KOKKOS_INLINE_FUNCTION
403  return coeff_[i];
404  }
405 
407  KOKKOS_INLINE_FUNCTION
408  reference operator[] (const ordinal_type& i) { return coeff_[i]; }
409 
411  KOKKOS_INLINE_FUNCTION
413  return coeff_[i]; }
414 
415  template <int i>
416  KOKKOS_INLINE_FUNCTION
417  reference getCoeff() { return coeff_[i]; }
418 
419  template <int i>
420  KOKKOS_INLINE_FUNCTION
421  volatile_reference getCoeff() volatile { return coeff_[i]; }
422 
423  template <int i>
424  KOKKOS_INLINE_FUNCTION
425  const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
426 
427  template <int i>
428  KOKKOS_INLINE_FUNCTION
429  const_reference getCoeff() const { return coeff_[i]; }
430 
432  KOKKOS_INLINE_FUNCTION
433  const_volatile_pointer coeff() const volatile { return coeff_; }
434 
436  KOKKOS_INLINE_FUNCTION
437  const_pointer coeff() const { return coeff_; }
438 
440  KOKKOS_INLINE_FUNCTION
441  volatile_pointer coeff() volatile { return coeff_; }
442 
444  KOKKOS_INLINE_FUNCTION
445  pointer coeff() { return coeff_; }
446 
447  private:
448 
451 
453 
455 
458 
460  bool is_view_;
461 
464 
465  };
466 
467 }
468 
471 
472 #endif // STOKHOS_DYNAMIC_STORAGE_HPP
pointer coeff_
Coefficient values.
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const volatile DynamicStorage &s) volatile
Assignment operator.
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0) volatile
Initialize values to an array of values.
Stokhos::DynArrayTraits< value_type, execution_space > ds
#define STOKHOS_STORAGE_HELPER_STRINGNAME_DYNAMIC(__storagename__)
KOKKOS_INLINE_FUNCTION const_volatile_pointer coeff() const volatile
Get coefficients.
KOKKOS_INLINE_FUNCTION void init(const_reference v)
Initialize values to a constant value.
KOKKOS_INLINE_FUNCTION DynamicStorage(const ordinal_type &sz=1, const value_type &x=value_type(0.0))
Constructor.
KOKKOS_INLINE_FUNCTION volatile_pointer coeff() volatile
Get coefficients.
KOKKOS_INLINE_FUNCTION const_volatile_reference getCoeff() const volatile
Kokkos::DefaultExecutionSpace execution_space
KOKKOS_INLINE_FUNCTION ~DynamicStorage()
Destructor.
device_t::memory_space memory_space
DynamicStorage< ord_t, val_t, dev_t > type
const volatile value_type & const_volatile_reference
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &s)
Assignment operator.
KOKKOS_INLINE_FUNCTION const_pointer coeff() const
Get coefficients.
Turn DynamicStorage into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION void shallowReset(pointer v, const ordinal_type &sz, const ordinal_type &stride, bool owned) volatile
Reset storage to given array, size, and stride.
KOKKOS_INLINE_FUNCTION DynamicStorage(const ordinal_type &sz, const value_type *x)
Constructor from array.
KOKKOS_INLINE_FUNCTION void resize(const ordinal_type &sz) volatile
Resize to new size (values are preserved)
KOKKOS_INLINE_FUNCTION const_reference operator[](const ordinal_type &i) const
Coefficient access (avoid if possible)
KOKKOS_INLINE_FUNCTION void init(const_reference v) volatile
Initialize values to a constant value.
static KOKKOS_INLINE_FUNCTION void fill(T *dest, std::size_t sz, const T &v)
Fill array dest of length sz with value v.
KOKKOS_INLINE_FUNCTION DynamicStorage(const volatile DynamicStorage &s)
Constructor.
static KOKKOS_INLINE_FUNCTION T * get_and_fill(std::size_t sz, const T &x=T(0.0))
Get memory for new array of length sz and fill with zeros.
device_t::execution_space execution_space
KOKKOS_INLINE_FUNCTION void resize(const ordinal_type &sz)
Resize to new size (values are preserved)
KOKKOS_INLINE_FUNCTION void load(pointer v) volatile
Load values to an array of values.
volatile value_type * volatile_pointer
KOKKOS_INLINE_FUNCTION const_reference getCoeff() const
KOKKOS_INLINE_FUNCTION volatile_reference getCoeff() volatile
ordinal_type sz_
Size of array used.
KOKKOS_INLINE_FUNCTION bool is_view() const
Return whether storage is a view.
KOKKOS_INLINE_FUNCTION pointer coeff()
Get coefficients.
volatile value_type & volatile_reference
KOKKOS_INLINE_FUNCTION bool is_view() const volatile
Return whether storage is a view.
KOKKOS_INLINE_FUNCTION ordinal_type size() const volatile
Return size.
KOKKOS_INLINE_FUNCTION void load(pointer v)
Load values to an array of values.
KOKKOS_INLINE_FUNCTION void shallowReset(pointer v, const ordinal_type &sz, const ordinal_type &stride, bool owned)
Reset storage to given array, size, and stride.
static KOKKOS_INLINE_FUNCTION void copy(const volatile T *src, volatile T *dest, std::size_t sz)
Copy array from src to dest of length sz.
Dynamic array allocation class that is specialized for scalar i.e., fundamental or built-in types (fl...
const value_type & const_reference
KOKKOS_INLINE_FUNCTION ordinal_type size() const
Return size.
bool is_view_
Do we own the array.
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const DynamicStorage &s) volatile
Assignment operator.
KOKKOS_INLINE_FUNCTION DynamicStorage(const DynamicStorage &s)
Constructor.
KOKKOS_INLINE_FUNCTION reference getCoeff()
const volatile value_type * const_volatile_pointer
KOKKOS_INLINE_FUNCTION DynamicStorage(const ordinal_type &sz, pointer v, bool owned)
Constructor for creating a view.
DynamicStorage< ordinal_type, value_type, device_t > type
KOKKOS_INLINE_FUNCTION DynamicStorage & operator=(const volatile DynamicStorage &s)
Assignment operator.
bool is_constant_
Is the coefficient array length-1.
value_type coeff_0_
Coefficient value when sz_ == 1 (i.e., a constant)
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0)
Initialize values to an array of values.
static KOKKOS_INLINE_FUNCTION void destroy_and_release(T *m, std::size_t sz)
Destroy array elements and release memory.