The RandomSetter is a wrapper object allowing to set/update a sparse matrix with random access.
More...
template<typename SparseMatrixType, template< typename T > class MapTraits = StdUnorderedMapTraits, 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
-
SparseMatrixType | the type of the sparse matrix we are updating |
MapTraits | a traits class representing the map implementation used for the temporary sparse storage. Its default value depends on the system. |
OuterPacketBits | defines 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:
{
RandomSetter<SparseMatrix<double> >
w(
m);
for(;;)
}
RowVector3d w
Definition: Matrix_resize_int.cpp:3
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
int * m
Definition: level2_cplx_impl.h:294
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)
- StdUnorderedMapTraits: corresponds to std::unordered_map
- 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, StdUnorderedMapTraits, and finally StdMapTraits.
For performance and memory consumption reasons it is highly recommended to use one of Google's hash_map implementations. To enable the support for them, you must define EIGEN_GOOGLEHASH_SUPPORT. This will include both <google/dense_hash_map> and <google/sparse_hash_map> for you.
- See also
- https://github.com/sparsehash/sparsehash
template<typename SparseMatrixType , template< typename T > class MapTraits = StdUnorderedMapTraits, int OuterPacketBits = 6>
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.
181 const Index outerSize =
SwapStorage ? target.innerSize() : target.outerSize();
182 const Index innerSize =
SwapStorage ? target.outerSize() : target.innerSize();
187 Index aux = innerSize - 1;
194 for (
Index k = 0; k < m_outerPackets; ++k) MapTraits<ScalarWrapper>::setInvalidKey(
m_hashmaps[
k], ik);
198 for (
typename SparseMatrixType::InnerIterator it(*
mp_target,
j); it; ++it)
Index m_outerPackets
Definition: RandomSetter.h:297
MapTraits< ScalarWrapper >::KeyType KeyType
Definition: RandomSetter.h:164
unsigned char m_keyBitsOffset
Definition: RandomSetter.h:298
MapTraits< ScalarWrapper >::Type HashMapType
Definition: RandomSetter.h:165
HashMapType * m_hashmaps
Definition: RandomSetter.h:295
static constexpr int OuterPacketMask
Definition: RandomSetter.h:166
SparseMatrixType * mp_target
Definition: RandomSetter.h:296
char char char int int * k
Definition: level2_impl.h:374
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
References j, k, 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.
template<typename SparseMatrixType , template< typename T > class MapTraits = StdUnorderedMapTraits, int OuterPacketBits = 6>
Destructor updating back the sparse matrix target
210 Index prevOuter = -1;
212 const Index outerOffset = (1 << OuterPacketBits) *
k;
214 for (
typename HashMapType::iterator it =
m_hashmaps[
k].begin(); it !=
end; ++it) {
216 const Index inner = it->first & keyBitsMask;
217 if (prevOuter != outer) {
221 mp_target->insertBackByOuterInner(outer, inner) = it->second.value;
226 VectorXi positions(
mp_target->outerSize());
231 for (
typename HashMapType::iterator it =
m_hashmaps[
k].begin(); it !=
end; ++it) {
232 const Index outer = it->first & keyBitsMask;
241 positions[
j] = count;
249 const Index outerOffset = (1 << OuterPacketBits) *
k;
251 for (
typename HashMapType::iterator it =
m_hashmaps[
k].begin(); it !=
end; ++it) {
253 const Index outer = it->first & keyBitsMask;
259 Index i = (positions[outer]++) - 1;
260 while ((
i >= posStart) && (
mp_target->innerIndexPtr()[
i] > inner)) {
265 mp_target->innerIndexPtr()[
i + 1] = internal::convert_index<StorageIndex>(inner);
266 mp_target->valuePtr()[
i + 1] = it->second.value;
int i
Definition: BiCGSTAB_step_by_step.cpp:9
SparseMatrixType::StorageIndex StorageIndex
Definition: RandomSetter.h:158
Index nonZeros() const
Definition: RandomSetter.h:288
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
Eigen::Matrix< Scalar, Dynamic, Dynamic, ColMajor > tmp
Definition: level3_impl.h:365
References Eigen::placeholders::end, i, j, k, 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(), Eigen::RandomSetter< SparseMatrixType, MapTraits, OuterPacketBits >::SwapStorage, and tmp.