# Thread affinitization: pinning Julia threads to cores

**URL:** <https://discourse.julialang.org/t/thread-affinitization-pinning-julia-threads-to-cores/58069>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [March 27, 2021, 8:04am UTC](https://discourse.julialang.org/t/thread-affinitization-pinning-julia-threads-to-cores/58069 "2021-03-27T08:04:36Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [January 27, 2022, 3:19pm UTC](https://discourse.julialang.org/t/thread-affinitization-pinning-julia-threads-to-cores/58069/10 "2022-01-27T15:19:28Z")

</div>

I’ve been trying around with ThreadPinning.jl related to this post here [Current OpenBLAS Versions (January 2022) do not support Intel gen 11 performantly? - #30 by fgerick](https://discourse.julialang.org/t/current-openblas-versions-january-2022-do-not-support-intel-gen-11-performantly/75104/30) .

I have some trouble understanding this behaviour:

```julia
#julia -t 48 
julia> using BenchmarkTools, Hwloc, ThreadPinning, LinearAlgebra, Octavian; A = rand(5_000,5_000); B = similar(A);

julia> BLAS.get_num_threads()
96

julia> BLAS.set_num_threads(48)

julia> @btime mul!($B, $A, $A);
  626.977 ms (0 allocations: 0 bytes)

julia> BLAS.set_num_threads(24)

julia> @btime mul!($B, $A, $A);
  377.458 ms (0 allocations: 0 bytes)

julia> @btime matmul!($B, $A, $A);
  521.579 ms (0 allocations: 0 bytes)

julia> pinthreads(:compact)

julia> @btime matmul!($B, $A, $A);
  223.912 ms (0 allocations: 0 bytes)

julia> @btime matmul!($B, $A, $A);
  233.511 ms (0 allocations: 0 bytes)

julia> pinthreads(:compact)

julia> @btime matmul!($B, $A, $A);
  188.991 ms (0 allocations: 0 bytes)

julia> @btime matmul!($B, $A, $A);
  186.621 ms (0 allocations: 0 bytes)

julia> @btime mul!($B, $A, $A);
  380.789 ms (0 allocations: 0 bytes)

```

The OpenMP threads are not affected by ThreadPinning as I understand. However, why do I see different benchmarks for the pure Julia code in Octavian’s `matmul!` after `pinthreads(:compact)` the first time and the second time? Is there a way to start Julia on just one socket, without having to pin afterwards?

---

_[View the full topic](https://discourse.julialang.org/t/thread-affinitization-pinning-julia-threads-to-cores/58069)._
