【发布时间】:2020-05-20 23:24:49
【问题描述】:
最初是 Windows 用户,我通过 VirtualBox 使用 Ubuntu 操作系统,并在 Ubuntu 操作系统上安装了 GCC 9.30。我在终端中使用以下命令安装了 Armadillo、BLAS 和 LAPACK:
sudo apt-get install liblapack-dev
sudo apt-get install libblas-dev
sudo apt-get install libboost-dev
sudo apt-get install libarmadillo-dev
之后我创建了以下 .cpp 文件,该文件测量执行 20 次 500 x 500 矩阵的乘法所花费的时间:
#include <iostream>
#include <armadillo>
#include <chrono>
using namespace std;
int main()
{
chrono::steady_clock sc;
int n = 500;
arma::Mat<double> A = arma::randu(n, n);
arma::Mat<double> B = arma::randu(n, n);
auto start = sc.now(); // start timer
for (int i = 0; i < 20; i++)
{
arma::Mat<double> C = A * B;
}
auto end = sc.now();
auto time_span = static_cast<chrono::duration<double>>(end - start);
cout << "Operation took: " << time_span.count() << " seconds !!!";
return 0;
}
我使用以下命令从 Linux 终端运行文件
g++ armaC.cpp -o armaC -O3 -march=native -fopenmp -larmadillo
结果证明这非常慢,平均为 1.5 秒,而 MATLAB 大约需要 0.08 秒。其实去掉-O3或者-fopenmp这个命令好像根本没有改变速度,这似乎说明我的编译方法出了点问题。
我还尝试在终端中使用以下行运行它,其中应包含 BLAS 和 LAPACK 包:
g++ armaC.cpp -o armaC -llapack -lblas
这给了我以下错误:
有人可以帮我解决这个问题吗?
【问题讨论】:
-
可能是由于“参考”(== 单线程,未优化)BLAS。安装不同的 BLAS 和 LAPACK 对。阿特拉斯可以调整。 OpenBLAS 是多线程的,你真的在比较苹果和橘子,因为 Matlab 附带了 MKL(你也可以安装它:github.com/eddelbuettel/mkl4deb。我发现 OpenBLAS 更可取(小得多,几乎一样快)。
-
犰狳文档中有一个注释,
when using GCC, use of -march=native in conjunction with -fopenmp may lead to speed regressions on recent processors.另外auto不建议在犰狳中使用。
标签: c++ linux ubuntu armadillo