// g++ -O3 -DNDEBUG sparse_01.cpp -I .. -o sparse_01 && ./sparse_01 #include #include #include #include "gmm/gmm.h" using namespace std; using namespace Eigen; USING_PART_OF_NAMESPACE_EIGEN #ifndef REPEAT #define REPEAT 10 #endif #define REPEATPRODUCT 1 #define SIZE 10 #define DENSITY 0.2 // #define NODENSEMATRIX typedef MatrixXf DenseMatrix; // typedef Matrix DenseMatrix; typedef SparseMatrix EigenSparseMatrix; typedef gmm::csc_matrix GmmSparse; typedef gmm::col_matrix< gmm::wsvector > GmmDynSparse; void fillMatrix(float density, int rows, int cols, DenseMatrix* pDenseMatrix, EigenSparseMatrix* pSparseMatrix, GmmSparse* pGmmMatrix=0) { GmmDynSparse gmmT(rows, cols); if (pSparseMatrix) pSparseMatrix->startFill(rows*cols*density); for(int j = 0; j < cols; j++) { for(int i = 0; i < rows; i++) { float v = (ei_random(0,1) < density) ? ei_random() : 0; if (pDenseMatrix) (*pDenseMatrix)(i,j) = v; if (v!=0) { if (pSparseMatrix) pSparseMatrix->fill(i,j) = v; if (pGmmMatrix) gmmT(i,j) = v; } } } if (pSparseMatrix) pSparseMatrix->endFill(); if (pGmmMatrix) gmm::copy(gmmT, *pGmmMatrix); } int main(int argc, char *argv[]) { int rows = SIZE; int cols = SIZE; float density = DENSITY; // dense matrices #ifndef NODENSEMATRIX DenseMatrix m1(rows,cols), m2(rows,cols), m3(rows,cols), m4(rows,cols); #endif // sparse matrices EigenSparseMatrix sm1(rows,cols), sm2(rows,cols), sm3(rows,cols), sm4(rows,cols); HashMatrix hm4(rows,cols); // GMM++ matrices GmmDynSparse gmmT4(rows,cols); GmmSparse gmmM1(rows,cols), gmmM2(rows,cols), gmmM3(rows,cols), gmmM4(rows,cols); #ifndef NODENSEMATRIX fillMatrix(density, rows, cols, &m1, &sm1, &gmmM1); fillMatrix(density, rows, cols, &m2, &sm2, &gmmM2); fillMatrix(density, rows, cols, &m3, &sm3, &gmmM3); #else fillMatrix(density, rows, cols, 0, &sm1, &gmmM1); fillMatrix(density, rows, cols, 0, &sm2, &gmmM2); fillMatrix(density, rows, cols, 0, &sm3, &gmmM3); #endif BenchTimer timer; //-------------------------------------------------------------------------------- // COEFF WISE OPERATORS //-------------------------------------------------------------------------------- #if 1 std::cout << "\n\n\"m4 = m1 + m2 + 2 * m3\":\n\n"; timer.reset(); timer.start(); asm("#begin"); for (int k=0; k lm5(rows, cols); lm5 = lm4; lm5 = sm4; cout << endl << lm5 << endl; sm3 = sm4.transpose(); cout << endl << lm5 << endl; cout << endl << "SM1 before random editing: " << endl << sm1 << endl; { SparseSetter w1(sm1); w1->coeffRef(4,2) = ei_random(); w1->coeffRef(2,6) = ei_random(); w1->coeffRef(0,4) = ei_random(); w1->coeffRef(9,3) = ei_random(); } cout << endl << "SM1 after random editing: " << endl << sm1 << endl; #endif return 0; }