OpenMp Part-1: Introduction to Parallelism
What is Parallelism
To understand Parallelism or Parallel programing we first need to understand what is concurrency.
Concurrency is a condition of a system in which multiple tasks are active at the same time. Even on older hardware (computers having single core processors) concurrency can be achieved by switching between active threads. This results in the increase of the total program throughput. Whereas Parallelism is a subset of concurrency which is a condition of a system where multiple tasks are actually active at the same time. Meaning more then one thread is being processed at the same time. This can be achieved only when we have more then one thread available in our CPU. Most modern computer have two threads in each core and have upto 4 cores. That leaves us with a total of 8 threads. Therefore such a system can process 8 threads at the same time. This increases the throughput and reduced the processing time of programs.
Why Parallelism was needed
Parallelism is needed due to hardware limitation that we have reached. CPU clocks cannot be increased because the power consumed by the CPU is very high and there is a Thermal barrier upto which we can keep our CPU units under respectable temperatures. Many computers try out exotic cooling techniques which even involve use of liquid nitrogen. The whole point here is we have reached a limit to which we can increase the CPU clock and keep the power consumed and temperatures low. Though the power consumed can be reduced by adding more cores to the cpu. A simple comparison shows that if a single core single threaded cpu consumes about “P” power then a similar dual core CPU consumes almost “0.6*P” of power, reducing power consumption by 40%. But off course our programs need to be utilizing the number of cores and threads made available to them for much greater benefit.
Learn about this in dept here on StackExchange
OpenMP
OpenMP is a set of APIs for writing multi threaded applications that can utilise all the threads available in a cpu. It is basically a set of compiler derivatives and library runtime for parallel computing programs. It makes writing parallel programing far more easier then most of the other implementation.
So lets not waste time and install OpenMP on our system to get started…Installing OpenMP
Installing OpenMP is very simple and straight forward. For linux follow the below steps (don’t copy the “$” symbol, it is just a prompt that’ll be displayed in your terminal window)
Make sure you have gcc compiler installed. For doing so run the following in your terminal
$ gcc --version
If the above command displays the gcc version installed then proceed to the next step else run the following to install gcc (make sure you enter your password)
$ sudo apt install gcc -y
This command will install gcc on your linux machine. You can verify this by running the first command again which will now display your gcc version
Now we install OpenMP and configure it to be used by our gcc compiler. We can do this by running the following
$ sudo apt install libomp-dev
Last but not the least we need to tell OpenMP how many threads it needs to use. You can do so by running the following
$ cat >> ~/.bashrc <<EOF export OMP_NUM_THREADS=4 EOF
Here depending on your CPU, you can let OpenMP use more or less number of threads. For example my CPU has 8 threads available so I can choose to use all 8 or anything less then that. Important point to note here is that if your CPU has lower number of threads then make sure you set this to 2. All this command does is add “export OMP_NUM_THREADS=4” in the end of your user’s bashrc file which mostly holds the configuration of your bash terminal and is run each time a terminal is started.
Now you are all set to right your first program in OpenMP
HelloWorld.c
This will be last section of our first module. Here we will write our classic old and boring Hello World program to print Hello world in our terminal. Either copy paste or write the small hello world program as the following
#include <stdio.h>
#include <omp.h>
int main (char *args[]) {
#pragma omp parallel
{
printf("Hello World!\n");
}
return 0;
}
Now to compile this small program that uses OpenMP we run the following command in the terminal (make sure terminal has the same directory open where the file resides)
$ gcc -o hello.out -fopenmp yourFileName.c
Make sure you replace “yourFileName” with whatever you had named your file and hit enter. On successful compilation the above command will generate a hello.out
file in the same directory. To see the output we just do the following
./hello.out
Hmm, see something unusual. Though in our c program we only call printf
once but in the output we have multiple number of Hello World
line as an output. More precisely the number is exactly same as the number of threads we gave OpenMP to work with. More details about this in the next module, for now you can assume that the code inside of the curly braces after the line #pragma omp parallel
gets copied to the number of threads we gave to OpenMp and each thread runs it once. Hence resulting in multiple “Hello World”s in our terminal.