3

I was trying to install OpenCV in Ubuntu based on few guides online. One of the guides is this one. It has the following line:

make -j $(($(nproc) + 1))

The nproc returns the number of processors/ threads available on the system. So, what is the advantage of going one higher than what's available?

Stephen Kitt
  • 434,908
skr
  • 157

1 Answers1

8

Most builds are I/O-limited, not CPU-limited, so while nproc is a decent starting point (see also How to determine the maximum number to pass to make -j option?), most builds can use more than that. This is especially true if you’re using small VMs to build, which you’ll often find on build farms; there you’d end up with -j 1 or -j 2, and using -j 2 or -j 3 will usually result in shorter build times without the risk associated with a formula such as $(nproc) * 2 (which could cause problems even on an 8-thread system, let alone the larger thread counts you find on servers).

Stephen Kitt
  • 434,908
  • Thank you for the reply. I still haven't quite understood how number of I/O jobs are tied to nproc. It seems like a technical topic that requires some research to develop a better intuition. Can you kindly provide some literature or books or other resources on the topic? – skr May 15 '19 at 14:49
  • 2
    They’re not tied to nproc. That’s the whole point of $(nproc) + 1. Starting from the beginning, if you run a linear build on a current system (or even a fifteen-year-old system), your CPU will be idle most of the time; so you want to increase the number of jobs run in parallel. The real limit on build time however isn’t CPU capacity, it’s I/O capacity, but there’s no handy metric to determine that. So nproc is a proxy: it tells you how many jobs you can run in parallel without exhausting your CPU resources at least. Adding one is a common technique to use a bit more CPU... – Stephen Kitt May 15 '19 at 14:57