# Computational Speed

**URL:** https://discourse.julialang.org/t/computational-speed/22572
**Category:** General Usage
**Tags:** question
**Created:** [March 31, 2019, 10:28pm UTC](https://discourse.julialang.org/t/computational-speed/22572 "2019-03-31T22:28:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 31, 2019, 10:28pm UTC](https://discourse.julialang.org/t/computational-speed/22572/1 "2019-03-31T22:28:37Z")

</div>

Dear All,

I have tested a simple benchmark:

In Juno 1.1.0, windows

```julia
using CPUTime
CPUtic()

MM=rand(Float64,10000,10000)
mmm=inv(MM)

CPUtoc()

elapsed CPU time: 285.61 seconds

```

However, in Matlab

```julia
tic()

MM=rand(10000,10000);
mmm=inv(MM);

toc()
Elapsed time is 20.122736 seconds.

```

Both simulations in the same pc (ie7, 4cpus), not using parallel simulations (parpool)

Why such a simulation in Julia runs too slowly?  
How can we improve the speed without MPI?

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [March 31, 2019, 10:45pm UTC](https://discourse.julialang.org/t/computational-speed/22572/2 "2019-03-31T22:45:16Z")

</div>

Julia uses OpenBLAS, and MATLAB uses MKL. The easiest way to use Julia with MKL is probably:

> **[GitHub - JuliaLinearAlgebra/MKL.jl: Intel MKL linear algebra backend for Julia](https://github.com/JuliaLinearAlgebra/MKL.jl)**
>
> Intel MKL linear algebra backend for Julia. Contribute to JuliaLinearAlgebra/MKL.jl development by creating an account on GitHub.

EDIT:  
BLAS is multithreaded.  
If BLAS is using 8 threads, CPU time will elapse at about 8 CPU seconds per second.

Therefore, be sure to actually time both in the same way.  
For example, simply

```julia
@time begin
   MM=rand(Float64,10000,10000)
   mmm=inv(MM)
end

```

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 31, 2019, 10:51pm UTC](https://discourse.julialang.org/t/computational-speed/22572/3 "2019-03-31T22:51:15Z")

</div>

> [@Elrod](#):
>
> @time

I have used your method in Julia

```julia

43.088418 seconds (13 allocations: 1.495 GiB, 0.42% gc time)

```

Matlab

```julia

20.122736 seconds.

```

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 31, 2019, 10:53pm UTC](https://discourse.julialang.org/t/computational-speed/22572/4 "2019-03-31T22:53:56Z")

</div>

If I use MKL, then

```julia

 43.684950 seconds (13 allocations: 1.495 GiB, 1.06% gc time)

```

```julia

using CPUTime
using LinearAlgebra

BLAS.vendor()
:mkl

#CPUtic()
@time begin
MM=rand(Float64,10000,10000)
mmm=inv(MM)
end
#CPUtoc()

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [March 31, 2019, 11:01pm UTC](https://discourse.julialang.org/t/computational-speed/22572/6 "2019-03-31T23:01:15Z")

</div>

How many physical cores does your CPU have?  
Often, that will be half of `Sys.CPU_THREADS`, but sometimes the two will be equal.

Try `BLAS.set_num_threads(NUMBER_PHYSICAL_CORES)`. When the numbers are not equal, this will be faster than using `Sys.CPU_THREADS` threads.

Almost 100% of the time is being spent in BLAS/LAPACK, so if both are using MKL, I don’t know why their ought to be a difference. Unless they’re defaulting to different BLAS calls under the hood.

Also, I’d just like to confirm that you restarted your Julia session after building MKL.jl?

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [March 31, 2019, 11:01pm UTC](https://discourse.julialang.org/t/computational-speed/22572/7 "2019-03-31T23:01:56Z")

</div>

If you use MKL in both cases, you can get a more realistic result. At least I find this:

With MATLAB:

```julia
>> tic()
MM = rand(10000,10000);
mmm = inv(MM);
toc()
Elapsed time is 15.251977 seconds.

```

With JuliaPro 0.6.0 (MKL):

```julia
function testInv(n)
    MM = rand(Float64,n,n)
   mmm = inv(MM)
end

julia> @time testInv(10000);
 13.208193 seconds (21 allocations: 2.285 GiB, 1.47% gc time)

```

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 31, 2019, 11:05pm UTC](https://discourse.julialang.org/t/computational-speed/22572/8 "2019-03-31T23:05:55Z")

</div>

IF you use **Juno**?

```julia
#CPUtic()
function testInv(n)
    MM = rand(Float64,n,n)
   mmm = inv(MM)
end

 @time testInv(10000);

```

> 47.049461 seconds (2.82 k allocations: 1.495 GiB, 0.36% gc time)

Matlab

```julia

tic()

MM=rand(10000,10000);
mmm=inv(MM);

toc()
Elapsed time is 23.070654 seconds.

```

Jupyter

```julia

 43.998483 seconds (1.19 M allocations: 1.552 GiB, 0.19% gc time)

```

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [March 31, 2019, 11:10pm UTC](https://discourse.julialang.org/t/computational-speed/22572/9 "2019-03-31T23:10:47Z")

</div>

Directly in the REPL, with OpenBLAS (Julia 1.1.0) I get:

```julia
 30.744067 seconds (13 allocations: 1.495 GiB, 0.19% gc time)

```

vs. MKL (JuliaPro 0.6.0):

```julia
 13.208193 seconds (21 allocations: 2.285 GiB, 1.47% gc time)

```

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [March 31, 2019, 11:12pm UTC](https://discourse.julialang.org/t/computational-speed/22572/10 "2019-03-31T23:12:50Z")

</div>

I am using Julia 1.1.0 in JUNO.

Therefore, If I want to improve the computational speed, I have to use JuliaPro…???

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 1, 2019, 12:19am UTC](https://discourse.julialang.org/t/computational-speed/22572/11 "2019-04-01T00:19:55Z")

</div>

FYI, benchmarking the inversion of a large matrix only depends on the BLAS library that is linked; it basically has nothing to do with the language. See also [OpenBLAS: Julia slower than R - #2 by stevengj](https://discourse.julialang.org/t/blas-julia-slower-than-r/21594/2)

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [April 1, 2019, 12:30am UTC](https://discourse.julialang.org/t/computational-speed/22572/12 "2019-04-01T00:30:15Z")

</div>

Sorry for that, may I ask why we donot use MKL as the default library ?

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 1, 2019, 12:35am UTC](https://discourse.julialang.org/t/computational-speed/22572/14 "2019-04-01T00:35:50Z")

</div>

> [@Aquaman](#):
>
> Sorry for that, may I ask why we donot use MKL as the default library ?

It is not open source.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [April 1, 2019, 12:38am UTC](https://discourse.julialang.org/t/computational-speed/22572/15 "2019-04-01T00:38:11Z")

</div>

> [@mauro3](#):
>
> MKL

Free [Intel Performance Libraries](https://software.intel.com/en-us/performance-libraries)for **Everyone**??

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [April 1, 2019, 12:39am UTC](https://discourse.julialang.org/t/computational-speed/22572/16 "2019-04-01T00:39:06Z")

</div>

Free but not open source.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [April 1, 2019, 1:01am UTC](https://discourse.julialang.org/t/computational-speed/22572/17 "2019-04-01T01:01:54Z")

</div>

> Warning: both Julia and NumPy are linked with MKL, which may cause conflicts and crashes (#433).

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 1, 2019, 8:55am UTC](https://discourse.julialang.org/t/computational-speed/22572/18 "2019-04-01T08:55:55Z")

</div>

I think it is time for a FAQ entry, so I made one:

> <https://github.com/JuliaLang/julia/pull/31570>
>
> This is a start to address the related questions that come up quite frequently o…n the discussion forum.
> 
> Suggestions welcome.

---

<div class="post-metadata">

### Author: ![Aquaman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aquaman/32/6586_2.png) [@Aquaman](https://discourse.julialang.org/u/Aquaman)
#### Post date: [April 1, 2019, 10:05am UTC](https://discourse.julialang.org/t/computational-speed/22572/19 "2019-04-01T10:05:26Z")

</div>

HI @Tamas_Papp, thank you!

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [April 1, 2019, 10:24am UTC](https://discourse.julialang.org/t/computational-speed/22572/20 "2019-04-01T10:24:32Z")

</div>

> [@Aquaman](#):
>
> Free [Intel Performance Libraries](https://software.intel.com/en-us/performance-libraries)for **Everyone**??

Gratis, but not free. In RMS’ words, free as in free beer, not free as in freedom.

Apart from politics, it is apparently nontrivial to sort out licensing in a way that permits to distribute compiled julia binaries with both MKL and GMP. Microsoft seems to believe that it is legally possible under some circumstances (cf existence of MRAN), and they presumably have competent lawyers who figured this out.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [April 1, 2019, 10:59am UTC](https://discourse.julialang.org/t/computational-speed/22572/21 "2019-04-01T10:59:12Z")

</div>

> [@foobar\_lv2](#):
>
> they presumably have competent lawyers who figured this out

It looks like they are [re-licensing](https://mran.microsoft.com/assets/text/mkl-eula.txt) MKL as “Microsoft R Services MKL”. I assume this involves a deal between MS and Intel, as legally you enter into a license agreement with MS if using this product (even the MKL part).

I understand the considerations for speed, but I think that using a FOSS library by default for Julia is the right choice. I would be uncomfortable with using a black box (a very nice, well-tested black box, but a black box nevertheless) for research. Especially since if someone really wants to do it, installing & using MKL is always an option.

IMO the discrepancy in naive benchmarks between various languages is an orthogonal issue and should be addressed by user education.

---

<div class="post-metadata">

### Author: ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)
#### Post date: [April 1, 2019, 12:13pm UTC](https://discourse.julialang.org/t/computational-speed/22572/22 "2019-04-01T12:13:40Z")

</div>

> [@Tamas\_Papp](#):
>
> I assume this involves a deal between MS and Intel

Afaik the main problem is the possibility of running afoul of the GPL when distributing binaries that form a derived work (MRAN, Julia) of GPL code (R, GMP) and unfree code (MKL). I don’t exactly see how a deal between MS and intel helps with that.

IANAL, but it is definitely plausible that this is OK, in the same way that some linux distros dare to ship binaries for zfs; and it is at least not totally implausible that this is problematic, and some distros don’t ship zfs binaries (i.e. require zfs users to compile at home, as currently necessary when using MKL with julia). I’ll assume that whatever MS is doing is legally sound, but can’t guess at what special circumstances are relevant to replicating that feat.

> [@Tamas\_Papp](#):
>
> but I think that using a FOSS library by default for Julia is the right choice.

Agree!

[Next page](https://discourse.julialang.org/t/computational-speed/22572.md?page=2)
