eigen/bench/sparse_setter.cpp

298 lines
8.3 KiB
C++
Raw Normal View History

2009-01-17 22:05:01 +08:00
//g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.005 -DSIZE=10000 && ./a.out
//g++ -O3 -g0 -DNDEBUG sparse_product.cpp -I.. -I/home/gael/Coding/LinearAlgebra/mtl4/ -DDENSITY=0.05 -DSIZE=2000 && ./a.out
// -DNOGMM -DNOMTL -DCSPARSE
// -I /home/gael/Coding/LinearAlgebra/CSparse/Include/ /home/gael/Coding/LinearAlgebra/CSparse/Lib/libcsparse.a
#ifndef SIZE
2009-01-18 17:53:06 +08:00
#define SIZE 100000
2009-01-17 22:05:01 +08:00
#endif
#ifndef NBPERROW
#define NBPERROW 24
2009-01-17 22:05:01 +08:00
#endif
#ifndef REPEAT
#define REPEAT 1
#endif
#ifndef NOGOOGLE
2009-01-17 22:05:01 +08:00
#define EIGEN_GOOGLEHASH_SUPPORT
#include <google/sparse_hash_map>
#endif
2009-01-17 22:05:01 +08:00
#include "BenchSparseUtil.h"
2009-01-18 17:53:06 +08:00
#define CHECK_MEM
// #define CHECK_MEM std/**/::cout << "check mem\n"; getchar();
2009-01-17 22:05:01 +08:00
#define BENCH(X) \
timer.reset(); \
for (int _j=0; _j<NBTRIES; ++_j) { \
timer.start(); \
for (int _k=0; _k<REPEAT; ++_k) { \
X \
} timer.stop(); }
typedef std::vector<Vector2i> Coordinates;
typedef std::vector<float> Values;
2009-01-18 17:53:06 +08:00
EIGEN_DONT_INLINE Scalar* setinnerrand_eigen(const Coordinates& coords, const Values& vals);
2009-01-17 22:05:01 +08:00
EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals);
EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals);
2009-01-17 22:05:01 +08:00
EIGEN_DONT_INLINE Scalar* setrand_mtl(const Coordinates& coords, const Values& vals);
int main(int argc, char *argv[])
{
int rows = SIZE;
int cols = SIZE;
2009-01-18 17:53:06 +08:00
bool fullyrand = false;
2009-01-17 22:05:01 +08:00
//float density = float(NBPERROW)/float(SIZE);
BenchTimer timer;
Coordinates coords;
Values values;
2009-01-18 17:53:06 +08:00
if(fullyrand)
2009-01-17 22:05:01 +08:00
{
2009-01-18 17:53:06 +08:00
for (int i=0; i<cols*NBPERROW; ++i)
{
coords.push_back(Vector2i(ei_random<int>(0,rows-1),ei_random<int>(0,cols-1)));
values.push_back(ei_random<Scalar>());
}
}
else
{
for (int j=0; j<cols; ++j)
for (int i=0; i<NBPERROW; ++i)
{
coords.push_back(Vector2i(ei_random<int>(0,rows-1),j));
values.push_back(ei_random<Scalar>());
}
2009-01-17 22:05:01 +08:00
}
std::cout << "nnz = " << coords.size() << "\n";
2009-01-18 17:53:06 +08:00
CHECK_MEM
2009-01-17 22:05:01 +08:00
// dense matrices
#ifdef DENSEMATRIX
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_eigen_dense(coords,values);
timer.stop();
std::cout << "Eigen Dense\t" << timer.value() << "\n";
}
#endif
// eigen sparse matrices
2009-01-18 17:53:06 +08:00
if (!fullyrand)
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setinnerrand_eigen(coords,values);
timer.stop();
std::cout << "Eigen fillrand\t" << timer.value() << "\n";
}
2009-01-17 22:05:01 +08:00
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_eigen_gnu_hash(coords,values);
timer.stop();
std::cout << "Eigen std::map\t" << timer.value() << "\n";
2009-01-17 22:05:01 +08:00
}
#ifndef NOGOOGLE
2009-01-17 22:05:01 +08:00
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_eigen_google_dense(coords,values);
timer.stop();
std::cout << "Eigen google dense\t" << timer.value() << "\n";
}
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_eigen_google_sparse(coords,values);
timer.stop();
std::cout << "Eigen google sparse\t" << timer.value() << "\n";
}
#endif
2009-01-17 22:05:01 +08:00
#ifndef NOUBLAS
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_ublas_mapped(coords,values);
timer.stop();
std::cout << "ublas mapped\t" << timer.value() << "\n";
}
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_ublas_genvec(coords,values);
timer.stop();
std::cout << "ublas vecofvec\t" << timer.value() << "\n";
}
/*{
2009-01-17 22:05:01 +08:00
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_ublas_compressed(coords,values);
timer.stop();
std::cout << "ublas comp\t" << timer.value() << "\n";
}
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_ublas_coord(coords,values);
timer.stop();
std::cout << "ublas coord\t" << timer.value() << "\n";
}*/
2009-01-17 22:05:01 +08:00
#endif
// MTL4
#ifndef NOMTL
{
timer.reset();
timer.start();
for (int k=0; k<REPEAT; ++k)
setrand_mtl(coords,values);
timer.stop();
std::cout << "MTL\t" << timer.value() << "\n";
}
#endif
return 0;
}
2009-01-18 17:53:06 +08:00
EIGEN_DONT_INLINE Scalar* setinnerrand_eigen(const Coordinates& coords, const Values& vals)
{
using namespace Eigen;
SparseMatrix<Scalar> mat(SIZE,SIZE);
mat.startFill(2000000/*coords.size()*/);
for (int i=0; i<coords.size(); ++i)
{
mat.fillrand(coords[i].x(), coords[i].y()) = vals[i];
}
mat.endFill();
CHECK_MEM;
return 0;
}
2009-01-17 22:05:01 +08:00
EIGEN_DONT_INLINE Scalar* setrand_eigen_gnu_hash(const Coordinates& coords, const Values& vals)
{
using namespace Eigen;
SparseMatrix<Scalar> mat(SIZE,SIZE);
{
RandomSetter<SparseMatrix<Scalar>, StdMapTraits > setter(mat);
for (int i=0; i<coords.size(); ++i)
{
setter(coords[i].x(), coords[i].y()) = vals[i];
}
2009-01-18 17:53:06 +08:00
CHECK_MEM;
2009-01-17 22:05:01 +08:00
}
return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
}
#ifndef NOGOOGLE
2009-01-17 22:05:01 +08:00
EIGEN_DONT_INLINE Scalar* setrand_eigen_google_dense(const Coordinates& coords, const Values& vals)
{
using namespace Eigen;
SparseMatrix<Scalar> mat(SIZE,SIZE);
{
RandomSetter<SparseMatrix<Scalar>, GoogleDenseHashMapTraits> setter(mat);
for (int i=0; i<coords.size(); ++i)
setter(coords[i].x(), coords[i].y()) = vals[i];
2009-01-18 17:53:06 +08:00
CHECK_MEM;
2009-01-17 22:05:01 +08:00
}
return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
}
EIGEN_DONT_INLINE Scalar* setrand_eigen_google_sparse(const Coordinates& coords, const Values& vals)
{
using namespace Eigen;
SparseMatrix<Scalar> mat(SIZE,SIZE);
{
RandomSetter<SparseMatrix<Scalar>, GoogleSparseHashMapTraits> setter(mat);
for (int i=0; i<coords.size(); ++i)
setter(coords[i].x(), coords[i].y()) = vals[i];
2009-01-18 17:53:06 +08:00
CHECK_MEM;
2009-01-17 22:05:01 +08:00
}
return 0;//&mat.coeffRef(coords[0].x(), coords[0].y());
}
#endif
2009-01-17 22:05:01 +08:00
#ifndef NOUBLAS
EIGEN_DONT_INLINE Scalar* setrand_ublas_mapped(const Coordinates& coords, const Values& vals)
{
using namespace boost;
using namespace boost::numeric;
using namespace boost::numeric::ublas;
mapped_matrix<Scalar> aux(SIZE,SIZE);
for (int i=0; i<coords.size(); ++i)
{
aux(coords[i].x(), coords[i].y()) = vals[i];
}
2009-01-18 17:53:06 +08:00
CHECK_MEM;
2009-01-17 22:05:01 +08:00
compressed_matrix<Scalar> mat(aux);
return 0;// &mat(coords[0].x(), coords[0].y());
}
/*EIGEN_DONT_INLINE Scalar* setrand_ublas_coord(const Coordinates& coords, const Values& vals)
2009-01-17 22:05:01 +08:00
{
using namespace boost;
using namespace boost::numeric;
using namespace boost::numeric::ublas;
coordinate_matrix<Scalar> aux(SIZE,SIZE);
for (int i=0; i<coords.size(); ++i)
{
aux(coords[i].x(), coords[i].y()) = vals[i];
}
compressed_matrix<Scalar> mat(aux);
return 0;//&mat(coords[0].x(), coords[0].y());
}
EIGEN_DONT_INLINE Scalar* setrand_ublas_compressed(const Coordinates& coords, const Values& vals)
{
using namespace boost;
using namespace boost::numeric;
using namespace boost::numeric::ublas;
compressed_matrix<Scalar> mat(SIZE,SIZE);
for (int i=0; i<coords.size(); ++i)
{
mat(coords[i].x(), coords[i].y()) = vals[i];
}
return 0;//&mat(coords[0].x(), coords[0].y());
}*/
EIGEN_DONT_INLINE Scalar* setrand_ublas_genvec(const Coordinates& coords, const Values& vals)
{
using namespace boost;
using namespace boost::numeric;
using namespace boost::numeric::ublas;
// ublas::vector<coordinate_vector<Scalar> > foo;
generalized_vector_of_vector<Scalar, row_major, ublas::vector<coordinate_vector<Scalar> > > aux(SIZE,SIZE);
for (int i=0; i<coords.size(); ++i)
{
aux(coords[i].x(), coords[i].y()) = vals[i];
}
2009-01-18 17:53:06 +08:00
CHECK_MEM;
compressed_matrix<Scalar,row_major> mat(aux);
return 0;//&mat(coords[0].x(), coords[0].y());
2009-01-17 22:05:01 +08:00
}
#endif
#ifndef NOMTL
EIGEN_DONT_INLINE void setrand_mtl(const Coordinates& coords, const Values& vals);
#endif