# ANN: MKLSparse

**URL:** <https://discourse.julialang.org/t/ann-mklsparse/3384>\
**Category:** Community\
**Tags:** mkl, iterative, sparse\
**Created:** [April 26, 2017, 1:40pm UTC](https://discourse.julialang.org/t/ann-mklsparse/3384 "2017-04-26T13:40:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 26, 2017, 1:40pm UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/1 "2017-04-26T13:40:46Z")

</div>

Hello everyone,

I recently updated the [MKLSparse.jl](https://github.com/JuliaSparse/MKLSparse.jl) package for 0.5 and 0.6.

The most useful feature of MKLSparse is likely the ability to seamlessly accelerate sparse matrix vector multiplications (which are the main workhorse in iterative solvers). Using a [representative matrix](https://gist.github.com/KristofferC/1660cd312d17454009644e3516c10c94) for benchmarking I get the following timings

```julia
julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
  2.901099 seconds (18.45 k allocations: 994.534 KiB)

julia> using MKLSparse

julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
  0.877888 seconds (31.31 k allocations: 1.641 MiB)

```

where we can see that performance is greatly increased by just loading `MKLSparse` (results will vary depending on the system this is run on).

A bonus with the new version of MKLSparse is that there is no longer a need to build Julia with MKL to use it. Instead, it is enough to have MKL installed and the paths correctly set for the package to work.

While the DSS (Direct Sparse Solver) interface is not yet wrapped, the package [Pardiso.jl](https://github.com/JuliaSparse/Pardiso.jl) can instead be used to solve general sparse systems using MKL.

// Kristoffer

---

<div class="post-metadata">

**Author:** ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)\
**Post date:** [July 5, 2018, 6:59pm UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/2 "2018-07-05T18:59:53Z")

</div>

Hi, thanks for MKLSparse. Is it normal if I see only 3% speed gains? I am in i7-6700 laptop. The total lf allocations and memory used in the the same order of magnitude as in your case but the final wall times are almost the same 😕

Cheers

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [July 5, 2018, 9:59pm UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/3 "2018-07-05T21:59:03Z")

</div>

@kristoffer.carlsson,  
Will it work with JuliaPro MKL edition out of the box?

Does the JuliaPro MKL Edition use MKL for Sparse Matrices to begin with?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 6, 2018, 7:38am UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/4 "2018-07-06T07:38:23Z")

</div>

> [@carlomontec](#):
>
> Is it normal if I see only 3% speed gains?

Perhaps MKL fails to be used at all; are the tests passing? Does the CPU usage indicate that multiple cores are used? What if you try larger matrices than in my first post?

> [@RoyiAvital](#):
>
> Will it work with JuliaPro MKL edition out of the box?

It should, but I haven’t tried it.

> [@RoyiAvital](#):
>
> Does the JuliaPro MKL Edition use MKL for Sparse Matrices to begin with?

I don’t think so, no.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [July 6, 2018, 7:57am UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/5 "2018-07-06T07:57:27Z")

</div>

It would be great if it worked with JuliaPro MKL Edition out of the box by utilizing the MKL packaged by Julia.  
Same holds for [`PARDISO.jl`](https://github.com/JuliaSparse/Pardiso.jl).

By the way, thank you for both!

---

<div class="post-metadata">

**Author:** ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)\
**Post date:** [July 6, 2018, 9:57am UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/6 "2018-07-06T09:57:32Z")

</div>

Hello Kristoffer, following your questions, I checked some stuff.

- All tests are passing
- CPU indicates that the “julia” process is taking 499% CPU usage.

I repeated the tests, but now I used the function for the “representative matrix” as linked in the post, but using the parameter 100, instead of 60 in getDivGrad(n,n,n).

Situation is now worse, using MKL makes it slower. from ~9.5 to ~11 seconds, using multiple cores.

```julia
 11.141514 seconds (24.98 k allocations: 1.296 MiB)

```

I am using version 0.6.2

```julia
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-6700HQ CPU @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)

```

I have been using Pardiso.jl with MKL with very good results in this same computer, which makes it even more strange this aforementioned results.

I have also a Julia version installed that was compiled with MKL and the situation is very similar.

I also tried with and without “# export JULIA\_NUM\_THREADS=4” at my .bashrc file, having no differences.

Any hint would be quite appreciated 🙂

Carlo

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 6, 2018, 11:12am UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/7 "2018-07-06T11:12:54Z")

</div>

Not sure, just tried on my mac (i7-4770HQ) and I get (running the timings twice)

```julia
julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
  2.716284 seconds

julia> using MKLSparse

julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
  1.115627 seconds

```

For large matrices the speedup is smaller but still significant:

```julia
julia> K = getDivGrad(100,100,100); b = rand(size(K,1)); c = similar(b);

julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
 12.858318 seconds

julia> using MKLSparse

julia> @time for i in 1:1000 A_mul_B!(c,K,b) end;
  8.882692 seconds

```

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [September 4, 2021, 10:40am UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/8 "2021-09-04T10:40:10Z")

</div>

What’s the scope of `MKLSparse`?  
I am interested in 2 cases:

1. It is used in global scope (`using MKLSparse;`), will it affect all packages used from any call to a function from that script?
2. I import it inside a module (`using MKLSparse;`), will it affect other modules? The global scope?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 4, 2021, 2:16pm UTC](https://discourse.julialang.org/t/ann-mklsparse/3384/9 "2021-09-04T14:16:21Z")

</div>

Yes, to both of those questions. It does type piracy to redirect any calls to supported operations to MKL.
