KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
Shape.h
Go to the documentation of this file.
1
34#ifndef KIDS_SHAPE_H
35#define KIDS_SHAPE_H
36
37#include <cassert>
38#include <iostream>
39#include <sstream>
40#include <vector>
41
42namespace PROJECT_NS {
43
44using Basic_Dimen = std::size_t;
45using Basic_Shape = std::vector<std::size_t>;
46
50class Shape {
51 public:
53 Shape(std::size_t size) : _rank{1}, _dims{{size}}, _ldims{1}, _size{size} { enable_dynamic = false; }
54
56 Shape(std::vector<std::size_t> dims) : _rank{dims.size()}, _dims{dims}, _ldims(dims.size(), 0), _size{1} {
57 enable_dynamic = false;
58 update();
59 }
60
62 Shape(std::vector<std::size_t*> dims_ptr)
63 : _rank{dims_ptr.size()},
64 _dims_ptr{dims_ptr},
65 _dims(_dims_ptr.size(), 0),
66 _ldims(_dims_ptr.size(), 0),
67 _size{1} {
68 enable_dynamic = true;
69 }
70
72 inline int rank() {
73 if (enable_dynamic) update();
74 return _rank;
75 }
76
78 inline int size() {
79 if (enable_dynamic) update();
80 return _size;
81 }
82
83 inline int operator()() { return _size; }
84
85 inline std::vector<std::size_t>& dims() {
86 if (enable_dynamic) update();
87 return _dims;
88 }
89
90 inline void static_build() {
91 update();
92 enable_dynamic = false;
93 }
94
95 inline std::string to_string() {
96 std::stringstream ss;
97 ss << "<";
98 for (auto& i : _dims) ss << i << ",";
99 ss << ">";
100 return ss.str();
101 }
102
103 private:
104 void update() {
105 if (enable_dynamic)
106 for (int i = 0; i < _rank; ++i) {
107 _dims[i] = *(_dims_ptr[i]);
108 assert(_dims[i] > 0);
109 }
110 _ldims[_rank - 1] = 1;
111 _size = _dims[_rank - 1];
112 for (int i = _rank - 2; i >= 0; --i) {
113 _ldims[i] = _ldims[i + 1] * (_dims[i + 1]);
114 _size *= _dims[i];
115 }
116 }
117
118 bool enable_dynamic = false;
119
120 std::size_t _rank;
121 std::vector<std::size_t*> _dims_ptr;
122 std::vector<std::size_t> _dims;
123 std::vector<std::size_t> _ldims;
124 std::size_t _size;
125};
126
127}; // namespace PROJECT_NS
128
129#endif // KIDS_SHAPE_H
Shape class provide information about of a Tensor's shape.
Definition Shape.h:50
std::vector< std::size_t > & dims()
Definition Shape.h:85
std::string to_string()
Definition Shape.h:95
std::vector< std::size_t * > _dims_ptr
pointers to dimensions for each rank
Definition Shape.h:121
int rank()
get data size described by a Shape
Definition Shape.h:72
int operator()()
Definition Shape.h:83
Shape(std::vector< std::size_t * > dims_ptr)
get rank of a Shape
Definition Shape.h:62
std::size_t _rank
rank of the shape
Definition Shape.h:120
std::vector< std::size_t > _dims
dimensions for each rank
Definition Shape.h:122
Shape(std::vector< std::size_t > dims)
construct from a vector of std::size_t pointer (dynamic shape)
Definition Shape.h:56
std::vector< std::size_t > _ldims
leading dimensions
Definition Shape.h:123
void update()
Definition Shape.h:104
std::size_t _size
size of data
Definition Shape.h:124
Shape(std::size_t size)
< construct from a number (rank-1 Shape)
Definition Shape.h:53
void static_build()
Definition Shape.h:90
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
std::size_t Basic_Dimen
Definition Shape.h:44
std::vector< std::size_t > Basic_Shape
Definition Shape.h:45