Search code examples
c++eigen

Eigen Library to Produce M by N matrix of standard normals


I am using the Eigen libary. And I created a matrix Z that of size M by N that contains random standard normals. Everything seems fine and dandy but when I increase M to 100,000 and N to 10,000 then my program breaks for no apparent reason.

To illustrate suppose M = 4 and N = 2 then I will have

 0.106358 -0.894365
 0.452166 -0.619815
 0.554152  0.257035
 0.654476  -1.26393

Which is what we expected. Now when I set M = 10000 and N = 1000, everything works fine as well. But as soon as I set M = 100000 and N = 10000, then I get this message on Eclipse

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I have had this happen to me before but when I restarted my computer, everything was okay. But for some reason I cannot seem to fix this issue. Any suggestions are greatly appreciated.

Here is my code:

MatrixXd Z(M,N);
    random_device rd;
    mt19937 e2(time(0));
    normal_distribution<double> dist(0.0, 1.0);
    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++){
            Z(i,j) = dist(e2);
        }
    }

Also, when I comment everything out except when I set MatrixXd Z(M,N) my program still crashes.


Solution

  • Matrix of doubles with size 10000x100000 will have a size:

    8*10000*100000 = 8 000 000 000 bytes
    

    That is far more than you can allocate on stack (1 MB by default in MSVC) and more that 4 GB that 32 bit program can allocate at all.

    So use heap allocation and compile your program in 64bit configuration.

    EDIT Yes, you also need a proper amount of RAM to make it run with a reasonable speed.