mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-12 19:59:05 +08:00
sparse module: add a RandomSetter based on a user defined map implementation
as described on the wiki (one map per N column) Here's some bench results for the 4 currently supported map impl: std::map => 18.3385 (581 MB) gnu::hash_map => 6.52574 (555 MB) google::dense => 2.87982 (315 MB) google::sparse => 15.7441 (165 MB) This is the time is second (and memory consumption) to insert/lookup 10 million of coeffs with random coords inside a 10000^2 matrix, with one map per packet of 64 columns => google::dense really rocks ! Note I use for the key value the index of the column in the packet (between 0 and 63) times the number of rows and I used the default hash function.... so maybe there is room for improvement here....
This commit is contained in:
parent
96538b976d
commit
3645d6c138
@ -67,6 +67,7 @@ namespace Eigen {
|
|||||||
#include "src/Sparse/SparseMatrixBase.h"
|
#include "src/Sparse/SparseMatrixBase.h"
|
||||||
#include "src/Sparse/SparseArray.h"
|
#include "src/Sparse/SparseArray.h"
|
||||||
#include "src/Sparse/AmbiVector.h"
|
#include "src/Sparse/AmbiVector.h"
|
||||||
|
#include "src/Sparse/RandomSetter.h"
|
||||||
#include "src/Sparse/SparseBlock.h"
|
#include "src/Sparse/SparseBlock.h"
|
||||||
#include "src/Sparse/SparseMatrix.h"
|
#include "src/Sparse/SparseMatrix.h"
|
||||||
#include "src/Sparse/HashMatrix.h"
|
#include "src/Sparse/HashMatrix.h"
|
||||||
|
125
Eigen/src/Sparse/RandomSetter.h
Normal file
125
Eigen/src/Sparse/RandomSetter.h
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
// This file is part of Eigen, a lightweight C++ template library
|
||||||
|
// for linear algebra. Eigen itself is part of the KDE project.
|
||||||
|
//
|
||||||
|
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
|
||||||
|
//
|
||||||
|
// Eigen is free software; you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU Lesser General Public
|
||||||
|
// License as published by the Free Software Foundation; either
|
||||||
|
// version 3 of the License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Alternatively, you can redistribute it and/or
|
||||||
|
// modify it under the terms of the GNU General Public License as
|
||||||
|
// published by the Free Software Foundation; either version 2 of
|
||||||
|
// the License, or (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public
|
||||||
|
// License and a copy of the GNU General Public License along with
|
||||||
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
#ifndef EIGEN_RANDOMSETTER_H
|
||||||
|
#define EIGEN_RANDOMSETTER_H
|
||||||
|
|
||||||
|
template<typename Scalar> struct StdMapTraits
|
||||||
|
{
|
||||||
|
typedef int KeyType;
|
||||||
|
typedef std::map<KeyType,Scalar> Type;
|
||||||
|
|
||||||
|
static void setInvalidKey(Type&, const KeyType&) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef _HASH_MAP
|
||||||
|
template<typename Scalar> struct GnuHashMapTraits
|
||||||
|
{
|
||||||
|
typedef int KeyType;
|
||||||
|
typedef __gnu_cxx::hash_map<KeyType,Scalar> Type;
|
||||||
|
|
||||||
|
static void setInvalidKey(Type&, const KeyType&) {}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _DENSE_HASH_MAP_H_
|
||||||
|
template<typename Scalar> struct GoogleDenseHashMapTraits
|
||||||
|
{
|
||||||
|
typedef int KeyType;
|
||||||
|
typedef google::dense_hash_map<KeyType,Scalar> Type;
|
||||||
|
|
||||||
|
static void setInvalidKey(Type& map, const KeyType& k)
|
||||||
|
{ map.set_empty_key(k); }
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _SPARSE_HASH_MAP_H_
|
||||||
|
template<typename Scalar> struct GoogleSparseHashMapTraits
|
||||||
|
{
|
||||||
|
typedef int KeyType;
|
||||||
|
typedef google::sparse_hash_map<KeyType,Scalar> Type;
|
||||||
|
|
||||||
|
static void setInvalidKey(Type&, const KeyType&) {}
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** \class RandomSetter
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
template<typename SparseMatrixType,
|
||||||
|
template <typename T> class HashMapTraits = StdMapTraits,
|
||||||
|
int OuterPacketBits = 6>
|
||||||
|
class RandomSetter
|
||||||
|
{
|
||||||
|
typedef typename ei_traits<SparseMatrixType>::Scalar Scalar;
|
||||||
|
struct ScalarWrapper
|
||||||
|
{
|
||||||
|
ScalarWrapper() : value(0) {}
|
||||||
|
Scalar value;
|
||||||
|
};
|
||||||
|
typedef typename HashMapTraits<ScalarWrapper>::KeyType KeyType;
|
||||||
|
typedef typename HashMapTraits<ScalarWrapper>::Type HashMapType;
|
||||||
|
static const int OuterPacketMask = (1 << OuterPacketBits) - 1;
|
||||||
|
enum {
|
||||||
|
RowMajor = SparseMatrixType::Flags & RowMajorBit
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
inline RandomSetter(SparseMatrixType& target)
|
||||||
|
: mp_target(&target)
|
||||||
|
{
|
||||||
|
int outerPackets = target.outerSize() >> OuterPacketBits;
|
||||||
|
if (target.outerSize()&OuterPacketMask)
|
||||||
|
outerPackets += 1;
|
||||||
|
m_hashmaps = new HashMapType[outerPackets];
|
||||||
|
KeyType ik = (1<<OuterPacketBits)*mp_target->innerSize()+1;
|
||||||
|
for (int k=0; k<outerPackets; ++k)
|
||||||
|
HashMapTraits<ScalarWrapper>::setInvalidKey(m_hashmaps[k],ik);
|
||||||
|
}
|
||||||
|
|
||||||
|
~RandomSetter()
|
||||||
|
{
|
||||||
|
delete[] m_hashmaps;
|
||||||
|
}
|
||||||
|
|
||||||
|
Scalar& operator() (int row, int col)
|
||||||
|
{
|
||||||
|
const int outer = RowMajor ? row : col;
|
||||||
|
const int inner = RowMajor ? col : row;
|
||||||
|
const int outerMajor = outer >> OuterPacketBits;
|
||||||
|
const int outerMinor = outer & OuterPacketMask;
|
||||||
|
const KeyType key = inner + outerMinor * mp_target->innerSize();
|
||||||
|
|
||||||
|
return m_hashmaps[outerMajor][key].value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
HashMapType* m_hashmaps;
|
||||||
|
SparseMatrixType* mp_target;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // EIGEN_RANDOMSETTER_H
|
Loading…
x
Reference in New Issue
Block a user