简体   繁体   English

C ++中的慢矩阵求逆

[英]Slow matrix inversion in C++

I'm currently trying to convert matlab code to C++ using armadillo. 我目前正在尝试使用犰狳将Matlab代码转换为C ++。 I converted some matlab code by following the aramdillo documentation to C++. 我按照aramdillo文档将某些matlab代码转换为C ++。 However the performance is disappointing compared to matlab. 但是,与matlab相比,性能令人失望。

In Matlab it takes about 0.1 sec to inverse a Matrix A of size (625x625) compared to over 3 seconds in C++. 在Matlab中,尺寸为625x625的矩阵A的求逆大约需要0.1秒,而在C ++中则需要3秒以上。

In C++ I have tried both 在C ++中,我都尝试过

solve()

as well as 以及

inv() 

I'm aware of the fact that inv produces less accurate results, thus I do not prefer to use it. 我知道inv产生的结果不太准确的事实,因此我不喜欢使用它。 Besides I really need the inverse of matrix A, as I use the diagonal elements later in the algorithm. 此外,我确实需要矩阵A的逆,因为我稍后将在算法中使用对角线元素。

The code that's producing these results: 产生这些结果的代码:

Matlab Matlab的

x=A\b
invA = A\eye(size(A))

C++ C ++

arma::mat x = solve(A,b)
arma::mat invA = solve(A,eye(625,625))

The versions I'm using: 我使用的版本:

C++: C ++:

  • Visual Studio 2013 Visual Studio 2013

  • Armadillo 8.300.1 犰狳8.300.1

  • Intel MKL 2018.1.156 英特尔MKL 2018.1.156

Matlab: Matlab:

  • matlab 2016b Matlab 2016b

  • version -blas 版本-blas

Intel(R) Math Kernel Library Version 11.3.1 Product Build 20151021 for Intel(R) 64 architecture applications, CNR branch AVX2 适用于Intel(R)64架构应用程序的Intel(R)数学内核库版本11.3.1产品内部版本20151021,CNR分支AVX2

  • version -lapack 版本-lapack

Intel(R) Math Kernel Library Version 11.3.1 Product Build 20151021 for Intel(R) 64 architecture applications, CNR branch AVX2 Linear Algebra PACKage Version 3.5.0 英特尔(R)数学内核库11.3.1版,适用于英特尔(R)64架构应用的产品版本20151021,CNR分支AVX2线性代数软件包版本3.5.0

Does anyone have an idea how to overcome this lack of speed in C++ using armadillo? 有谁知道如何使用犰狳克服C ++中这种速度不足的问题?

Have you tried : add to your code #ARMA_USE_LAPACK ? 您是否尝试过:将代码添加到#ARMA_USE_LAPACK中? This allows your code to use more optimized version of .inv() function. 这使您的代码可以使用.inv()函数的更优化版本。 Also check the documentation: http://arma.sourceforge.net/docs.html 还要检查文档: http : //arma.sourceforge.net/docs.html

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

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