Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits > Class Template Reference

The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access. More...

#include <src/eigen/unsupported/Eigen/src/SparseExtra/RandomSetter.h>

Classes

struct  ScalarWrapper
 

Public Member Functions

 RandomSetter (SparseMatrixType &target)
 
 ~RandomSetter ()
 
Scalaroperator() (Index row, Index col)
 
Index nonZeros () const
 

Protected Attributes

HashMapTypem_hashmaps
 
SparseMatrixType * mp_target
 
Index m_outerPackets
 
unsigned char m_keyBitsOffset
 

Private Types

enum  { SwapStorage = 1 - MapTraits<ScalarWrapper>::IsSorted , TargetRowMajor = (SparseMatrixType::Flags & RowMajorBit) ? 1 : 0 , SetterRowMajor = SwapStorage ? 1-TargetRowMajor : TargetRowMajor }
 
typedef SparseMatrixType::Scalar Scalar
 
typedef SparseMatrixType::StorageIndex StorageIndex
 
typedef MapTraits< ScalarWrapper >::KeyType KeyType
 
typedef MapTraits< ScalarWrapper >::Type HashMapType
 

Static Private Attributes

static const int OuterPacketMask = (1 << OuterPacketBits) - 1
 

Detailed Description

template<typename SparseMatrixType, template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
class Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >

The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access.

Template Parameters
SparseMatrixTypethe type of the sparse matrix we are updating
MapTraitsa traits class representing the map implementation used for the temporary sparse storage. Its default value depends on the system.
OuterPacketBitsdefines the number of rows (or columns) manage by a single map object as a power of two exponent.

This class temporarily represents a sparse matrix object using a generic map implementation allowing for efficient random access. The conversion from the compressed representation to a hash_map object is performed in the RandomSetter constructor, while the sparse matrix is updated back at destruction time. This strategy suggest the use of nested blocks as in this example:

SparseMatrix<double> m(rows,cols);
{
// don't use m but w instead with read/write random access to the coefficients:
for(;;)
w(rand(),rand()) = rand;
}
// when w is deleted, the data are copied back to m
// and m is ready to use.
The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access.
Definition RandomSetter.h:155
A versatible sparse matrix representation.
Definition SparseMatrix.h:98

Since hash_map objects are not fully sorted, representing a full matrix as a single hash_map would involve a big and costly sort to update the compressed matrix back. To overcome this issue, a RandomSetter use multiple hash_map, each representing 2^OuterPacketBits columns or rows according to the storage order. To reach optimal performance, this value should be adjusted according to the average number of nonzeros per rows/columns.

The possible values for the template parameter MapTraits are:

  • StdMapTraits: corresponds to std::map. (does not perform very well)
  • GnuHashMapTraits: corresponds to __gnu_cxx::hash_map (available only with GCC)
  • GoogleDenseHashMapTraits: corresponds to google::dense_hash_map (best efficiency, reasonable memory consumption)
  • GoogleSparseHashMapTraits: corresponds to google::sparse_hash_map (best memory consumption, relatively good performance)

The default map implementation depends on the availability, and the preferred order is: GoogleSparseHashMapTraits, GnuHashMapTraits, and finally StdMapTraits.

For performance and memory consumption reasons it is highly recommended to use one of the Google's hash_map implementation. To enable the support for them, you have two options:

  • #include <google/dense_hash_map> yourself before Eigen/Sparse header
  • define EIGEN_GOOGLEHASH_SUPPORT In the later case the inclusion of <google/dense_hash_map> is made for you.
See also
http://code.google.com/p/google-sparsehash/

Member Typedef Documentation

◆ HashMapType

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
typedef MapTraits<ScalarWrapper>::Type Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::HashMapType
private

◆ KeyType

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
typedef MapTraits<ScalarWrapper>::KeyType Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::KeyType
private

◆ Scalar

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
typedef SparseMatrixType::Scalar Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::Scalar
private

◆ StorageIndex

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
typedef SparseMatrixType::StorageIndex Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::StorageIndex
private

Member Enumeration Documentation

◆ anonymous enum

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
anonymous enum
private
Enumerator
SwapStorage 
TargetRowMajor 
SetterRowMajor 
167 {
168 SwapStorage = 1 - MapTraits<ScalarWrapper>::IsSorted,
169 TargetRowMajor = (SparseMatrixType::Flags & RowMajorBit) ? 1 : 0,
171 };
@ SwapStorage
Definition RandomSetter.h:168
@ SetterRowMajor
Definition RandomSetter.h:170
@ TargetRowMajor
Definition RandomSetter.h:169
const unsigned int RowMajorBit
Definition Constants.h:61

Constructor & Destructor Documentation

◆ RandomSetter()

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::RandomSetter ( SparseMatrixType &  target)
inline

Constructs a random setter object from the sparse matrix target

Note that the initial value of target are imported. If you want to re-set a sparse matrix from scratch, then you must set it to zero first using the setZero() function.

182 : mp_target(&target)
183 {
184 const Index outerSize = SwapStorage ? target.innerSize() : target.outerSize();
185 const Index innerSize = SwapStorage ? target.outerSize() : target.innerSize();
186 m_outerPackets = outerSize >> OuterPacketBits;
187 if (outerSize&OuterPacketMask)
188 m_outerPackets += 1;
190 // compute number of bits needed to store inner indices
191 Index aux = innerSize - 1;
192 m_keyBitsOffset = 0;
193 while (aux)
194 {
196 aux = aux >> 1;
197 }
198 KeyType ik = (1<<(OuterPacketBits+m_keyBitsOffset));
199 for (Index k=0; k<m_outerPackets; ++k)
200 MapTraits<ScalarWrapper>::setInvalidKey(m_hashmaps[k],ik);
201
202 // insert current coeffs
203 for (Index j=0; j<mp_target->outerSize(); ++j)
204 for (typename SparseMatrixType::InnerIterator it(*mp_target,j); it; ++it)
205 (*this)(TargetRowMajor?j:it.index(), TargetRowMajor?it.index():j) = it.value();
206 }
Index m_outerPackets
Definition RandomSetter.h:321
MapTraits< ScalarWrapper >::KeyType KeyType
Definition RandomSetter.h:164
static const int OuterPacketMask
Definition RandomSetter.h:166
unsigned char m_keyBitsOffset
Definition RandomSetter.h:322
MapTraits< ScalarWrapper >::Type HashMapType
Definition RandomSetter.h:165
HashMapType * m_hashmaps
Definition RandomSetter.h:319
SparseMatrixType * mp_target
Definition RandomSetter.h:320
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:33

References Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_hashmaps, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_keyBitsOffset, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_outerPackets, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::mp_target, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::OuterPacketMask, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::SwapStorage, and Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::TargetRowMajor.

◆ ~RandomSetter()

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::~RandomSetter ( )
inline

Destructor updating back the sparse matrix target

210 {
211 KeyType keyBitsMask = (1<<m_keyBitsOffset)-1;
212 if (!SwapStorage) // also means the map is sorted
213 {
214 mp_target->setZero();
215 mp_target->makeCompressed();
216 mp_target->reserve(nonZeros());
217 Index prevOuter = -1;
218 for (Index k=0; k<m_outerPackets; ++k)
219 {
220 const Index outerOffset = (1<<OuterPacketBits) * k;
221 typename HashMapType::iterator end = m_hashmaps[k].end();
222 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
223 {
224 const Index outer = (it->first >> m_keyBitsOffset) + outerOffset;
225 const Index inner = it->first & keyBitsMask;
226 if (prevOuter!=outer)
227 {
228 for (Index j=prevOuter+1;j<=outer;++j)
229 mp_target->startVec(j);
230 prevOuter = outer;
231 }
232 mp_target->insertBackByOuterInner(outer, inner) = it->second.value;
233 }
234 }
235 mp_target->finalize();
236 }
237 else
238 {
239 VectorXi positions(mp_target->outerSize());
240 positions.setZero();
241 // pass 1
242 for (Index k=0; k<m_outerPackets; ++k)
243 {
244 typename HashMapType::iterator end = m_hashmaps[k].end();
245 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
246 {
247 const Index outer = it->first & keyBitsMask;
248 ++positions[outer];
249 }
250 }
251 // prefix sum
252 Index count = 0;
253 for (Index j=0; j<mp_target->outerSize(); ++j)
254 {
255 Index tmp = positions[j];
256 mp_target->outerIndexPtr()[j] = count;
257 positions[j] = count;
258 count += tmp;
259 }
260 mp_target->makeCompressed();
261 mp_target->outerIndexPtr()[mp_target->outerSize()] = count;
262 mp_target->resizeNonZeros(count);
263 // pass 2
264 for (Index k=0; k<m_outerPackets; ++k)
265 {
266 const Index outerOffset = (1<<OuterPacketBits) * k;
267 typename HashMapType::iterator end = m_hashmaps[k].end();
268 for (typename HashMapType::iterator it = m_hashmaps[k].begin(); it!=end; ++it)
269 {
270 const Index inner = (it->first >> m_keyBitsOffset) + outerOffset;
271 const Index outer = it->first & keyBitsMask;
272 // sorted insertion
273 // Note that we have to deal with at most 2^OuterPacketBits unsorted coefficients,
274 // moreover those 2^OuterPacketBits coeffs are likely to be sparse, an so only a
275 // small fraction of them have to be sorted, whence the following simple procedure:
276 Index posStart = mp_target->outerIndexPtr()[outer];
277 Index i = (positions[outer]++) - 1;
278 while ( (i >= posStart) && (mp_target->innerIndexPtr()[i] > inner) )
279 {
280 mp_target->valuePtr()[i+1] = mp_target->valuePtr()[i];
281 mp_target->innerIndexPtr()[i+1] = mp_target->innerIndexPtr()[i];
282 --i;
283 }
284 mp_target->innerIndexPtr()[i+1] = inner;
285 mp_target->valuePtr()[i+1] = it->second.value;
286 }
287 }
288 }
289 delete[] m_hashmaps;
290 }
Index nonZeros() const
Definition RandomSetter.h:308
T * begin(Slic3r::Mat< N, M, T > &mat)
Definition Point.hpp:605
T * end(Slic3r::Mat< N, M, T > &mat)
Definition Point.hpp:608
IGL_INLINE void count(const Eigen::SparseMatrix< XType > &X, const int dim, Eigen::SparseVector< SType > &S)
Definition count.cpp:12

References Eigen::begin(), Eigen::end(), Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_hashmaps, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_keyBitsOffset, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_outerPackets, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::mp_target, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::nonZeros(), and Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::SwapStorage.

+ Here is the call graph for this function:

Member Function Documentation

◆ nonZeros()

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
Index Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::nonZeros ( ) const
inline
Returns
the number of non zero coefficients
Note
According to the underlying map/hash_map implementation, this function might be quite expensive.
309 {
310 Index nz = 0;
311 for (Index k=0; k<m_outerPackets; ++k)
312 nz += static_cast<Index>(m_hashmaps[k].size());
313 return nz;
314 }
constexpr auto size(const C &c) -> decltype(c.size())
Definition span.hpp:183

References Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_hashmaps, and Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_outerPackets.

Referenced by Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::~RandomSetter().

+ Here is the caller graph for this function:

◆ operator()()

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
Scalar & Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::operator() ( Index  row,
Index  col 
)
inline
Returns
a reference to the coefficient at given coordinates row, col
294 {
295 const Index outer = SetterRowMajor ? row : col;
296 const Index inner = SetterRowMajor ? col : row;
297 const Index outerMajor = outer >> OuterPacketBits; // index of the packet/map
298 const Index outerMinor = outer & OuterPacketMask; // index of the inner vector in the packet
299 const KeyType key = internal::convert_index<KeyType>((outerMinor<<m_keyBitsOffset) | inner);
300 return m_hashmaps[outerMajor][key].value;
301 }
EIGEN_DEVICE_FUNC RowXpr row(Index i)
This is the const version of row(). *‍/.
Definition BlockMethods.h:859
EIGEN_DEVICE_FUNC ColXpr col(Index i)
This is the const version of col().
Definition BlockMethods.h:838

References col(), Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_hashmaps, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_keyBitsOffset, Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::OuterPacketMask, row(), and Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::SetterRowMajor.

+ Here is the call graph for this function:

Member Data Documentation

◆ m_hashmaps

◆ m_keyBitsOffset

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
unsigned char Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_keyBitsOffset
protected

◆ m_outerPackets

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
Index Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::m_outerPackets
protected

◆ mp_target

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
SparseMatrixType* Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::mp_target
protected

◆ OuterPacketMask

template<typename SparseMatrixType , template< typename T > class MapTraits = StdMapTraits, int OuterPacketBits = 6>
const int Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::OuterPacketMask = (1 << OuterPacketBits) - 1
staticprivate

The documentation for this class was generated from the following file: