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

SVD and access violation

Tags: None
(comma "," separated)
chef_seppel
Registered Member
Posts
5
Karma
0

SVD and access violation

Wed Sep 22, 2010 1:56 pm
Hi,

I got a problem with the Eigen 3.0-beta1 SVD Module. Im using it to solve a Problem with a few thousand equations. Sometimes I get "access violation at address ..." (I hope its the correct translation :). The real Problem is the "sometimes". In 3 out of 10 tries i get the error, and the other tries work perfectly.

The Error occurs in the Line 471 in the SVD.h while executing
"Scalar si = dec().singularValues().coeff(i);"

I tested and debugged the last two days and im nearly sure that i did not broke anything in the memory :)

My programm is a little to big to show it here, but i made a little example where i get the same error:

Code: Select all
    Eigen::MatrixXd aa;    aa = Eigen::MatrixXd::Random(7000, 5);
    Eigen::VectorXd xc;    xc = Eigen::VectorXd::Random(7000);   

    Eigen::SVD<Eigen::MatrixXd> A(aa);   
    Eigen::MatrixXd res= A.solve(xc);


I know that 7000 is a big number, but i do not get a bad alloc. Bad_allocs occur with numbers bigger 10000 (at my machine), which is absolutely logical :)

With Eigen 2.0 everything works good... except the fact that the results are not exact enough :(

edit: Now i happens already with 500 instead of 7000.... and i definitely have enough memory for this :)

Any hints?

greets
chef_seppel
Hauke
Registered Member
Posts
109
Karma
3
OS

Re: SVD and access violation

Thu Sep 23, 2010 7:37 am
Hi,

we need more detail in order to help you. Right now, I am unable to reproduce your error on my machine (vs2010, 64bit build, debug+release) with this code

Code: Select all
#include <Eigen/Core>

using namespace Eigen;

int main(int argc, char* argv[])
{
  for (int i=0; i<100; ++i)
  {
    Eigen::MatrixXd aa;    aa = Eigen::MatrixXd::Random(500, 5);
    Eigen::VectorXd xc;    xc = Eigen::VectorXd::Random(500);

    Eigen::SVD<Eigen::MatrixXd> A(aa);   
    Eigen::MatrixXd res= A.solve(xc);
  }
  return 0;
}


In order to be able to help you, we need a self-contained example (i.e. I can take it and compile it) that shows the problem and information regarding your build environment (compiler version, are you building 32 or 64 bit version, is vectorization enabled, etc).

Typically I would say, such random behavior is most likely being caused by a bad memory access but since you say Eigen 2 is working I am a bit surprised.

Regards,
Hauke
chef_seppel
Registered Member
Posts
5
Karma
0

Re: SVD and access violation

Thu Sep 23, 2010 8:31 am
Thanks for the reply, here are more informations:
I'm using Windows7, Visual Studio 2008 on a Intel Core 2Quad CPU @ 2.66GHz with a 32-bit Operating System.
I'm not sure with the Vectorization.. but i did not change anything, so it should be the default.

To reproduct the error: Visual Studio-> New Project-> Win32-Console Application and everything on default. Then add the Eigen-directory to the include-Directories and replace everything in the .cpp with this:
Code: Select all
#include "stdafx.h"

#include <Eigen/Core>
#include <Eigen/SVD>

using namespace Eigen;

int main(int argc, char* argv[])
{   
    Eigen::MatrixXd aa;    aa = Eigen::MatrixXd::Random(500, 5);
    Eigen::VectorXd xc;    xc = Eigen::VectorXd::Random(500);

    Eigen::SVD<Eigen::MatrixXd> A(aa);   
    Eigen::MatrixXd res= A.solve(xc);
   
    printf("%f", res(0,0));
    std::getchar();   
    return 0;
}
}


I just noticed that it works in release mode but crashes in the Debug Mode at the above mentioned point.

but since you say Eigen 2 is working I am a bit surprised.


Me two :)

Greets
chef_seppel
Hauke
Registered Member
Posts
109
Karma
3
OS

Re: SVD and access violation

Thu Sep 23, 2010 1:03 pm
We can reproduce the error and its being worked on.

Thanks for pointing this out!

- Hauke
chef_seppel
Registered Member
Posts
5
Karma
0

Re: SVD and access violation

Thu Sep 23, 2010 2:01 pm
We can reproduce the error and its being worked on.


Thats great!

Thanks for pointing this out!


Thank you for all your work!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: SVD and access violation

Thu Sep 23, 2010 2:02 pm
It turns out that the reason why your latter program is failing is that there actually IS NO SOLUTION to your equation. Your 500-dimensional random RHS vector just isn't in the linear span of the 5 columns of your matrix. Our documentation is pretty clear that in this case the result is undefined.

Pushed Hauke's fix to your access violation error, plus an improved unit test and an assertion that rows >= cols.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
chef_seppel
Registered Member
Posts
5
Karma
0

Re: SVD and access violation

Fri Sep 24, 2010 7:42 am
It turns out that the reason why your latter program is failing is that there actually IS NO SOLUTION to your equation. Your 500-dimensional random RHS vector just isn't in the linear span of the 5 columns of your matrix.


Hmm... I do not understand the problem. I got 500 Equations and only 5 parameters to optimize... why shouldn't that be possible. (Except the fact that it's all random and the results don't mean anything). I got a highly overdetermined equation system (i hope the translation is right :), which should be solvable.

Our documentation is pretty clear that in this case the result is undefined.


Where exactly? I don't find the right part.

Thanks again for everything :)
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: SVD and access violation

Fri Sep 24, 2010 11:14 am
chef_seppel wrote:Hmm... I do not understand the problem. I got 500 Equations and only 5 parameters to optimize... why shouldn't that be possible. (Except the fact that it's all random and the results don't mean anything). I got a highly overdetermined equation system (i hope the translation is right :), which should be solvable.


I was talking about the example we discussed in this thread. Sure you can see it as an overdetermined system, but since the coeffs are *random*, the solution you'll get won't have any sort of significance. Yes, if you apply this to a real-world overdetermined system (i.e. when a good approximate solution is meant to exist) you'll get your solution, because SVD::solve is implicitly doing least squares.

Important: for this kind of very-rectangular matrices, you will get much, much better results using JacobiSVD than SVD (in Eigen 3).

You'll get even better results if you let Eigen know at compile time that the number of columns is 5, by using this matrix type:

Matrix<double, Dynamic, 5>

instead of MatrixXd.


Where exactly? I don't find the right part.


i was thinking about
http://eigen.tuxfamily.org/dox-devel/cl ... 028fa45b4d

but it's true that in the case of SVD, it is implicitly doing least-squares, so you can ignore what I said. It's just that in the example here, with random coeffs, least squares doesn't make much sense.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
chef_seppel
Registered Member
Posts
5
Karma
0

Re: SVD and access violation

Fri Sep 24, 2010 1:51 pm
Important: for this kind of very-rectangular matrices, you will get much, much better results using JacobiSVD than SVD (in Eigen 3).


but JacobiSVD doesn't have a solve-function?

You'll get even better results if you let Eigen know at compile time that the number of columns is 5, by using this matrix type:

Matrix<double, Dynamic, 5>

instead of MatrixXd.


Good to know, but in my problem not possible :)

Thanks again
chef_seppel
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: SVD and access violation

Fri Sep 24, 2010 1:56 pm
chef_seppel wrote:but JacobiSVD doesn't have a solve-function?


True. But it's so easy to implement!

Notice that you can implement least squares using only V, not U, which will allow you to go much faster. (Dont remember the details but it's in the wikipedia page on least squares).


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

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