Can any program be run on multiple cores? Will the CPU decide that? Or can only certain programs run on multiple cores?

So I'm a little confused about the multicore CPUs - the impression I was initially under and what I thought I heard before was that some programs can't be run across multiple cores - those in which the input to another loop depends on the previous output, for example

On the other hand, I have seen others claim that seemingly any program can be run across multiple cores (would this have to be specified by the programmer (e.g. maybe by allocating threads), or would the CPU be able to distribute an intensive program across cores?)

At the same time though, I have heard others say that a process can run on one and only 1 core

I'm hoping someone can clear this up or provide some pointers for where I should look. I feel like perhaps what I'm reading - if not wrong - might at least be out of date or perhaps varies between processor model etc

Thanks in advance for any help though


Solution 1:

This is largely dependent on the program.

Some common programming languages I'm familiar with (e.g. C, C++) default to single-threaded, requiring explicit steps to make a program capable of being run on multiple cores / CPUs.

I believe there are other programming languages that are written in a way to make them easily parallelizable (made to run on multiple cores / CPUs) by default.

You can run more than one iteration of a single-threaded program on a multi-core system at the same time, with each one on a different core, but that doesn't make it multi-threaded. It doesn't speed anything up to e.g. encrypt a single file multiple times at the same time. You just end up with multiple copies of the same data.

If you want to perform the same operation on multiple different inputs, then it might make sense to do something like this, and this is definitely taking advantage of multiple CPUs / cores for increased performance. The individual iterations are still single-threaded, but because they are completely independent, you can run as many as you like at one time.

You are right about some of the limitations of parallelism. For each step that depends on the output of a previous step, you have to run those two steps in sequence. You can probably create a program that uses multiple threads for that, but you don't get any performance gains, so it wouldn't make sense to go to extra effort in general.

Another item worth noting is that a single core can typically only run a single process / thread at a time (hyper-threading is a clever stretch of this rule). This is because a single thread requires the core to maintain context that is swapped in when the thread starts executing, and out when execution switches to a new thread. This is different from the inverse that you stated above.