# Operations on small matrices and BLAS in 1.6.0 RC1

**URL:** <https://discourse.julialang.org/t/operations-on-small-matrices-and-blas-in-1-6-0-rc1/56344>\
**Category:** New to Julia\
**Created:** [March 2, 2021, 3:56pm UTC](https://discourse.julialang.org/t/operations-on-small-matrices-and-blas-in-1-6-0-rc1/56344 "2021-03-02T15:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [March 2, 2021, 3:56pm UTC](https://discourse.julialang.org/t/operations-on-small-matrices-and-blas-in-1-6-0-rc1/56344/1 "2021-03-02T15:56:02Z")

</div>

With some code that uses only 4x4 matrices and that did nothing multi-threaded I was surprised to see CPU usage go up to all 4 cores on my machine (with a large percentage of system time) when using 1.6.0RC1 on Arch Linux. After some confused digging around (and disabling various parts like the GC) this turned out to be apparently caused by BLAS. One indication was the symbol `blas_thread_server` appeared above all julia related calls in a `perf` trace. Plus I saw millions of `sched_yield()` calls with `strace` on a runtime of about a minute, which doesn’t make much sense for single-threaded code.

Setting `BLAS.set_num_threads(1)` apparently restricts execution to a single core, and was also almost 50% faster. With 1.5.3 I don’t see this auto-threading behaviour, so perhaps some BLAS-related setting changed in 1.6?

More generally, I can understand such BLAS optimizations make sense when working with large matrices. But for small sizes like 4x4 isn’t there quite a bit of overhead in the parts of BLAS that determine whether to do such threading optimizations?

---

<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:** [March 2, 2021, 4:02pm UTC](https://discourse.julialang.org/t/operations-on-small-matrices-and-blas-in-1-6-0-rc1/56344/2 "2021-03-02T16:02:27Z")

</div>

> [@paulmelis](#):
>
> With some code that uses only 4x4 matrices

PS. I would definitely recommend using StaticArrays for this sort of thing.

---

<div class="post-metadata">

**Author:** ![paulmelis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paulmelis/32/35063_2.png) [@paulmelis](https://discourse.julialang.org/u/paulmelis)\
**Post date:** [March 2, 2021, 4:06pm UTC](https://discourse.julialang.org/t/operations-on-small-matrices-and-blas-in-1-6-0-rc1/56344/3 "2021-03-02T16:06:50Z")

</div>

> [@stevengj](#):
>
> PS. I would definitely recommend using StaticArrays for this sort of thing.

Right, although that’s a conclusion I’ve only come to in the last hour myself 🙂 It was a bit unexpected to see the full might of BLAS being applied to such small matrices, although the manual states clearly that it is used. I just wasn’t expecting it to be used for all matrix sizes.
