# Why does julia launch kernel threads?

**URL:** <https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053>\
**Category:** Performance\
**Created:** [April 17, 2024, 8:51am UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053 "2024-04-17T08:51:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpuriousEigenstate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spuriouseigenstate/32/208390_2.png) [@SpuriousEigenstate](https://discourse.julialang.org/u/SpuriousEigenstate)\
**Post date:** [April 17, 2024, 8:51am UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/1 "2024-04-17T08:51:40Z")

</div>

I’m currently trying to parallelize my code to run on a cluster, and using `Threads.@threads`. I noticed that in `htop` the cpu usage indicates that the running threads are “kernel threads” (red), meaning (insofar I understand) that the thread is managed by the system scheduler. I have never seen that happen using python’s `multiprocessing` for example.

 ![Screenshot from 2024-04-17 10-44-31](https://global.discourse-cdn.com/julialang/original/3X/8/0/8058bdbf7ac94baa85f005c7c4712d83646bedf0.png)

Why is this? And what is the difference with “normal” threads?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 17, 2024, 9:58am UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/2 "2024-04-17T09:58:36Z")

</div>

Red in the CPU usage bar in `htop` means that the time is being spent in the kernel, not that the time is being spent in “kernel threads”. This can mean that you’re spending a lot of time waiting on IO, that you’re spending a lot of time allocating memory, or similar activities that require some work to be done inside of the kernel.

Seeing as your processes are using 25GiB of memory _each_, I’d wager your code allocates a lot.

---

<div class="post-metadata">

**Author:** ![SpuriousEigenstate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spuriouseigenstate/32/208390_2.png) [@SpuriousEigenstate](https://discourse.julialang.org/u/SpuriousEigenstate)\
**Post date:** [April 17, 2024, 12:29pm UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/3 "2024-04-17T12:29:39Z")

</div>

Ok, thanks for the clarification.

Also, I don’t think it’s 25G each. Most of the processes are threads, and use the same memory; the actual size is 9G, which is still a lot, but not unexpected as I’m dealing with large arrays.

There might be a problem with unwanted allocation though. I’m looking into it 🙂

---

<div class="post-metadata">

**Author:** ![SpuriousEigenstate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spuriouseigenstate/32/208390_2.png) [@SpuriousEigenstate](https://discourse.julialang.org/u/SpuriousEigenstate)\
**Post date:** [April 22, 2024, 12:04pm UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/4 "2024-04-22T12:04:14Z")

</div>

It seems that the `eigen` function from `LinearAlgebra` causes this behavior, in fact it is easy to reproduce:

```julia
using LinearAlgebra

A = rand(800,800)

Threads.@threads for i in 1:30
    eigen(A)
end

```

Just running this and my htop looks exactly like in the original post. Leaving out `Threads.@threads` makes no difference, except that not all threads ate at 100% simultaneously.

Any ideas why this is, and whether this is at all a bad thing? I can’t tell.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 22, 2024, 12:07pm UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/5 "2024-04-22T12:07:57Z")

</div>

BLAS doesn’t use Julia’s threads. If you are using Julia’s multithreading, you likely want `BLAS.set_num_threads(1)` which will make all your BLAS operations single threaded.

---

<div class="post-metadata">

**Author:** ![SpuriousEigenstate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spuriouseigenstate/32/208390_2.png) [@SpuriousEigenstate](https://discourse.julialang.org/u/SpuriousEigenstate)\
**Post date:** [April 22, 2024, 12:26pm UTC](https://discourse.julialang.org/t/why-does-julia-launch-kernel-threads/113053/6 "2024-04-22T12:26:52Z")

</div>

You’re right! Setting `BLAS.set_num_threads(1)` stops this behavior

 ![Screenshot from 2024-04-22 14-23-38](https://global.discourse-cdn.com/julialang/original/3X/9/3/934a4e6ea10720cb6cb77cd3048323b6ff40da76.png)

The execution speed is much faster too, thanks a lot!
