tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
permuted_view_constexpr.h
Go to the documentation of this file.
1#ifndef FUSED_PERMUTED_VIEW_CONSTEXPR_H
2#define FUSED_PERMUTED_VIEW_CONSTEXPR_H
3
4#include "config.h"
5#include "fused/BaseExpr.h"
7#include "helper_traits.h"
8
34template <typename Tensor, my_size_t... Perm>
35class PermutedViewConstExpr : public BaseExpr<PermutedViewConstExpr<Tensor, Perm...>>
36{
37 // Static assertions to validate the permutation pack at compile time
38 static_assert(sizeof...(Perm) == Tensor::NumDims,
39 "Permutation pack must match tensor's number of dimensions");
40
41 static_assert(all_unique<Perm...>(),
42 "Permutation indices must be unique");
43
44 static_assert(max_value<Perm...>() < Tensor::NumDims,
45 "Max value of permutation pack is greater than the tensor's number of dimensions");
46
47 static_assert(min_value<Perm...>() == 0,
48 "Min value of permutation pack is not equal to 0");
49
50public:
51 using value_type = typename Tensor::value_type;
52 using PadPolicy = typename Tensor::AccessPolicy::PadPolicy;
54
55 static constexpr my_size_t NumDims = Layout::NumDims;
56 static constexpr my_size_t Dim[] = {Tensor::Dim[Perm]...};
57 static constexpr my_size_t TotalSize = Tensor::TotalSize;
58
59 explicit PermutedViewConstExpr(const Tensor &t) noexcept
60 : t_(t) {}
61
62 // Views are non-copyable, non-movable — they're lightweight references.
63 // delete copy constructor and copy assignment to avoid accidental copies
66
67 // delete move constructor and move assignment to avoid accidental moves
70
71 template <typename Output>
72 bool may_alias(const Output &output) const noexcept
73 {
74 return t_.may_alias(output); // recurse to underlying tensor
75 }
76
77 // Const version of the access operator, because this is a view
78 template <typename... Indices>
79 requires(sizeof...(Indices) == NumDims)
80 FORCE_INLINE const value_type &operator()(Indices... indices) const TESSERACT_CONDITIONAL_NOEXCEPT
81 {
82 my_size_t idxArray[] = {static_cast<my_size_t>(indices)...};
83 return t_.data_.data()[Layout::logical_coords_to_physical_flat(idxArray)];
84 }
85
86 // Const version of the access operator with array of indices, because this is a view
88 {
89 return t_.data_.data()[Layout::logical_coords_to_physical_flat(indices)];
90 }
91
93 {
94 return t_.data_.data()[Layout::logical_coords_to_physical_flat(indices)];
95 }
96
97 // ========================================================================
98 // PermutedViewConstExpr::evalu — logical flat, K::gather
99 // ========================================================================
100 // Only used by permuted path. Each logical flat index is remapped
101 // through the permuted layout to a source physical offset.
102 // Consecutive logical flats map to non-contiguous source positions → gather.
103
123 template <typename T, my_size_t Bits, typename Arch>
125 {
127 constexpr my_size_t width = K::simdWidth;
128
129 my_size_t idxList[width];
130 for (my_size_t i = 0; i < width; ++i)
131 idxList[i] = Layout::logical_flat_to_physical_flat(logical_flat + i);
132
133 return K::gather(t_.data_.data(), idxList);
134 }
135
136 template <typename T, my_size_t Bits, typename Arch>
138 {
139 // evalu already expects logical flat for permuted views
140 return evalu<T, Bits, Arch>(logical_flat);
141 }
142
147
152
153 FORCE_INLINE static constexpr my_size_t getNumDims() noexcept { return NumDims; }
154
155 FORCE_INLINE static constexpr my_size_t getTotalSize() noexcept { return TotalSize; }
156
157 // print layout info
158 void printLayoutInfo() const
159 {
160 MyErrorHandler::log("PermutedView Layout Info:", ErrorLevel::Info);
161 MyErrorHandler::log("Number of Dimensions: " + std::to_string(NumDims), ErrorLevel::Info);
164 for (my_size_t i = 0; i < NumDims; ++i)
165 MyErrorHandler::log(std::to_string(getStride(i)) + " ", ErrorLevel::Info);
167 }
168
169 // Inverse permutation — restores the base tensor
170 FORCE_INLINE const Tensor &transpose() const noexcept { return t_; }
171
172 // Utility function to retrieve the shape of the tensor as (1,5,6) for a 3D tensor use the getNumDims
173 std::string getShape() const
174 {
175 std::string shape = "(";
176 for (my_size_t i = 0; i < NumDims; ++i)
177 {
178 shape += std::to_string(getDim(i));
179 if (i < NumDims - 1)
180 shape += ",";
181 }
182 shape += ")";
183 return shape;
184 }
185
186 FORCE_INLINE constexpr const value_type *data() const noexcept { return t_.data_.data(); }
187 FORCE_INLINE constexpr value_type *data() noexcept { return t_.data_.data(); }
188
189private:
190 const Tensor &t_;
191
192 // FORCE_INLINE constexpr const value_type *data() const noexcept { return t_.data_.data(); }
193
194 // template <typename, my_size_t, typename>
195 // friend struct KernelOps;
196};
197
198#endif // FUSED_PERMUTED_VIEW_CONSTEXPR_H
Definition BaseExpr.h:15
static void log(const T &msg, ErrorLevel level=ErrorLevel::Plain)
Definition error_handler.h:18
Compile-time permuted view over a tensor.
Definition permuted_view_constexpr.h:36
bool may_alias(const Output &output) const noexcept
Definition permuted_view_constexpr.h:72
static FORCE_INLINE constexpr my_size_t getTotalSize() noexcept
Definition permuted_view_constexpr.h:155
PermutedViewConstExpr(const PermutedViewConstExpr &)=delete
FORCE_INLINE Microkernel< T, Bits, Arch >::VecType evalu(my_size_t logical_flat) const noexcept
SIMD EVALUATION — logical flat, K::gather.
Definition permuted_view_constexpr.h:124
static constexpr my_size_t Dim[]
Definition permuted_view_constexpr.h:56
static FORCE_INLINE constexpr my_size_t getDim(my_size_t i) TESSERACT_CONDITIONAL_NOEXCEPT
Definition permuted_view_constexpr.h:143
FORCE_INLINE Microkernel< T, Bits, Arch >::VecType logical_evalu(my_size_t logical_flat) const noexcept
Definition permuted_view_constexpr.h:137
static constexpr my_size_t NumDims
Definition permuted_view_constexpr.h:55
FORCE_INLINE const value_type & operator()(const my_size_t *indices) const TESSERACT_CONDITIONAL_NOEXCEPT
Definition permuted_view_constexpr.h:92
std::string getShape() const
Definition permuted_view_constexpr.h:173
void printLayoutInfo() const
Definition permuted_view_constexpr.h:158
typename Tensor::value_type value_type
Definition permuted_view_constexpr.h:51
PermutedViewConstExpr & operator=(PermutedViewConstExpr &&)=delete
FORCE_INLINE constexpr const value_type * data() const noexcept
Definition permuted_view_constexpr.h:186
static FORCE_INLINE constexpr my_size_t getStride(my_size_t i) TESSERACT_CONDITIONAL_NOEXCEPT
Definition permuted_view_constexpr.h:148
typename Tensor::AccessPolicy::PadPolicy PadPolicy
Definition permuted_view_constexpr.h:52
FORCE_INLINE constexpr value_type * data() noexcept
Definition permuted_view_constexpr.h:187
static FORCE_INLINE constexpr my_size_t getNumDims() noexcept
Definition permuted_view_constexpr.h:153
PermutedViewConstExpr(PermutedViewConstExpr &&)=delete
FORCE_INLINE const value_type & operator()(my_size_t(&indices)[NumDims]) const TESSERACT_CONDITIONAL_NOEXCEPT
Definition permuted_view_constexpr.h:87
static constexpr my_size_t TotalSize
Definition permuted_view_constexpr.h:57
PermutedViewConstExpr & operator=(const PermutedViewConstExpr &)=delete
FORCE_INLINE const Tensor & transpose() const noexcept
Definition permuted_view_constexpr.h:170
PermutedViewConstExpr(const Tensor &t) noexcept
Definition permuted_view_constexpr.h:59
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126
#define TESSERACT_CONDITIONAL_NOEXCEPT
Definition config.h:56
#define FORCE_INLINE
Hint the compiler to always inline a function.
Definition config.h:26
consteval my_size_t max_value()
Compile-time maximum of a non-type parameter pack.
Definition helper_traits.h:49
consteval my_size_t min_value()
Compile-time minimum of a non-type parameter pack.
Definition helper_traits.h:68
consteval bool all_unique()
Check if all values in a pack are unique.
Definition helper_traits.h:133
Definition microkernel_base.h:16
T VecType
Definition microkernel_base.h:18
Compile-time strided layout with optional permutation.
Definition strided_layout_constexpr.h:38
static constexpr my_size_t NumDims
Definition strided_layout_constexpr.h:39
static FORCE_INLINE constexpr my_size_t logical_flat_to_physical_flat(my_size_t logical_flat) TESSERACT_CONDITIONAL_NOEXCEPT
Definition strided_layout_constexpr.h:290
static FORCE_INLINE constexpr my_size_t stride(my_size_t i) TESSERACT_CONDITIONAL_NOEXCEPT
Get physical stride at dimension i (with permutation applied).
Definition strided_layout_constexpr.h:263
static FORCE_INLINE constexpr my_size_t logical_coords_to_physical_flat(const my_size_t(&indices)[NumDims]) TESSERACT_CONDITIONAL_NOEXCEPT
Logical coordinates (Array multi-index) to physical flat index (bounds-checked).
Definition strided_layout_constexpr.h:328
static FORCE_INLINE constexpr my_size_t logical_dim(my_size_t i) TESSERACT_CONDITIONAL_NOEXCEPT
Get logical dimension at index i (with permutation applied).
Definition strided_layout_constexpr.h:239