* coefficient wise operators are more generic, with controllable result type.

- compatible with current STL's functors as well as with the extention proposal (TR1)
 * thanks to the above, Cast and ScalarMultiple have been removed
 * benchmark_suite is more flexible (compiler and matrix size)
This commit is contained in:
Gael Guennebaud
2008-03-06 11:36:27 +00:00
parent 8e0d548039
commit 138aad0ed0
17 changed files with 257 additions and 338 deletions

11
doc/bench_unrolling Executable file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
# gcc : CXX="g++ -finline-limit=10000 -ftemplate-depth-2000 --param max-inline-recursive-depth=2000"
# icc : CXX="icpc -fast -no-inline-max-size -fno-exceptions"
for ((i=1; i<16; ++i)); do
echo "Matrix size: $i x $i :"
$CXX -O3 -I.. -DNDEBUG benchmark.cpp -DMATSIZE=$i -DEIGEN_UNROLLING_LIMIT_OPEQUAL=1024 -DEIGEN_UNROLLING_LIMIT_PRODUCT=25 -o benchmark && time ./benchmark >/dev/null
$CXX -O3 -I.. -DNDEBUG -finline-limit=10000 benchmark.cpp -DMATSIZE=$i -DEIGEN_DONT_USE_UNROLLED_LOOPS=1 -o benchmark && time ./benchmark >/dev/null
echo " "
done

View File

@@ -1,24 +1,31 @@
// g++ -O3 -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark
// g++ -O3 -DNDEBUG -DMATSIZE=<x> benchmark.cpp -o benchmark && time ./benchmark
#include <cstdlib>
#include <cmath>
#include <Eigen/Core>
//using namespace std;
#ifndef MATSIZE
#define MATSIZE 3
#endif
using namespace std;
USING_PART_OF_NAMESPACE_EIGEN
int main(int argc, char *argv[])
{
Matrix3d I;
Matrix3d m;
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++)
{
I(i,j) = (i==j);
m(i,j) = (i+3*j);
}
for(int a = 0; a < 400000000; a++)
{
m = I + 0.00005 * (m + m*m);
}
std::cout << m << std::endl;
return 0;
Matrix<double,MATSIZE,MATSIZE> I;
Matrix<double,MATSIZE,MATSIZE> m;
for(int i = 0; i < MATSIZE; i++)
for(int j = 0; j < MATSIZE; j++)
{
I(i,j) = (i==j);
m(i,j) = (i+MATSIZE*j);
}
asm("#begin");
for(int a = 0; a < 40000000; a++)
{
m = I + 0.00005 * (m + m*m);
}
asm("#end");
cout << m << endl;
return 0;
}

View File

@@ -7,7 +7,7 @@ USING_PART_OF_NAMESPACE_EIGEN
int main(int argc, char *argv[])
{
MatrixXd I = MatrixXd::identity(20);
MatrixXd I = MatrixXd::identity(20,20);
MatrixXd m(20,20);
for(int i = 0; i < 20; i++) for(int j = 0; j < 20; j++)
{

View File

@@ -1,16 +1,17 @@
#!/bin/bash
echo "Fixed size 3x3, ColumnMajor, -DNDEBUG"
g++ -O3 -I .. -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark >/dev/null
$CXX -O3 -I .. -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark >/dev/null
echo "Fixed size 3x3, ColumnMajor, with asserts"
g++ -O3 -I .. benchmark.cpp -o benchmark && time ./benchmark >/dev/null
$CXX -O3 -I .. benchmark.cpp -o benchmark && time ./benchmark >/dev/null
echo "Fixed size 3x3, RowMajor, -DNDEBUG"
g++ -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark >/dev/null
$CXX -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR -DNDEBUG benchmark.cpp -o benchmark && time ./benchmark >/dev/null
echo "Fixed size 3x3, RowMajor, with asserts"
g++ -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR benchmark.cpp -o benchmark && time ./benchmark >/dev/null
$CXX -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR benchmark.cpp -o benchmark && time ./benchmark >/dev/null
echo "Dynamic size 20x20, ColumnMajor, -DNDEBUG"
g++ -O3 -I .. -DNDEBUG benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
$CXX -O3 -I .. -DNDEBUG benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
echo "Dynamic size 20x20, ColumnMajor, with asserts"
g++ -O3 -I .. benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
$CXX -O3 -I .. benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
echo "Dynamic size 20x20, RowMajor, -DNDEBUG"
g++ -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR -DNDEBUG benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
$CXX -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR -DNDEBUG benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
echo "Dynamic size 20x20, RowMajor, with asserts"
g++ -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null
$CXX -O3 -I .. -DEIGEN_DEFAULT_TO_ROW_MAJOR benchmarkX.cpp -o benchmarkX && time ./benchmarkX >/dev/null

View File

@@ -3,16 +3,13 @@ USING_PART_OF_NAMESPACE_EIGEN
using namespace std;
template<typename Scalar, typename Derived>
const Eigen::Cast<
typename Eigen::NumTraits<Scalar>::FloatingPoint,
const Eigen::CwiseUnaryOp<
Eigen::ScalarCastOp<typename Eigen::NumTraits<Scalar>::FloatingPoint>,
Derived
>
castToFloatingPoint(const MatrixBase<Scalar, Derived>& m)
{
return Eigen::Cast<
typename Eigen::NumTraits<Scalar>::FloatingPoint,
Derived
>(m.asArg());
return m.template cast<typename Eigen::NumTraits<Scalar>::FloatingPoint>();
}
int main(int, char**)

View File

@@ -3,9 +3,9 @@ USING_PART_OF_NAMESPACE_EIGEN
using namespace std;
// define a custom template binary functor
struct CwiseMinOp {
struct CwiseMinOp EIGEN_EMPTY_STRUCT {
template<typename Scalar>
static Scalar op(const Scalar& a, const Scalar& b) { return std::min(a,b); }
Scalar operator()(const Scalar& a, const Scalar& b) const { return std::min(a,b); }
};
// define a custom binary operator between two matrices
@@ -14,14 +14,13 @@ const Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>
cwiseMin(const MatrixBase<Scalar, Derived1> &mat1, const MatrixBase<Scalar, Derived2> &mat2)
{
return Eigen::CwiseBinaryOp<CwiseMinOp, Derived1, Derived2>(mat1.asArg(), mat2.asArg());
// Note that the above is equivalent to:
// return mat1.template cwise<CwiseMinOp>(mat2);
}
int main(int, char**)
{
Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random();
cout << cwiseMin(m1,m2) << endl; // use our new global operator
cout << m1.cwise<CwiseMinOp>(m2) << endl; // directly use the generic expression member
cout << cwiseMin(m1,m2) << endl; // use our new global operator
cout << m1.cwise<CwiseMinOp>(m2) << endl; // directly use the generic expression member
cout << m1.cwise(m2, CwiseMinOp()) << endl; // directly use the generic expression member (variant)
return 0;
}

View File

@@ -0,0 +1,17 @@
#include <Eigen/Core>
USING_PART_OF_NAMESPACE_EIGEN
using namespace std;
// define a custom template binary functor
template<typename Scalar>
struct CwiseClampOp EIGEN_EMPTY_STRUCT {
CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {}
Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup : m_sup : x); }
};
int main(int, char**)
{
Matrix4d m1 = Matrix4d::random(), m2 = Matrix4d::random();
cout << m1.cwise(m2, CwiseClampOp<Matrix4d::Scalar>(-0.5,0.5)) << endl;
return 0;
}