Thursday, 25 July 2013

OpenMP Getting Started

Posted by Unknown

openmp, OpenMP

What is OpenMP ?

OpenMP is an API for writing portable multi-threaded parallel programs. It consists of  specification of a set of compiler directives, environment variables and library routines carved to enable shared memory programming for C/C++ as well as Fortran programs. OpenMP stands for Open Multi Processing.

• OpenMP is managed by OpenMP Architecture Review Board(ARB).
In most of the cases it is much more easier to program in OpenMP than using POSIX threads(pthreads).
• It needs explicit compiler support, compilers such as GCC 4.2 onwards, Portland Group's pgcc, Solaris cc, Intel’s icc, SGI cc, IBM xlc, etc.

The beauty of OpenMP is that it enables one to carry out incremental parallelization of the sequential code. 

OpenMP Getting Started :

OpenMP is based on fork-join model, as shown in the figure. So, initially you have a single master thread which is doing the sequential part, which is forked into multiple threads whenever the parallel region is encountered. Once the work in the parallel region is accomplished, then the master thread again begins the succeeding work after parallel region.
OpenMP, openmp, OpenMP, openmp, OpenMP, openmp, OpenMP, openmp OpenMP Fork Join, OpenMP Getting Started, OpenMP Fork Join, OpenMP Getting Started, OpenMP Fork Join, OpenMP Getting Started, OpenMP Fork Join, OpenMP Getting Started, OpenMP Fork Join, OpenMP Getting Started, OpenMP Fork Join, OpenMP Getting Started
  • Initially to start with, the parallel region is been specified with the following compiler directive.
          #pragma omp parallel
                 //code for work to be done by each thread to be placed here.
  • To set the desired number of threads for your code can be done through the library routine omp_set_num_threads() , and another way for setting number of threads is by setting the environment variable OMP_NUM_THREADS to the desired number of threads value.
  • To get how many threads are running in the given parallel region one can use the library routine omp_get_num_threads()  , its return value is number of threads.
  •  Each thread can get its thread ID amongst the team of threads in the parallel region by using the library routine, omp_get_thread_num()
  •  To synchronize the threads to carry out some work which needs mutual exclusion, one can use, 
        #pragma omp critical

        {               // Code which requires mutual exclusion
         Another mechanism is to use library routine omp_set_lock(lock), also if  mutual exclusion is needed for a single instruction then its good to use,  #pragma omp atomic  
Following is the Hello World OpenMP program using C consisting of 4 threads.

int main()

    #pragma omp parallel
        printf("\nHello World from thread ID - %d from total_threads - %d\n", omp_get_thread_num(), omp_get_thread_num());
For compiling the above code using GCC 4.2 or higher version following command need to be used.

gcc "your_prog_name.c" -fopenmp
The output of this program would be something like


1 comment: