Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_DynamicStridedStorage.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_STRIDED_STORAGE_HPP
11 #define STOKHOS_DYNAMIC_STRIDED_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 = true;
30 
31  typedef ordinal_t ordinal_type;
32  typedef value_t value_type;
33  typedef device_t execution_space;
35  typedef volatile value_type& volatile_reference;
36  typedef const value_type& const_reference;
37  typedef const volatile value_type& const_volatile_reference;
38  typedef value_type* pointer;
39  typedef volatile value_type* volatile_pointer;
40  typedef const value_type* const_pointer;
41  typedef const volatile value_type* const_volatile_pointer;
43 
45  template <typename ord_t, typename val_t = value_t, typename dev_t = device_t >
46  struct apply {
48  };
49 
51  KOKKOS_INLINE_FUNCTION
53  const value_type& x = value_type(0.0)) :
54  sz_(sz), stride_(1), is_owned_(true) {
56  }
57 
59  KOKKOS_INLINE_FUNCTION
61  sz_(sz), stride_(1), is_owned_(true) {
63  }
64 
66  KOKKOS_INLINE_FUNCTION
67  DynamicStridedStorage(const ordinal_type& sz, pointer v, bool owned) :
68  coeff_(v), sz_(sz), stride_(1), is_owned_(owned) {}
69 
71  KOKKOS_INLINE_FUNCTION
73  sz_(s.sz_), stride_(1), is_owned_(true) {
74  if (s.stride_ == 1)
76  else {
78  for (ordinal_type i=0; i<sz_; ++i)
79  coeff_[i] = s[i];
80  }
81  }
82 
84  KOKKOS_INLINE_FUNCTION
86  sz_(s.sz_), stride_(1), is_owned_(true) {
87  if (s.stride_ == 1)
89  else {
91  for (ordinal_type i=0; i<sz_; ++i)
92  coeff_[i] = s[i];
93  }
94  }
95 
97  KOKKOS_INLINE_FUNCTION
100  }
101 
103  KOKKOS_INLINE_FUNCTION
105  if (&s != this) {
106  if (s.sz_ != sz_) {
107  if (is_owned_)
109  if (s.stride_ == 1)
111  else {
113  for (ordinal_type i=0; i<s.sz_; ++i)
114  coeff_[i] = s[i];
115  }
116  sz_ = s.sz_;
117  stride_ = 1;
118  is_owned_ = true;
119  }
120  else {
121  if (stride_ == 1 and s.stride_ == 1)
122  ds::copy(s.coeff_, coeff_, sz_);
123  else
124  for (ordinal_type i=0; i<s.sz_; ++i)
125  coeff_[i*stride_] = s[i];
126  }
127  }
128  return *this;
129  }
130 
132  KOKKOS_INLINE_FUNCTION
134  if (&s != this) {
135  if (s.sz_ != sz_) {
136  if (is_owned_)
138  if (s.stride_ == 1)
140  else {
142  for (ordinal_type i=0; i<s.sz_; ++i)
143  coeff_[i] = s[i];
144  }
145  sz_ = s.sz_;
146  stride_ = 1;
147  is_owned_ = true;
148  }
149  else {
150  if (stride_ == 1 and s.stride_ == 1)
151  ds::copy(s.coeff_, coeff_, sz_);
152  else
153  for (ordinal_type i=0; i<s.sz_; ++i)
154  coeff_[i*stride_] = s[i];
155  }
156  }
157  return *this;
158  }
159 
161  KOKKOS_INLINE_FUNCTION
162  volatile DynamicStridedStorage&
163  operator=(const DynamicStridedStorage& s) volatile {
164  if (&s != this) {
165  if (s.sz_ != sz_) {
166  if (is_owned_)
168  if (s.stride_ == 1)
170  else {
172  for (ordinal_type i=0; i<s.sz_; ++i)
173  coeff_[i] = s[i];
174  }
175  sz_ = s.sz_;
176  stride_ = 1;
177  is_owned_ = true;
178  }
179  else {
180  if (stride_ == 1 and s.stride_ == 1)
181  ds::copy(s.coeff_, coeff_, sz_);
182  else
183  for (ordinal_type i=0; i<s.sz_; ++i)
184  coeff_[i*stride_] = s[i];
185  }
186  }
187  return *this;
188  }
189 
191  KOKKOS_INLINE_FUNCTION
192  volatile DynamicStridedStorage&
193  operator=(const volatile DynamicStridedStorage& s) volatile {
194  if (&s != this) {
195  if (s.sz_ != sz_) {
196  if (is_owned_)
198  if (s.stride_ == 1)
200  else {
202  for (ordinal_type i=0; i<s.sz_; ++i)
203  coeff_[i] = s[i];
204  }
205  sz_ = s.sz_;
206  stride_ = 1;
207  is_owned_ = true;
208  }
209  else {
210  if (stride_ == 1 and s.stride_ == 1)
211  ds::copy(s.coeff_, coeff_, sz_);
212  else
213  for (ordinal_type i=0; i<s.sz_; ++i)
214  coeff_[i*stride_] = s[i];
215  }
216  }
217  return *this;
218  }
219 
221  KOKKOS_INLINE_FUNCTION
223  if (stride_ == 1)
224  ds::fill(coeff_, sz_, v);
225  else
226  for (ordinal_type i=0; i<sz_; ++i)
227  coeff_[i*stride_] = v;
228  }
229 
231  KOKKOS_INLINE_FUNCTION
232  void init(const_reference v) volatile {
233  if (stride_ == 1)
234  ds::fill(coeff_, sz_, v);
235  else
236  for (ordinal_type i=0; i<sz_; ++i)
237  coeff_[i*stride_] = v;
238  }
239 
241  KOKKOS_INLINE_FUNCTION
242  void init(const_pointer v, const ordinal_type& sz = 0) {
243  ordinal_type my_sz = sz;
244  if (sz == 0)
245  my_sz = sz_;
246  if (stride_ == 1)
247  ds::copy(v, coeff_, my_sz);
248  else
249  for (ordinal_type i=0; i<my_sz; ++i)
250  coeff_[i*stride_] = v[i];
251  }
252 
254  KOKKOS_INLINE_FUNCTION
255  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
256  ordinal_type my_sz = sz;
257  if (sz == 0)
258  my_sz = sz_;
259  if (stride_ == 1)
260  ds::copy(v, coeff_, my_sz);
261  else
262  for (ordinal_type i=0; i<my_sz; ++i)
263  coeff_[i*stride_] = v[i];
264  }
265 
267  KOKKOS_INLINE_FUNCTION
268  void load(pointer v) {
269  if (stride_ == 1)
270  copy(v, coeff_, sz_);
271  for (ordinal_type i=0; i<sz_; ++i)
272  coeff_[i*stride_] = v[i];
273  }
274 
276  KOKKOS_INLINE_FUNCTION
277  void load(pointer v) volatile {
278  if (stride_ == 1)
279  copy(v, coeff_, sz_);
280  for (ordinal_type i=0; i<sz_; ++i)
281  coeff_[i*stride_] = v[i];
282  }
283 
285  KOKKOS_INLINE_FUNCTION
286  void resize(const ordinal_type& sz) {
287  if (sz != sz_) {
288  value_type *coeff_new = ds::get_and_fill(sz);
289  ordinal_type my_sz = sz_;
290  if (sz_ > sz)
291  my_sz = sz;
292  if (stride_ == 1)
293  ds::copy(coeff_, coeff_new, my_sz);
294  else
295  for (ordinal_type i=0; i<my_sz; ++i)
296  coeff_new[i] = coeff_[i*stride_];
297  if (is_owned_)
299  coeff_ = coeff_new;
300  sz_ = sz;
301  stride_ = 1;
302  is_owned_ = true;
303  }
304  }
305 
307  KOKKOS_INLINE_FUNCTION
308  void resize(const ordinal_type& sz) volatile {
309  if (sz != sz_) {
310  value_type *coeff_new = ds::get_and_fill(sz);
311  ordinal_type my_sz = sz_;
312  if (sz_ > sz)
313  my_sz = sz;
314  if (stride_ == 1)
315  ds::copy(coeff_, coeff_new, my_sz);
316  else
317  for (ordinal_type i=0; i<my_sz; ++i)
318  coeff_new[i] = coeff_[i*stride_];
319  if (is_owned_)
321  coeff_ = coeff_new;
322  sz_ = sz;
323  stride_ = 1;
324  is_owned_ = true;
325  }
326  }
327 
329  KOKKOS_INLINE_FUNCTION
330  void shallowReset(pointer v, const ordinal_type& sz,
331  const ordinal_type& stride, bool owned) {
332  if (is_owned_)
334  coeff_ = v;
335  sz_ = sz;
336  stride_ = stride;
337  is_owned_ = owned;
338  }
339 
341  KOKKOS_INLINE_FUNCTION
342  void shallowReset(pointer v, const ordinal_type& sz,
343  const ordinal_type& stride, bool owned) volatile {
344  if (is_owned_)
346  coeff_ = v;
347  sz_ = sz;
348  stride_ = stride;
349  is_owned_ = owned;
350  }
351 
353  KOKKOS_INLINE_FUNCTION
354  ordinal_type size() const { return sz_; }
355 
357  KOKKOS_INLINE_FUNCTION
358  ordinal_type size() const volatile { return sz_; }
359 
361  KOKKOS_INLINE_FUNCTION
363  return coeff_[i*stride_];
364  }
365 
367  KOKKOS_INLINE_FUNCTION
369  return coeff_[i*stride_];
370  }
371 
373  KOKKOS_INLINE_FUNCTION
375  return coeff_[i*stride_];
376  }
377 
379  KOKKOS_INLINE_FUNCTION
381  return coeff_[i*stride_];
382  }
383 
384  template <int i>
385  KOKKOS_INLINE_FUNCTION
386  reference getCoeff() { return coeff_[i*stride_]; }
387 
388  template <int i>
389  KOKKOS_INLINE_FUNCTION
390  volatile_reference getCoeff() volatile { return coeff_[i*stride_]; }
391 
392  template <int i>
393  KOKKOS_INLINE_FUNCTION
394  const_reference getCoeff() const { return coeff_[i*stride_]; }
395 
396  template <int i>
397  KOKKOS_INLINE_FUNCTION
398  const_volatile_reference getCoeff() const volatile { return coeff_[i*stride_]; }
399 
401  KOKKOS_INLINE_FUNCTION
402  const_volatile_pointer coeff() const volatile { return coeff_; }
403 
405  KOKKOS_INLINE_FUNCTION
406  const_pointer coeff() const { return coeff_; }
407 
409  KOKKOS_INLINE_FUNCTION
410  volatile_pointer coeff() volatile { return coeff_; }
411 
413  KOKKOS_INLINE_FUNCTION
414  pointer coeff() { return coeff_; }
415 
416  private:
417 
420 
423 
426 
428  bool is_owned_;
429 
430  };
431 
432 }
433 
435 STOKHOS_STORAGE_HELPER_STRINGNAME_DYNAMIC(DynamicStridedStorage)
436 
437 #endif // STOKHOS_DYNAMIC_STORAGE_HPP
KOKKOS_INLINE_FUNCTION DynamicStridedStorage(const ordinal_type &sz=1, const value_type &x=value_type(0.0))
Constructor.
KOKKOS_INLINE_FUNCTION void resize(const ordinal_type &sz)
Resize to new size (values are preserved)
KOKKOS_INLINE_FUNCTION volatile_pointer coeff() volatile
Get coefficients.
#define STOKHOS_STORAGE_HELPER_STRINGNAME_DYNAMIC(__storagename__)
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0) volatile
Initialize values to an array of values.
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 void resize(const ordinal_type &sz) volatile
Resize to new size (values are preserved)
KOKKOS_INLINE_FUNCTION ordinal_type size() const
Return size.
KOKKOS_INLINE_FUNCTION DynamicStridedStorage(const volatile DynamicStridedStorage &s)
Constructor.
KOKKOS_INLINE_FUNCTION void init(const_reference v) volatile
Initialize values to a constant value.
KOKKOS_INLINE_FUNCTION const_volatile_pointer coeff() const volatile
Get coefficients.
KOKKOS_INLINE_FUNCTION const_volatile_reference getCoeff() const volatile
KOKKOS_INLINE_FUNCTION ordinal_type size() const volatile
Return size.
Turn DynamicStridedStorage into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION ~DynamicStridedStorage()
Destructor.
Stokhos::DynArrayTraits< value_type, execution_space > ds
KOKKOS_INLINE_FUNCTION DynamicStridedStorage(const ordinal_type &sz, pointer v, bool owned)
Constructor for creating a view.
DynamicStridedStorage< ord_t, val_t, dev_t > type
static KOKKOS_INLINE_FUNCTION void fill(T *dest, std::size_t sz, const T &v)
Fill array dest of length sz with value v.
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.
KOKKOS_INLINE_FUNCTION pointer coeff()
Get coefficients.
KOKKOS_INLINE_FUNCTION const_reference getCoeff() const
KOKKOS_INLINE_FUNCTION volatile DynamicStridedStorage & operator=(const DynamicStridedStorage &s) volatile
Assignment operator.
const volatile value_type & const_volatile_reference
KOKKOS_INLINE_FUNCTION const_reference operator[](const ordinal_type &i) const
Coefficient access (avoid if possible)
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.
KOKKOS_INLINE_FUNCTION reference getCoeff()
KOKKOS_INLINE_FUNCTION const_pointer coeff() const
Get coefficients.
KOKKOS_INLINE_FUNCTION DynamicStridedStorage & operator=(const volatile DynamicStridedStorage &s)
Assignment operator.
const volatile value_type * const_volatile_pointer
KOKKOS_INLINE_FUNCTION void load(pointer v) volatile
Load values to an array of values.
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.
ordinal_type sz_
Size of array used.
KOKKOS_INLINE_FUNCTION void init(const_reference v)
Initialize values to a constant value.
Dynamic array allocation class that is specialized for scalar i.e., fundamental or built-in types (fl...
KOKKOS_INLINE_FUNCTION DynamicStridedStorage(const ordinal_type &sz, const value_type *x)
Constructor from array.
KOKKOS_INLINE_FUNCTION void load(pointer v)
Load values to an array of values.
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0)
Initialize values to an array of values.
KOKKOS_INLINE_FUNCTION volatile DynamicStridedStorage & operator=(const volatile DynamicStridedStorage &s) volatile
Assignment operator.
KOKKOS_INLINE_FUNCTION DynamicStridedStorage & operator=(const DynamicStridedStorage &s)
Assignment operator.
KOKKOS_INLINE_FUNCTION DynamicStridedStorage(const DynamicStridedStorage &s)
Constructor.
KOKKOS_INLINE_FUNCTION volatile_reference getCoeff() volatile
static KOKKOS_INLINE_FUNCTION void destroy_and_release(T *m, std::size_t sz)
Destroy array elements and release memory.