# Julia seems to be running multithreaded when I don't want it to

**URL:** <https://discourse.julialang.org/t/julia-seems-to-be-running-multithreaded-when-i-dont-want-it-to/95391>\
**Category:** Julia at Scale\
**Created:** [March 1, 2023, 2:39pm UTC](https://discourse.julialang.org/t/julia-seems-to-be-running-multithreaded-when-i-dont-want-it-to/95391 "2023-03-01T14:39:50Z")\
**Posts on this page:** 1\
**Showing post:** 22

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 1, 2023, 4:50pm UTC](https://discourse.julialang.org/t/julia-seems-to-be-running-multithreaded-when-i-dont-want-it-to/95391/22 "2023-03-01T16:50:32Z")

</div>

I took the liberty of optimizing your Julia implementation a bit. I removed all of the slicing allocations by using `@view` and used `mul!` for in-place multiplication. Neither of these changed the runtime significantly at these large problem sizes, but they’ll reduce the memory churn which is never a bad thing. More significantly, I also used `@view` to trim computations on parts of your arrays that were still all-zero for a 2x speedup.

I checked that this still gave virtually the same results, but you should check it too.

```julia
using LinearAlgebra: norm, mul!

function CGS2_alt(X::AbstractMatrix)
  Base.require_one_based_indexing(X) # ensure X has 1-based indices
  T = float(eltype(X))
  n, m = size(X)
  Q = zeros(T, n, m)
  R = zeros(T, m, m)
  r = zeros(T, m)
  w = zeros(T, n)
  R[1, 1] = norm(@view X[:, 1])
  Q[:, 1] .= @view(X[:, 1]) ./ R[1, 1]
  for i in 2:m
	iprev = 1:i-1
    w .= @view X[:, i]
	Qi = @view Q[:,iprev]
	ri = @view r[iprev]
    mul!(ri, Qi', w) # ri .= (w'Qi)'
    R[iprev, i] = ri
    mul!(w, Qi, ri, -1, 1) # w .-= Qi * ri
    mul!(ri, Qi', w) # ri .= (w'Qi)'
    mul!(w, Qi, ri, -1, 1) # w .-= Qi * ri
    R[i, i] = norm(w)
    Q[:, i] .= w ./ R[i, i]
  end
  return Q, R
end

```

---

_[View the full topic](https://discourse.julialang.org/t/julia-seems-to-be-running-multithreaded-when-i-dont-want-it-to/95391)._
