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
66template<typename Store>
67inline
68void
69check_dynamic(Index const dimension)
70{
71 Index const
72 maximum_dimension = static_cast<Index>(std::numeric_limits<Index>::digits);
73
74 assert(Store::IS_DYNAMIC == true);
75
76 if (dimension > maximum_dimension) {
77 MT_ERROR_EXIT("Requested dimension exceeds maximum allowed: %d", dimension);
78 }
79}
80
82template<Index D, Index O>
87 static constexpr Index value = 0;
88};
89
93template<Index D>
94struct dimension_power<D, 1> {
98 static constexpr Index value = D;
99};
100
104template<Index D>
105struct dimension_power<D, 2> {
109 static constexpr Index value = D * D;
110};
111
115template<Index D>
116struct dimension_power<D, 3> {
120 static constexpr Index value = D * D * D;
121};
122
126template<Index D>
127struct dimension_power<D, 4> {
131 static constexpr Index value = D * D * D * D;
132};
133
135template<Index N>
140 static constexpr Index value = 0;
141};
142
146template<>
151 static constexpr Index value = DYNAMIC;
152};
153
157template <> struct dimension_square<1> { static constexpr Index value = 1; };
160
164template <> struct dimension_square<2> { static constexpr Index value = 4; };
167
171template <> struct dimension_square<3> { static constexpr Index value = 9; };
174
178template <> struct dimension_square<4> { static constexpr Index value = 16; };
181
185template <Index N> struct dimension_sqrt { static constexpr Index value = 0; };
188
192template<>
197 static constexpr Index value = DYNAMIC;
198};
199
203template <> struct dimension_sqrt<1> { static constexpr Index value = 1; };
206
210template <> struct dimension_sqrt<4> { static constexpr Index value = 2; };
213
217template <> struct dimension_sqrt<9> { static constexpr Index value = 3; };
220
224template <> struct dimension_sqrt<16> { static constexpr Index value = 4; };
227
229template<Index N, Index P>
234 static constexpr Index value = N + P;
235};
236
240template<Index P>
245 static constexpr Index value = DYNAMIC;
246};
247
251template<Index N, Index P>
256 static constexpr Index value = N - P;
257};
258
262template<Index P>
267 static constexpr Index value = DYNAMIC;
268};
269
273template<Index N, Index P>
278 static constexpr Index value = N * P;
279};
280
284template<Index N>
289 static constexpr Index value = DYNAMIC;
290};
291
295template<Index P>
300 static constexpr Index value = DYNAMIC;
301};
302
306template<>
311 static constexpr Index value = DYNAMIC;
312};
313
320template<typename T, Index N>
322{
323public:
327 using value_type = T;
331 using pointer_type = T *;
335 using reference_type = T &;
339 using const_pointer_type = T const *;
343 using const_reference_type = T const &;
344
348 static constexpr
349 bool
350 IS_STATIC = true;
351
355 static constexpr
356 bool
357 IS_DYNAMIC = false;
358
361 {
362 }
363
367 explicit KOKKOS_INLINE_FUNCTION Storage(Index const number_entries) {
368 resize(number_entries);
369 }
370
371 Storage(Storage<T, N> const & s) = delete;
372
374 operator=(Storage<T, N> const & s) = delete;
375
378 {
379 }
380
385 T const &
386 operator[](Index const i) const
387 {
388 assert(i < size());
389#pragma GCC diagnostic push
390#pragma GCC diagnostic ignored "-Warray-bounds"
391 return storage_[i];
392#pragma GCC diagnostic pop
393 }
394
399 T &
401 {
402 assert(i < size());
403#pragma GCC diagnostic push
404#pragma GCC diagnostic ignored "-Warray-bounds"
405 return storage_[i];
406#pragma GCC diagnostic pop
407 }
408
413 Index
414 size() const
415 {
416 return size_;
417 }
418
423 void
424 resize(Index const number_entries)
425 {
426 assert(number_entries <= N);
427 size_ = number_entries;
428 }
429
434 void
436 {
437 }
438
445 {
446 return &storage_[0];
447 }
448
455 {
456 return &storage_[0];
457 }
458
462 static KOKKOS_INLINE_FUNCTION constexpr Index static_size() { return N; }
463
464private:
465
469 T
471
475 Index
477};
478
485template<typename T>
487{
488public:
492 using value_type = T;
496 using pointer_type = T *;
500 using reference_type = T &;
504 using const_pointer_type = T const *;
508 using const_reference_type = T const &;
509
513 static constexpr
514 bool
516
520 static constexpr
521 bool
522 IS_STATIC = false;
523
526 {
527 }
528
532 explicit KOKKOS_INLINE_FUNCTION Storage(Index const number_entries) {
533 resize(number_entries);
534 }
535
536 Storage(Storage<T, DYNAMIC> const & s) = delete;
537
539 operator=(Storage<T, DYNAMIC> const & s) = delete;
540
543 {
544 clear();
545 }
546
551 T const &
552 operator[](Index const i) const
553 {
554 assert(i < size());
555 return storage_[i];
556 }
557
562 T &
564 {
565 assert(i < size());
566 return storage_[i];
567 }
568
573 Index
574 size() const
575 {
576 return size_;
577 }
578
583 void
584 resize(Index const number_entries)
585 {
586 if (number_entries != size_) {
587 clear();
588 storage_ = new T[number_entries];
589 size_ = number_entries;
590 }
591 }
592
597 void
599 {
600 if (storage_ != nullptr) {
601 delete[] storage_;
602 storage_ = nullptr;
603 size_ = 0;
604 }
605 }
606
613 {
614 return storage_;
615 }
616
623 {
624 return storage_;
625 }
626
630 static KOKKOS_INLINE_FUNCTION constexpr Index static_size() { return 0; }
631
632private:
633
637 T *
638 storage_{nullptr};
639
643 Index
645};
646
647} // namespace minitensor
648
649namespace minitensor {
650
651// Place holder for now.
652
654} // namespace minitensor
655
656#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
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.