# Gemm! vs symm! performance

**URL:** <https://discourse.julialang.org/t/gemm-vs-symm-performance/92201>\
**Category:** Performance\
**Created:** [December 27, 2022, 10:25pm UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201 "2022-12-27T22:25:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [December 27, 2022, 10:25pm UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/1 "2022-12-27T22:25:55Z")

</div>

Hi!

I try to compare gemm! or symm! performance and get these results for small matrices. gemm! have much better performance, when to use symm!?

```julia
C = rand(10, 10)
A = rand(10, 10)
B = rand(10, 10)

```

```julia
julia> @benchmark LinearAlgebra.BLAS.symm!('R', 'U', true, A, B, true, C)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max): 36.900 μs … 110.900 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 42.800 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 43.492 μs ± 3.949 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

            ▁▂▅▆▇▃██▂▂
  ▁▁▁▁▁▂▃▃▅▇███████████▅▃▃▃▂▂▂▂▁▁▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
  36.9 μs Histogram: frequency by time 59.3 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark LinearAlgebra.BLAS.gemm!('N', 'N', true, A, B, true, C)
BenchmarkTools.Trial: 10000 samples with 417 evaluations.
 Range (min … max): 251.799 ns … 459.712 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 264.029 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 264.118 ns ± 4.118 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

                                             █▂▂▂▁
  ▂▁▂▂▂▁▂▁▂▂▁▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▅▆▆▇███████▇▆▅▇▄▃▃▂▂▂▂▂ ▃
  252 ns Histogram: frequency by time 268 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> 

```

---

<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:** [December 28, 2022, 12:01am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/2 "2022-12-28T00:01:32Z")

</div>

BLAS functions tend to be optimized for large matrices. For very small matrices, [StaticArrays](https://github.com/JuliaArrays/StaticArrays.jl) are faster, at least if the size of the matrix is known at compile-time:

```julia
julia> using LinearAlgebra, StaticArrays, BenchmarkTools

julia> C,A,B = rand(10,10), rand(10,10), rand(10,10);

julia> @btime LinearAlgebra.BLAS.gemm!('N', 'N', true, $A, $B, true, $C);
  301.444 ns (0 allocations: 0 bytes)

julia> @btime LinearAlgebra.BLAS.symm!('R', 'U', true, $A, $B, true, $C);
  110.352 μs (0 allocations: 0 bytes)

julia> As,Bs = SMatrix{10,10}(A), SMatrix{10,10}(B);

julia> @btime $As * $Bs;
  85.477 ns (0 allocations: 0 bytes)

```

(You could probably implement an even faster method for `Symmetric{<:SMatrix}` multiplication too, but I don’t think this exists right now.)

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 28, 2022, 8:54am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/3 "2022-12-28T08:54:00Z")

</div>

Such a large difference between symm! and gemm! is too unreasonable. Actually, I cannot reproduce your result. On my machine, the difference is only ~5% for your setup.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 28, 2022, 8:58am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/4 "2022-12-28T08:58:56Z")

</div>

My env:

```julia
Julia Version 1.6.7
Commit 3b76b25b64 (2022-07-19 15:11 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-4771 CPU @ 3.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, haswell)

```

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 28, 2022, 9:05am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/5 "2022-12-28T09:05:59Z")

</div>

I get similar measurements as @stevengj and @PharmCat:

```julia
julia> using LinearAlgebra, StaticArrays, BenchmarkTools

julia> C,A,B = rand(10,10), rand(10,10), rand(10,10);

julia> @btime LinearAlgebra.BLAS.gemm!('N', 'N', true, $A, $B, true, $C);
  189.628 ns (0 allocations: 0 bytes)

julia> @btime LinearAlgebra.BLAS.symm!('R', 'U', true, $A, $B, true, $C);
  41.400 μs (0 allocations: 0 bytes)

julia> As,Bs = SMatrix{10,10}(A), SMatrix{10,10}(B);

julia> @btime $As * $Bs;
  50.557 ns (0 allocations: 0 bytes)

julia> versioninfo()
Julia Version 1.9.0-alpha1
Commit 0540f9d739 (2022-11-15 14:37 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 20 × 12th Gen Intel(R) Core(TM) i9-12900HK
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, alderlake)
  Threads: 20 on 20 virtual cores

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [December 28, 2022, 9:08am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/6 "2022-12-28T09:08:55Z")

</div>

Seems it’s a bug for a newer CPU or newer BLAS version.

---

<div class="post-metadata">

**Author:** ![PharmCat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pharmcat/32/6953_2.png) [@PharmCat](https://discourse.julialang.org/u/PharmCat)\
**Post date:** [December 28, 2022, 10:29am UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/7 "2022-12-28T10:29:56Z")

</div>

> [@uniment](#):
>
> `versioninfo()`

My versioninfo:

```julia
julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf26 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 32 × AMD Ryzen 9 5950X 16-Core Processor
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, znver3)
  Threads: 16 on 32 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 16

```

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [January 2, 2023, 8:19pm UTC](https://discourse.julialang.org/t/gemm-vs-symm-performance/92201/8 "2023-01-02T20:19:58Z")

</div>

I think you should file this issue at [GitHub - xianyi/OpenBLAS: OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.](https://github.com/xianyi/OpenBLAS). It looks like there is a performance issue with zsymm for your architecture.
