I have an Eigen/C++ code the purpose of which is to perform row major sparse-dense and dense-col major sparse matrix multiplication, both of which are multithreaded in Eigen.
However, I found that only the row major sparse - dense multiplication is scaling, but not the dense-col major sparse multiplication. Why is this? Below is the code and timings.
/*timer function*/
double getHighResolutionTime(void) {
struct timeval tod;
gettimeofday(&tod, NULL);
double time_seconds = (double) tod.tv_sec + ((double) tod.tv_usec / 1000000.0);
return time_seconds;
}
...
//define Col Major Sparse
Map<SparseMatrix<double,ColMajor> > gcol (m, n, nz, jc_int, ir_int, pr);
//define the same matrix but Row Major
Map<SparseMatrix<double,RowMajor> > grow (m, n, nz, jc_int, ir_int, pr);
//define dense matrix
Map<MatrixXd> G (PR1, M, N );
//define result
Map<MatrixXd> result (PR2, M, N);
//row major sparse - dense product
double tic=getHighResolutionTime();
result=grow*G;
double toc=getHighResolutionTime();
printf("\nsparse-dense time: %f seconds", (toc - tic));
//dense - col major sparse product
tic=getHighResolutionTime();
result=G*gcol;
toc=getHighResolutionTime();
printf("\ndense-sparse time: %f seconds\n", (toc - tic));
Output and timings using 1,2,4,8, and 16 threads (on a 16 core machine). Only sparse-dense scales, not dense-sparse.
Using 1 threads...
sparse-dense time: 5.184886 seconds
dense-sparse time: 3.278560 seconds
Using 2 threads...
sparse-dense time: 2.808550 seconds
dense-sparse time: 3.275191 seconds
Using 4 threads...
sparse-dense time: 1.589596 seconds
dense-sparse time: 3.278983 seconds
Using 8 threads...
sparse-dense time: 1.005600 seconds
dense-sparse time: 3.279466 seconds
Using 16 threads...
sparse-dense time: 0.736803 seconds
dense-sparse time: 3.278893 seconds
Additional info: The matrices are 7000x7000 random,real, and double. The sparse matrix is random with a 1% density. The numerical result of both multiplications is correct. I am compiling with the following flags:
-fomit-frame-pointer -O3 -DNDEBUG -fopenmp -march=native -fPIC
Edit:
The answer by ggael works great. Here are the new scalings:
Using 1 threads...
sparse-dense time: 5.070809 seconds
dense-sparse time: 3.270347 seconds
Using 2 threads...
sparse-dense time: 2.786790 seconds
dense-sparse time: 2.070378 seconds
Using 4 threads...
sparse-dense time: 1.580925 seconds
dense-sparse time: 1.243466 seconds
Using 8 threads...
sparse-dense time: 1.000152 seconds
dense-sparse time: 0.887953 seconds
Using 16 threads...
sparse-dense time: 0.898228 seconds
dense-sparse time: 0.909603 seconds