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 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_DYNAMIC_STRIDED_STORAGE_HPP
43 #define STOKHOS_DYNAMIC_STRIDED_STORAGE_HPP
44 
46 
47 #include "Kokkos_Macros.hpp"
48 
49 #include "Sacado_Traits.hpp"
50 #include "Stokhos_KokkosTraits.hpp"
51 #include <sstream>
52 
53 namespace Stokhos {
54 
55  template <typename ordinal_t, typename value_t, typename device_t>
57  public:
58 
59  static const bool is_static = false;
60  static const int static_size = 0;
61  static const bool supports_reset = true;
62 
63  typedef ordinal_t ordinal_type;
64  typedef value_t value_type;
65  typedef device_t execution_space;
67  typedef volatile value_type& volatile_reference;
68  typedef const value_type& const_reference;
69  typedef const volatile value_type& const_volatile_reference;
70  typedef value_type* pointer;
71  typedef volatile value_type* volatile_pointer;
72  typedef const value_type* const_pointer;
73  typedef const volatile value_type* const_volatile_pointer;
75 
77  template <typename ord_t, typename val_t = value_t, typename dev_t = device_t >
78  struct apply {
80  };
81 
83  KOKKOS_INLINE_FUNCTION
85  const value_type& x = value_type(0.0)) :
86  sz_(sz), stride_(1), is_owned_(true) {
88  }
89 
91  KOKKOS_INLINE_FUNCTION
93  sz_(sz), stride_(1), is_owned_(true) {
95  }
96 
98  KOKKOS_INLINE_FUNCTION
99  DynamicStridedStorage(const ordinal_type& sz, pointer v, bool owned) :
100  coeff_(v), sz_(sz), stride_(1), is_owned_(owned) {}
101 
103  KOKKOS_INLINE_FUNCTION
105  sz_(s.sz_), stride_(1), is_owned_(true) {
106  if (s.stride_ == 1)
108  else {
110  for (ordinal_type i=0; i<sz_; ++i)
111  coeff_[i] = s[i];
112  }
113  }
114 
116  KOKKOS_INLINE_FUNCTION
118  sz_(s.sz_), stride_(1), is_owned_(true) {
119  if (s.stride_ == 1)
121  else {
123  for (ordinal_type i=0; i<sz_; ++i)
124  coeff_[i] = s[i];
125  }
126  }
127 
129  KOKKOS_INLINE_FUNCTION
132  }
133 
135  KOKKOS_INLINE_FUNCTION
137  if (&s != this) {
138  if (s.sz_ != sz_) {
139  if (is_owned_)
141  if (s.stride_ == 1)
143  else {
145  for (ordinal_type i=0; i<s.sz_; ++i)
146  coeff_[i] = s[i];
147  }
148  sz_ = s.sz_;
149  stride_ = 1;
150  is_owned_ = true;
151  }
152  else {
153  if (stride_ == 1 and s.stride_ == 1)
154  ds::copy(s.coeff_, coeff_, sz_);
155  else
156  for (ordinal_type i=0; i<s.sz_; ++i)
157  coeff_[i*stride_] = s[i];
158  }
159  }
160  return *this;
161  }
162 
164  KOKKOS_INLINE_FUNCTION
166  if (&s != this) {
167  if (s.sz_ != sz_) {
168  if (is_owned_)
170  if (s.stride_ == 1)
172  else {
174  for (ordinal_type i=0; i<s.sz_; ++i)
175  coeff_[i] = s[i];
176  }
177  sz_ = s.sz_;
178  stride_ = 1;
179  is_owned_ = true;
180  }
181  else {
182  if (stride_ == 1 and s.stride_ == 1)
183  ds::copy(s.coeff_, coeff_, sz_);
184  else
185  for (ordinal_type i=0; i<s.sz_; ++i)
186  coeff_[i*stride_] = s[i];
187  }
188  }
189  return *this;
190  }
191 
193  KOKKOS_INLINE_FUNCTION
194  volatile DynamicStridedStorage&
195  operator=(const DynamicStridedStorage& s) volatile {
196  if (&s != this) {
197  if (s.sz_ != sz_) {
198  if (is_owned_)
200  if (s.stride_ == 1)
202  else {
204  for (ordinal_type i=0; i<s.sz_; ++i)
205  coeff_[i] = s[i];
206  }
207  sz_ = s.sz_;
208  stride_ = 1;
209  is_owned_ = true;
210  }
211  else {
212  if (stride_ == 1 and s.stride_ == 1)
213  ds::copy(s.coeff_, coeff_, sz_);
214  else
215  for (ordinal_type i=0; i<s.sz_; ++i)
216  coeff_[i*stride_] = s[i];
217  }
218  }
219  return *this;
220  }
221 
223  KOKKOS_INLINE_FUNCTION
224  volatile DynamicStridedStorage&
225  operator=(const volatile DynamicStridedStorage& s) volatile {
226  if (&s != this) {
227  if (s.sz_ != sz_) {
228  if (is_owned_)
230  if (s.stride_ == 1)
232  else {
234  for (ordinal_type i=0; i<s.sz_; ++i)
235  coeff_[i] = s[i];
236  }
237  sz_ = s.sz_;
238  stride_ = 1;
239  is_owned_ = true;
240  }
241  else {
242  if (stride_ == 1 and s.stride_ == 1)
243  ds::copy(s.coeff_, coeff_, sz_);
244  else
245  for (ordinal_type i=0; i<s.sz_; ++i)
246  coeff_[i*stride_] = s[i];
247  }
248  }
249  return *this;
250  }
251 
253  KOKKOS_INLINE_FUNCTION
255  if (stride_ == 1)
256  ds::fill(coeff_, sz_, v);
257  else
258  for (ordinal_type i=0; i<sz_; ++i)
259  coeff_[i*stride_] = v;
260  }
261 
263  KOKKOS_INLINE_FUNCTION
264  void init(const_reference v) volatile {
265  if (stride_ == 1)
266  ds::fill(coeff_, sz_, v);
267  else
268  for (ordinal_type i=0; i<sz_; ++i)
269  coeff_[i*stride_] = v;
270  }
271 
273  KOKKOS_INLINE_FUNCTION
274  void init(const_pointer v, const ordinal_type& sz = 0) {
275  ordinal_type my_sz = sz;
276  if (sz == 0)
277  my_sz = sz_;
278  if (stride_ == 1)
279  ds::copy(v, coeff_, my_sz);
280  else
281  for (ordinal_type i=0; i<my_sz; ++i)
282  coeff_[i*stride_] = v[i];
283  }
284 
286  KOKKOS_INLINE_FUNCTION
287  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
288  ordinal_type my_sz = sz;
289  if (sz == 0)
290  my_sz = sz_;
291  if (stride_ == 1)
292  ds::copy(v, coeff_, my_sz);
293  else
294  for (ordinal_type i=0; i<my_sz; ++i)
295  coeff_[i*stride_] = v[i];
296  }
297 
299  KOKKOS_INLINE_FUNCTION
300  void load(pointer v) {
301  if (stride_ == 1)
302  copy(v, coeff_, sz_);
303  for (ordinal_type i=0; i<sz_; ++i)
304  coeff_[i*stride_] = v[i];
305  }
306 
308  KOKKOS_INLINE_FUNCTION
309  void load(pointer v) volatile {
310  if (stride_ == 1)
311  copy(v, coeff_, sz_);
312  for (ordinal_type i=0; i<sz_; ++i)
313  coeff_[i*stride_] = v[i];
314  }
315 
317  KOKKOS_INLINE_FUNCTION
318  void resize(const ordinal_type& sz) {
319  if (sz != sz_) {
320  value_type *coeff_new = ds::get_and_fill(sz);
321  ordinal_type my_sz = sz_;
322  if (sz_ > sz)
323  my_sz = sz;
324  if (stride_ == 1)
325  ds::copy(coeff_, coeff_new, my_sz);
326  else
327  for (ordinal_type i=0; i<my_sz; ++i)
328  coeff_new[i] = coeff_[i*stride_];
329  if (is_owned_)
331  coeff_ = coeff_new;
332  sz_ = sz;
333  stride_ = 1;
334  is_owned_ = true;
335  }
336  }
337 
339  KOKKOS_INLINE_FUNCTION
340  void resize(const ordinal_type& sz) volatile {
341  if (sz != sz_) {
342  value_type *coeff_new = ds::get_and_fill(sz);
343  ordinal_type my_sz = sz_;
344  if (sz_ > sz)
345  my_sz = sz;
346  if (stride_ == 1)
347  ds::copy(coeff_, coeff_new, my_sz);
348  else
349  for (ordinal_type i=0; i<my_sz; ++i)
350  coeff_new[i] = coeff_[i*stride_];
351  if (is_owned_)
353  coeff_ = coeff_new;
354  sz_ = sz;
355  stride_ = 1;
356  is_owned_ = true;
357  }
358  }
359 
361  KOKKOS_INLINE_FUNCTION
362  void shallowReset(pointer v, const ordinal_type& sz,
363  const ordinal_type& stride, bool owned) {
364  if (is_owned_)
366  coeff_ = v;
367  sz_ = sz;
368  stride_ = stride;
369  is_owned_ = owned;
370  }
371 
373  KOKKOS_INLINE_FUNCTION
374  void shallowReset(pointer v, const ordinal_type& sz,
375  const ordinal_type& stride, bool owned) volatile {
376  if (is_owned_)
378  coeff_ = v;
379  sz_ = sz;
380  stride_ = stride;
381  is_owned_ = owned;
382  }
383 
385  KOKKOS_INLINE_FUNCTION
386  ordinal_type size() const { return sz_; }
387 
389  KOKKOS_INLINE_FUNCTION
390  ordinal_type size() const volatile { return sz_; }
391 
393  KOKKOS_INLINE_FUNCTION
395  return coeff_[i*stride_];
396  }
397 
399  KOKKOS_INLINE_FUNCTION
401  return coeff_[i*stride_];
402  }
403 
405  KOKKOS_INLINE_FUNCTION
407  return coeff_[i*stride_];
408  }
409 
411  KOKKOS_INLINE_FUNCTION
413  return coeff_[i*stride_];
414  }
415 
416  template <int i>
417  KOKKOS_INLINE_FUNCTION
418  reference getCoeff() { return coeff_[i*stride_]; }
419 
420  template <int i>
421  KOKKOS_INLINE_FUNCTION
422  volatile_reference getCoeff() volatile { return coeff_[i*stride_]; }
423 
424  template <int i>
425  KOKKOS_INLINE_FUNCTION
426  const_reference getCoeff() const { return coeff_[i*stride_]; }
427 
428  template <int i>
429  KOKKOS_INLINE_FUNCTION
430  const_volatile_reference getCoeff() const volatile { return coeff_[i*stride_]; }
431 
433  KOKKOS_INLINE_FUNCTION
434  const_volatile_pointer coeff() const volatile { return coeff_; }
435 
437  KOKKOS_INLINE_FUNCTION
438  const_pointer coeff() const { return coeff_; }
439 
441  KOKKOS_INLINE_FUNCTION
442  volatile_pointer coeff() volatile { return coeff_; }
443 
445  KOKKOS_INLINE_FUNCTION
446  pointer coeff() { return coeff_; }
447 
448  private:
449 
452 
455 
458 
460  bool is_owned_;
461 
462  };
463 
464 }
465 
466 namespace Sacado {
467  template <typename ordinal_t, typename value_t, typename device_t>
468  struct StringName< Stokhos::DynamicStridedStorage<ordinal_t,
469  value_t,
470  device_t> > {
471  static std::string eval() {
472  std::stringstream ss;
473  ss << "Stokhos::DynamicStridedStorage<"
474  << StringName<ordinal_t>::eval() << ","
475  << StringName<value_t>::eval() << ","
476  << StringName<device_t>::eval() << ">";
477  return ss.str();
478  }
479  };
480 }
481 
482 #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.
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.