sparse_trisolver.cpp File Reference
#include "BenchSparseUtil.h"

Macros

#define SIZE   10000
 
#define DENSITY   0.01
 
#define REPEAT   1
 
#define MINDENSITY   0.0004
 
#define NBTRIES   10
 
#define BENCH(X)
 

Typedefs

typedef SparseMatrix< Scalar, UpperTriangular > EigenSparseTriMatrix
 
typedef SparseMatrix< Scalar, RowMajorBit|UpperTriangular > EigenSparseTriMatrixRow
 

Functions

void fillMatrix (float density, int rows, int cols, EigenSparseTriMatrix &dst)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ BENCH

#define BENCH (   X)
Value:
timer.reset(); \
for (int _j = 0; _j < NBTRIES; ++_j) { \
timer.start(); \
for (int _k = 0; _k < REPEAT; ++_k) { \
X \
} \
timer.stop(); \
}
#define REPEAT
Definition: sparse_trisolver.cpp:17
#define NBTRIES
Definition: sparse_trisolver.cpp:27

◆ DENSITY

#define DENSITY   0.01

◆ MINDENSITY

#define MINDENSITY   0.0004

◆ NBTRIES

#define NBTRIES   10

◆ REPEAT

#define REPEAT   1

◆ SIZE

#define SIZE   10000

Typedef Documentation

◆ EigenSparseTriMatrix

typedef SparseMatrix<Scalar, UpperTriangular> EigenSparseTriMatrix

◆ EigenSparseTriMatrixRow

typedef SparseMatrix<Scalar, RowMajorBit | UpperTriangular> EigenSparseTriMatrixRow

Function Documentation

◆ fillMatrix()

void fillMatrix ( float  density,
int  rows,
int  cols,
EigenSparseTriMatrix dst 
)
43  {
44  dst.startFill(rows * cols * density);
45  for (int j = 0; j < cols; j++) {
46  for (int i = 0; i < j; i++) {
47  Scalar v = (internal::random<float>(0, 1) < density) ? internal::random<Scalar>() : 0;
48  if (v != 0) dst.fill(i, j) = v;
49  }
50  dst.fill(j, j) = internal::random<Scalar>();
51  }
52  dst.endFill();
53 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
density
Definition: UniformPSDSelfTest.py:19
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References cols, UniformPSDSelfTest::density, i, j, rows, and v.

Referenced by main().

◆ main()

int main ( int argc  ,
char argv[] 
)
55  {
56  int rows = SIZE;
57  int cols = SIZE;
58  float density = DENSITY;
60 #if 1
63  DenseVector b = DenseVector::Random(cols);
64  DenseVector x = DenseVector::Random(cols);
65 
66  bool densedone = false;
67 
68  for (float density = DENSITY; density >= MINDENSITY; density *= 0.5) {
70  fillMatrix(density, rows, cols, sm1);
71 
72 // dense matrices
73 #ifdef DENSEMATRIX
74  if (!densedone) {
75  densedone = true;
76  std::cout << "Eigen Dense\t" << density * 100 << "%\n";
79  eiToDense(sm1, m1);
80  m2 = m1;
81 
82  BENCH(x = m1.marked<UpperTriangular>().solveTriangular(b);)
83  std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
84  // std::cerr << x.transpose() << "\n";
85 
86  BENCH(x = m2.marked<UpperTriangular>().solveTriangular(b);)
87  std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
88  // std::cerr << x.transpose() << "\n";
89  }
90 #endif
91 
92  // eigen sparse matrices
93  {
94  std::cout << "Eigen sparse\t" << density * 100 << "%\n";
95  EigenSparseTriMatrixRow sm2 = sm1;
96 
97  BENCH(x = sm1.solveTriangular(b);)
98  std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
99  // std::cerr << x.transpose() << "\n";
100 
101  BENCH(x = sm2.solveTriangular(b);)
102  std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
103  // std::cerr << x.transpose() << "\n";
104 
105  // x = b;
106  // BENCH(sm1.inverseProductInPlace(x);)
107  // std::cout << " colmajor^-1 * b:\t" << timer.value() << " (inplace)" << endl;
108  // std::cerr << x.transpose() << "\n";
109  //
110  // x = b;
111  // BENCH(sm2.inverseProductInPlace(x);)
112  // std::cout << " rowmajor^-1 * b:\t" << timer.value() << " (inplace)" << endl;
113  // std::cerr << x.transpose() << "\n";
114  }
115 
116 // CSparse
117 #ifdef CSPARSE
118  {
119  std::cout << "CSparse \t" << density * 100 << "%\n";
120  cs *m1;
121  eiToCSparse(sm1, m1);
122 
123  BENCH(x = b; if (!cs_lsolve(m1, x.data())) {
124  std::cerr << "cs_lsolve failed\n";
125  break;
126  };)
127  std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
128  }
129 #endif
130 
131 // GMM++
132 #ifndef NOGMM
133  {
134  std::cout << "GMM++ sparse\t" << density * 100 << "%\n";
135  GmmSparse m1(rows, cols);
136  gmm::csr_matrix<Scalar> m2;
137  eiToGmm(sm1, m1);
138  gmm::copy(m1, m2);
139  std::vector<Scalar> gmmX(cols), gmmB(cols);
140  Map<Matrix<Scalar, Dynamic, 1> >(&gmmX[0], cols) = x;
141  Map<Matrix<Scalar, Dynamic, 1> >(&gmmB[0], cols) = b;
142 
143  gmmX = gmmB;
144  BENCH(gmm::upper_tri_solve(m1, gmmX, false);)
145  std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
146  // std::cerr << Map<Matrix<Scalar,Dynamic,1> >(&gmmX[0], cols).transpose() << "\n";
147 
148  gmmX = gmmB;
149  BENCH(gmm::upper_tri_solve(m2, gmmX, false);)
150  timer.stop();
151  std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
152  // std::cerr << Map<Matrix<Scalar,Dynamic,1> >(&gmmX[0], cols).transpose() << "\n";
153  }
154 #endif
155 
156 // MTL4
157 #ifndef NOMTL
158  {
159  std::cout << "MTL4\t" << density * 100 << "%\n";
160  MtlSparse m1(rows, cols);
162  eiToMtl(sm1, m1);
163  m2 = m1;
164  mtl::dense_vector<Scalar> x(rows, 1.0);
165  mtl::dense_vector<Scalar> b(rows, 1.0);
166 
167  BENCH(x = mtl::upper_trisolve(m1, b);)
168  std::cout << " colmajor^-1 * b:\t" << timer.value() << endl;
169  // std::cerr << x << "\n";
170 
171  BENCH(x = mtl::upper_trisolve(m2, b);)
172  std::cout << " rowmajor^-1 * b:\t" << timer.value() << endl;
173  // std::cerr << x << "\n";
174  }
175 #endif
176 
177  std::cout << "\n\n";
178  }
179 #endif
180 
181 #if 0
182  // bench small matrices (in-place versus return bye value)
183  {
184  timer.reset();
185  for (int _j=0; _j<10; ++_j) {
186  Matrix4f m = Matrix4f::Random();
187  Vector4f b = Vector4f::Random();
188  Vector4f x = Vector4f::Random();
189  timer.start();
190  for (int _k=0; _k<1000000; ++_k) {
191  b = m.inverseProduct(b);
192  }
193  timer.stop();
194  }
195  std::cout << "4x4 :\t" << timer.value() << endl;
196  }
197 
198  {
199  timer.reset();
200  for (int _j=0; _j<10; ++_j) {
201  Matrix4f m = Matrix4f::Random();
202  Vector4f b = Vector4f::Random();
203  Vector4f x = Vector4f::Random();
204  timer.start();
205  for (int _k=0; _k<1000000; ++_k) {
206  m.inverseProductInPlace(x);
207  }
208  timer.stop();
209  }
210  std::cout << "4x4 IP :\t" << timer.value() << endl;
211  }
212 #endif
213 
214  return 0;
215 }
void eiToGmm(const EigenSparseMatrix &src, GmmSparse &dst)
Definition: BenchSparseUtil.h:64
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::row_major > > MtlSparseRowMajor
Definition: BenchSparseUtil.h:75
void eiToDense(const EigenSparseMatrix &src, DenseMatrix &dst)
Definition: BenchSparseUtil.h:54
mtl::compressed2D< Scalar, mtl::matrix::parameters< mtl::tag::col_major > > MtlSparse
Definition: BenchSparseUtil.h:74
gmm::csc_matrix< Scalar > GmmSparse
Definition: BenchSparseUtil.h:62
void eiToMtl(const EigenSparseMatrix &src, MtlSparse &dst)
Definition: BenchSparseUtil.h:76
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: BenchSparseUtil.h:24
Matrix3d m1
Definition: IOFormat.cpp:2
MatrixType m2(n_dims)
Scalar * b
Definition: benchVecAdd.cpp:17
Definition: BenchTimer.h:55
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:96
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
int * m
Definition: level2_cplx_impl.h:294
squared absolute value
Definition: GlobalFunctions.h:87
list x
Definition: plotDoE.py:28
t
Definition: plotPSD.py:36
double timer
Definition: oomph_metis_from_parmetis_3.1.1/struct.h:210
#define MINDENSITY
Definition: sparse_trisolver.cpp:23
void fillMatrix(float density, int rows, int cols, EigenSparseTriMatrix &dst)
Definition: sparse_trisolver.cpp:43
#define BENCH(X)
Definition: sparse_trisolver.cpp:30
#define SIZE
Definition: sparse_trisolver.cpp:9
#define DENSITY
Definition: sparse_trisolver.cpp:13

References b, BENCH, cols, copy(), UniformPSDSelfTest::density, DENSITY, eiToDense(), eiToGmm(), eiToMtl(), fillMatrix(), m, m1, m2(), MINDENSITY, rows, SIZE, and plotDoE::x.