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 #include "Kokkos_Core_fwd.hpp"
49 
50 #include "Sacado_Traits.hpp"
51 #include "Stokhos_KokkosTraits.hpp"
52 #include <sstream>
53 
54 namespace Stokhos {
55 
57  template <typename ordinal_t, typename value_t, int Num, typename device_t>
59  public:
60 
61  static const bool is_static = true ;
62  static const int static_size = Num;
63  static const bool supports_reset = false;
64 
65  typedef ordinal_t ordinal_type;
66  typedef value_t value_type;
68  typedef typename device_t::memory_space memory_space;
70  typedef volatile value_type& volatile_reference;
71  typedef const value_type& const_reference;
72  typedef const volatile value_type& const_volatile_reference;
73  typedef value_type* pointer;
74  typedef volatile value_type* volatile_pointer;
75  typedef const value_type* const_pointer;
76  typedef const volatile value_type* const_volatile_pointer;
78 
80 
82  template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
83  struct apply {
85  };
86 
87  template <int N>
88  struct apply_N {
90  };
91 
93  KOKKOS_INLINE_FUNCTION
94  StaticFixedStorage() = default;
95 
97  KOKKOS_INLINE_FUNCTION
99  const value_type& x = value_type(0.0)) {
100  ss::fill(coeff_, Num, x);
101  }
102 
104  KOKKOS_INLINE_FUNCTION
106  ss::copy(x, coeff_, sz);
107  }
108 
110  KOKKOS_INLINE_FUNCTION
111  StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
112 
114  KOKKOS_INLINE_FUNCTION
115  StaticFixedStorage(const StaticFixedStorage& s) = default;
116 
118  KOKKOS_INLINE_FUNCTION
120  ss::copy(s.coeff_, coeff_, Num);
121  }
122 
124  KOKKOS_INLINE_FUNCTION
125  ~StaticFixedStorage() = default;
126 
128  KOKKOS_INLINE_FUNCTION
129  StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
130 
132  KOKKOS_INLINE_FUNCTION
134  ss::copy(s.coeff_, coeff_, Num);
135  return *this;
136  }
137 
139  KOKKOS_INLINE_FUNCTION
140  /*volatile*/ StaticFixedStorage&
141  operator=(const StaticFixedStorage& s) volatile {
142  ss::copy(s.coeff_, coeff_, Num);
143  return const_cast<StaticFixedStorage&>(*this);
144  }
145 
147  KOKKOS_INLINE_FUNCTION
148  /*volatile*/ StaticFixedStorage&
149  operator=(const volatile StaticFixedStorage& s) volatile {
150  ss::copy(s.coeff_, coeff_, Num);
151  return const_cast<StaticFixedStorage&>(*this);
152  }
153 
155  KOKKOS_INLINE_FUNCTION
157  ss::fill(coeff_, Num, v);
158  }
159 
161  KOKKOS_INLINE_FUNCTION
162  void init(const_reference v) volatile {
163  ss::fill(coeff_, Num, v);
164  }
165 
167  KOKKOS_INLINE_FUNCTION
168  void init(const_pointer v, const ordinal_type& sz = 0) {
169  if (sz == 0)
170  ss::copy(v, coeff_, Num);
171  else
172  ss::copy(v, coeff_, sz);
173  }
174 
176  KOKKOS_INLINE_FUNCTION
177  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
178  if (sz == 0)
179  ss::copy(v, coeff_, Num);
180  else
181  ss::copy(v, coeff_, sz);
182  }
183 
185  KOKKOS_INLINE_FUNCTION
186  void load(pointer v) {
187  ss::copy(v, coeff_, Num);
188  }
189 
191  KOKKOS_INLINE_FUNCTION
192  void load(pointer v) volatile {
193  ss::copy(v, coeff_, Num);
194  }
195 
197  KOKKOS_INLINE_FUNCTION
198  void resize(const ordinal_type& sz) {}
199 
201  KOKKOS_INLINE_FUNCTION
202  void resize(const ordinal_type& sz) volatile {}
203 
205  KOKKOS_INLINE_FUNCTION
206  void shallowReset(pointer v, const ordinal_type& sz,
207  const ordinal_type& stride, bool owned) {}
208 
210  KOKKOS_INLINE_FUNCTION
211  void shallowReset(pointer v, const ordinal_type& sz,
212  const ordinal_type& stride, bool owned) volatile {}
213 
215  KOKKOS_INLINE_FUNCTION
216  static ordinal_type size() { return Num; }
217 
219  KOKKOS_INLINE_FUNCTION
221  return coeff_[i];
222  }
223 
225  KOKKOS_INLINE_FUNCTION
227  return coeff_[i];
228  }
229 
231  KOKKOS_INLINE_FUNCTION
232  reference operator[] (const ordinal_type& i) { return coeff_[i]; }
233 
235  KOKKOS_INLINE_FUNCTION
237  return coeff_[i]; }
238 
239  template <int i>
240  KOKKOS_INLINE_FUNCTION
241  reference getCoeff() { return coeff_[i]; }
242 
243  template <int i>
244  KOKKOS_INLINE_FUNCTION
245  volatile_reference getCoeff() volatile { return coeff_[i]; }
246 
247  template <int i>
248  KOKKOS_INLINE_FUNCTION
249  const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
250 
251  template <int i>
252  KOKKOS_INLINE_FUNCTION
253  const_reference getCoeff() const { return coeff_[i]; }
254 
256  KOKKOS_INLINE_FUNCTION
257  const_volatile_pointer coeff() const volatile { return coeff_; }
258 
260  KOKKOS_INLINE_FUNCTION
261  const_pointer coeff() const { return coeff_; }
262 
264  KOKKOS_INLINE_FUNCTION
265  volatile_pointer coeff() volatile { return coeff_; }
266 
268  KOKKOS_INLINE_FUNCTION
269  pointer coeff() { return coeff_; }
270 
271  private:
272 
274 #if STOKHOS_ALIGN_MEMORY && ( defined(__INTEL_COMPILER) )
275  value_type coeff_[Num] __attribute__((aligned(MemTraits::Alignment)));
276 #else
278 #endif
279 
280  };
281 
282 #if defined(KOKKOS_HAVE_CUDA)
283 
285 
289  template <typename ordinal_t, typename value_t, int Num>
290  class StaticFixedStorage<ordinal_t,value_t,Num,Kokkos::Cuda> {
291  public:
292 
293  static const bool is_static = true ;
294  static const int static_size = Num;
295  static const bool supports_reset = false;
296 
297  typedef ordinal_t ordinal_type;
298  typedef value_t value_type;
299  typedef Kokkos::Cuda execution_space;
300  typedef value_type& reference;
301  typedef volatile value_type& volatile_reference;
302  typedef const value_type& const_reference;
303  typedef const volatile value_type& const_volatile_reference;
304  typedef value_type* pointer;
305  typedef volatile value_type* volatile_pointer;
306  typedef const value_type* const_pointer;
307  typedef const volatile value_type* const_volatile_pointer;
309 
310  typedef typename execution_space::memory_space memory_space;
312 
314  template <typename ord_t, typename val_t = value_t , typename dev_t = execution_space >
315  struct apply {
317  };
318 
319  template <int N>
320  struct apply_N {
321  typedef StaticFixedStorage<ordinal_type,value_type,N,execution_space> type;
322  };
323 
325  KOKKOS_INLINE_FUNCTION
326  StaticFixedStorage() = default;
327 
329  KOKKOS_INLINE_FUNCTION
331  const value_type& x = value_type(0.0)) {
332  ss::fill(coeff_, Num, x);
333  }
334 
336  KOKKOS_INLINE_FUNCTION
337  StaticFixedStorage(const ordinal_type& sz, const value_type* x) {
338  ss::copy(x, coeff_, sz);
339  }
340 
342  KOKKOS_INLINE_FUNCTION
343  StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
344 
346  KOKKOS_INLINE_FUNCTION
347  StaticFixedStorage(const StaticFixedStorage& s) = default;
348 
350  KOKKOS_INLINE_FUNCTION
351  StaticFixedStorage(const volatile StaticFixedStorage& s) {
352  ss::copy(s.coeff_, coeff_, Num);
353  }
354 
356  KOKKOS_INLINE_FUNCTION
357  ~StaticFixedStorage() = default;
358 
360  KOKKOS_INLINE_FUNCTION
361  StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
362 
364  KOKKOS_INLINE_FUNCTION
365  StaticFixedStorage& operator=(const volatile StaticFixedStorage& s) {
366  ss::copy(s.coeff_, coeff_, Num);
367  return *this;
368  }
369 
371  KOKKOS_INLINE_FUNCTION
372  /*volatile*/ StaticFixedStorage&
373  operator=(const StaticFixedStorage& s) volatile {
374  ss::copy(s.coeff_, coeff_, Num);
375  return const_cast<StaticFixedStorage&>(*this);
376  }
377 
379  KOKKOS_INLINE_FUNCTION
380  /*volatile*/ StaticFixedStorage&
381  operator=(const volatile StaticFixedStorage& s) volatile {
382  ss::copy(s.coeff_, coeff_, Num);
383  return const_cast<StaticFixedStorage&>(*this);
384  }
385 
387  KOKKOS_INLINE_FUNCTION
388  void init(const_reference v) {
389  ss::fill(coeff_, Num, v);
390  }
391 
393  KOKKOS_INLINE_FUNCTION
394  void init(const_reference v) volatile {
395  ss::fill(coeff_, Num, v);
396  }
397 
399  KOKKOS_INLINE_FUNCTION
400  void init(const_pointer v, const ordinal_type& sz = 0) {
401  if (sz == 0)
402  ss::copy(v, coeff_, Num);
403  else
404  ss::copy(v, coeff_, sz);
405  }
406 
408  KOKKOS_INLINE_FUNCTION
409  void init(const_pointer v, const ordinal_type& sz = 0) volatile {
410  if (sz == 0)
411  ss::copy(v, coeff_, Num);
412  else
413  ss::copy(v, coeff_, sz);
414  }
415 
417  KOKKOS_INLINE_FUNCTION
418  void load(pointer v) {
419  ss::copy(v, coeff_, Num);
420  }
421 
423  KOKKOS_INLINE_FUNCTION
424  void load(pointer v) volatile {
425  ss::copy(v, coeff_, Num);
426  }
427 
429  KOKKOS_INLINE_FUNCTION
430  void resize(const ordinal_type& sz) {}
431 
433  KOKKOS_INLINE_FUNCTION
434  void resize(const ordinal_type& sz) volatile {}
435 
437  KOKKOS_INLINE_FUNCTION
438  void shallowReset(pointer v, const ordinal_type& sz,
439  const ordinal_type& stride, bool owned) {}
440 
442  KOKKOS_INLINE_FUNCTION
443  void shallowReset(pointer v, const ordinal_type& sz,
444  const ordinal_type& stride, bool owned) volatile {}
445 
447  KOKKOS_INLINE_FUNCTION
448  static ordinal_type size() { return Num; }
449 
451  KOKKOS_INLINE_FUNCTION
452  const_reference operator[] (const ordinal_type& i) const {
453  return coeff_[i];
454  }
455 
457  KOKKOS_INLINE_FUNCTION
458  const_volatile_reference operator[] (const ordinal_type& i) const volatile {
459  return coeff_[i];
460  }
461 
463  KOKKOS_INLINE_FUNCTION
464  reference operator[] (const ordinal_type& i) { return coeff_[i]; }
465 
467  KOKKOS_INLINE_FUNCTION
468  volatile_reference operator[] (const ordinal_type& i) volatile {
469  return coeff_[i]; }
470 
471  template <int i>
472  KOKKOS_INLINE_FUNCTION
473  reference getCoeff() { return coeff_[i]; }
474 
475  template <int i>
476  KOKKOS_INLINE_FUNCTION
477  volatile_reference getCoeff() volatile { return coeff_[i]; }
478 
479  template <int i>
480  KOKKOS_INLINE_FUNCTION
481  const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
482 
483  template <int i>
484  KOKKOS_INLINE_FUNCTION
485  const_reference getCoeff() const { return coeff_[i]; }
486 
488  KOKKOS_INLINE_FUNCTION
489  const_volatile_pointer coeff() const volatile { return coeff_; }
490 
492  KOKKOS_INLINE_FUNCTION
493  const_pointer coeff() const { return coeff_; }
494 
496  KOKKOS_INLINE_FUNCTION
497  volatile_pointer coeff() volatile { return coeff_; }
498 
500  KOKKOS_INLINE_FUNCTION
501  pointer coeff() { return coeff_; }
502 
503  private:
504 
506  value_type coeff_[Num];
507 
508  };
509 
510 #endif
511 
512 }
513 
514 namespace Sacado {
515  template <typename ordinal_t, typename value_t, int Num, typename device_t>
516  struct StringName< Stokhos::StaticFixedStorage<ordinal_t,
517  value_t,
518  Num,
519  device_t> > {
520  static std::string eval() {
521  std::stringstream ss;
522  ss << "Stokhos::StaticFixedStorage<"
523  << StringName<ordinal_t>::eval() << ","
524  << StringName<value_t>::eval() << ","
525  << Num << ","
526  << StringName<device_t>::eval() << ">";
527  return ss.str();
528  }
529  };
530 }
531 
532 #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::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 StaticFixedStorage &s)=default
Assignment operator.
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.
KOKKOS_INLINE_FUNCTION ~StaticFixedStorage()=default
Destructor.
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_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()=default
Constructor.
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.
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.