2
$\begingroup$

Let's say there's a single process performed in the machine which utilizes all available processors (whether physical or logical processors). The process use 100% processor utilization most of the time on all processors.

Which machine will perform better?

  • Machine with 2 cores, 1 thread per core (2 physical processors); or the
  • Machine with 2 cores, 2 threads per core (4 logical processors)

This is assuming everything (memory, clock speed, etc) is the same and the only thing that differ is the number of threads.

My initial thoughts would be that the former -- machine with 2 raw cores (1 thread) -- will perform better because it doesn't have context switching. Whereas the latter would have overhead on the context switching.

Which machine will perform better?

$\endgroup$
5
  • $\begingroup$ Are the threads CPU-bound, or do they spend time waiting for I/O or other resources? $\endgroup$
    – Pseudonym
    Commented Feb 9, 2021 at 22:59
  • $\begingroup$ @Pseudonym assuming a generic process, they spend some amount of time waiting for I/O or other resources $\endgroup$ Commented Feb 9, 2021 at 23:02
  • $\begingroup$ but most of the time, processors reach 100% utilization $\endgroup$ Commented Feb 9, 2021 at 23:11
  • $\begingroup$ You don't let on whether to assume said single generic process multi-threaded or not. $\endgroup$
    – greybeard
    Commented Feb 10, 2021 at 8:15
  • $\begingroup$ en.wikipedia.org/wiki/Hyper-threading#Performance_claims $\endgroup$
    – D.W.
    Commented Feb 15, 2021 at 23:30

2 Answers 2

3
$\begingroup$

There's no real way to answer this without a lot of details about both the code to be executed, and the micro-architecture of the CPU executing the code.

A CPU that can execute multiple threads per core will typically start with a pool of instructions, decoded and ready to execute. As instructions enter the pool, a scoreboard1 gets updated to reflect inputs to an instruction, execution resource(s) needed by that instruction, and the output of the instruction.

So you might have an instruction like add r0, r1, r2, meaning add register 0 to register 1 and put the result in register 2. The inputs are registers 0 and 1, the execution resource needed is an integer adder, and the output is register 2.

Then there's a scheduler that looks at the resource usage and tries to find instructions that don't conflict, and each clock cycle, tries to find as many of those as possible to execute. As it does so, it updates the scoreboard so the next clock cycle, it'll know what resources are in use now.

That all leads to the basic question: given the parallel resources provided by this CPU, and the instructions in the streams to be executed, what is the average level of conflict between instructions?

The drastically simplified answer is that more often than not, executing two independent streams of instructions will result in more instructions that can execute with fewer conflicts, so overall speed will increase much more often than not. It's rarely a question of which is faster--only of how much speed you'll gain by executing more threads in parallel, and whether that gain is enough to justify the extra labor to make that work, or whether you'd have gained more by expending more the CPU budget on other things that could have made an even bigger difference (e.g., bigger cache, better branch prediction, etc.)

Goal

There's also room for a bit of question about your goal here. A single core executing two threads will typically execute each thread a little slower than that thread would execute on a core by itself. On the other hand, the overall throughput of the system will be somewhat greater than if you ran only one thread at a time.

For example, if you had a CPU that was split almost perfectly between two threads, each might run at 60% of the speed it would if it was the only thread running on a core.

So, if you really care almost exclusively about the performance of one thread, then running only one thread per core may well improve the speed of a thread when it is running.

At the same time, if you have two threads each running at 60% the speed it would on its own, your overall speed is 120% of what it would be with each thread running on its own, meaning you're getting about 20% more work done per unit time.

In a game (for one example) it's pretty common to have one thread that really matters, so you really want to optimize performance for one thread. If that's your case, you probably only want one thread per core.

On the other hand, with something like a server you often have lots of requests happening all the time, and one major concern is maximizing overall throughput. In such a case, more threads per core can make a lot more sense (but also note that total throughput isn't usually the only concern even in this case).

CPU Utilization

Since "CPU Utilization" was mentioned in a comment, I'll comment on it a bit as well. Generally speaking, when an operating system reports something like percentage of CPU utilization, it's basically irrelevant to questions like this.

An OS is basically just reporting what percentage of CPUs have threads assigned to execute on them at any given time. For example, if a single-threaded CPU executes an instruction to read from main memory, it could easily take 50-100 clock cycles to execute a single instruction. According to the OS, that's keeping the CPU 100% busy for those clock cycles--but most of the CPU's actual resources would be available, so it could perfectly well execute other instructions during that time.


  1. Well, the early versions were called "scoreboards". More recently there are more complex structures with other names, but I'm going to call all of them scoreboards to keep things simple.
$\endgroup$
1
  • $\begingroup$ These make a lot of sense. Unfortunately though, I can not answer the question: What is the average level of conflict between instructions? This is something unknown especially if you are not the developer of the software and just installed it. :( That's why the question I posted here is more of a very generic standpoint. $\endgroup$ Commented Feb 10, 2021 at 10:28
1
$\begingroup$

The machine with 2 threads per core will have always have more throughput than a machine with a single thread per core. A machine with more threads will be able to execute more threads concurrently and it reduces the idle time spent by a CPU's resource (example: Arithmetic Logic Unit). It does this by switching to a different thread if the current thread is stalled.

In the unlikely event that there are no threads to switch, there will neither be a performance gain nor a performance penalty since it uses the single thread and there is no context switching overhead. So a machine with more threads per core can never be slower than a machine with lesser threads per core.

Multithreading will be done only when there are multiple independent threads.

Conclusion: A multithreaded core will have better performance than a single-threaded core.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.