-1

There are a lot of articles that discuss multi-core myth. That, in order to really benefit from multiple cores, one needs to write parallel algorithms. Many of them mention Amdahl's law.

Lets assume for simplicity that we have a desktop computer with a 4-core commodity CPU. And assume that the goal is to improve our application performance, as well as overall system performance.

I wonder how CPU cores are used to perform tasks.

  • Whether threads from a single process are allocated all cores
  • Or threads from different processes are scheduled to run on different cores.

If the latter is the case, then why is the myth even discussed? Won't multitasking OSes always benefit from multi-core CPUs, even if all the processes are single threaded? Are threads from the same process more likely to be scheduled at the same time on multiple cores?

  • What are some factors that matter? CPU cache maybe? Some application related maybe? Why?
  • Why would you ever want to use parallel libraries/algorithms? After all, CPU resources are shared between all running processes and there are always enough of them.

Is there an "active process" notion? i.e. process that gets most attention from the scheduler. If so, then how much more attention does this process usually get?

Vakhtang
  • 307
  • 5
  • 9
  • 1
    This is a very broad topic, and in my opinion not appropriate for Stackoverflow. An answer to your questions would be very long and involved: certainly much longer than would comfortably fit here. You'd be better off doing some research into operating systems and then coming back with specific questions after you understand the basics of how they work. The short answer to your question is that modern operating systems take advantage of multi-core CPUs to the extent possible. Most do their scheduling on a per-thread rather than per-process basis. – Jim Mischel Nov 05 '14 at 15:11
  • 3
    The operating system schedules threads, not processes. The priority of threads can be raised when they are owned by a process that is the "foreground", interacting with the user. If you didn't write code that uses threads then a multicore processor is only useful when you run more than one process at the same time and those processes burn core. Not the common case on a personal computer. – Hans Passant Nov 05 '14 at 15:16
  • Voting -1 for broad question with no obvious programming impact (http://stackoverflow.com/help/on-topic) and very little research effort shown. You may try to ask your question at [Theoretical Computer Science Stack Exchange](http://cstheory.stackexchange.com/) or use pointers in http://stackoverflow.com/questions/26675766/threads-accessing-same-cache-line to learn something more before your retry to construct a practicall useful question – xmojmr Nov 05 '14 at 15:46
  • I am not a student and I have a lot of other things to research besides having a lot of work to do. I am not one of the lucky ones who is surrounded by geeks and can get answers to tough questions. However broad this question may be (and there is not one), what is wrong with answering some questions if you know the answers? "Whether threads from a single process are allocated all cores", oh so hard to answer... "Is there an "active process" notion?" oh so hard... Giving downvotes is easy. Answering questions is not... – Vakhtang Nov 05 '14 at 17:02
  • Thank you Hans Passant. You got my idea. Whatever short your answer may be, it helped me. I am not writing a thesis about this topic... I write programs. – Vakhtang Nov 05 '14 at 17:12
  • 2
    @user2248972 funnily enough, many SO contributors are skilled and experienced developers with 'a lot of work to do', for which they are paid $$$/hr. Attempting to offload your badly-researched question to them is likely to just piss them off. – Martin James Nov 06 '14 at 00:46
  • Consider a common use case where you have a single threaded video encoder running. You'll have one core going at 100% and the other three sitting at 1% running other threads/processes. Sure, you'll benefit from having multiple cores but how much more are you getting out of your CPU than a single core machine? The only way you're getting any real benefits is if you're running 3 other processes that are hitting 100% of the core they're scheduled on or you have a multithreaded video encoder. – tangrs Nov 06 '14 at 00:52
  • funnily enough, doing research and writing the answer does not take the same amount of time. and even more funnily, if you are so busy and paid hourly and so on, you don't have to write anything... not even downvote. I am not asking those people who get pissed off. I am asking those who will kindly support. If you don't have anything good to say, don't say anything at all. – Vakhtang Nov 06 '14 at 02:18
  • ...bb..but I WANT to downvote. – Martin James Nov 08 '14 at 00:12

2 Answers2

7

Whether threads from a single process are allocated all cores

Yes.

Or threads from different processes are scheduled to run on different cores.

Yes, that too.

If the latter is the case, then why is the myth even discussed? Won't multitasking OSes always benefit from multi-core CPUs, even if all the processes are single threaded?

To some extent, yes. But if that process is doing a lot of computation and the only one we care about at some particular time, the benefit will be pretty low.

On the other hand, it also means the process won't be as likely to be interrupted just because the OS has to do something like handle a disk interrupt, an arriving network packet, or something like that. Interrupting a process to handle some hardware task not only reduce the CPU time the process gets but it also pollutes the CPU caches causing the process to run more slowly when it resumes. So multi-core CPUs can allow a single-threaded process to command a core for a higher percentage of the time and in longer bursts.

Are threads from the same process more likely to be scheduled at the same time on multiple cores?

Typically no. Why would you want to do that? That would tend to degrade overall system performance as threads from the same process are more likely to step on each other's toes. You want the system to get other process' work done efficiently so you get the CPU back.

Is there an "active process" notion?

To some extent. Windows has precisely such a notion -- a "foreground process". Most OSes don't. But they do have a "dynamic priority boost" feature. Basically, if a process is sitting around doing nothing and then needs to do something, it is given some priority as a "reward". This allows a process that sits around waiting for work to be done to get its work done quickly and makes the system feel more interactive and responsive. It often makes little sense on servers, but it's helpful on desktops. Whether this is implemented on threads individually or on all the threads of a process as a group is implementation specific.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

If you run separate processes or threads that doesn't needs to interact each others then it will be far better having 4 cores rather then having just 1.

As soon as the processes or threads needs to share some data, you will get the overhead to serialize the access to the shared data.

A lot depends on how good an application is written to run on a multi-core CPU. It may happen in the worst case that trying to run an application on a 4-core CPU is slower than running it on a single core CPU; more likely the increase in performance would be far less than 100%.

Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
Giordano
  • 311
  • 2
  • 5