# mul! much slower than \* for QRCompactWYQ

**URL:** <https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258>\
**Category:** Performance\
**Tags:** linearalgebra\
**Created:** [November 7, 2018, 2:32pm UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258 "2018-11-07T14:32:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nrontsis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nrontsis/32/38535_2.png) [@nrontsis](https://discourse.julialang.org/u/nrontsis)\
**Post date:** [November 7, 2018, 2:32pm UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/1 "2018-11-07T14:32:31Z")

</div>

Suppose I have generated the following QRCompactWYQ:

```julia
Q = qr(randn(500, 10)).Q;

```

and I want to do in-place multiplication of Q with

```julia
x = randn(500)

```

to

```julia
y = randn(500)

```

i.e.

```julia
@btime mul!(y, Q, x)

```

This runs in **2.9s** in my machine, which is significantly slower than doing

```julia
@btime y = Q*x

```

which runs in **8.8μs**.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [November 7, 2018, 3:54pm UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/2 "2018-11-07T15:54:24Z")

</div>

The LAPACK routine that does this modifies the right-hand side directly. It is interfaced via `lmul!(Q, x)`. I’m guessing it’s an oversight that the corresponding `mul!` method is missing.

The `mul!` function falls back to a generic method, which becomes extremely slow since the elements of Q take time to compute. It should be easy to write the missing method as it just needs to copy `x` into `y` and then call `lmul!`.

---

<div class="post-metadata">

**Author:** ![nrontsis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nrontsis/32/38535_2.png) [@nrontsis](https://discourse.julialang.org/u/nrontsis)\
**Post date:** [November 7, 2018, 4:24pm UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/3 "2018-11-07T16:24:09Z")

</div>

Thanks, I will create an issue/PR in Julia’s github repo then.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 22, 2019, 10:28am UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/4 "2019-02-22T10:28:08Z")

</div>

@nrontsis Have you created an issue/PR about this? If so, could you share a link here?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 22, 2019, 10:31am UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/5 "2019-02-22T10:31:34Z")

</div>

I ran into this issue for a `QRPackedQ` from a `qr` with pivoting.

MWE:

```julia
x = rand(64,64);
y = rand(64,64);
z = rand(64,64);
F = qr(x, Val(true));

@time mul!(x, F.Q, y);
@time mul!(x, Matrix(F.Q), y);
@time mul!(x, y, y);

# even worse if we want to use an adjoint
@time mul!(x, F.Q', y);
@time mul!(x, Matrix(F.Q)', y);
@time mul!(x, y', y);

```

which on my machine gives

```julia
0.318706 seconds (32.79 k allocations: 273.067 MiB, 7.32% gc time)
0.000707 seconds (9 allocations: 80.922 KiB)
0.000030 seconds (4 allocations: 160 bytes)

# adjoint
10.129926 seconds (1.08 M allocations: 8.798 GiB, 6.68% gc time)
0.000769 seconds (10 allocations: 80.938 KiB)
0.000039 seconds (5 allocations: 176 bytes)

```

(`typeof(F.Q) == LinearAlgebra.QRPackedQ{Float64,Array{Float64,2}}`)

EDIT: [Critical slowdown of `mul!` when called with a `QRPackedQ` · Issue #31146 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/31146)

---

<div class="post-metadata">

**Author:** ![nrontsis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nrontsis/32/38535_2.png) [@nrontsis](https://discourse.julialang.org/u/nrontsis)\
**Post date:** [February 22, 2019, 11:01am UTC](https://discourse.julialang.org/t/mul-much-slower-than-for-qrcompactwyq/17258/6 "2019-02-22T11:01:11Z")

</div>

Thanks for reminding me about this. I have just opened an [issue](https://github.com/JuliaLang/julia/issues/31147).
