MiniTensor Version of the Day
Loading...
Searching...
No Matches
MiniTensor_Storage.h
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MiniTensor Package
4//
5// Copyright 2016 NTESS and the MiniTensor contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Kokkos_Macros.hpp"
11#if !defined(MiniTensor_Storage_h)
12#define MiniTensor_Storage_h
13
14#include "MiniTensor_Traits.h"
15
16namespace minitensor {
17
20
22template<Index N, Index C>
27 static constexpr Index value = C;
28};
29
33template<Index C>
38 static constexpr Index value = DYNAMIC;
39};
40
42template<Index D>
44
45#if defined(KOKKOS_ENABLE_CUDA)
46 // Empty
47#else
51 static constexpr Index maximum_dimension =
52 static_cast<Index>(std::numeric_limits<Index>::digits);
53
54 static_assert(D > maximum_dimension, "Dimension is too large");
55#endif
56
60 static constexpr Index value = D;
61};
62
74template<Index N>
76constexpr Index
77bound_dimension(Index const dimension)
78{
79 if constexpr (N == DYNAMIC) {
80 return dimension;
81 } else {
82 return dimension <= N ? dimension : N;
83 }
84}
85
89template<typename Store>
90inline
91void
92check_dynamic(Index const dimension)
93{
94 Index const
95 maximum_dimension = static_cast<Index>(std::numeric_limits<Index>::digits);
96
97 assert(Store::IS_DYNAMIC == true);
98
99 if (dimension > maximum_dimension) {
100 MT_ERROR_EXIT("Requested dimension exceeds maximum allowed: %d", dimension);
101 }
102}
103
105template<Index D, Index O>
110 static constexpr Index value = 0;
111};
112
116template<Index D>
117struct dimension_power<D, 1> {
121 static constexpr Index value = D;
122};
123
127template<Index D>
128struct dimension_power<D, 2> {
132 static constexpr Index value = D * D;
133};
134
138template<Index D>
139struct dimension_power<D, 3> {
143 static constexpr Index value = D * D * D;
144};
145
149template<Index D>
150struct dimension_power<D, 4> {
154 static constexpr Index value = D * D * D * D;
155};
156
158template<Index N>
163 static constexpr Index value = 0;
164};
165
169template<>
174 static constexpr Index value = DYNAMIC;
175};
176
180template <> struct dimension_square<1> { static constexpr Index value = 1; };
183
187template <> struct dimension_square<2> { static constexpr Index value = 4; };
190
194template <> struct dimension_square<3> { static constexpr Index value = 9; };
197
201template <> struct dimension_square<4> { static constexpr Index value = 16; };
204
208template <Index N> struct dimension_sqrt { static constexpr Index value = 0; };
211
215template<>
220 static constexpr Index value = DYNAMIC;
221};
222
226template <> struct dimension_sqrt<1> { static constexpr Index value = 1; };
229
233template <> struct dimension_sqrt<4> { static constexpr Index value = 2; };
236
240template <> struct dimension_sqrt<9> { static constexpr Index value = 3; };
243
247template <> struct dimension_sqrt<16> { static constexpr Index value = 4; };
250
252template<Index N, Index P>
257 static constexpr Index value = N + P;
258};
259
263template<Index P>
268 static constexpr Index value = DYNAMIC;
269};
270
274template<Index N, Index P>
279 static constexpr Index value = N - P;
280};
281
285template<Index P>
290 static constexpr Index value = DYNAMIC;
291};
292
296template<Index N, Index P>
301 static constexpr Index value = N * P;
302};
303
307template<Index N>
312 static constexpr Index value = DYNAMIC;
313};
314
318template<Index P>
323 static constexpr Index value = DYNAMIC;
324};
325
329template<>
334 static constexpr Index value = DYNAMIC;
335};
336
343template<typename T, Index N>
345{
346public:
350 using value_type = T;
354 using pointer_type = T *;
358 using reference_type = T &;
362 using const_pointer_type = T const *;
366 using const_reference_type = T const &;
367
371 static constexpr
372 bool
373 IS_STATIC = true;
374
378 static constexpr
379 bool
380 IS_DYNAMIC = false;
381
384 {
385 }
386
390 explicit KOKKOS_INLINE_FUNCTION Storage(Index const number_entries) {
391 resize(number_entries);
392 }
393
394 Storage(Storage<T, N> const & s) = delete;
395
397 operator=(Storage<T, N> const & s) = delete;
398
401 {
402 }
403
408 T const &
409 operator[](Index const i) const
410 {
411 assert(i < size());
412#pragma GCC diagnostic push
413#pragma GCC diagnostic ignored "-Warray-bounds"
414 return storage_[i];
415#pragma GCC diagnostic pop
416 }
417
422 T &
424 {
425 assert(i < size());
426#pragma GCC diagnostic push
427#pragma GCC diagnostic ignored "-Warray-bounds"
428 return storage_[i];
429#pragma GCC diagnostic pop
430 }
431
436 Index
437 size() const
438 {
439 // resize() asserts size_ <= N, but the assert is gone from optimized
440 // builds and the optimizer cannot see the invariant. Stating it here lets
441 // GCC prove that every loop bounded by size() indexes within storage_,
442 // instead of reporting -Warray-bounds and -Wstringop-overflow at the
443 // writes those loops make through operator[].
444 return size_ <= N ? size_ : N;
445 }
446
451 void
452 resize(Index const number_entries)
453 {
454 assert(number_entries <= N);
455 size_ = number_entries;
456 }
457
462 void
464 {
465 }
466
473 {
474 return &storage_[0];
475 }
476
483 {
484 return &storage_[0];
485 }
486
490 static KOKKOS_INLINE_FUNCTION constexpr Index static_size() { return N; }
491
492private:
493
497 T
499
503 Index
505};
506
513template<typename T>
515{
516public:
520 using value_type = T;
524 using pointer_type = T *;
528 using reference_type = T &;
532 using const_pointer_type = T const *;
536 using const_reference_type = T const &;
537
541 static constexpr
542 bool
544
548 static constexpr
549 bool
550 IS_STATIC = false;
551
554 {
555 }
556
560 explicit KOKKOS_INLINE_FUNCTION Storage(Index const number_entries) {
561 resize(number_entries);
562 }
563
564 Storage(Storage<T, DYNAMIC> const & s) = delete;
565
567 operator=(Storage<T, DYNAMIC> const & s) = delete;
568
571 {
572 clear();
573 }
574
579 T const &
580 operator[](Index const i) const
581 {
582 assert(i < size());
583 return storage_[i];
584 }
585
590 T &
592 {
593 assert(i < size());
594 return storage_[i];
595 }
596
601 Index
602 size() const
603 {
604 return size_;
605 }
606
611 void
612 resize(Index const number_entries)
613 {
614 if (number_entries != size_) {
615 clear();
616 storage_ = new T[number_entries];
617 size_ = number_entries;
618 }
619 }
620
625 void
627 {
628 if (storage_ != nullptr) {
629 delete[] storage_;
630 storage_ = nullptr;
631 size_ = 0;
632 }
633 }
634
641 {
642 return storage_;
643 }
644
651 {
652 return storage_;
653 }
654
658 static KOKKOS_INLINE_FUNCTION constexpr Index static_size() { return 0; }
659
660private:
661
665 T *
666 storage_{nullptr};
667
671 Index
673};
674
675} // namespace minitensor
676
677namespace minitensor {
678
679// Place holder for now.
680
682} // namespace minitensor
683
684#endif // MiniTensor_Storage_h
#define KOKKOS_INLINE_FUNCTION
#define MT_ERROR_EXIT(...)
static constexpr Index value
KOKKOS_INLINE_FUNCTION T const & operator[](Index const i) const
KOKKOS_INLINE_FUNCTION pointer_type get_pointer()
KOKKOS_INLINE_FUNCTION T & operator[](Index const i)
Storage< T, DYNAMIC > & operator=(Storage< T, DYNAMIC > const &s)=delete
static constexpr Index value
KOKKOS_INLINE_FUNCTION const_pointer_type get_const_pointer() const
KOKKOS_INLINE_FUNCTION constexpr Index bound_dimension(Index const dimension)
static constexpr bool IS_STATIC
KOKKOS_INLINE_FUNCTION void resize(Index const number_entries)
static constexpr Index value
static constexpr Index maximum_dimension
static constexpr Index value
static constexpr Index value
KOKKOS_INLINE_FUNCTION ~Storage()
KOKKOS_INLINE_FUNCTION pointer_type get_pointer()
KOKKOS_INLINE_FUNCTION Index size() const
KOKKOS_INLINE_FUNCTION void clear()
static constexpr bool IS_DYNAMIC
static KOKKOS_INLINE_FUNCTION constexpr Index static_size()
static KOKKOS_INLINE_FUNCTION constexpr Index static_size()
void check_dynamic(Index const dimension)
KOKKOS_INLINE_FUNCTION T const & operator[](Index const i) const
Storage< T, N > & operator=(Storage< T, N > const &s)=delete
Storage(Storage< T, N > const &s)=delete
KOKKOS_INLINE_FUNCTION T & operator[](Index const i)
KOKKOS_INLINE_FUNCTION Index size() const
KOKKOS_INLINE_FUNCTION Storage(Index const number_entries)
Storage(Storage< T, DYNAMIC > const &s)=delete
KOKKOS_INLINE_FUNCTION void clear()
KOKKOS_INLINE_FUNCTION void resize(Index const number_entries)
KOKKOS_INLINE_FUNCTION const_pointer_type get_const_pointer() const
KOKKOS_INLINE_FUNCTION Storage(Index const number_entries)
KOKKOS_INLINE_FUNCTION Storage()
uint32_t Index
Indexing type.
constexpr Index DYNAMIC
Indicator for dynamic storage.
Manipulation of static and dynamic dimensions.
Set to constant value if not dynamic.
Integer power template restricted to orders defined below.
Integer square for manipulations between 2nd and 4rd-order tensors.