How many cores does the Pi 2 model B use by default when executing a command? The reason I am asking this is my friend told me to use the suffix -j4
when I ran the code sudo make
. He told me that it would use all cores, so I assumed it didn't use them all in the first place.
2 Answers
This depends on the command (and note this applies to all multicore systems, not just the Pi 2). Not all tasks can be run in parallel. By analogy, consider a simple math problem
3 * 4 + 5 =
None of this can be done in parallel. You have to multiply the first two numbers to get a result, then add that to 5, so for a task like this, no matter how many cores you have available and want to use, only one will be involved with the problem at any give point in time.
However,
3 * 4 + (6 / 2) =
Can be broken into two distinct, separate problems that can be solved in parallel.
Contemporary processors are not designed to analyse tasks and decide if and how they can be parallelized (to the extent that this might be possible, I am sure the cost of such runtime analysis would make the whole event less efficient). For this reason, and because making such judgements in advance spares any runtime analysis costs, programs can be written to exploit parallelism -- but this does not mean they always are.
GNU Make (and possibly some others; it's not specified by POSIX) includes the -j
switch for potentially running component tasks in parallel. Normally, it will do them in a predetermined, logical order, but if run with j
it is often simple enough to do some of them at the same time -- for example, if an executable has a set of prerequisite independent binary objects that also need compiling, those can be compiled independently in parallel first.
However, if they're already compiled and the only thing that has to be done is compile the executable, then from make's perspective there is no potential parallelism and using -j
will not make any difference. If you're not sure, it can't hurt. A common rule of thumb with this is to use one more than your total number of cores, because it's fine to have the OS queue them if make
tries to do too many things at once.
Some tasks run by make
may themselves involve parallelism, and this will apply regardless of -j
.
-
Thank you for straightening me out on this I now see the way it works! So in regards to selecting with
-j
what is the best way to determine the the amount of parallelism you should select? Commented Mar 15, 2017 at 23:39 -
2This has been discussed here: stackoverflow.com/questions/2499070/…– MadMikeCommented Mar 15, 2017 at 23:42
Separating a given task to be automatically processed by multiple CPUs, also called automatic parallelization, is generally a complex task. For some tasks (like compiling source-code) this is somewhat easier. It depends at the task at hand how difficult it is to be parallelized.
As a programmer, with most programming languages, you have the possibility to write a multi-threaded program, which will then use more than one core.
The bad news is:
- It's hard to write good multi-threaded programs.
- It's easy to write bad multi-threaded programs which, sometimes, enter in a race condition or a deadlock. Which are then again hard to debug.
So the number of cores used on your Raspberry Pi will solely depend on how the program was written. make
has the neat option to easily allow you to run with multiple parallel jobs with -j<num>
thus using more than a single core. Most other programs don't.
cat /proc/cpuinfo
orlscpu
to find out the number of cores/cpu