[英]Matrix multiplication in c++ armadillo is very slow
I'm doing some basic multiplication using armadillo but for some reason it takes very long to complete.我正在使用犰狳做一些基本的乘法,但由于某种原因,它需要很长时间才能完成。 I'm quite new to c++ so I might be doing something wrong, but I can't see it even in this very basic example:
我对 c++ 很陌生,所以我可能做错了什么,但即使在这个非常基本的例子中我也看不到它:
#include <armadillo>
#include <iostream>
using namespace arma;
int main(){
arma::vec coefficients = {1.0, 1.09, 1.08};
arma::mat X = arma::mat(100000, 3, fill::randu) * coefficients;
cout << X.n_cols;
}
when I mean very slow, I have run this example for some minutes and it doesn't finish当我的意思是非常慢时,我已经运行了这个例子几分钟但它没有完成
I run the script with perf stat./main
, but stopped it after some time because it shouldn't take that long.我用
perf stat./main
运行脚本,但一段时间后停止了它,因为它不应该花那么长时间。 This is the output.这是 output。
^C./main: Interrupt
Performance counter stats for './main':
257,169.20 msec task-clock # 1.003 CPUs utilized
3,342 context-switches # 12.995 /sec
215 cpu-migrations # 0.836 /sec
1,312 page-faults # 5.102 /sec
963,025,520,077 cycles # 3.745 GHz
542,959,361,927 instructions # 0.56 insn per cycle
113,002,342,332 branches # 439.409 M/sec
1,095,168,312 branch-misses # 0.97% of all branches
256.349026907 seconds time elapsed
147.860947000 seconds user
109.317743000 seconds sys
Armadillo is a template-based library that can be used as a header-only library. Armadillo 是一个基于模板的库,可用作仅头文件库。 Just include its header and make sure you link with some BLAS and LAPACK implementation.
只需包括其 header 并确保您链接到一些 BLAS 和 LAPACK 实现。 When used like this, armadillo assumes you have a BLAS and LAPACK implementation available .
当像这样使用时,犰狳假设你有一个可用的 BLAS 和 LAPACK 实现。 You will get link errors if you try to use any functionality in armadillo that requires them without linking with them.
如果您尝试在犰狳中使用任何需要它们而不与它们链接的功能,您将收到链接错误。 If you don't have BLAS and/or LAPACK, you can change the
armadillo_bits/config.hpp
file and comment out some defines there such that armadillo uses its own (slower) implementation of that functionality.如果您没有 BLAS 和/或 LAPACK,您可以更改
armadillo_bits/config.hpp
文件并注释掉其中的一些定义,以便 armadillo 使用其自己的(较慢)该功能的实现。
Alternatively, armadillo can be compiled as a wrapper library, where in that case you just link with the "armadillo" wrapper library.或者,可以将犰狳编译为包装库,在这种情况下,您只需链接“犰狳”包装库。 It's CMake code will try to determine during configure time what you have available and "comment-out the appropriated defines" in case you don't have some requirement available, which in turn will make it use the slower implementation.
它的 CMake 代码将尝试在配置期间确定您可用的内容并“注释掉适当的定义”以防您没有可用的要求,这反过来又会使其使用较慢的实现。 That "configure" code is wrongly determining that you don't have BLAS available, since BLAS is the one providing fast matrix multiplication.
该“配置”代码错误地确定您没有可用的 BLAS,因为 BLAS 是提供快速矩阵乘法的代码。
My suggestion is to just make sure you have BLAS and LAPACK installed and use armadillo as a header-only library, making sure to link your program with BLAS and LAPACK.我的建议是确保您安装了 BLAS 和 LAPACK,并将犰狳用作仅头文件库,确保将您的程序与 BLAS 和 LAPACK 链接。
Another option is using the conan package manager to install armadillo.另一种选择是使用柯南package管理器安装犰狳。 Conan added a recipe to install armadillo recently.
柯南最近添加了一个安装犰狳的配方。 It has the advantage that it will install everything that armadillo needs (it installs openblas, which provides both a BLAS and LAPACK implementation) and it is system agnostic (similar to virtual environments in Python).
它的优点是它将安装犰狳所需的一切(它安装了 openblas,它提供了 BLAS 和 LAPACK 实现)并且它与系统无关(类似于 Python 中的虚拟环境)。
In the comments you mentioned that it worked with g++ main.cpp -o main -DARMA_DONT_USE_WRAPPER -larmadillo -llapack
.在您提到的评论中,它与
g++ main.cpp -o main -DARMA_DONT_USE_WRAPPER -larmadillo -llapack
。 The reason is that even if you installed the wrapper library, if you define ARMA_DONT_USE_WRAPPER
you are effectivelly using armadillo as a header-only library.原因是即使你安装了包装库,如果你定义
ARMA_DONT_USE_WRAPPER
,你实际上是在使用犰狳作为一个只有头文件的库。 You can replace -larmadillo -llapack
with -lblas -llapack
.您可以将
-larmadillo -llapack
替换为-lblas -llapack
。
Why not use Julia or ruby instead?为什么不使用 Julia 或 ruby 代替?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.