Julia 的特征分解比 Mathematica 慢 5 倍? [英] Eigendecompositions are 5 times slower in Julia than in Mathematica?

查看:18
本文介绍了Julia 的特征分解比 Mathematica 慢 5 倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Julia 的新手,主要在 Mathematica 工作,所以我可能会遇到一些基本错误.我试图计算 Julia 计算随机矩阵的特征系统所花费的时间,发现它比 Mathematica 慢 5-6 倍.

I am new to Julia and primarily work in Mathematica, so I probably have a few elementary mistakes floating around. I attempted to time how long Julia took to compute the eigensystem of a random matrix, and found it was 5-6 times slower than in Mathematica.

在朱莉娅:

D=1000*(rand(1000,1000)-0.5);
@time (E,F)=eig(D);

Out: elapsed time: 7.47950706 seconds (79638920 bytes allocated*)

在数学中:

First@Timing@Eigensystem[RandomReal[{-500, 500}, {1000, 1000}]]

Out: 1.310408

对于 2000 x 2000 数组,它是类似的,虽然 Julia 结果比等效的 Mathematica 调用慢一点,但它仍然更慢;Julia 需要 22 秒,而 Mathematica 需要 8 秒.

For 2000 x 2000 arrays it's similar, although the Julia result slowed down slightly less than the equivalent Mathematica call, but it's still slower; Julia takes 22 seconds, whereas Mathematica computes it in 8 seconds.

据我阅读 Julia 线性标准库代数,分解是通过调用LAPACK来实现的,我认为这应该很好,所以我很困惑为什么Julia代码运行得这么慢.有谁知道为什么会这样?它是否在做某种 Mathematica 不做的平衡或阵列对称检测?还是实际上更慢?

As far as I read in the Julia standard library for linear algebra, decompositions are implemented by calling LAPACK, which I thought was supposed to be very good, so I'm confused as to why the Julia code is running so much slower. Does anyone know why this is the case? Is it doing some kind of balancing or array-symmetry-detection that Mathematica doesn't do? Or is it actually slower?

另外,这是一个语法问题,可能是一个愚蠢的错误,但是如何更改 Julia 中的平衡?我试过了

Also, this is a syntax question and probably a silly error, but how do you change the balancing in Julia? I tried

@time (E,F)=eig(D[, balance=:nobalance]);

与从 Julia 手册中复制和粘贴的完全一样,但它只是给出了一个语法错误,所以出了点问题.

exactly as copied and pasted from the Julia manual, but it just gave a syntax error, so something's wrong.

我使用的是 Windows 7 64 位,Julia 版本 0.2.0 64 位,使用 Steven Johnson 网站上的说明,首先安装 Anaconda 以处理先决条件.我使用的是 Mathematica 学生版 9.0.1.

I am using Windows 7 64-bit, with Julia version 0.2.0 64-bit, installed using the instructions at Steven Johnson's site, with Anaconda installed first to take care of prerequisites. I am using Mathematica student edition version 9.0.1.

编辑 1:

执行 versioninfo() 产生了

Julia Version 0.2.0
Commit 05c6461 (2013-11-16 23:44 UTC)
Platform Info:
System: Windows (x86_64-w64-mingw32)
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
LAPACK: libopenblas
LIBM: libopenlibm

所以看起来我正在将 openBLAS 用于 LAPACK 和 BLAS.一旦我得到 Mathematica 实现信息,我也会添加它.

So it looks like I'm using the openBLAS for LAPACK and BLAS. Once I get the Mathematica implementation info I will add that as well.

编辑 2:

Windows Mathematica 似乎 可能使用英特尔 MKL BLAS.

It appears that Windows Mathematica probably uses Intel MKL BLAS.

推荐答案

Julia 中的特征计算外包给 LAPACKBLAS,我认为 Mathematica 也是如此.Julia 可以使用不同版本的 BLAS 和 LAPACK,因此您可以有效地将您为 Julia 选择的 LAPACK 和 BLAS 与 Mathematica 的 LAPACK 和 BLAS(可能是英特尔 MKL)进行比较.

The eigen calculation in Julia is outsourced to LAPACK and BLAS, and I think it is also the case for Mathematica. Julia can use different versions of BLAS and LAPACK and you are therefore effectively comparing your choice of LAPACK and BLAS for Julia with Mathematica's LAPACK and BLAS (probably Intel MKL).

Julia 的默认选择是 OpenBLAS,这在大多数架构上都很快,在我的机器上,Julia 的特征计算比 Mathematica 快.如果您在 Linux 上并从 repo 中选择了 BLAS 和 LAPACK,那么它们很可能比 OpenBLAS 慢得多.

The default choice for Julia is OpenBLAS which is fast on most architectures and on my machine Julia is faster than Mathematica for the eigen calculation. If you are on Linux and have chosen BLAS and LAPACK from a repo, it is very likely that they are much slower than OpenBLAS.

最近在Julia中添加了平衡选项,错误地没有将选项添加到函数eig中,它只是eigfact函数的MATLAB兼容接口.编写 eigfact(A,balance=:nobalance) 应该可以工作.

The option for balancing has recently been added to Julia and mistakenly the option was not added to the function eig, which is only a MATLAB compatible interface to the eigfact function. Writing eigfact(A,balance=:nobalance) should work.

编辑 1:进一步的调查表明,这种差异是由于 Windows 上的 OpenBLAS 中的线程问题造成的.如果 Julia 的 BLAS 仅限于一个线程,则时间与 Mathematica 相当,但如果允许更多线程,则计算速度会变慢.这在 Mac 或 Linux 上似乎不是问题,但如上所述,通常 OpenBLAS 的性能取决于架构.

Edit 1: Further investigation has shown that the difference is due to a threading problem in OpenBLAS on Windows. If Julia's BLAS is restricted to one thread the timings are comparable to Mathematica, but if more threads are allowed the calculation slows down. This doesn't seem to be a problem on Mac or Linux, but as mentioned above, in general the performance of OpenBLAS depends on the architecture.

编辑 2:最近,平衡选项发生了变化.可以通过编写 eigfact(A,permute=false,scale=false) 来关闭平衡.

Edit 2: Recently, the balancing option has changed. Balancing can be switched off by writing eigfact(A,permute=false,scale=false).

这篇关于Julia 的特征分解比 Mathematica 慢 5 倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆