# Multithreading over multiple CPU

**URL:** <https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106>\
**Category:** Performance\
**Tags:** question, multithreading\
**Created:** [May 19, 2023, 11:05am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106 "2023-05-19T11:05:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![astro-kevin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astro-kevin/32/43342_2.png) [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Post date:** [May 19, 2023, 11:05am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/1 "2023-05-19T11:05:17Z")

</div>

Is the `@threads` macro available for multithreading over multiple CPU, or does it only work for setups with one CPU?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 19, 2023, 11:11am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/2 "2023-05-19T11:11:13Z")

</div>

It’s unclear what you’re asking. Do you want to distribute your work across multiple CPUs or processes? Multithreading (ability to spawn multiple tasks within a _single process_ on a _single machine_) is distinct from distributed computing (ability to have _multiple processes_ communicating with each other, running on _one or more machines_) and they use [distinct tools](https://docs.julialang.org/en/v1/manual/parallel-computing/).

---

<div class="post-metadata">

**Author:** ![astro-kevin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astro-kevin/32/43342_2.png) [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Post date:** [May 19, 2023, 11:13am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/3 "2023-05-19T11:13:10Z")

</div>

I’m looking at a single machine which has 2 CPUs with both CPU having multiple cores, so I don’t know whether it is possible to use multithreading in this case.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 19, 2023, 11:15am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/4 "2023-05-19T11:15:34Z")

</div>

You can [have a single Julia process with as many threads as you want](https://docs.julialang.org/en/v1/manual/multi-threading/#Starting-Julia-with-multiple-threads), but generally not more than the logical threads supported by your system, more often closer to the number of physical cores you have. `@threads` will distribute your workload across those threads.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 19, 2023, 11:17am UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/5 "2023-05-19T11:17:12Z")

</div>

It’s an implementation detail, but when 2+ CPU, they share same memory space (“NUMA”), and I believe the sum of the number of cores is reported, and that’s what Julia looks at and exploits, as it should.

When you have two or more CPUs “in the same machine” but the memory space isn’t shared it’s called a cluster and then it’s just impossible to share threads across so you use Distributed, and different “computers” in the cluster have their own address space/process each. The sum of the computers is then a cluster, but each computer has its own core count.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 19, 2023, 12:34pm UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/6 "2023-05-19T12:34:32Z")

</div>

I am interested in learning more on this topic as well. If I buy a dual-CPU high-end computer, does it really appear to Julia (or other programs) as a single machine? I always steered clear because I thought that it would not, and I would run into this distributed fiasco, when really I just want a single powerful computer.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 19, 2023, 1:11pm UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/7 "2023-05-19T13:11:31Z")

</div>

I suggest reading up on the first touch policy and NUMA domains.  
It will appear and work like one machine, but you’ll need to be much more careful to get good performance.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [May 19, 2023, 2:00pm UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/8 "2023-05-19T14:00:21Z")

</div>

I recall dual first from Pentim Pro days, i.e. two CPUs separate on the motherboard. Is there any reason to do that now? You can get one or two dies now in the same chip. I’m not sure, but those might look like two CPUs to the computer. If that’s what you mean i.e. a multi-chip module, then it meant in the past that both chips inside have separate cache (but shared memory, while cache traffic between slows down), now you would have one shared L3 in the chip to bridge those two CPUs, I think. I very much doubt it should worry you that it looks to the OS as two CPUs, but I think it would very likely be overkill for most people to have more than on CPU.

> **[Best Dual CPU Motherboards to Get in 2021 | Ideal CPU](https://idealcpu.com/best-dual-cpu-motherboards/)**
>
> These are the best dual CPU motherboards in the market if you are building a high-end server, workstation, or a gaming PC.

> Having a motherboard that comes with a 2 CPU socket gives you a slight edge when it comes to more cores and performance. Before we dig in and review

Are you looking for throughput performance (as in web server), or for a single app? As Elrod mentioned, it’s hard to get performance (out of one app), but with tuning the gain might be more than “a slight edge”. I think he means HPC style, not throughput style performance.

I’m not sure, the reason for these dual-CPU might be to get two cheaper CPUs, combined cost, i.e. for throughput. You might want to max out both CPUs, or not. I’m not sure why people do this, and doubt e.g. helps in games.

---

<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:** [May 19, 2023, 3:57pm UTC](https://discourse.julialang.org/t/multithreading-over-multiple-cpu/99106/9 "2023-05-19T15:57:00Z")

</div>

This still exists for servers. Dual CPU servers are getting less common now that single CPUs get up to 64 cores, but dual CPUs are still pretty common (either for going to 128 cores or for the extra PCIE lanes).
