Debian Codegraph @

packages/stokhos/src/sacado/kokkos/Stokhos_DynamicStridedStorage.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_DYNAMIC_STRIDED_STORAGE_HPP
11 #define STOKHOS_DYNAMIC_STRIDED_STORAGE_HPP
12
13 #include "Stokhos_DynArrayTraits.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 template <typename ordinal_t, typename value_t, typename device_t>
24 class DynamicStridedStorage {
25 public:
26
27 static const bool is_static = false;
28 static const int static_size = 0;
29 static const bool supports_reset = true;
30
31 typedef ordinal_t ordinal_type;
32 typedef value_t value_type;
33 typedef device_t execution_space;
34 typedef value_type& reference;
35 typedef volatile value_type& volatile_reference;
36 typedef const value_type& const_reference;
37 typedef const volatile value_type& const_volatile_reference;
38 typedef value_type* pointer;
39 typedef volatile value_type* volatile_pointer;
40 typedef const value_type* const_pointer;
41 typedef const volatile value_type* const_volatile_pointer;
42 typedef Stokhos::DynArrayTraits<value_type,execution_space> ds;
43
44 //! Turn DynamicStridedStorage into a meta-function class usable with mpl::apply
45 template <typename ord_t, typename val_t = value_t, typename dev_t = device_t >
46 struct apply {
47 typedef DynamicStridedStorage<ord_t,val_t,dev_t> type;
48 };
49
50 //! Constructor
51 KOKKOS_INLINE_FUNCTION
52 DynamicStridedStorage(const ordinal_type& sz = 1,
53 const value_type& x = value_type(0.0)) :
54 sz_(sz), stride_(1), is_owned_(true) {
55 coeff_ = ds::get_and_fill(sz_, x);
56 }
57
58 //! Constructor from array
59 KOKKOS_INLINE_FUNCTION
60 DynamicStridedStorage(const ordinal_type& sz, const value_type* x) :
61 sz_(sz), stride_(1), is_owned_(true) {
62 coeff_ = ds::get_and_fill(x, sz_);
63 }
64
65 //! Constructor for creating a view
66 KOKKOS_INLINE_FUNCTION
67 DynamicStridedStorage(const ordinal_type& sz, pointer v, bool owned) :
68 coeff_(v), sz_(sz), stride_(1), is_owned_(owned) {}
69
70 //! Constructor
71 KOKKOS_INLINE_FUNCTION
72 DynamicStridedStorage(const DynamicStridedStorage& s) :
73 sz_(s.sz_), stride_(1), is_owned_(true) {
74 if (s.stride_ == 1)
75 coeff_ = ds::get_and_fill(s.coeff_, sz_);
76 else {
77 coeff_ = ds::get_and_fill(sz_);
78 for (ordinal_type i=0; i<sz_; ++i)
79 coeff_[i] = s[i];
80 }
81 }
82
83 //! Constructor
84 KOKKOS_INLINE_FUNCTION
85 DynamicStridedStorage(const volatile DynamicStridedStorage& s) :
86 sz_(s.sz_), stride_(1), is_owned_(true) {
87 if (s.stride_ == 1)
88 coeff_ = ds::get_and_fill(s.coeff_, sz_);
89 else {
90 coeff_ = ds::get_and_fill(sz_);
91 for (ordinal_type i=0; i<sz_; ++i)
92 coeff_[i] = s[i];
93 }
94 }
95
96 //! Destructor
97 KOKKOS_INLINE_FUNCTION
98 ~DynamicStridedStorage() {
99 if (is_owned_) ds::destroy_and_release(coeff_, sz_*stride_);
100 }
101
102 //! Assignment operator
103 KOKKOS_INLINE_FUNCTION
104 DynamicStridedStorage& operator=(const DynamicStridedStorage& s) {
105 if (&s != this) {
106 if (s.sz_ != sz_) {
107 if (is_owned_)
108 ds::destroy_and_release(coeff_, sz_*stride_);
109 if (s.stride_ == 1)
110 coeff_ = ds::get_and_fill(s.coeff_, s.sz_);
111 else {
112 coeff_ = ds::get_and_fill(s.sz_);
113 for (ordinal_type i=0; i<s.sz_; ++i)
114 coeff_[i] = s[i];
115 }
116 sz_ = s.sz_;
117 stride_ = 1;
118 is_owned_ = true;
119 }
120 else {
121 if (stride_ == 1 and s.stride_ == 1)
122 ds::copy(s.coeff_, coeff_, sz_);
123 else
124 for (ordinal_type i=0; i<s.sz_; ++i)
125 coeff_[i*stride_] = s[i];
126 }
127 }
128 return *this;
129 }
130
131 //! Assignment operator
132 KOKKOS_INLINE_FUNCTION
133 DynamicStridedStorage& operator=(const volatile DynamicStridedStorage& s) {
134 if (&s != this) {
135 if (s.sz_ != sz_) {
136 if (is_owned_)
137 ds::destroy_and_release(coeff_, sz_*stride_);
138 if (s.stride_ == 1)
139 coeff_ = ds::get_and_fill(s.coeff_, s.sz_);
140 else {
141 coeff_ = ds::get_and_fill(s.sz_);
142 for (ordinal_type i=0; i<s.sz_; ++i)
143 coeff_[i] = s[i];
144 }
145 sz_ = s.sz_;
146 stride_ = 1;
147 is_owned_ = true;
148 }
149 else {
150 if (stride_ == 1 and s.stride_ == 1)
151 ds::copy(s.coeff_, coeff_, sz_);
152 else
153 for (ordinal_type i=0; i<s.sz_; ++i)
154 coeff_[i*stride_] = s[i];
155 }
156 }
157 return *this;
158 }
159
160 //! Assignment operator
161 KOKKOS_INLINE_FUNCTION
162 volatile DynamicStridedStorage&
163 operator=(const DynamicStridedStorage& s) volatile {
164 if (&s != this) {
165 if (s.sz_ != sz_) {
166 if (is_owned_)
167 ds::destroy_and_release(coeff_, sz_*stride_);
168 if (s.stride_ == 1)
169 coeff_ = ds::get_and_fill(s.coeff_, s.sz_);
170 else {
171 coeff_ = ds::get_and_fill(s.sz_);
172 for (ordinal_type i=0; i<s.sz_; ++i)
173 coeff_[i] = s[i];
174 }
175 sz_ = s.sz_;
176 stride_ = 1;
177 is_owned_ = true;
178 }
179 else {
180 if (stride_ == 1 and s.stride_ == 1)
181 ds::copy(s.coeff_, coeff_, sz_);
182 else
183 for (ordinal_type i=0; i<s.sz_; ++i)
184 coeff_[i*stride_] = s[i];
185 }
186 }
187 return *this;
188 }
189
190 //! Assignment operator
191 KOKKOS_INLINE_FUNCTION
192 volatile DynamicStridedStorage&
193 operator=(const volatile DynamicStridedStorage& s) volatile {
194 if (&s != this) {
195 if (s.sz_ != sz_) {
196 if (is_owned_)
197 ds::destroy_and_release(coeff_, sz_*stride_);
198 if (s.stride_ == 1)
199 coeff_ = ds::get_and_fill(s.coeff_, s.sz_);
200 else {
201 coeff_ = ds::get_and_fill(s.sz_);
202 for (ordinal_type i=0; i<s.sz_; ++i)
203 coeff_[i] = s[i];
204 }
205 sz_ = s.sz_;
206 stride_ = 1;
207 is_owned_ = true;
208 }
209 else {
210 if (stride_ == 1 and s.stride_ == 1)
211 ds::copy(s.coeff_, coeff_, sz_);
212 else
213 for (ordinal_type i=0; i<s.sz_; ++i)
214 coeff_[i*stride_] = s[i];
215 }
216 }
217 return *this;
218 }
219
220 //! Initialize values to a constant value
221 KOKKOS_INLINE_FUNCTION
222 void init(const_reference v) {
223 if (stride_ == 1)
224 ds::fill(coeff_, sz_, v);
225 else
226 for (ordinal_type i=0; i<sz_; ++i)
227 coeff_[i*stride_] = v;
228 }
229
230 //! Initialize values to a constant value
231 KOKKOS_INLINE_FUNCTION
232 void init(const_reference v) volatile {
233 if (stride_ == 1)
234 ds::fill(coeff_, sz_, v);
235 else
236 for (ordinal_type i=0; i<sz_; ++i)
237 coeff_[i*stride_] = v;
238 }
239
240 //! Initialize values to an array of values
241 KOKKOS_INLINE_FUNCTION
242 void init(const_pointer v, const ordinal_type& sz = 0) {
243 ordinal_type my_sz = sz;
244 if (sz == 0)
245 my_sz = sz_;
246 if (stride_ == 1)
247 ds::copy(v, coeff_, my_sz);
248 else
249 for (ordinal_type i=0; i<my_sz; ++i)
250 coeff_[i*stride_] = v[i];
251 }
252
253 //! Initialize values to an array of values
254 KOKKOS_INLINE_FUNCTION
255 void init(const_pointer v, const ordinal_type& sz = 0) volatile {
256 ordinal_type my_sz = sz;
257 if (sz == 0)
258 my_sz = sz_;
259 if (stride_ == 1)
260 ds::copy(v, coeff_, my_sz);
261 else
262 for (ordinal_type i=0; i<my_sz; ++i)
263 coeff_[i*stride_] = v[i];
264 }
265
266 //! Load values to an array of values
267 KOKKOS_INLINE_FUNCTION
268 void load(pointer v) {
269 if (stride_ == 1)
270 copy(v, coeff_, sz_);
271 for (ordinal_type i=0; i<sz_; ++i)
272 coeff_[i*stride_] = v[i];
273 }
274
275 //! Load values to an array of values
276 KOKKOS_INLINE_FUNCTION
277 void load(pointer v) volatile {
278 if (stride_ == 1)
279 copy(v, coeff_, sz_);
280 for (ordinal_type i=0; i<sz_; ++i)
281 coeff_[i*stride_] = v[i];
282 }
283
284 //! Resize to new size (values are preserved)
285 KOKKOS_INLINE_FUNCTION
286 void resize(const ordinal_type& sz) {
287 if (sz != sz_) {
288 value_type *coeff_new = ds::get_and_fill(sz);
289 ordinal_type my_sz = sz_;
290 if (sz_ > sz)
291 my_sz = sz;
292 if (stride_ == 1)
293 ds::copy(coeff_, coeff_new, my_sz);
294 else
295 for (ordinal_type i=0; i<my_sz; ++i)
296 coeff_new[i] = coeff_[i*stride_];
297 if (is_owned_)
298 ds::destroy_and_release(coeff_, sz_*stride_);
299 coeff_ = coeff_new;
300 sz_ = sz;
301 stride_ = 1;
302 is_owned_ = true;
303 }
304 }
305
306 //! Resize to new size (values are preserved)
307 KOKKOS_INLINE_FUNCTION
308 void resize(const ordinal_type& sz) volatile {
309 if (sz != sz_) {
310 value_type *coeff_new = ds::get_and_fill(sz);
311 ordinal_type my_sz = sz_;
312 if (sz_ > sz)
313 my_sz = sz;
314 if (stride_ == 1)
315 ds::copy(coeff_, coeff_new, my_sz);
316 else
317 for (ordinal_type i=0; i<my_sz; ++i)
318 coeff_new[i] = coeff_[i*stride_];
319 if (is_owned_)
320 ds::destroy_and_release(coeff_, sz_*stride_);
321 coeff_ = coeff_new;
322 sz_ = sz;
323 stride_ = 1;
324 is_owned_ = true;
325 }
326 }
327
328 //! Reset storage to given array, size, and stride
329 KOKKOS_INLINE_FUNCTION
330 void shallowReset(pointer v, const ordinal_type& sz,
331 const ordinal_type& stride, bool owned) {
332 if (is_owned_)
333 ds::destroy_and_release(coeff_, sz_*stride_);
334 coeff_ = v;
335 sz_ = sz;
336 stride_ = stride;
337 is_owned_ = owned;
338 }
339
340 //! Reset storage to given array, size, and stride
341 KOKKOS_INLINE_FUNCTION
342 void shallowReset(pointer v, const ordinal_type& sz,
343 const ordinal_type& stride, bool owned) volatile {
344 if (is_owned_)
345 ds::destroy_and_release(coeff_, sz_*stride_);
346 coeff_ = v;
347 sz_ = sz;
348 stride_ = stride;
349 is_owned_ = owned;
350 }
351
352 //! Return size
353 KOKKOS_INLINE_FUNCTION
354 ordinal_type size() const { return sz_; }
355
356 //! Return size
357 KOKKOS_INLINE_FUNCTION
358 ordinal_type size() const volatile { return sz_; }
359
360 //! Coefficient access (avoid if possible)
361 KOKKOS_INLINE_FUNCTION
362 const_reference operator[] (const ordinal_type& i) const {
363 return coeff_[i*stride_];
364 }
365
366 //! Coefficient access (avoid if possible)
367 KOKKOS_INLINE_FUNCTION
368 const_volatile_reference operator[] (const ordinal_type& i) const volatile {
369 return coeff_[i*stride_];
370 }
371
372 //! Coefficient access (avoid if possible)
373 KOKKOS_INLINE_FUNCTION
374 reference operator[] (const ordinal_type& i) {
375 return coeff_[i*stride_];
376 }
377
378 //! Coefficient access (avoid if possible)
379 KOKKOS_INLINE_FUNCTION
380 volatile_reference operator[] (const ordinal_type& i) volatile {
381 return coeff_[i*stride_];
382 }
383
384 template <int i>
385 KOKKOS_INLINE_FUNCTION
386 reference getCoeff() { return coeff_[i*stride_]; }
387
388 template <int i>
389 KOKKOS_INLINE_FUNCTION
390 volatile_reference getCoeff() volatile { return coeff_[i*stride_]; }
391
392 template <int i>
393 KOKKOS_INLINE_FUNCTION
394 const_reference getCoeff() const { return coeff_[i*stride_]; }
395
396 template <int i>
397 KOKKOS_INLINE_FUNCTION
398 const_volatile_reference getCoeff() const volatile { return coeff_[i*stride_]; }
399
400 //! Get coefficients
401 KOKKOS_INLINE_FUNCTION
402 const_volatile_pointer coeff() const volatile { return coeff_; }
403
404 //! Get coefficients
405 KOKKOS_INLINE_FUNCTION
406 const_pointer coeff() const { return coeff_; }
407
408 //! Get coefficients
409 KOKKOS_INLINE_FUNCTION
410 volatile_pointer coeff() volatile { return coeff_; }
411
412 //! Get coefficients
413 KOKKOS_INLINE_FUNCTION
414 pointer coeff() { return coeff_; }
415
416 private:
417
418 //! Coefficient values
419 pointer coeff_;
420
421 //! Size of array used
422 ordinal_type sz_;
423
424 //! Stride of array
425 ordinal_type stride_;
426
427 //! Do we own the array
428 bool is_owned_;
429
430 };
431
432 }
433
434 #include "Stokhos_StorageHelpers.hpp"
435 STOKHOS_STORAGE_HELPER_STRINGNAME_DYNAMIC(DynamicStridedStorage)
436
437 #endif // STOKHOS_DYNAMIC_STORAGE_HPP