Eigen-Contrib  5.0.1-dev+master
 
Loading...
Searching...
No Matches
RandomSetter.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_RANDOMSETTER_H
12#define EIGEN_RANDOMSETTER_H
13
14#if defined(EIGEN_GOOGLEHASH_SUPPORT)
15// Ensure the ::google namespace exists, required for checking existence of
16// ::google::dense_hash_map and ::google::sparse_hash_map.
17namespace google {}
18#endif
19
20// IWYU pragma: private
21#include "./InternalHeaderCheck.h"
22
23namespace Eigen {
24
29template <typename Scalar>
31 using KeyType = int;
32 using Type = std::map<KeyType, Scalar>;
33 enum { IsSorted = 1 };
34
35 static void setInvalidKey(Type&, const KeyType&) {}
36};
37
41template <typename Scalar>
43 using KeyType = int;
44 using Type = std::unordered_map<KeyType, Scalar>;
45 enum { IsSorted = 0 };
46
47 static void setInvalidKey(Type&, const KeyType&) {}
48};
49
50#if defined(EIGEN_GOOGLEHASH_SUPPORT)
51
52namespace google {
53
54// Namespace work-around, since sometimes dense_hash_map and sparse_hash_map
55// are in the global namespace, and other times they are under ::google.
56using namespace ::google;
57
58template <typename KeyType, typename Scalar>
59struct DenseHashMap {
60 using type = dense_hash_map<KeyType, Scalar>;
61};
62
63template <typename KeyType, typename Scalar>
64struct SparseHashMap {
65 using type = sparse_hash_map<KeyType, Scalar>;
66};
67
68} // namespace google
69
74template <typename Scalar>
75struct GoogleDenseHashMapTraits {
76 using KeyType = int;
77 using Type = typename google::DenseHashMap<KeyType, Scalar>::type;
78 enum { IsSorted = 0 };
79
80 static void setInvalidKey(Type& map, const KeyType& k) { map.set_empty_key(k); }
81};
82
87template <typename Scalar>
88struct GoogleSparseHashMapTraits {
89 using KeyType = int;
90 using Type = typename google::SparseHashMap<KeyType, Scalar>::type;
91 enum { IsSorted = 0 };
92
93 static void setInvalidKey(Type&, const KeyType&) {}
94};
95#endif
96
148template <typename SparseMatrixType,
149 template <typename T> class MapTraits =
150#if defined(EIGEN_GOOGLEHASH_SUPPORT)
151 GoogleDenseHashMapTraits
152#else
154#endif
155 ,
156 int OuterPacketBits = 6>
158 using Scalar = typename SparseMatrixType::Scalar;
159 using StorageIndex = typename SparseMatrixType::StorageIndex;
160
161 struct ScalarWrapper {
162 Scalar value = Scalar(0);
163 };
164 using KeyType = typename MapTraits<ScalarWrapper>::KeyType;
165 using HashMapType = typename MapTraits<ScalarWrapper>::Type;
166 static constexpr int OuterPacketMask = (1 << OuterPacketBits) - 1;
167 enum {
168 SwapStorage = 1 - MapTraits<ScalarWrapper>::IsSorted,
169 TargetRowMajor = (SparseMatrixType::Flags & RowMajorBit) ? 1 : 0,
170 SetterRowMajor = SwapStorage ? 1 - TargetRowMajor : TargetRowMajor
171 };
172
173 public:
180 inline RandomSetter(SparseMatrixType& target) : m_target(&target) {
181 const Index outerSize = SwapStorage ? target.innerSize() : target.outerSize();
182 const Index innerSize = SwapStorage ? target.outerSize() : target.innerSize();
183 m_outerPackets = outerSize >> OuterPacketBits;
184 if (outerSize & OuterPacketMask) m_outerPackets += 1;
185 m_hashmaps = new HashMapType[m_outerPackets];
186 // compute number of bits needed to store inner indices
187 Index aux = innerSize - 1;
188 m_keyBitsOffset = 0;
189 while (aux) {
190 ++m_keyBitsOffset;
191 aux = aux >> 1;
192 }
193 KeyType ik = (1 << (OuterPacketBits + m_keyBitsOffset));
194 for (Index k = 0; k < m_outerPackets; ++k) MapTraits<ScalarWrapper>::setInvalidKey(m_hashmaps[k], ik);
195
196 // insert current coeffs
197 for (Index j = 0; j < m_target->outerSize(); ++j)
198 for (typename SparseMatrixType::InnerIterator it(*m_target, j); it; ++it)
199 (*this)(TargetRowMajor ? j : it.index(), TargetRowMajor ? it.index() : j) = it.value();
200 }
201
204 KeyType keyBitsMask = (1 << m_keyBitsOffset) - 1;
205 // The constructor copied the target's coefficients into the hash maps. Reset the target before rewriting its index
206 // arrays: compacting an uncompressed target with the new offsets would read stale data.
207 m_target->setZero();
208 m_target->makeCompressed();
209 if (!SwapStorage) // also means the map is sorted
210 {
211 m_target->reserve(nonZeros());
212 Index prevOuter = -1;
213 for (Index k = 0; k < m_outerPackets; ++k) {
214 const Index outerOffset = (1 << OuterPacketBits) * k;
215 typename HashMapType::iterator end = m_hashmaps[k].end();
216 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it != end; ++it) {
217 const Index outer = (it->first >> m_keyBitsOffset) + outerOffset;
218 const Index inner = it->first & keyBitsMask;
219 if (prevOuter != outer) {
220 for (Index j = prevOuter + 1; j <= outer; ++j) m_target->startVec(j);
221 prevOuter = outer;
222 }
223 m_target->insertBackByOuterInner(outer, inner) = it->second.value;
224 }
225 }
226 m_target->finalize();
227 } else {
228 VectorXi positions(m_target->outerSize());
229 positions.setZero();
230 // pass 1
231 for (Index k = 0; k < m_outerPackets; ++k) {
232 typename HashMapType::iterator end = m_hashmaps[k].end();
233 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it != end; ++it) {
234 const Index outer = it->first & keyBitsMask;
235 ++positions[outer];
236 }
237 }
238 // prefix sum
239 StorageIndex count = 0;
240 for (Index j = 0; j < m_target->outerSize(); ++j) {
241 StorageIndex tmp = positions[j];
242 m_target->outerIndexPtr()[j] = count;
243 positions[j] = count;
244 count += tmp;
245 }
246 m_target->outerIndexPtr()[m_target->outerSize()] = count;
247 m_target->resizeNonZeros(count);
248 // pass 2
249 for (Index k = 0; k < m_outerPackets; ++k) {
250 const Index outerOffset = (1 << OuterPacketBits) * k;
251 typename HashMapType::iterator end = m_hashmaps[k].end();
252 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it != end; ++it) {
253 const Index inner = (it->first >> m_keyBitsOffset) + outerOffset;
254 const Index outer = it->first & keyBitsMask;
255 // sorted insertion
256 // Note that we have to deal with at most 2^OuterPacketBits unsorted coefficients,
257 // moreover those 2^OuterPacketBits coeffs are likely to be sparse, and so only a
258 // small fraction of them have to be sorted, whence the following simple procedure:
259 Index posStart = m_target->outerIndexPtr()[outer];
260 Index i = (positions[outer]++) - 1;
261 while ((i >= posStart) && (m_target->innerIndexPtr()[i] > inner)) {
262 m_target->valuePtr()[i + 1] = m_target->valuePtr()[i];
263 m_target->innerIndexPtr()[i + 1] = m_target->innerIndexPtr()[i];
264 --i;
265 }
266 m_target->innerIndexPtr()[i + 1] = internal::convert_index<StorageIndex>(inner);
267 m_target->valuePtr()[i + 1] = it->second.value;
268 }
269 }
270 }
271 delete[] m_hashmaps;
272 }
273
275 Scalar& operator()(Index row, Index col) {
276 const Index outer = SetterRowMajor ? row : col;
277 const Index inner = SetterRowMajor ? col : row;
278 const Index outerMajor = outer >> OuterPacketBits; // index of the packet/map
279 const Index outerMinor = outer & OuterPacketMask; // index of the inner vector in the packet
280 const KeyType key = internal::convert_index<KeyType>((outerMinor << m_keyBitsOffset) | inner);
281 return m_hashmaps[outerMajor][key].value;
282 }
283
284#ifdef EIGEN_MULTIDIMENSIONAL_SUBSCRIPT
286 Scalar& operator[](Index row, Index col) { return operator()(row, col); }
287#endif
288
294 Index nonZeros() const {
295 Index nz = 0;
296 for (Index k = 0; k < m_outerPackets; ++k) nz += static_cast<Index>(m_hashmaps[k].size());
297 return nz;
298 }
299
300 protected:
301 HashMapType* m_hashmaps;
302 SparseMatrixType* m_target;
303 Index m_outerPackets;
304 unsigned char m_keyBitsOffset;
305};
306
307} // end namespace Eigen
308
309#endif // EIGEN_RANDOMSETTER_H
Derived & setZero(Index rows, Index cols)
~RandomSetter()
Definition RandomSetter.h:203
RandomSetter(SparseMatrixType &target)
Definition RandomSetter.h:180
Scalar & operator()(Index row, Index col)
Definition RandomSetter.h:275
Index nonZeros() const
Definition RandomSetter.h:294
constexpr unsigned int RowMajorBit
Matrix< int, Dynamic, 1 > VectorXi
Namespace containing all symbols from the Eigen library.
Definition RandomSetter.h:30
Definition RandomSetter.h:42