Debian Codegraph @

packages/stokhos/src/sacado/kokkos/Stokhos_ViewStorage.hpp

raw
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
13 #include "Stokhos_DynArrayTraits.hpp"
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
24 struct error_storage_type_is_not_allocateable {};
25 struct error_storage_type_is_not_resizeable {};
26
27 //! Dynamic storage with view semantics and contiguous access
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
54 //! Turn ViewStorage into a meta-function class usable with mpl::apply
55 template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
56 struct apply {
57 typedef ViewStorage<ord_t,val_t,static_length,static_stride,dev_t> type;
58 };
59
60 //! Constructor to satisfy Sacado::MP::Vector, disabled via error type.
61 KOKKOS_INLINE_FUNCTION
62 ViewStorage( const error_storage_type_is_not_allocateable & z =
63 error_storage_type_is_not_allocateable(),
64 const value_type& x = value_type(0) );
65
66 //! Constructor
67 KOKKOS_INLINE_FUNCTION
68 ViewStorage( pointer v ,
69 const ordinal_type & arg_size = 0 ,
70 const ordinal_type & arg_stride = 0 ) :
71 coeff_(v), size_(arg_size), stride_(arg_stride) {}
72
73 //! Constructor from array
74 KOKKOS_INLINE_FUNCTION
75 ViewStorage(const ordinal_type& sz, const value_type* x) {}
76
77 //! Constructor for creating a view
78 KOKKOS_INLINE_FUNCTION
79 ViewStorage(const ordinal_type& sz, pointer v, bool owned) :
80 coeff_(v), size_(sz) {}
81
82 //! Constructor
83 KOKKOS_INLINE_FUNCTION
84 ViewStorage(const ViewStorage& s) :
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
94 //! Destructor
95 KOKKOS_INLINE_FUNCTION
96 ~ViewStorage() {}
97
98 private:
99 //! Assignment operator
100 KOKKOS_INLINE_FUNCTION
101 ViewStorage& operator=(const ViewStorage& );
102 public:
103
104 //! Initialize values to a constant value
105 KOKKOS_INLINE_FUNCTION
106 void init(const_reference v) {
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
117 //! Initialize values to an array of values
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
129 //! Load values to an array of values
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
140 //! Resize function disabled.
141 KOKKOS_INLINE_FUNCTION
142 void resize( ordinal_type s ) {}
143 //void resize( const error_storage_type_is_not_resizeable & );
144
145 //! Reset function disabled.
146 KOKKOS_INLINE_FUNCTION
147 void shallowReset( pointer v ,
148 const error_storage_type_is_not_resizeable & ,
149 const error_storage_type_is_not_resizeable & ,
150 const bool owned );
151
152 //! Return size
153 KOKKOS_INLINE_FUNCTION
154 ordinal_type size() const { return size_.value ; }
155
156 //! Coefficient access (avoid if possible)
157 KOKKOS_INLINE_FUNCTION
158 reference operator[] (const ordinal_type& i) const
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
171 //! Get coefficients
172 KOKKOS_INLINE_FUNCTION
173 const_pointer coeff() const { return coeff_; }
174
175 //! Get coefficients
176 KOKKOS_INLINE_FUNCTION
177 pointer coeff() { return coeff_; }
178
179 private:
180
181 //! Coefficient values
182 const pointer coeff_;
183
184 //! Size of array
185 const Kokkos::Impl::integral_nonzero_constant< ordinal_t , static_length > size_ ;
186
187 //! Stride of array
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 */