Debian Codegraph @

packages/stokhos/src/sacado/kokkos/Stokhos_StaticFixedStorage.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_FIXED_STORAGE_HPP
11 #define STOKHOS_STATIC_FIXED_STORAGE_HPP
12
13 #include "Stokhos_StaticArrayTraits.hpp"
14 #include "Stokhos_MemoryTraits.hpp"
15
16 // We are hooking into Kokkos Core internals here
17 // Need to define this macro since we include non-public headers
18 #ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
19 #define KOKKOS_IMPL_PUBLIC_INCLUDE
20 #define KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
21 #endif
22 #include "Kokkos_Core_fwd.hpp"
23 #include "Cuda/Kokkos_Cuda.hpp"
24 #ifdef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
25 #undef KOKKOS_IMPL_PUBLIC_INCLUDE
26 #undef KOKKOS_IMPL_PUBLIC_INCLUDE_NOTDEFINED_CORE
27 #endif
28
29 #include "Sacado_Traits.hpp"
30 #include "Stokhos_KokkosTraits.hpp"
31 #include <sstream>
32
33 namespace Stokhos {
34
35 //! Statically allocated storage class
36 template <typename ordinal_t, typename value_t, int Num, typename device_t>
37 class StaticFixedStorage {
38 public:
39
40 static const bool is_static = true ;
41 static const int static_size = Num;
42 static const bool supports_reset = false;
43
44 typedef ordinal_t ordinal_type;
45 typedef value_t value_type;
46 typedef typename device_t::execution_space execution_space;
47 typedef typename device_t::memory_space memory_space;
48 typedef value_type& reference;
49 typedef volatile value_type& volatile_reference;
50 typedef const value_type& const_reference;
51 typedef const volatile value_type& const_volatile_reference;
52 typedef value_type* pointer;
53 typedef volatile value_type* volatile_pointer;
54 typedef const value_type* const_pointer;
55 typedef const volatile value_type* const_volatile_pointer;
56 typedef Stokhos::StaticArrayTraits<value_type,execution_space> ss;
57
58 typedef typename Stokhos::MemoryTraits<memory_space> MemTraits;
59
60 //! Turn StaticFixedStorage into a meta-function class usable with mpl::apply
61 template <typename ord_t, typename val_t = value_t , typename dev_t = device_t >
62 struct apply {
63 typedef StaticFixedStorage<ord_t,val_t,Num,dev_t> type;
64 };
65
66 template <int N>
67 struct apply_N {
68 typedef StaticFixedStorage<ordinal_type,value_type,N,device_t> type;
69 };
70
71 //! Constructor
72 KOKKOS_DEFAULTED_FUNCTION
73 StaticFixedStorage() = default;
74
75 //! Constructor
76 KOKKOS_INLINE_FUNCTION
77 StaticFixedStorage(const ordinal_type& sz,
78 const value_type& x = value_type(0.0)) {
79 ss::fill(coeff_, Num, x);
80 }
81
82 //! Constructor from array
83 KOKKOS_INLINE_FUNCTION
84 StaticFixedStorage(const ordinal_type& sz, const value_type* x) {
85 ss::copy(x, coeff_, sz);
86 }
87
88 //! Constructor for creating a view (not allowed)
89 KOKKOS_INLINE_FUNCTION
90 StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
91
92 //! Copy constructor
93 KOKKOS_DEFAULTED_FUNCTION
94 StaticFixedStorage(const StaticFixedStorage& s) = default;
95
96 //! Copy constructor
97 KOKKOS_INLINE_FUNCTION
98 StaticFixedStorage(const volatile StaticFixedStorage& s) {
99 ss::copy(s.coeff_, coeff_, Num);
100 }
101
102 //! Destructor
103 KOKKOS_DEFAULTED_FUNCTION
104 ~StaticFixedStorage() = default;
105
106 //! Assignment operator
107 KOKKOS_DEFAULTED_FUNCTION
108 StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
109
110 //! Assignment operator
111 KOKKOS_INLINE_FUNCTION
112 StaticFixedStorage& operator=(const volatile StaticFixedStorage& s) {
113 ss::copy(s.coeff_, coeff_, Num);
114 return *this;
115 }
116
117 //! Assignment operator
118 KOKKOS_INLINE_FUNCTION
119 /*volatile*/ StaticFixedStorage&
120 operator=(const StaticFixedStorage& s) volatile {
121 ss::copy(s.coeff_, coeff_, Num);
122 return const_cast<StaticFixedStorage&>(*this);
123 }
124
125 //! Assignment operator
126 KOKKOS_INLINE_FUNCTION
127 /*volatile*/ StaticFixedStorage&
128 operator=(const volatile StaticFixedStorage& s) volatile {
129 ss::copy(s.coeff_, coeff_, Num);
130 return const_cast<StaticFixedStorage&>(*this);
131 }
132
133 //! Initialize values to a constant value
134 KOKKOS_INLINE_FUNCTION
135 void init(const_reference v) {
136 ss::fill(coeff_, Num, v);
137 }
138
139 //! Initialize values to a constant value
140 KOKKOS_INLINE_FUNCTION
141 void init(const_reference v) volatile {
142 ss::fill(coeff_, Num, v);
143 }
144
145 //! Initialize values to an array of values
146 KOKKOS_INLINE_FUNCTION
147 void init(const_pointer v, const ordinal_type& sz = 0) {
148 if (sz == 0)
149 ss::copy(v, coeff_, Num);
150 else
151 ss::copy(v, coeff_, sz);
152 }
153
154 //! Initialize values to an array of values
155 KOKKOS_INLINE_FUNCTION
156 void init(const_pointer v, const ordinal_type& sz = 0) volatile {
157 if (sz == 0)
158 ss::copy(v, coeff_, Num);
159 else
160 ss::copy(v, coeff_, sz);
161 }
162
163 //! Load values to an array of values
164 KOKKOS_INLINE_FUNCTION
165 void load(pointer v) {
166 ss::copy(v, coeff_, Num);
167 }
168
169 //! Load values to an array of values
170 KOKKOS_INLINE_FUNCTION
171 void load(pointer v) volatile {
172 ss::copy(v, coeff_, Num);
173 }
174
175 //! Resize to new size (values are preserved)
176 KOKKOS_INLINE_FUNCTION
177 void resize(const ordinal_type& sz) {}
178
179 //! Resize to new size (values are preserved)
180 KOKKOS_INLINE_FUNCTION
181 void resize(const ordinal_type& sz) volatile {}
182
183 //! Reset storage to given array, size, and stride
184 KOKKOS_INLINE_FUNCTION
185 void shallowReset(pointer v, const ordinal_type& sz,
186 const ordinal_type& stride, bool owned) {}
187
188 //! Reset storage to given array, size, and stride
189 KOKKOS_INLINE_FUNCTION
190 void shallowReset(pointer v, const ordinal_type& sz,
191 const ordinal_type& stride, bool owned) volatile {}
192
193 //! Return size
194 KOKKOS_INLINE_FUNCTION
195 static ordinal_type size() { return Num; }
196
197 //! Coefficient access (avoid if possible)
198 KOKKOS_INLINE_FUNCTION
199 const_reference operator[] (const ordinal_type& i) const {
200 return coeff_[i];
201 }
202
203 //! Coefficient access (avoid if possible)
204 KOKKOS_INLINE_FUNCTION
205 const_volatile_reference operator[] (const ordinal_type& i) const volatile {
206 return coeff_[i];
207 }
208
209 //! Coefficient access (avoid if possible)
210 KOKKOS_INLINE_FUNCTION
211 reference operator[] (const ordinal_type& i) { return coeff_[i]; }
212
213 //! Coefficient access (avoid if possible)
214 KOKKOS_INLINE_FUNCTION
215 volatile_reference operator[] (const ordinal_type& i) volatile {
216 return coeff_[i]; }
217
218 template <int i>
219 KOKKOS_INLINE_FUNCTION
220 reference getCoeff() { return coeff_[i]; }
221
222 template <int i>
223 KOKKOS_INLINE_FUNCTION
224 volatile_reference getCoeff() volatile { return coeff_[i]; }
225
226 template <int i>
227 KOKKOS_INLINE_FUNCTION
228 const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
229
230 template <int i>
231 KOKKOS_INLINE_FUNCTION
232 const_reference getCoeff() const { return coeff_[i]; }
233
234 //! Get coefficients
235 KOKKOS_INLINE_FUNCTION
236 const_volatile_pointer coeff() const volatile { return coeff_; }
237
238 //! Get coefficients
239 KOKKOS_INLINE_FUNCTION
240 const_pointer coeff() const { return coeff_; }
241
242 //! Get coefficients
243 KOKKOS_INLINE_FUNCTION
244 volatile_pointer coeff() volatile { return coeff_; }
245
246 //! Get coefficients
247 KOKKOS_INLINE_FUNCTION
248 pointer coeff() { return coeff_; }
249
250 private:
251
252 //! Coefficient values
253 #if STOKHOS_ALIGN_MEMORY && ( defined(__INTEL_COMPILER) )
254 value_type coeff_[Num] __attribute__((aligned(MemTraits::Alignment)));
255 #else
256 value_type coeff_[Num];
257 #endif
258
259 };
260
261 #if defined(KOKKOS_ENABLE_CUDA)
262
263 //! Statically allocated storage class
264 /*!
265 * Partial specialization for Cuda where there is no alignment specified
266 * for the coeff_ array.
267 */
268 template <typename ordinal_t, typename value_t, int Num>
269 class StaticFixedStorage<ordinal_t,value_t,Num,Kokkos::Cuda> {
270 public:
271
272 static const bool is_static = true ;
273 static const int static_size = Num;
274 static const bool supports_reset = false;
275
276 typedef ordinal_t ordinal_type;
277 typedef value_t value_type;
278 typedef Kokkos::Cuda execution_space;
279 typedef value_type& reference;
280 typedef volatile value_type& volatile_reference;
281 typedef const value_type& const_reference;
282 typedef const volatile value_type& const_volatile_reference;
283 typedef value_type* pointer;
284 typedef volatile value_type* volatile_pointer;
285 typedef const value_type* const_pointer;
286 typedef const volatile value_type* const_volatile_pointer;
287 typedef Stokhos::StaticArrayTraits<value_type,execution_space> ss;
288
289 typedef typename execution_space::memory_space memory_space;
290 typedef typename Stokhos::MemoryTraits<memory_space> MemTraits;
291
292 //! Turn StaticFixedStorage into a meta-function class usable with mpl::apply
293 template <typename ord_t, typename val_t = value_t , typename dev_t = execution_space >
294 struct apply {
295 typedef StaticFixedStorage<ord_t,val_t,Num,dev_t> type;
296 };
297
298 template <int N>
299 struct apply_N {
300 typedef StaticFixedStorage<ordinal_type,value_type,N,execution_space> type;
301 };
302
303 //! Constructor
304 KOKKOS_DEFAULTED_FUNCTION
305 StaticFixedStorage() = default;
306
307 //! Constructor
308 KOKKOS_INLINE_FUNCTION
309 StaticFixedStorage(const ordinal_type& sz,
310 const value_type& x = value_type(0.0)) {
311 ss::fill(coeff_, Num, x);
312 }
313
314 //! Constructor from array
315 KOKKOS_INLINE_FUNCTION
316 StaticFixedStorage(const ordinal_type& sz, const value_type* x) {
317 ss::copy(x, coeff_, sz);
318 }
319
320 //! Constructor for creating a view (not allowed)
321 KOKKOS_INLINE_FUNCTION
322 StaticFixedStorage(const ordinal_type& sz, pointer v, bool owned) {}
323
324 //! Copy constructor
325 KOKKOS_DEFAULTED_FUNCTION
326 StaticFixedStorage(const StaticFixedStorage& s) = default;
327
328 //! Copy constructor
329 KOKKOS_INLINE_FUNCTION
330 StaticFixedStorage(const volatile StaticFixedStorage& s) {
331 ss::copy(s.coeff_, coeff_, Num);
332 }
333
334 //! Destructor
335 KOKKOS_DEFAULTED_FUNCTION
336 ~StaticFixedStorage() = default;
337
338 //! Assignment operator
339 KOKKOS_DEFAULTED_FUNCTION
340 StaticFixedStorage& operator=(const StaticFixedStorage& s) = default;
341
342 //! Assignment operator
343 KOKKOS_INLINE_FUNCTION
344 StaticFixedStorage& operator=(const volatile StaticFixedStorage& s) {
345 ss::copy(s.coeff_, coeff_, Num);
346 return *this;
347 }
348
349 //! Assignment operator
350 KOKKOS_INLINE_FUNCTION
351 /*volatile*/ StaticFixedStorage&
352 operator=(const StaticFixedStorage& s) volatile {
353 ss::copy(s.coeff_, coeff_, Num);
354 return const_cast<StaticFixedStorage&>(*this);
355 }
356
357 //! Assignment operator
358 KOKKOS_INLINE_FUNCTION
359 /*volatile*/ StaticFixedStorage&
360 operator=(const volatile StaticFixedStorage& s) volatile {
361 ss::copy(s.coeff_, coeff_, Num);
362 return const_cast<StaticFixedStorage&>(*this);
363 }
364
365 //! Initialize values to a constant value
366 KOKKOS_INLINE_FUNCTION
367 void init(const_reference v) {
368 ss::fill(coeff_, Num, v);
369 }
370
371 //! Initialize values to a constant value
372 KOKKOS_INLINE_FUNCTION
373 void init(const_reference v) volatile {
374 ss::fill(coeff_, Num, v);
375 }
376
377 //! Initialize values to an array of values
378 KOKKOS_INLINE_FUNCTION
379 void init(const_pointer v, const ordinal_type& sz = 0) {
380 if (sz == 0)
381 ss::copy(v, coeff_, Num);
382 else
383 ss::copy(v, coeff_, sz);
384 }
385
386 //! Initialize values to an array of values
387 KOKKOS_INLINE_FUNCTION
388 void init(const_pointer v, const ordinal_type& sz = 0) volatile {
389 if (sz == 0)
390 ss::copy(v, coeff_, Num);
391 else
392 ss::copy(v, coeff_, sz);
393 }
394
395 //! Load values to an array of values
396 KOKKOS_INLINE_FUNCTION
397 void load(pointer v) {
398 ss::copy(v, coeff_, Num);
399 }
400
401 //! Load values to an array of values
402 KOKKOS_INLINE_FUNCTION
403 void load(pointer v) volatile {
404 ss::copy(v, coeff_, Num);
405 }
406
407 //! Resize to new size (values are preserved)
408 KOKKOS_INLINE_FUNCTION
409 void resize(const ordinal_type& sz) {}
410
411 //! Resize to new size (values are preserved)
412 KOKKOS_INLINE_FUNCTION
413 void resize(const ordinal_type& sz) volatile {}
414
415 //! Reset storage to given array, size, and stride
416 KOKKOS_INLINE_FUNCTION
417 void shallowReset(pointer v, const ordinal_type& sz,
418 const ordinal_type& stride, bool owned) {}
419
420 //! Reset storage to given array, size, and stride
421 KOKKOS_INLINE_FUNCTION
422 void shallowReset(pointer v, const ordinal_type& sz,
423 const ordinal_type& stride, bool owned) volatile {}
424
425 //! Return size
426 KOKKOS_INLINE_FUNCTION
427 static ordinal_type size() { return Num; }
428
429 //! Coefficient access (avoid if possible)
430 KOKKOS_INLINE_FUNCTION
431 const_reference operator[] (const ordinal_type& i) const {
432 return coeff_[i];
433 }
434
435 //! Coefficient access (avoid if possible)
436 KOKKOS_INLINE_FUNCTION
437 const_volatile_reference operator[] (const ordinal_type& i) const volatile {
438 return coeff_[i];
439 }
440
441 //! Coefficient access (avoid if possible)
442 KOKKOS_INLINE_FUNCTION
443 reference operator[] (const ordinal_type& i) { return coeff_[i]; }
444
445 //! Coefficient access (avoid if possible)
446 KOKKOS_INLINE_FUNCTION
447 volatile_reference operator[] (const ordinal_type& i) volatile {
448 return coeff_[i]; }
449
450 template <int i>
451 KOKKOS_INLINE_FUNCTION
452 reference getCoeff() { return coeff_[i]; }
453
454 template <int i>
455 KOKKOS_INLINE_FUNCTION
456 volatile_reference getCoeff() volatile { return coeff_[i]; }
457
458 template <int i>
459 KOKKOS_INLINE_FUNCTION
460 const_volatile_reference getCoeff() const volatile { return coeff_[i]; }
461
462 template <int i>
463 KOKKOS_INLINE_FUNCTION
464 const_reference getCoeff() const { return coeff_[i]; }
465
466 //! Get coefficients
467 KOKKOS_INLINE_FUNCTION
468 const_volatile_pointer coeff() const volatile { return coeff_; }
469
470 //! Get coefficients
471 KOKKOS_INLINE_FUNCTION
472 const_pointer coeff() const { return coeff_; }
473
474 //! Get coefficients
475 KOKKOS_INLINE_FUNCTION
476 volatile_pointer coeff() volatile { return coeff_; }
477
478 //! Get coefficients
479 KOKKOS_INLINE_FUNCTION
480 pointer coeff() { return coeff_; }
481
482 private:
483
484 //! Coefficient values
485 value_type coeff_[Num];
486
487 };
488
489 #endif
490
491 }
492
493 #include "Stokhos_StorageHelpers.hpp"
494 STOKHOS_STORAGE_HELPER_STRINGNAME_STATIC(StaticFixedStorage)
495
496 #endif // STOKHOS_STATIC_FIXED_STORAGE_HPP