Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_StaticFixedStorage.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_STATIC_FIXED_STORAGE_HPP
43 #define STOKHOS_STATIC_FIXED_STORAGE_HPP
44 
46 #include "Stokhos_MemoryTraits.hpp"
47 
48 // We are hooking into Kokkos Core internals here
49 // Need to define this macro since we include non-public headers
50 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
51 #define KOKKOS_IMPL_PUBLIC_INCLUDE
52 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
53 #endif
54 #include "Kokkos_Core_fwd.hpp"
55 #include "Kokkos_Cuda.hpp"
56 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
57 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
58 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
59 #endif
60 
61 #include "Sacado_Traits.hpp"
62 #include "Stokhos_KokkosTraits.hpp"
63 #include <sstream>
64 
65 namespace Stokhos {
66 
68  template <typename ordinal_t, typename value_t, int Num, typename device_t>
70  public:
71 
72  static const bool is_static = true ;
73  static const int static_size = Num;
74  static const bool supports_reset = false;
75 
76  typedef ordinal_t ordinal_type;
77  typedef value_t value_type;
79  typedef typename device_t::memory_space memory_space;
81  typedef volatile value_type& volatile_reference;
82  typedef const value_type& const_reference;
83  typedef const volatile value_type& const_volatile_reference;
84  typedef value_type* pointer;
85  typedef volatile value_type* volatile_pointer;
86  typedef const value_type* const_pointer;
87  typedef const volatile value_type* const_volatile_pointer;
89 
91 
93  template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
94  struct apply {
96  };
97 
98  template <int N>
99  struct apply_N {
101  };
102 
104  KOKKOS_DEFAULTED_FUNCTION
105  StaticFixedStorage() = default;
106 
108  KOKKOS_INLINE_FUNCTION
110  const value_type& x = value_type(0.0)) {
111  ss::fill(coeff_, Num, x);
112  }
113 
115  KOKKOS_INLINE_FUNCTION
117  ss::copy(x, coeff_, sz);
118  }
119 
121  KOKKOS_INLINE_FUNCTION
122  StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
123 
125  KOKKOS_DEFAULTED_FUNCTION
126  StaticFixedStorage(const StaticFixedStorage& s) = default;
127 
129  KOKKOS_INLINE_FUNCTION
131  ss::copy(s.coeff_, coeff_, Num);
132  }
133 
135  KOKKOS_DEFAULTED_FUNCTION
136  ~StaticFixedStorage() = default;
137 
139  KOKKOS_DEFAULTED_FUNCTION
140  StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
141 
143  KOKKOS_INLINE_FUNCTION
145  ss::copy(s.coeff_, coeff_, Num);
146  return *this;
147  }
148 
150  KOKKOS_INLINE_FUNCTION
151  /*volatile*/ StaticFixedStorage&
152  operator=(const StaticFixedStorage& s) volatile {
153  ss::copy(s.coeff_, coeff_, Num);
154  return const_cast<StaticFixedStorage&>(*this);
155  }
156 
158  KOKKOS_INLINE_FUNCTION
159  /*volatile*/ StaticFixedStorage&
160  operator=(const volatile StaticFixedStorage& s) volatile {
161  ss::copy(s.coeff_, coeff_, Num);
162  return const_cast<StaticFixedStorage&>(*this);
163  }
164 
166  KOKKOS_INLINE_FUNCTION
168  ss::fill(coeff_, Num, v);
169  }
170 
172  KOKKOS_INLINE_FUNCTION
173  void init(const_reference v) volatile {
174  ss::fill(coeff_, Num, v);
175  }
176 
178  KOKKOS_INLINE_FUNCTION
179  void init(const_pointer v, const ordinal_type& sz = 0) {
180  if (sz == 0)
181  ss::copy(v, coeff_, Num);
182  else
183  ss::copy(v, coeff_, sz);
184  }
185 
187  KOKKOS_INLINE_FUNCTION
188  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
189  if (sz == 0)
190  ss::copy(v, coeff_, Num);
191  else
192  ss::copy(v, coeff_, sz);
193  }
194 
196  KOKKOS_INLINE_FUNCTION
197  void load(pointer v) {
198  ss::copy(v, coeff_, Num);
199  }
200 
202  KOKKOS_INLINE_FUNCTION
203  void load(pointer v) volatile {
204  ss::copy(v, coeff_, Num);
205  }
206 
208  KOKKOS_INLINE_FUNCTION
209  void resize(const ordinal_type& sz) {}
210 
212  KOKKOS_INLINE_FUNCTION
213  void resize(const ordinal_type& sz) volatile {}
214 
216  KOKKOS_INLINE_FUNCTION
217  void shallowReset(pointer v, const ordinal_type& sz,
218  const ordinal_type& stride, bool owned) {}
219 
221  KOKKOS_INLINE_FUNCTION
222  void shallowReset(pointer v, const ordinal_type& sz,
223  const ordinal_type& stride, bool owned) volatile {}
224 
226  KOKKOS_INLINE_FUNCTION
227  static ordinal_type size() { return Num; }
228 
230  KOKKOS_INLINE_FUNCTION
232  return coeff_[i];
233  }
234 
236  KOKKOS_INLINE_FUNCTION
238  return coeff_[i];
239  }
240 
242  KOKKOS_INLINE_FUNCTION
243  reference operator[] (const ordinal_type& i) { return coeff_[i]; }
244 
246  KOKKOS_INLINE_FUNCTION
248  return coeff_[i]; }
249 
250  template <int i>
251  KOKKOS_INLINE_FUNCTION
252  reference getCoeff() { return coeff_[i]; }
253 
254  template <int i>
255  KOKKOS_INLINE_FUNCTION
256  volatile_reference getCoeff() volatile { return coeff_[i]; }
257 
258  template <int i>
259  KOKKOS_INLINE_FUNCTION
260  const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
261 
262  template <int i>
263  KOKKOS_INLINE_FUNCTION
264  const_reference getCoeff() const { return coeff_[i]; }
265 
267  KOKKOS_INLINE_FUNCTION
268  const_volatile_pointer coeff() const volatile { return coeff_; }
269 
271  KOKKOS_INLINE_FUNCTION
272  const_pointer coeff() const { return coeff_; }
273 
275  KOKKOS_INLINE_FUNCTION
276  volatile_pointer coeff() volatile { return coeff_; }
277 
279  KOKKOS_INLINE_FUNCTION
280  pointer coeff() { return coeff_; }
281 
282  private:
283 
285 #if STOKHOS_ALIGN_MEMORY && ( defined(__INTEL_COMPILER) )
286  value_type coeff_[Num] __attribute__((aligned(MemTraits::Alignment)));
287 #else
289 #endif
290 
291  };
292 
293 #if defined(KOKKOS_ENABLE_CUDA)
294 
296 
300  template <typename ordinal_t, typename value_t, int Num>
301  class StaticFixedStorage<ordinal_t,value_t,Num,Kokkos::Cuda> {
302  public:
303 
304  static const bool is_static = true ;
305  static const int static_size = Num;
306  static const bool supports_reset = false;
307 
308  typedef ordinal_t ordinal_type;
309  typedef value_t value_type;
310  typedef Kokkos::Cuda execution_space;
311  typedef value_type& reference;
312  typedef volatile value_type& volatile_reference;
313  typedef const value_type& const_reference;
314  typedef const volatile value_type& const_volatile_reference;
315  typedef value_type* pointer;
316  typedef volatile value_type* volatile_pointer;
317  typedef const value_type* const_pointer;
318  typedef const volatile value_type* const_volatile_pointer;
320 
321  typedef typename execution_space::memory_space memory_space;
323 
325  template <typename ord_t, typename val_t = value_t , typename dev_t = execution_space >
326  struct apply {
328  };
329 
330  template <int N>
331  struct apply_N {
332  typedef StaticFixedStorage<ordinal_type,value_type,N,execution_space> type;
333  };
334 
336  KOKKOS_DEFAULTED_FUNCTION
337  StaticFixedStorage() = default;
338 
340  KOKKOS_INLINE_FUNCTION
342  const value_type& x = value_type(0.0)) {
343  ss::fill(coeff_, Num, x);
344  }
345 
347  KOKKOS_INLINE_FUNCTION
348  StaticFixedStorage(const ordinal_type& sz, const value_type* x) {
349  ss::copy(x, coeff_, sz);
350  }
351 
353  KOKKOS_INLINE_FUNCTION
354  StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
355 
357  KOKKOS_DEFAULTED_FUNCTION
358  StaticFixedStorage(const StaticFixedStorage& s) = default;
359 
361  KOKKOS_INLINE_FUNCTION
362  StaticFixedStorage(const volatile StaticFixedStorage& s) {
363  ss::copy(s.coeff_, coeff_, Num);
364  }
365 
367  KOKKOS_DEFAULTED_FUNCTION
368  ~StaticFixedStorage() = default;
369 
371  KOKKOS_DEFAULTED_FUNCTION
372  StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
373 
375  KOKKOS_INLINE_FUNCTION
376  StaticFixedStorage& operator=(const volatile StaticFixedStorage& s) {
377  ss::copy(s.coeff_, coeff_, Num);
378  return *this;
379  }
380 
382  KOKKOS_INLINE_FUNCTION
383  /*volatile*/ StaticFixedStorage&
384  operator=(const StaticFixedStorage& s) volatile {
385  ss::copy(s.coeff_, coeff_, Num);
386  return const_cast<StaticFixedStorage&>(*this);
387  }
388 
390  KOKKOS_INLINE_FUNCTION
391  /*volatile*/ StaticFixedStorage&
392  operator=(const volatile StaticFixedStorage& s) volatile {
393  ss::copy(s.coeff_, coeff_, Num);
394  return const_cast<StaticFixedStorage&>(*this);
395  }
396 
398  KOKKOS_INLINE_FUNCTION
399  void init(const_reference v) {
400  ss::fill(coeff_, Num, v);
401  }
402 
404  KOKKOS_INLINE_FUNCTION
405  void init(const_reference v) volatile {
406  ss::fill(coeff_, Num, v);
407  }
408 
410  KOKKOS_INLINE_FUNCTION
411  void init(const_pointer v, const ordinal_type& sz = 0) {
412  if (sz == 0)
413  ss::copy(v, coeff_, Num);
414  else
415  ss::copy(v, coeff_, sz);
416  }
417 
419  KOKKOS_INLINE_FUNCTION
420  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
421  if (sz == 0)
422  ss::copy(v, coeff_, Num);
423  else
424  ss::copy(v, coeff_, sz);
425  }
426 
428  KOKKOS_INLINE_FUNCTION
429  void load(pointer v) {
430  ss::copy(v, coeff_, Num);
431  }
432 
434  KOKKOS_INLINE_FUNCTION
435  void load(pointer v) volatile {
436  ss::copy(v, coeff_, Num);
437  }
438 
440  KOKKOS_INLINE_FUNCTION
441  void resize(const ordinal_type& sz) {}
442 
444  KOKKOS_INLINE_FUNCTION
445  void resize(const ordinal_type& sz) volatile {}
446 
448  KOKKOS_INLINE_FUNCTION
449  void shallowReset(pointer v, const ordinal_type& sz,
450  const ordinal_type& stride, bool owned) {}
451 
453  KOKKOS_INLINE_FUNCTION
454  void shallowReset(pointer v, const ordinal_type& sz,
455  const ordinal_type& stride, bool owned) volatile {}
456 
458  KOKKOS_INLINE_FUNCTION
459  static ordinal_type size() { return Num; }
460 
462  KOKKOS_INLINE_FUNCTION
463  const_reference operator[] (const ordinal_type& i) const {
464  return coeff_[i];
465  }
466 
468  KOKKOS_INLINE_FUNCTION
469  const_volatile_reference operator[] (const ordinal_type& i) const volatile {
470  return coeff_[i];
471  }
472 
474  KOKKOS_INLINE_FUNCTION
475  reference operator[] (const ordinal_type& i) { return coeff_[i]; }
476 
478  KOKKOS_INLINE_FUNCTION
479  volatile_reference operator[] (const ordinal_type& i) volatile {
480  return coeff_[i]; }
481 
482  template <int i>
483  KOKKOS_INLINE_FUNCTION
484  reference getCoeff() { return coeff_[i]; }
485 
486  template <int i>
487  KOKKOS_INLINE_FUNCTION
488  volatile_reference getCoeff() volatile { return coeff_[i]; }
489 
490  template <int i>
491  KOKKOS_INLINE_FUNCTION
492  const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
493 
494  template <int i>
495  KOKKOS_INLINE_FUNCTION
496  const_reference getCoeff() const { return coeff_[i]; }
497 
499  KOKKOS_INLINE_FUNCTION
500  const_volatile_pointer coeff() const volatile { return coeff_; }
501 
503  KOKKOS_INLINE_FUNCTION
504  const_pointer coeff() const { return coeff_; }
505 
507  KOKKOS_INLINE_FUNCTION
508  volatile_pointer coeff() volatile { return coeff_; }
509 
511  KOKKOS_INLINE_FUNCTION
512  pointer coeff() { return coeff_; }
513 
514  private:
515 
517  value_type coeff_[Num];
518 
519  };
520 
521 #endif
522 
523 }
524 
527 
528 #endif // STOKHOS_STATIC_FIXED_STORAGE_HPP
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 reference getCoeff()
KOKKOS_INLINE_FUNCTION void init(const_reference v) volatile
Initialize values to a constant value.
Statically allocated storage class.
KOKKOS_DEFAULTED_FUNCTION StaticFixedStorage()=default
Constructor.
Kokkos::DefaultExecutionSpace execution_space
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const ordinal_type &sz, const value_type &x=value_type(0.0))
Constructor.
Stokhos::StaticArrayTraits< value_type, execution_space > ss
const volatile value_type & const_volatile_reference
Static array allocation class.
KOKKOS_INLINE_FUNCTION void load(pointer v)
Load values to an array of values.
KOKKOS_INLINE_FUNCTION void load(pointer v) volatile
Load values to an array of values.
const volatile value_type * const_volatile_pointer
Stokhos::MemoryTraits< memory_space > MemTraits
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0)
Initialize values to an array of values.
KOKKOS_INLINE_FUNCTION void resize(const ordinal_type &sz)
Resize to new size (values are preserved)
Turn StaticFixedStorage into a meta-function class usable with mpl::apply.
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
Definition: csr_vector.h:260
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 resize(const ordinal_type &sz) volatile
Resize to new size (values are preserved)
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const ordinal_type &sz, pointer v, bool owned)
Constructor for creating a view (not allowed)
StaticFixedStorage< ordinal_type, value_type, N, device_t > type
Top-level namespace for Stokhos classes and functions.
Traits class encapsulting memory alignment.
KOKKOS_INLINE_FUNCTION StaticFixedStorage & operator=(const volatile StaticFixedStorage &s) volatile
Assignment operator.
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const ordinal_type &sz, const value_type *x)
Constructor from array.
device_t::execution_space execution_space
KOKKOS_INLINE_FUNCTION StaticFixedStorage & operator=(const StaticFixedStorage &s) volatile
Assignment operator.
KOKKOS_INLINE_FUNCTION pointer coeff()
Get coefficients.
static const unsigned Alignment
Bytes to which memory allocations are aligned.
static KOKKOS_INLINE_FUNCTION ordinal_type size()
Return size.
KOKKOS_INLINE_FUNCTION const_volatile_reference getCoeff() const volatile
KOKKOS_INLINE_FUNCTION volatile_reference getCoeff() volatile
KOKKOS_INLINE_FUNCTION const_reference getCoeff() const
KOKKOS_INLINE_FUNCTION const_volatile_pointer coeff() const volatile
Get coefficients.
KOKKOS_INLINE_FUNCTION const_pointer coeff() const
Get coefficients.
KOKKOS_INLINE_FUNCTION void init(const_reference v)
Initialize values to a constant value.
StaticFixedStorage< ord_t, val_t, Num, dev_t > type
KOKKOS_DEFAULTED_FUNCTION StaticFixedStorage & operator=(const StaticFixedStorage &s)=default
Assignment operator.
KOKKOS_INLINE_FUNCTION StaticFixedStorage & operator=(const volatile StaticFixedStorage &s)
Assignment operator.
KOKKOS_INLINE_FUNCTION const_reference operator[](const ordinal_type &i) const
Coefficient access (avoid if possible)
KOKKOS_INLINE_FUNCTION StaticFixedStorage(const volatile StaticFixedStorage &s)
Copy constructor.
value_type coeff_[Num]
Coefficient values.
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP... > >::value >::type fill(const Kokkos::View< XD, XP... > &x, const typename Kokkos::View< XD, XP... >::non_const_value_type &val)
KOKKOS_INLINE_FUNCTION volatile_pointer coeff() volatile
Get coefficients.
#define STOKHOS_STORAGE_HELPER_STRINGNAME_STATIC(__storagename__)
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_DEFAULTED_FUNCTION ~StaticFixedStorage()=default
Destructor.