简体   繁体   English

优化Eigen中的大矩阵乘法

[英]Optimize large matrices multiplication in Eigen

I'm doing some large stochastic matrices (at least 1000x1000) calculation in C++, using the Eigen Library, my code consists of the following functions : 我正在使用特征库在C ++中进行一些大型随机矩阵(至少1000x1000)计算,我的代码包含以下函数:

Eigen::VectorXd grid(...); Eigen :: VectorXd网格(...); 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. 使用快速排序算法和ran1算法初始化(逐个元素)对数正态分布值的排序向量,假设大小为N,则所有矩阵的大小为NxN。

Eigen::VectorXd conditionGrid(...); Eigen :: VectorXd conditionGrid(...); a loop that returns a vector containing the grid vector minus a time dependent value. 返回包含网格向量减去时间相关值的向量的循环。

Eigen::MatrixXd xjkMatrix(...); Eigen :: MatrixXd xjkMatrix(...); matrix constructed from the two vectors through a loop. 通过循环从两个向量构造的矩阵。

Eigen::MatrixXd qzMatrix(...); Eigen :: MatrixXd qzMatrix(...); uses the xjk matrix to construct a new one using a probability density function, element by element. 使用xjk矩阵使用概率密度函数逐元素地构造一个新矩阵。

Eigen::MatrixXd q1zMatrix(...); Eigen :: MatrixXd q1zMatrix(...); uses the xjk matrix too, to construct a new one using a probability density function, element by element. 也使用xjk矩阵,使用概率密度函数逐元素地构造一个新的矩阵。

Eigen::MatrixXd qjkMatrix(...); Eigen :: MatrixXd qjkMatrix(...); combination of the qz and the grid, element by element loop. qz和网格的组合,逐个元素循环。

Eigen::MatrixXd q1jkMatrix(...); Eigen :: MatrixXd q1jkMatrix(...); combination of the qz, q1z and the grid, element by element loop. qz,q1z和网格的组合,逐个元素循环。

Eigen::MatrixXd mjkMatrix(...); Eigen :: MatrixXd mjkMatrix(...); sums elements from qjk and q1jk, element by element loop. qjk和q1jk中的元素,逐个元素循环。

Eigen::MatrixXd globalMatrix(...); 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. 循环所有上述函数(一般120次),除了固定的网格,并乘以120 mjk矩阵以获得全局矩阵。

A global matrix 200x200 takes about 20 seconds of calculations, 500x500 is about 200 seconds. 全局矩阵200x200需要大约20秒的计算,500x500大约需要200秒。 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. 我正在使用MinGW64。

Make sure you compile with full optimizations, eg g++ -O3 -DEIGEN_NO_DEBUG if using g++. 确保使用完全优化进行编译,例如g++ -O3 -DEIGEN_NO_DEBUG如果使用g ++)。 Also, turning on parallelization via OpenMP may help, use -fopenmp when compiling and linking. 此外,通过OpenMP启用并行化可能会有所帮助,在编译和链接时使用-fopenmp

I use the following to compile most Eigen code (with g++): 我使用以下代码编译大多数Eigen代码(使用g ++):

g++ -I/path/to/eigen -O3 -DEIGEN_NO_DEBUG -fopenmp program.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM