Debian Codegraph @

packages/stokhos/src/sacado/kokkos/Stokhos_StaticStorage.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_STATIC_STORAGE_HPP
11 #define STOKHOS_STATIC_STORAGE_HPP
12
13 #include "Stokhos_StaticArrayTraits.hpp"
14
15 #include "Kokkos_Macros.hpp"
16
17 #include "Sacado_Traits.hpp"
18 #include "Stokhos_KokkosTraits.hpp"
19 #include <sstream>
20
21 namespace Stokhos {
22
23 //! Statically allocated storage class
24 template <typename ordinal_t, typename value_t, int Num, typename device_t>
25 class StaticStorage {
26 public:
27
28 static const bool is_static = false;
29 static const int static_size = 0;
30 static const bool supports_reset = false;
31
32 typedef ordinal_t ordinal_type;
33 typedef value_t value_type;
34 typedef device_t execution_space;
35 typedef value_type& reference;
36 typedef volatile value_type& volatile_reference;
37 typedef const value_type& const_reference;
38 typedef const volatile value_type& const_volatile_reference;
39 typedef value_type* pointer;
40 typedef volatile value_type* volatile_pointer;
41 typedef const value_type* const_pointer;
42 typedef const volatile value_type* const_volatile_pointer;
43 typedef Stokhos::StaticArrayTraits<value_type,execution_space> ss;
44
45 //! Turn StaticStorage into a meta-function class usable with mpl::apply
46 template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
47 struct apply {
48 typedef StaticStorage<ord_t,val_t,Num,dev_t> type;
49 };
50
51 //! Constructor
52 KOKKOS_INLINE_FUNCTION
53 StaticStorage(const ordinal_type& sz = 1,
54 const value_type& x = value_type(0.0)) : sz_(sz) {
55 ss::fill(coeff_, sz_, x);
56 }
57
58 //! Constructor from array
59 KOKKOS_INLINE_FUNCTION
60 StaticStorage(const ordinal_type& sz, const value_type* x) : sz_(sz) {
61 ss::copy(x, coeff_, sz);
62 }
63
64 //! Constructor for creating a view (not allowed)
65 KOKKOS_INLINE_FUNCTION
66 StaticStorage(const ordinal_type& sz, pointer v, bool owned) {}
67
68 //! Copy constructor
69 KOKKOS_INLINE_FUNCTION
70 StaticStorage(const StaticStorage& s) : sz_(s.sz_) {
71 ss::copy(s.coeff_, coeff_, sz_);
72 }
73
74 //! Copy constructor
75 KOKKOS_INLINE_FUNCTION
76 StaticStorage(const volatile StaticStorage& s) : sz_(s.sz_) {
77 ss::copy(s.coeff_, coeff_, sz_);
78 }
79
80 //! Destructor
81 KOKKOS_INLINE_FUNCTION
82 ~StaticStorage() {}
83
84 //! Assignment operator
85 KOKKOS_INLINE_FUNCTION
86 StaticStorage& operator=(const StaticStorage& s) {
87 sz_ = s.sz_;
88 ss::copy(s.coeff_, coeff_, sz_);
89 return *this;
90 }
91
92 //! Assignment operator
93 KOKKOS_INLINE_FUNCTION
94 StaticStorage& operator=(const volatile StaticStorage& s) {
95 sz_ = s.sz_;
96 ss::copy(s.coeff_, coeff_, sz_);
97 return *this;
98 }
99
100 //! Assignment operator
101 KOKKOS_INLINE_FUNCTION
102 volatile StaticStorage& operator=(const StaticStorage& s) volatile {
103 sz_ = s.sz_;
104 ss::copy(s.coeff_, coeff_, sz_);
105 return *this;
106 }
107
108 //! Assignment operator
109 KOKKOS_INLINE_FUNCTION
110 volatile StaticStorage& operator=(const volatile StaticStorage& s) volatile {
111 sz_ = s.sz_;
112 ss::copy(s.coeff_, coeff_, sz_);
113 return *this;
114 }
115
116 //! Initialize values to a constant value
117 KOKKOS_INLINE_FUNCTION
118 void init(const_reference v) {
119 ss::fill(coeff_, sz_, v);
120 }
121
122 //! Initialize values to a constant value
123 KOKKOS_INLINE_FUNCTION
124 void init(const_reference v) volatile {
125 ss::fill(coeff_, sz_, v);
126 }
127
128 //! Initialize values to an array of values
129 KOKKOS_INLINE_FUNCTION
130 void init(const_pointer v, const ordinal_type& sz = 0) {
131 if (sz == 0)
132 ss::copy(v, coeff_, sz_);
133 else
134 ss::copy(v, coeff_, sz);
135 }
136
137 //! Initialize values to an array of values
138 KOKKOS_INLINE_FUNCTION
139 void init(const_pointer v, const ordinal_type& sz = 0) volatile {
140 if (sz == 0)
141 ss::copy(v, coeff_, sz_);
142 else
143 ss::copy(v, coeff_, sz);
144 }
145
146 //! Load values to an array of values
147 KOKKOS_INLINE_FUNCTION
148 void load(pointer v) {
149 ss::copy(v, coeff_, sz_);
150 }
151
152 //! Load values to an array of values
153 KOKKOS_INLINE_FUNCTION
154 void load(pointer v) volatile {
155 ss::copy(v, coeff_, sz_);
156 }
157
158 //! Resize to new size (values are preserved)
159 KOKKOS_INLINE_FUNCTION
160 void resize(const ordinal_type& sz) {
161 if (sz > sz_)
162 ss::fill(coeff_+sz_, sz-sz_, value_type(0.0));
163 sz_ = sz;
164 }
165
166 //! Resize to new size (values are preserved)
167 KOKKOS_INLINE_FUNCTION
168 void resize(const ordinal_type& sz) volatile {
169 if (sz > sz_)
170 ss::fill(coeff_+sz_, sz-sz_, value_type(0.0));
171 sz_ = sz;
172 }
173
174 //! Reset storage to given array, size, and stride
175 KOKKOS_INLINE_FUNCTION
176 void shallowReset(pointer v, const ordinal_type& sz,
177 const ordinal_type& stride, bool owned) {}
178
179 //! Reset storage to given array, size, and stride
180 KOKKOS_INLINE_FUNCTION
181 void shallowReset(pointer v, const ordinal_type& sz,
182 const ordinal_type& stride, bool owned) volatile {}
183
184 //! Return size
185 KOKKOS_INLINE_FUNCTION
186 ordinal_type size() const { return sz_; }
187
188 //! Return size
189 KOKKOS_INLINE_FUNCTION
190 ordinal_type size() const volatile { return sz_; }
191
192 //! Coefficient access (avoid if possible)
193 KOKKOS_INLINE_FUNCTION
194 const_reference operator[] (const ordinal_type& i) const {
195 return coeff_[i];
196 }
197
198 //! Coefficient access (avoid if possible)
199 KOKKOS_INLINE_FUNCTION
200 const_volatile_reference operator[] (const ordinal_type& i) const volatile {
201 return coeff_[i];
202 }
203
204 //! Coefficient access (avoid if possible)
205 KOKKOS_INLINE_FUNCTION
206 reference operator[] (const ordinal_type& i) { return coeff_[i]; }
207
208 //! Coefficient access (avoid if possible)
209 KOKKOS_INLINE_FUNCTION
210 volatile_reference operator[] (const ordinal_type& i) volatile {
211 return coeff_[i];
212 }
213
214 template <int i>
215 KOKKOS_INLINE_FUNCTION
216 reference getCoeff() { return coeff_[i]; }
217
218 template <int i>
219 KOKKOS_INLINE_FUNCTION
220 volatile_reference getCoeff() volatile { return coeff_[i]; }
221
222 template <int i>
223 KOKKOS_INLINE_FUNCTION
224 const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
225
226 template <int i>
227 KOKKOS_INLINE_FUNCTION
228 const_reference getCoeff() const { return coeff_[i]; }
229
230 //! Get coefficients
231 KOKKOS_INLINE_FUNCTION
232 const_volatile_pointer coeff() const volatile { return coeff_; }
233
234 //! Get coefficients
235 KOKKOS_INLINE_FUNCTION
236 const_pointer coeff() const { return coeff_; }
237
238 //! Get coefficients
239 KOKKOS_INLINE_FUNCTION
240 volatile_pointer coeff() volatile { return coeff_; }
241
242 //! Get coefficients
243 KOKKOS_INLINE_FUNCTION
244 pointer coeff() { return coeff_; }
245
246 private:
247
248 //! Coefficient values
249 value_type coeff_[Num];
250
251 //! Size of array used
252 ordinal_type sz_;
253
254 };
255
256 }
257
258 #include "Stokhos_StorageHelpers.hpp"
259 STOKHOS_STORAGE_HELPER_STRINGNAME_STATIC(StaticStorage)
260
261 #endif // STOKHOS_STATIC_STORAGE_HPP