1

I'm doing some large stochastic matrices (at least 1000x1000) calculation in C++, using the Eigen Library, my code consists of the following functions :

Eigen::VectorXd grid(...); initializes (element by element) a sorted vector of log-normally distributed values, using the quicksort algorithm and the ran1 algorithm, let's say of size N, all matrices are then of size NxN.

Eigen::VectorXd conditionGrid(...); a loop that returns a vector containing the grid vector minus a time dependent value.

Eigen::MatrixXd xjkMatrix(...); matrix constructed from the two vectors through a loop.

Eigen::MatrixXd qzMatrix(...); uses the xjk matrix to construct a new one using a probability density function, element by element.

Eigen::MatrixXd q1zMatrix(...); uses the xjk matrix too, to construct a new one using a probability density function, element by element.

Eigen::MatrixXd qjkMatrix(...); combination of the qz and the grid, element by element loop.

Eigen::MatrixXd q1jkMatrix(...); combination of the qz, q1z and the grid, element by element loop.

Eigen::MatrixXd mjkMatrix(...); sums elements from qjk and q1jk, element by element loop.

Eigen::MatrixXd globalMatrix(...); loops all the above functions (120 times generally) except the grid, which is fixed, and multiplies the 120 mjk matrices to obtain a global one.

A global matrix 200x200 takes about 20 seconds of calculations, 500x500 is about 200 seconds. How can I make my code run faster, and optimize my matrix multiplications ? (I have tried sparse matrices, but it took even longer).

I'm using MinGW64.

Naucle
  • 626
  • 12
  • 28

1 Answers1

5

Make sure you compile with full optimizations, e.g. g++ -O3 -DEIGEN_NO_DEBUG if using g++. Also, turning on parallelization via OpenMP may help, use -fopenmp when compiling and linking.

I use the following to compile most Eigen code (with g++):

g++ -I/path/to/eigen -O3 -DEIGEN_NO_DEBUG -fopenmp program.cpp
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • For the record, doing 120 multiplication of matrices of size 500^2 takes 0.5 second with the devel branch of Eigen without even using multithreading (2.6GHz core i7) – ggael Mar 05 '15 at 09:25
  • I was actually compiling my code as a single .cpp file (in codeblocks), then, to try your compiler options, i created a projet and added the .cpp file. Curiously, compiling the code as a projet made it x10 times faster, and I have no idea how or why (I'm a beginner), the compiler options didn't make much difference. It's still not fast enough, but it is a good start. Any more recommendations ? – Naucle Mar 05 '15 at 11:41
  • @ggael Can you please elaborate ? What's the devel branch of Eigen ? – Naucle Mar 05 '15 at 11:47
  • @Naucle, the devel branch of Eigen is the development branch, i.e. the next to be version. When you compile your file as a project, the compiler probably inserts some optimization flags for you. Try compiling in Release mode. Also, I would try just compiling the file from the command line with the flags I told you. – vsoftco Mar 05 '15 at 13:54
  • @vsoftco I did actually, and I got run duration similar to the compiled project, but not with -fopenmp. This option compiles in command line but give longer run duration, and it brings out an error in codeblocks. – Naucle Mar 05 '15 at 15:49
  • @Naucle can you provide also the `example.txt` file that you use? I am curios how long it takes on my machine. – vsoftco Mar 05 '15 at 16:05
  • @vsoftco I'm not using a exemple.txt, the algorithm generates it. In this case, it is a 500x500 matrix generated in 19 seconds, the txt is about 9mo big. Here is a 100x100 matrix exemple : http://pastebin.com/hCw1fKCm EDIT : You can try different matrix sizes by changing the first argument of globalMatrix(...) function in the main. – Naucle Mar 05 '15 at 16:19
  • @Naucle it runs on my end in about 6 seconds, for your original code (500 x 500 matrix). iMac core i5, 8GB RAM – vsoftco Mar 05 '15 at 16:34
  • @vsoftco Sony Vaio i5, 4GB RAM and MinGW x64-4.9.2 here, what would the problem be ? – Naucle Mar 05 '15 at 16:41
  • @Naucle not sure, it shouldn't take so much time. – vsoftco Mar 05 '15 at 17:25