Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOST uBLAS matrix product extremely slow

Is there a way to improve the boost ublas product performance?

I have two matrices A,B which i want to mulitply/add/sub/...

In MATLAB vs. C++ i get the following times [s] for a 2000x2000 matrix Operations

OPERATION | MATLAB | C++ (MSVC10)
A + B     |  0.04  |  0.04
A - B     |  0.04  |  0.04
AB        |  1.0   | 62.66
A'B'      |  1.0   | 54.35

Why there is such a huge performance loss here?

The matrices are only real doubles. But i also need positive definites,symmetric,rectangular products.

EDIT: The code is trivial

matrix<double> A( 2000 , 2000 );
// Fill Matrix A
matrix<double> B = A;

C = A + B;
D = A - B;
E = prod(A,B);
F = prod(trans(A),trans(B));

EDIT 2: The results are mean values of 10 trys. The stddev was less than 0.005

I would expect an factor 2-3 maybe to but not 50 (!)

EDIT 3: Everything was benched in Release ( NDEBUG/MOVE_SEMANTICS/.. ) mode.

EDIT 4: Preallocated Matrices for the product results did not affect the runtime.

like image 529
8472 Avatar asked Feb 22 '23 16:02

8472


2 Answers

Post your C+ code for advice on any possible optimizations.

You should be aware however that Matlab is highly specialized for its designed task, and you are unlikely to be able to match it using Boost. On the other hand - Boost is free, while Matlab decidedly not.

I believe that best Boost performance can be had by binding the uBlas code to an underlying LAPACK implementation.

like image 69
Steve Townsend Avatar answered Mar 03 '23 20:03

Steve Townsend


You should use noalias in the left hand side of matrix multiplications in order to get rid of unnecessary copies.

Instead of E = prod(A,B); use noalias(E) = prod(A,b);

From documentation:

If you know for sure that the left hand expression and the right hand expression have no common storage, then assignment has no aliasing. A more efficient assignment can be specified in this case: noalias(C) = prod(A, B); This avoids the creation of a temporary matrix that is required in a normal assignment. 'noalias' assignment requires that the left and right hand side be size conformant.

like image 44
tunc Avatar answered Mar 03 '23 20:03

tunc