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

svd computation in Eigen3

Tags: None
(comma "," separated)
shibainu
Registered Member
Posts
2
Karma
0

svd computation in Eigen3

Thu Sep 22, 2011 9:23 am
Hello, I am a new user of Eigen. Previously, I code in Matlab and now I want to port my code to C/C++. I am actually looking for a good library for computing extensive linear algebra in C/C++ and interested in Eigen.
I have just downloaded Eigen3 (ver3.0.2) and did a small test of the computation speed of SVD function for matrix with data type double and size 225x225. However, the computation speed is not satisfying. I am wondering if there is a mistake in my code since I haven't read the documentation of Eigen3 completely. Below are the code and the comparison if I am using built-in svd function from OpenCV:

Code: Select all
#include <iostream>
#include <eigen/Dense>

using namespace Eigen;
int main(void)
{
    clock_t start;
    MatrixXd m1 = MatrixXd::Random(225,225);
    JacobiSVD<MatrixXd> svd;
    start = clock();
    svd.compute(m1);
    double speed = (double)(clock() - start)/CLOCKS_PER_SEC;
    std::cout << "speed is " << speed  << "seconds" << std::endl ;
    return 0;
}

The output:
Code: Select all
speed is 4.59441seconds


Compared with the OpenCV svd:
Code: Select all
#include "opencv2/core/core.hpp"
#include <iostream>

using namespace cv;

int main(void)
{
    clock_t start;
    Mat m1(225,225, CV_64F);
    cv::Mat w,u,v;
    randn(m1, Scalar::all(1000), Scalar::all(1000));
    start = clock();
    SVD::compute(m1, w,u,v);
    double speed = (double)(clock()-start)/CLOCKS_PER_SEC;
    std::cout << "speed is " << speed  << "seconds" << std::endl ;
    return 0;
}

The Output:
Code: Select all
speed is 0.147352seconds


The compiler:
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)

Any comment or suggestion? Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: svd computation in Eigen3

Thu Sep 22, 2011 9:42 am
Eigen's SVD implements the Jacobi method which is more reliable than more standard implementations based on Householder reflexion. On the other hand it is also slower for large matrices. That said, a factor 30 for a 225^2 matrix does not make sense. Make sure you compiled you program with optimizations -O2.

I also recommend you to switch to macport' gcc 4.5 or 4.6. Default macosx compiler has a lot of issues.
shibainu
Registered Member
Posts
2
Karma
0

Re: svd computation in Eigen3

Thu Sep 22, 2011 10:15 am
Thank you for the reply. With the optimizations -O2, the speed improves alot:
Code: Select all
speed is 0.262015seconds


Bookmarks



Who is online

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