Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_ViewStorage.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_VIEW_STORAGE_HPP
11 #define STOKHOS_VIEW_STORAGE_HPP
12 
14 
15 #include "Kokkos_Macros.hpp"
16 #include "impl/Kokkos_Traits.hpp"
17 
18 #include "Sacado_Traits.hpp"
19 #include "Stokhos_KokkosTraits.hpp"
20 #include <sstream>
21 
22 namespace Stokhos {
23 
26 
28  template < typename ordinal_t ,
29  typename value_t ,
30  unsigned static_length ,
31  unsigned static_stride ,
32  typename device_t >
33  class ViewStorage {
34  private:
35  // Enumerated flag so logic is evaluated at compile-time
36  enum { stride_one = 1 == static_stride };
37  public:
38 
39  static const bool is_static = static_length != 0 ;
40  static const int static_size = static_length ;
41  static const bool supports_reset = false ;
42  static const bool supports_resize = ! is_static ;
43  static const bool supports_view = true ;
44 
45  typedef ordinal_t ordinal_type;
46  typedef value_t value_type;
47  typedef device_t execution_space;
48  typedef value_type & reference;
49  typedef const value_type & const_reference;
50  typedef value_type * pointer;
51  typedef const value_type * const_pointer;
52  // typedef Stokhos::DynArrayTraits<value_type,execution_space> ds;
53 
55  template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
56  struct apply {
58  };
59 
61  KOKKOS_INLINE_FUNCTION
64  const value_type& x = value_type(0) );
65 
67  KOKKOS_INLINE_FUNCTION
69  const ordinal_type & arg_size = 0 ,
70  const ordinal_type & arg_stride = 0 ) :
71  coeff_(v), size_(arg_size), stride_(arg_stride) {}
72 
74  KOKKOS_INLINE_FUNCTION
75  ViewStorage(const ordinal_type& sz, const value_type* x) {}
76 
78  KOKKOS_INLINE_FUNCTION
79  ViewStorage(const ordinal_type& sz, pointer v, bool owned) :
80  coeff_(v), size_(sz) {}
81 
83  KOKKOS_INLINE_FUNCTION
85  coeff_(s.coeff_), size_(s.size_), stride_(s.stride_) {}
86 
87  KOKKOS_INLINE_FUNCTION
88  ViewStorage( const ViewStorage& s , const ordinal_type & arg_begin , const ordinal_type & arg_end )
89  : coeff_( ( ordinal_type(size_.value) < arg_end ) ? pointer(0) : s.coeff_ + arg_begin * ordinal_type(s.stride_.value) )
90  , size_( ( ordinal_type(size_.value) < arg_end ) ? ordinal_type(0) : arg_end - arg_begin )
91  , stride_( s.stride_ )
92  {}
93 
95  KOKKOS_INLINE_FUNCTION
97 
98 private:
100  KOKKOS_INLINE_FUNCTION
102 public:
103 
105  KOKKOS_INLINE_FUNCTION
107  if ( stride_one ) {
108  for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i] = v ; }
109  }
110  else {
111  for ( ordinal_type i = 0 ; i < size_.value * stride_.value ; i += stride_.value ) {
112  coeff_[i] = v ;
113  }
114  }
115  }
116 
118  KOKKOS_INLINE_FUNCTION
119  void init(const_pointer v, const ordinal_type& sz = 0) {
120  const ordinal_type n = sz ? sz : size_.value ;
121  if ( stride_one ) {
122  for ( ordinal_type i = 0 ; i < n ; ++i ) { coeff_[i] = v[i] ; }
123  }
124  else {
125  for ( ordinal_type i = 0 ; i < n ; ++i ) { coeff_[i*stride_.value] = v[i] ; }
126  }
127  }
128 
130  KOKKOS_INLINE_FUNCTION
131  void load(pointer v) {
132  if ( stride_one ) {
133  for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i] = v[i] ; }
134  }
135  else {
136  for ( ordinal_type i = 0 ; i < size_.value ; ++i ) { coeff_[i*stride_.value] = v[i] ; }
137  }
138  }
139 
141  KOKKOS_INLINE_FUNCTION
142  void resize( ordinal_type s ) {}
143  //void resize( const error_storage_type_is_not_resizeable & );
144 
146  KOKKOS_INLINE_FUNCTION
147  void shallowReset( pointer v ,
150  const bool owned );
151 
153  KOKKOS_INLINE_FUNCTION
154  ordinal_type size() const { return size_.value ; }
155 
157  KOKKOS_INLINE_FUNCTION
159  {
160  return coeff_[ stride_one ? i : i * stride_.value ];
161  }
162 
163  template <int i>
164  KOKKOS_INLINE_FUNCTION
165  reference getCoeff() { return coeff_[ stride_one ? i : i * stride_.value ]; }
166 
167  template <int i>
168  KOKKOS_INLINE_FUNCTION
169  const_reference getCoeff() const { return coeff_[ stride_one ? i : i * stride_.value ]; }
170 
172  KOKKOS_INLINE_FUNCTION
173  const_pointer coeff() const { return coeff_; }
174 
176  KOKKOS_INLINE_FUNCTION
177  pointer coeff() { return coeff_; }
178 
179  private:
180 
183 
185  const Kokkos::Impl::integral_nonzero_constant< ordinal_t , static_length > size_ ;
186 
188  const Kokkos::Impl::integral_nonzero_constant< ordinal_t , static_stride > stride_ ;
189  };
190 
191  template< class Storage >
192  struct is_ViewStorage { enum { value = false }; };
193 
194  template < typename ordinal_t ,
195  typename value_t ,
196  unsigned static_length ,
197  unsigned static_stride ,
198  typename device_t >
199  struct is_ViewStorage< ViewStorage< ordinal_t , value_t , static_length , static_stride , device_t > >
200  { enum { value = true }; };
201 
202 
203 } /* namespace Stokhos */
204 
205 namespace Sacado {
206  template <typename ordinal_t, typename value_t, unsigned static_length,
207  unsigned static_stride, typename device_t>
208  struct StringName< Stokhos::ViewStorage<ordinal_t,
209  value_t,
210  static_length,
211  static_stride,
212  device_t> > {
213  static std::string eval() {
214  std::stringstream ss;
215  ss << "Stokhos::ViewStorage<"
216  << StringName<ordinal_t>::eval() << ","
217  << StringName<value_t>::eval() << ","
218  << static_length << ","
219  << static_stride << ","
220  << StringName<device_t>::eval() << ">";
221  return ss.str();
222  }
223  };
224 }
225 
226 #endif /* #ifndef STOKHOS_VIEW_STORAGE_HPP */
KOKKOS_INLINE_FUNCTION ~ViewStorage()
Destructor.
KOKKOS_INLINE_FUNCTION void init(const_pointer v, const ordinal_type &sz=0)
Initialize values to an array of values.
Dynamic storage with view semantics and contiguous access.
const Kokkos::Impl::integral_nonzero_constant< ordinal_t, static_stride > stride_
Stride of array.
KOKKOS_INLINE_FUNCTION void init(const_reference v)
Initialize values to a constant value.
KOKKOS_INLINE_FUNCTION ordinal_type size() const
Return size.
KOKKOS_INLINE_FUNCTION const_pointer coeff() const
Get coefficients.
KOKKOS_INLINE_FUNCTION void resize(ordinal_type s)
Resize function disabled.
const Kokkos::Impl::integral_nonzero_constant< ordinal_t, static_length > size_
Size of array.
KOKKOS_INLINE_FUNCTION pointer coeff()
Get coefficients.
KOKKOS_INLINE_FUNCTION reference operator[](const ordinal_type &i) const
Coefficient access (avoid if possible)
const value_type & const_reference
static const int static_size
KOKKOS_INLINE_FUNCTION ViewStorage & operator=(const ViewStorage &)
Assignment operator.
KOKKOS_INLINE_FUNCTION void load(pointer v)
Load values to an array of values.
static const bool is_static
KOKKOS_INLINE_FUNCTION ViewStorage(const error_storage_type_is_not_allocateable &z=error_storage_type_is_not_allocateable(), const value_type &x=value_type(0))
Constructor to satisfy Sacado::MP::Vector, disabled via error type.
KOKKOS_INLINE_FUNCTION void shallowReset(pointer v, const error_storage_type_is_not_resizeable &, const error_storage_type_is_not_resizeable &, const bool owned)
Reset function disabled.
KOKKOS_INLINE_FUNCTION const_reference getCoeff() const
static const bool supports_resize
const pointer coeff_
Coefficient values.
KOKKOS_INLINE_FUNCTION ViewStorage(const ViewStorage &s)
Constructor.
KOKKOS_INLINE_FUNCTION ViewStorage(pointer v, const ordinal_type &arg_size=0, const ordinal_type &arg_stride=0)
Constructor.
KOKKOS_INLINE_FUNCTION ViewStorage(const ViewStorage &s, const ordinal_type &arg_begin, const ordinal_type &arg_end)
const value_type * const_pointer
ViewStorage< ord_t, val_t, static_length, static_stride, dev_t > type
KOKKOS_INLINE_FUNCTION ViewStorage(const ordinal_type &sz, const value_type *x)
Constructor from array.
Turn ViewStorage into a meta-function class usable with mpl::apply.
static const bool supports_reset
KOKKOS_INLINE_FUNCTION ViewStorage(const ordinal_type &sz, pointer v, bool owned)
Constructor for creating a view.
KOKKOS_INLINE_FUNCTION reference getCoeff()
int n
static const bool supports_view