This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Summation slower than multiplication

Tags: None
(comma "," separated)
klemensimonic
Registered Member
Posts
3
Karma
0

Summation slower than multiplication

Tue Sep 04, 2012 10:18 pm
Bellow is the program and the problem:

typedef Eigen::SparseMatrix<double> SparseMatrix;
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrix;

SparseMatrix X;
DenseMatrix C;

// At some point in code I need to calculate the following for some integers i from a given set:
C * X.col(i)

//At the same time, I need to calculate the following for the same integers i. Integer index is fixed for all integers i:
C.row(index) += X.col(i).transpose();

// In other words, the first time we multiply a dense matrix with a given sparse columns,
// while the second time, we simply sum the same given sparse columns into a single dense vector.

Why is summing (in my experiments) 4 times slower than multiplication? Should not it be faster?

The size of matrices in my experiments are:
X : cols = 2.2 Million
X : rows = 1300
C : cols = 1300
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, this is because dense += sparse is currently treated as "dense = anotherdense + sparse" and so this is O(C.cols()) operation while it should be a O(X.col(i).nonZeros()) op.
klemensimonic
Registered Member
Posts
3
Karma
0
Great, thanks a lot.
What would be then the "fastest" way to sum a given set of sparse vectors into a single dense vector?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
in the meantime:

for(SparseMatrixType::InnerIterator it(X,i); it; ++it) C(i,it.index()) += it.value();


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], Yahoo [Bot]