## Matrix multiplication VERY slow...

rachelp
Registered Member
Posts
2
Karma
0

### Matrix multiplication VERY slow...

Tue Jul 16, 2013 7:40 pm
Hi,

I've only been working with Eigen for a couple weeks and I'm also fairly new to coding in general. I'm working on a program which will use fairly large matrices and vectors (anywhere from 50 to 1000+ rows and columns). Most of my code runs very fast but the the small portion of my code that I've copied below runs very slowly. When the matrices are small ( < 15 x 15) the multiplication takes about a minute or two to compute, but when the matrices are larger ( > 40 x 40) it takes an hour if not much, much longer to compute.

float b_eta;
MatrixXd I, K, M, tempmat;
VectorXd eta;

tempmat = eta.transpose() * (I.setIdentity() - K * M * K.transpose()) * eta;
float b_eta_p = b_eta + 0.5 * tempmat(0,0);

The above I found is just slightly faster than:
float b_eta_p = b_eta + 0.5 * eta.transpose() * (I.setIdentity() - K * M * K.transpose()) * eta;

Is there a better way I can write out this multiplication so that it computes faster? Any suggestions would be appreciated.
ggael
Moderator
Posts
2689
Karma
16
OS

### Re: Matrix multiplication VERY slow...

Tue Jul 16, 2013 9:19 pm
How many times are you executing this expression? Even for 1000^2 matrices it should take less than a second. Make sure optimizations are on (e.g., -O2). Which compiler? Which CPU?

Finally you can significantly reduce the number of operations by performing matrix-vector products only, instead of matrix-matrix products, e.g.:

VectorXf tmp = K.transpose() * eta;
b_eta_p = b_eta + 0.5*(eta.squaredNorm() - tmp.dot(M*tmp));
rachelp
Registered Member
Posts
2
Karma
0

### Re: Matrix multiplication VERY slow...

Thu Jul 18, 2013 2:13 pm
ggael wrote:How many times are you executing this expression? Even for 1000^2 matrices it should take less than a second. Make sure optimizations are on (e.g., -O2). Which compiler? Which CPU?

Finally you can significantly reduce the number of operations by performing matrix-vector products only, instead of matrix-matrix products, e.g.:

VectorXf tmp = K.transpose() * eta;
b_eta_p = b_eta + 0.5*(eta.squaredNorm() - tmp.dot(M*tmp));

Thanks very much for your help! The expression is only executed once, and optimizations were on. Your suggestion of performing only matrix-vector products helped a lot though and it now runs much much much faster.

## Who is online

Registered users: ABravo, Baidu [Spider], Bing [Bot], davidemme, Exabot [Bot], Fabian L., Google [Bot], google01103, jdh8, jmacleod, joaob, koriun, Majestic-12 [Bot], Mamarok, metzman, mmolch, nylnook, pedrorodriguez, rv8ter, smankowski, TheraHedwig, weaktransform, wolfi323, Xiceph, Yahoo [Bot]