Xped
Loading...
Searching...
No Matches
StorageType_vecofmat.hpp
Go to the documentation of this file.
1#ifndef XPED_STORAGE_TYPE_VEC_OF_MAT_HPP_
2#define XPED_STORAGE_TYPE_VEC_OF_MAT_HPP_
3
7
8namespace Xped {
9
10template <typename Scalar, std::size_t Rank, std::size_t CoRank, typename Symmetry, typename AllocationPolicy>
11struct StorageType
12{
16 using qType = typename Symmetry::qType;
17
20
21private:
22 using DictType = std::unordered_map<qType,
23 std::size_t,
24 std::hash<qType>,
25 std::equal_to<qType>,
26 typename AllocationPolicy::template Allocator<std::pair<const qType, std::size_t>>>;
27
28public:
29 StorageType() = default;
30
31 StorageType(const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> basis_domain,
32 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> basis_codomain,
34 : m_uncoupled_domain(basis_domain)
35 , m_uncoupled_codomain(basis_codomain)
36 , m_world(world)
37 {
38 m_domain = internal::build_FusionTree(m_uncoupled_domain);
39 m_codomain = internal::build_FusionTree(m_uncoupled_codomain);
40 // uninitialized_resize();
41 }
42
43 StorageType(const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> basis_domain,
44 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> basis_codomain,
45 const Scalar* data,
46 std::size_t size,
48 : m_uncoupled_domain(basis_domain)
49 , m_uncoupled_codomain(basis_codomain)
50 , m_world(world)
51 {
52 m_domain = internal::build_FusionTree(m_uncoupled_domain);
53 m_codomain = internal::build_FusionTree(m_uncoupled_codomain);
54 initialized_resize(data, size);
55 }
56
58 // {
59 // sector_ = other.derived().sector();
60 // // block_.resize(sector_.size());
61 // storage_ = other.storage();
62 // // for(std::size_t i = 0; i < sector_.size(); i++) { block_[i] = other.derived().block(i); }
63 // dict_ = other.derived().dict();
64 // world_ = other.derived().world();
65 // uncoupled_domain = other.derived().uncoupledDomain();
66 // uncoupled_codomain = other.derived().uncoupledCodomain();
67 // domain = other.derived().coupledDomain();
68 // codomain = other.derived().coupledCodomain();
69 // }
70
71 // template <template <typename> typename OtherAllocator>
72 // StorageType(const StorageType<Scalar, Rank, CoRank, Symmetry, OtherAllocator>& other);
73
74 static constexpr bool IS_CONTIGUOUS() { return false; }
75
76 std::size_t plainSize() const
77 {
78 return std::accumulate(m_data.cbegin(), m_data.cend(), 0, [](std::size_t cur, const MatrixType& mat) { return cur + mat.size(); });
79 }
80
81 void resize()
82 {
83 m_data.reserve(std::max(m_domain.Nq(), m_codomain.Nq()));
84
85 for(const auto& [q, dim, plain] : m_domain) {
86 if(m_codomain.IS_PRESENT(q)) {
87 m_sector.push_back(q);
88 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
89 MatrixType Mtmp = PlainInterface::construct<Scalar>(m_domain.inner_dim(q), m_codomain.inner_dim(q), m_world);
90 m_data.push_back(Mtmp);
91 // m_data.emplace_back(m_domain.inner_dim(q), m_codomain.inner_dim(q));
92 }
93 }
94
95 m_data.shrink_to_fit();
96 }
97
98 const MatrixType& block(std::size_t i) const
99 {
100 assert(i < m_data.size());
101 return m_data[i];
102 }
103
104 MatrixType& block(std::size_t i)
105 {
106 assert(i < m_data.size());
107 return m_data[i];
108 }
109
110 const MatrixType& block(qType q) const
111 {
112 auto it = m_dict.find(q);
113 assert(it != m_dict.end());
114 return m_data[it->second];
115 }
116
118 {
119 auto it = m_dict.find(q);
120 assert(it != m_dict.end());
121 return m_data[it->second];
122 }
123
124 const DictType& dict() const { return m_dict; }
125 DictType& dict() { return m_dict; }
126
127 const std::vector<qType, typename AllocationPolicy::template Allocator<qType>>& sector() const { return m_sector; }
128 qType sector(std::size_t i) const { return m_sector[i]; }
129
130 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank>& uncoupledDomain() const { return m_uncoupled_domain; }
131 const std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank>& uncoupledCodomain() const { return m_uncoupled_codomain; }
132
133 const Qbasis<Symmetry, Rank, AllocationPolicy>& coupledDomain() const { return m_domain; }
135
136 void push_back(const qType& q, const MatrixType& M)
137 {
138 m_sector.push_back(q);
139 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
140 m_data.push_back(M);
141 }
142
143 void reserve(std::size_t size) { m_data.reserve(size); }
144
145 void clear()
146 {
147 m_data.clear();
148 m_sector.clear();
149 }
150
152 {
153 VecOfMatIterator<Scalar> out(&m_data);
154 return out;
155 }
157 {
158 VecOfMatIterator<Scalar> out(&m_data, m_data.size());
159 return out;
160 }
161
162 const mpi::XpedWorld& world() const { return m_world; }
163
164private:
165 std::vector<MatrixType, typename AllocationPolicy::template Allocator<MatrixType>> m_data;
166
167 DictType m_dict; // sector --> number
168 std::vector<qType, typename AllocationPolicy::template Allocator<qType>> m_sector;
169
170 std::array<Qbasis<Symmetry, 1, AllocationPolicy>, Rank> m_uncoupled_domain;
171 std::array<Qbasis<Symmetry, 1, AllocationPolicy>, CoRank> m_uncoupled_codomain;
174
175 mpi::XpedWorld m_world;
176
177 void initialized_resize(const Scalar* data, std::size_t size)
178 {
179 m_data.reserve(std::min(m_domain.Nq(), m_codomain.Nq()));
180
181 std::size_t current_dim = 0;
182 for(const auto& [q, dim, plain] : m_domain) {
183 if(m_codomain.IS_PRESENT(q)) {
184 m_sector.push_back(q);
185 m_dict.insert(std::make_pair(q, m_sector.size() - 1));
186 cMapMatrixType tmp(data + current_dim, m_domain.inner_dim(q), m_codomain.inner_dim(q));
187 m_data.emplace_back(tmp);
188 current_dim += m_domain.inner_dim(q) * m_codomain.inner_dim(q);
189 }
190 }
191 assert(current_dim == size and "You specified an incompatible data array for this tensor.");
192
193 m_data.shrink_to_fit();
194 }
195};
196
197} // namespace Xped
198
199#endif
Definition: Qbasis.hpp:39
std::size_t Nq() const
Definition: Qbasis.hpp:97
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
Definition: VecOfMatIterator.hpp:14
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_vecofmat.hpp:134
const Qbasis< Symmetry, Rank, AllocationPolicy > & coupledDomain() const
Definition: StorageType_vecofmat.hpp:133
typename Symmetry::qType qType
Definition: StorageType_contiguous.hpp:18
VecOfMatIterator< Scalar > begin()
Definition: StorageType_vecofmat.hpp:151
MatrixType & block(std::size_t i)
Definition: StorageType_vecofmat.hpp:104
void push_back(const qType &q, const MatrixType &M)
Definition: StorageType_vecofmat.hpp:136
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_vecofmat.hpp:31
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, CoRank > & uncoupledCodomain() const
Definition: StorageType_vecofmat.hpp:131
static constexpr bool IS_CONTIGUOUS()
Definition: StorageType_vecofmat.hpp:74
std::size_t plainSize() const
Definition: StorageType_vecofmat.hpp:76
PlainInterface::MType< Scalar > MatrixType
Definition: StorageType_contiguous.hpp:15
const MatrixType & block(std::size_t i) const
Definition: StorageType_vecofmat.hpp:98
DictType & dict()
Definition: StorageType_vecofmat.hpp:125
void reserve(std::size_t size)
Definition: StorageType_vecofmat.hpp:143
PlainInterface::MapMType< Scalar > MapMatrixType
Definition: StorageType_contiguous.hpp:16
const std::vector< qType, typename AllocationPolicy::template Allocator< qType > > & sector() const
Definition: StorageType_vecofmat.hpp:127
qType sector(std::size_t i) const
Definition: StorageType_vecofmat.hpp:128
PlainInterface::cMapMType< Scalar > cMapMatrixType
Definition: StorageType_contiguous.hpp:17
StorageType()=default
void clear()
Definition: StorageType_vecofmat.hpp:145
MatrixType & block(qType q)
Definition: StorageType_vecofmat.hpp:117
const Scalar * data() const
Definition: StorageType_contiguous.hpp:133
const std::array< Qbasis< Symmetry, 1, AllocationPolicy >, Rank > & uncoupledDomain() const
Definition: StorageType_vecofmat.hpp:130
MapMatrixType MatrixReturnType
Definition: StorageType_contiguous.hpp:20
StorageType(const StorageType< Scalar, Rank, CoRank, Symmetry, AllocationPolicy > &other)=default
void resize()
Definition: StorageType_vecofmat.hpp:81
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_vecofmat.hpp:43
cMapMatrixType ConstMatrixReturnType
Definition: StorageType_contiguous.hpp:21
VecOfMatIterator< Scalar > end()
Definition: StorageType_vecofmat.hpp:156
const MatrixType & block(qType q) const
Definition: StorageType_vecofmat.hpp:110
const DictType & dict() const
Definition: StorageType_vecofmat.hpp:124
Definition: Mpi.hpp:34