NForge
Tensor library
Loading...
Searching...
No Matches
tensor_layout.h
1#ifndef TENSOR_LAYOUT_H
2#define TENSOR_LAYOUT_H
3
4#include <array>
5#include <cstdlib>
6
7#include "nforge/core/tensor_shape.h"
8
9#define MAX_DIMS 8
10
17 std::array<size_t, MAX_DIMS> shape;
18
20 std::array<size_t, MAX_DIMS> strides;
21
23 size_t offset = 0;
24
26 size_t rank = 0;
27
30
33 TensorLayout(const Tensor::Shape& _shape);
34
38 TensorLayout(const Tensor::Shape& _shape, const std::vector<size_t>& _strides);
39
43 TensorLayout(const Tensor::Shape& _shape, const std::vector<size_t>& _strides, size_t _offset);
44
47 TensorLayout(std::array<size_t, MAX_DIMS> _shape, std::array<size_t, MAX_DIMS> _strides,
48 size_t _offset, size_t _rank);
49
51 bool operator==(const TensorLayout& rhs) const;
52
54 bool operator!=(const TensorLayout& rhs) const;
55};
56
58static inline size_t physicalOffset(size_t linear, const TensorLayout& L) {
59 size_t off = L.offset;
60 for (int d = L.rank - 1; d >= 0; d--) {
61 std::lldiv_t results = std::lldiv((long long)linear, (long long)L.shape[d]);
62
63 linear = results.quot;
64 off += results.rem * L.strides[d];
65 }
66 return off;
67}
68
69#endif // TENSOR_LAYOUT_H
Definition tensor_shape.h:15
Definition tensor_layout.h:15
size_t rank
Number of active dimensions.
Definition tensor_layout.h:26
bool operator==(const TensorLayout &rhs) const
True if layouts have equal rank, offset, and active shape/stride entries.
Definition tensor_layout.cpp:42
bool operator!=(const TensorLayout &rhs) const
Negation of operator==.
Definition tensor_layout.cpp:58
std::array< size_t, MAX_DIMS > shape
Shape extent per dimension. Only first rank entries active.
Definition tensor_layout.h:17
size_t offset
Storage offset before indexing begins.
Definition tensor_layout.h:23
TensorLayout()
Default constructor. Rank and offset are zero.
Definition tensor_layout.h:29
std::array< size_t, MAX_DIMS > strides
Stride (element count) per dimension. Only first rank entries active.
Definition tensor_layout.h:20