What is covered: In this tutorial, we explain how to speed up the CMAKE build/compile process on Linux Ubuntu by using all CPU processors/cores. You will be able to speed up the build process up to 5-10 times depending on how many CPU processors or cores you have.
Motivation: If you run cmake –build command, you will notice that in many cases, this command is very slow. It might even take an hour to build smaller to medium-sized projects. This is unacceptably long. Consequently, you need to learn how to use all the computer resources to speed up the build process. In fact, you can significantly speed up everything by executing the compilation in parallel on different cores. Another approach is to use ninja. However, we will not cover ninja in this tutorial.
As a demonstration example, we will build llama.cpp from source. Llama.cpp is a program that runs large language models locally.
The YouTube tutorial explaining all the steps is given below.
Installation Instructions
As a demonstration example, we will build llama.cpp from source. Llama.cpp is a program for running large language models locally. The original GitHub repository is given at this link
https://github.com/ggerganov/llama.cpp
First, let us install the necessary software
sudo apt install git
sudo apt install cmake
Create the test folder
cd ~
mkdir testFolder
cd testFolder
Then, clone the Llama.cpp remote repository
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp
As a reference, first run the slow version of cmake-build
cmake -B build
cmake --build build --config Release
You will see that the build process is very slow. Press CTRL+C to stop the installation process. Then erase the folder, and repeat the git clone. Then, get the cmake help:
cmake --build . --help
You will see the following instructions for running cmake on several CPUs at the same time
--parallel [<jobs>], -j [<jobs>]
= Build in parallel using the given number of jobs.
If <jobs> is omitted the native build tool's
default number is used.
The CMAKE_BUILD_PARALLEL_LEVEL environment variable
specifies a default parallel level when this option
is not given.
From this help, we see that to run cmake in parallel, we need to execute any of these two sets of commands
cmake -B build
cmake --build build --config Release --parallel $(nproc)
or
cmake -B build
cmake --build build --config Release -j $(nproc)