svdvals is alarmingly slow

Straightforward use of svdvals on a freshly restarted Julia session:

using BenchmarkTools
B=randn(100,100);
@btime svdvals($B);
versioninfo()

Here’s the result.

  10.423 ms (11 allocations: 138.20 KiB)

Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin14.5.0)
  CPU: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

Run seconds later in MATLAB:

>> B=randn(100);
>> tic, for k=1:10000, svd(B); end, t=toc/10000
t =
     5.541419457000000e-04

(The results don’t change much for different random matrices.) So Julia is taking nearly 20x as long to compute the singular values. Have I overlooked something?

– Toby

On my computer:

julia> @btime svdvals($B);
  710.531 μs (11 allocations: 138.20 KiB)

julia> versioninfo()
Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Prescott)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

while in Matlab I get:

t =

   6.2645e-04

(but Julia is running in VirtualBox and Matlab natively in Windows).

Interesting, I’m getting slow times on my Mac as well, but not when run on the same machine in a Linux VM.

It was about 0.8 ms for me on Linux. Just for the hell of it I tried

sqrt.(eigvals(B'*B))

and got 760.292 μs (45 allocations: 196.22 KiB) , are these exactly equivalent in Base?

It’s gratifying that sqrt.(eigvals(B'*B)) is so close to MATLAB’s svdvals when things are working properly :smile:.

I’ve opened an issue here: https://github.com/JuliaLang/julia/issues/26162

Unfortunately we’re comparing apples to oragnes here: Julia’s svdvals vs Matlab’s svd. (Matlab doesn’t have an equivalent to svdvals AFAIK). Julia’s svdfact is 6x slower than Matlab’s svd on my machine.

Oh well, I had hope.

I think the single output argument of Matlab svd is equivalent to Julia’s svdvals (I don’t know enough Matlab to say which is called with no arguments).

We have seen that OpenBLAS is slower than Intel MKL.

I wonder if it has to do with that.
Does svdvals use OpenBLAS?

I suggest you try the JuliaPro distribution with Intel MKL.

I always build Julia with Mac’s system BLAS because OpenBLAS is so slow

@barche’s benchmarks show that OpenBLAS is a big part of it:

https://github.com/barche/julia-blas-benchmarks/blob/master/BenchmarkResults.ipynb

That is correct. So, Matlab 2017b on windows:

>> tic, for k=1:10000, s=svd(B); end, t=toc/10000
t =
    5.8891e-04

which is slightly faster than Julia running on Linux in a virtual machine.

Try it with Julia MKL to do a more 1-1 comparison.

That doesn’t show the equivalence between MATLAB’s svd() with one output to Julia’s svdvals().

You need to show that in case you call MATLAB svd() with 2 output parameter the run time changes.
This will imply that in case of one output MATLAB is doing something optimized for getting the Singular Values only.

Took the advice of @RoyiAvital and @ChrisRackauckas to download JuliaPro with MKL. Timing went down to about 500 μs, which is completely on par with my MATLAB results.

Funny, I always just assumed the “Pro” meant $$. So I went with more DYI, except not enough DYI to build from source…

Thanks to all for the quick answers.

Yeah, I think there’s some licensing reason why it’s not default? :man_shrugging:

OK, just to summarize my timings:

Matlab, [u,s,v] = svd(B) : 1.4 ms
Julia, s=svdfact($B) : 2.38 ms

Matlab, s = svd(B) : 585 us
Julia, s = svdvals(B) : 673 us

Perhaps we should just use the system BLAS on OS X (since it is the one platform where the system BLAS is actually good).

4 Likes

@mbaz: are you able to try it on the Julia Windows binary? I would be interested to know if it is a problem there as well.

I don’t have Julia installed in Windows, but I’ll try to find time this weekend to do it.