OpenMP | Hello World program - GeeksforGeeks

29 Jun, 2021
Prerequisite: OpenMP | Introduction with Installation GuideIn C/C++/Fortran, parallel programming can be achieved using OpenMP. In this article, we will learn how to create a parallel Hello World Program using OpenMP.STEPS TO CREATE A PARALLEL PROGRAM  
Include the header file: We have to include the OpenMP header for our program along with the standard header files.  
Include the header file: We have to include the OpenMP header for our program along with the standard header files.  
//OpenMP header
#include <omp.h>
 Specify the parallel region: In OpenMP, we need to mention the region which we are going to make it as parallel using the keyword pragma omp parallel. The pragma omp parallel is used to fork additional threads to carry out the work enclosed in the parallel. The original thread will be denoted as the master thread with thread ID 0. Code for creating a parallel region would be,  
 
Specify the parallel region: In OpenMP, we need to mention the region which we are going to make it as parallel using the keyword pragma omp parallel. The pragma omp parallel is used to fork additional threads to carry out the work enclosed in the parallel. The original thread will be denoted as the master thread with thread ID 0. Code for creating a parallel region would be,  
#pragma omp parallel
{
  //Parallel region code 
} 
So, here we include  
So, here we include  
#pragma omp parallel                   
{
    printf("Hello World... from thread = %d\n", 
           omp_get_thread_num());
}  
 Set the number of threads: we can set the number of threads to execute the program using the external variable.  
 
Set the number of threads: we can set the number of threads to execute the program using the external variable.  
export OMP_NUM_THREADS=5
Diagram of parallel region  
Diagram of parallel region  
As per the above figure, Once the compiler encounters the parallel regions code, the master thread(thread which has thread id 0) will fork into the specified number of threads. Here it will get forked into 5 threads because we will initialise the number of threads to be executed as 5, using the command export OMP_NUM_THREADS=5. Entire code within the parallel region will be executed by all threads concurrently. Once the parallel region ended, all threads will get merged into the master thread. Compile and Run: Compile:  
As per the above figure, Once the compiler encounters the parallel regions code, the master thread(thread which has thread id 0) will fork into the specified number of threads. Here it will get forked into 5 threads because we will initialise the number of threads to be executed as 5, using the command export OMP_NUM_THREADS=5. Entire code within the parallel region will be executed by all threads concurrently. Once the parallel region ended, all threads will get merged into the master thread. 
Compile and Run: Compile:  
gcc -o hello -fopenmp hello.c
Execute:  
Execute:  
./hello
 
 
Below is the complete program with the output of the above approach:Program: Since we specified the number of threads to be executed as 5, 5 threads will execute the same print statement at the same point of time. Here we can’t assure the order of execution of threads, i.e Order of statement execution in the parallel region won’t be the same for all executions. In the below picture, while executing the program for first-time thread 1 gets completed first whereas, in the second run, thread 0 completed first. omp_get_thread_num() will return the thread number associated with the thread. 
OpenMP Hello World program
// OpenMP program to print Hello World// using C language // OpenMP header#include <omp.h> #include <stdio.h>#include <stdlib.h> int main(int argc, char* argv[]){     // Beginning of parallel region    #pragma omp parallel    {         printf("Hello World... from thread = %d\n",               omp_get_thread_num());    }    // Ending of parallel region}
Output:  
When run for 1st time:  
 
When run for multiple time: Order of execution of threads changes every time.  
 
 
abhishek0719kadiyan
OpenMP
Articles
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Time Complexity and Space Complexity
Mutex vs Semaphore
Analysis of Algorithms | Set 3 (Asymptotic Notations)
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Arrays in C/C++
std::sort() in C++ STL
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++