Xped
Loading...
Searching...
No Matches
StorageType_contiguous.hpp
Go to the documentation of this file.
1#ifndef XPED_STORAGE_TYPE_CONTIGOUS_HPP_
2#define XPED_STORAGE_TYPE_CONTIGOUS_HPP_
3
4#include "yas/serialize.hpp"
5#include "yas/std_types.hpp"
6
9
10namespace Xped {
11
12template <typename Scalar, std::size_t Rank, std::size_t CoRank, typename Symmetry, typename AllocationPolicy>
14{
18 using qType = typename Symmetry::qType;
19
22
23private:
24 using DictType = std::unordered_map<qType,
25 std::size_t,
26 std::hash<qType>,
27 std::equal_to<qType>,
28 typename AllocationPolicy::template Allocator<std::pair<const qType, std::size_t>>>;
29
30public:
31 StorageType() = default;
32
33 StorageType(const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> basis_domain,
34 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> basis_codomain,
36 : m_uncoupled_domain(basis_domain)
37 , m_uncoupled_codomain(basis_codomain)
38 , m_world(world)
39 {
40 m_domain = internal::build_FusionTree(m_uncoupled_domain);
41 m_codomain = internal::build_FusionTree(m_uncoupled_codomain);
42 // uninitialized_resize();
43 }
44
45 StorageType(const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> basis_domain,
46 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> basis_codomain,
47 const Scalar* data,
48 std::size_t size,
50 : m_uncoupled_domain(basis_domain)
51 , m_uncoupled_codomain(basis_codomain)
52 , m_world(world)
53 {
54 m_domain = internal::build_FusionTree(m_uncoupled_domain);
55 m_codomain = internal::build_FusionTree(m_uncoupled_codomain);
56 initialized_resize(data, size);
57 }
58
60 // {
61 // sector_ = other.derived().sector();
62 // // block_.resize(sector_.size());
63 // storage_ = other.storage();
64 // // for(std::size_t i = 0; i < sector_.size(); i++) { block_[i] = other.derived().block(i); }
65 // dict_ = other.derived().dict();
66 // world_ = other.derived().world();
67 // uncoupled_domain = other.derived().uncoupledDomain();
68 // uncoupled_codomain = other.derived().uncoupledCodomain();
69 // domain = other.derived().coupledDomain();
70 // codomain = other.derived().coupledCodomain();
71 // }
72
73 // template <template <typename> typename OtherAllocator>
74 // StorageType(const StorageType<Scalar, Rank, CoRank, Symmetry, OtherAllocator>& other);
75
76 static constexpr bool IS_CONTIGUOUS() { return true; }
77
78 void resize()
79 {
80 clear();
81 std::size_t curr = 0;
82 for(const auto& [q, dim, plain] : m_domain) {
83 if(m_codomain.IS_PRESENT(q)) {
84 m_sector.push_back(q);
85 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
86 m_offsets.push_back(curr);
87 curr += m_domain.inner_dim(q) * m_codomain.inner_dim(q);
88 }
89 }
90 m_data.resize(curr);
91 }
92
93 void set_data(const Scalar* data, std::size_t size)
94 {
95 assert(m_sector.size() > 0 and "Block data needs to already set. Use initialized_resize() instead.");
96 assert(m_offsets.size() == m_sector.size() and "Corrupted storage.");
97 assert(m_dict.size() == m_sector.size() and "Corrupted storage.");
98 m_data.assign(data, data + size);
99 }
100
101 cMapMatrixType block(std::size_t i) const
102 {
103 assert(i < m_offsets.size());
104 return cMapMatrixType(m_data.data() + m_offsets[i], m_domain.inner_dim(m_sector[i]), m_codomain.inner_dim(m_sector[i]));
105 }
106
107 MapMatrixType block(std::size_t i)
108 {
109 assert(i < m_offsets.size());
110 return MapMatrixType(m_data.data() + m_offsets[i], m_domain.inner_dim(m_sector[i]), m_codomain.inner_dim(m_sector[i]));
111 }
112
114 {
115 auto it = m_dict.find(q);
116 assert(it != m_dict.end());
117 return block(it->second);
118 }
119
121 {
122 auto it = m_dict.find(q);
123 assert(it != m_dict.end());
124 return block(it->second);
125 }
126
127 const DictType& dict() const { return m_dict; }
128 DictType& dict() { return m_dict; }
129
130 const std::vector<qType, typename AllocationPolicy::template Allocator<qType>>& sector() const { return m_sector; }
131 qType sector(std::size_t i) const { return m_sector[i]; }
132
133 const Scalar* data() const { return m_data.data(); }
134 Scalar* data() { return m_data.data(); }
135
136 auto begin() { return m_data.begin(); }
137 auto end() { return m_data.end(); }
138
139 const auto cbegin() const { return m_data.cbegin(); }
140 const auto cend() const { return m_data.cend(); }
141
142 std::size_t plainSize() const { return m_data.size(); }
143
144 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank>& uncoupledDomain() const { return m_uncoupled_domain; }
145 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank>& uncoupledCodomain() const { return m_uncoupled_codomain; }
146
147 const Qbasis<Symmetry, Rank, AllocationPolicy>& coupledDomain() const { return m_domain; }
149
150 void push_back(const qType& q, const MatrixType& M)
151 {
152 if(m_offsets.size() == 0) {
153 m_offsets.push_back(0);
154 } else {
155 m_offsets.push_back(m_offsets.back() + m_domain.inner_dim(m_sector.back()) * m_codomain.inner_dim(m_sector.back()));
156 }
157 m_sector.push_back(q);
158 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
159 m_data.insert(m_data.end(), M.data(), M.data() + M.size());
160 }
161
162 void reserve(std::size_t size) { m_data.reserve(size); }
163
164 void clear()
165 {
166 m_data.clear();
167 m_sector.clear();
168 m_dict.clear();
169 m_offsets.clear();
170 }
171
172 template <typename Ar>
173 void serialize(Ar& ar)
174 {
175 ar& YAS_OBJECT_NVP("x_storage",
176 ("data", m_data),
177 ("dict", m_dict),
178 ("sectors", m_sector),
179 ("uncoupled_domain", m_uncoupled_domain),
180 ("uncoupled_codomain", m_uncoupled_codomain),
181 ("domain", m_domain),
182 ("codomain", m_codomain),
183 ("world", m_world),
184 ("offsets", m_offsets));
185 }
186
187 const mpi::XpedWorld& world() const { return m_world; }
188
189private:
190 std::vector<Scalar, typename AllocationPolicy::template Allocator<Scalar>> m_data;
191
192 DictType m_dict; // sector --> number
193 std::vector<qType, typename AllocationPolicy::template Allocator<qType>> m_sector;
194
195 std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> m_uncoupled_domain;
196 std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> m_uncoupled_codomain;
199
200 mpi::XpedWorld m_world;
201
202 std::vector<std::size_t, typename AllocationPolicy::template Allocator<std::size_t>> m_offsets;
203
204 void initialized_resize(const Scalar* data, std::size_t size)
205 {
206 m_data.assign(data, data + size);
207
208 std::size_t curr = 0;
209 for(const auto& [q, dim, plain] : m_domain) {
210 if(m_codomain.IS_PRESENT(q)) {
211 m_sector.push_back(q);
212 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
213 m_offsets.push_back(curr);
214 curr += m_domain.inner_dim(q) * m_codomain.inner_dim(q);
215 }
216 }
217 assert(curr == size and "You specified an incompatible data array for this tensor.");
218 }
219};
220
221} // namespace Xped
222
223#endif
Definition: Qbasis.hpp:39
size_t inner_dim(const size_t &num_in) const
Definition: Qbasis.cpp:195
bool IS_PRESENT(const qType &q) const
Definition: Qbasis.hpp:133
Qbasis< Symmetry, Rank, AllocationPolicy > build_FusionTree(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > &uncoupled)
Definition: TensorHelper.cpp:29
XpedWorld & getUniverse()
Definition: Mpi.hpp:49
Definition: bench.cpp:62
CTF::Matrix< Scalar > MapMType
Definition: MatrixInterface_Cyclops_impl.hpp:45
const CTF::Matrix< Scalar > cMapMType
Definition: MatrixInterface_Cyclops_impl.hpp:47
CTF::Matrix< Scalar > MType
Definition: MatrixInterface_Cyclops_impl.hpp:40
Definition: StorageType_contiguous.hpp:14
const mpi::XpedWorld & world() const
Definition: StorageType_contiguous.hpp:187
const Qbasis< Symmetry, CoRank, AllocationPolicy > & coupledCodomain() const
Definition: StorageType_contiguous.hpp:148
const auto cbegin() const
Definition: StorageType_contiguous.hpp:139
const Qbasis< Symmetry, Rank, AllocationPolicy > & coupledDomain() const
Definition: StorageType_contiguous.hpp:147
typename Symmetry::qType qType
Definition: StorageType_contiguous.hpp:18
MapMatrixType block(qType q)
Definition: StorageType_contiguous.hpp:120
void push_back(const qType &q, const MatrixType &M)
Definition: StorageType_contiguous.hpp:150
void serialize(Ar &ar)
Definition: StorageType_contiguous.hpp:173
StorageType(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > basis_domain, const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > basis_codomain, const mpi::XpedWorld &world=mpi::getUniverse())
Definition: StorageType_contiguous.hpp:33
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > & uncoupledCodomain() const
Definition: StorageType_contiguous.hpp:145
static constexpr bool IS_CONTIGUOUS()
Definition: StorageType_contiguous.hpp:76
std::size_t plainSize() const
Definition: StorageType_contiguous.hpp:142
void set_data(const Scalar *data, std::size_t size)
Definition: StorageType_contiguous.hpp:93
auto begin()
Definition: StorageType_contiguous.hpp:136
PlainInterface::MType< Scalar > MatrixType
Definition: StorageType_contiguous.hpp:15
cMapMatrixType block(std::size_t i) const
Definition: StorageType_contiguous.hpp:101
auto end()
Definition: StorageType_contiguous.hpp:137
DictType & dict()
Definition: StorageType_contiguous.hpp:128
void reserve(std::size_t size)
Definition: StorageType_contiguous.hpp:162
PlainInterface::MapMType< Scalar > MapMatrixType
Definition: StorageType_contiguous.hpp:16
cMapMatrixType block(qType q) const
Definition: StorageType_contiguous.hpp:113
const std::vector< qType, typename AllocationPolicy::template Allocator< qType > > & sector() const
Definition: StorageType_contiguous.hpp:130
qType sector(std::size_t i) const
Definition: StorageType_contiguous.hpp:131
Scalar * data()
Definition: StorageType_contiguous.hpp:134
PlainInterface::cMapMType< Scalar > cMapMatrixType
Definition: StorageType_contiguous.hpp:17
StorageType()=default
void clear()
Definition: StorageType_contiguous.hpp:164
const Scalar * data() const
Definition: StorageType_contiguous.hpp:133
MapMatrixType block(std::size_t i)
Definition: StorageType_contiguous.hpp:107
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > & uncoupledDomain() const
Definition: StorageType_contiguous.hpp:144
MapMatrixType MatrixReturnType
Definition: StorageType_contiguous.hpp:20
StorageType(const StorageType< Scalar, Rank, CoRank, Symmetry, AllocationPolicy > &other)=default
void resize()
Definition: StorageType_contiguous.hpp:78
StorageType(const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > basis_domain, const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > basis_codomain, const Scalar *data, std::size_t size, const mpi::XpedWorld &world=mpi::getUniverse())
Definition: StorageType_contiguous.hpp:45
cMapMatrixType ConstMatrixReturnType
Definition: StorageType_contiguous.hpp:21
const auto cend() const
Definition: StorageType_contiguous.hpp:140
const DictType & dict() const
Definition: StorageType_contiguous.hpp:127
Definition: Mpi.hpp:34