Information About

Openmp




The OpenMP (Open Multi-Processing) is an Application Programming Interface (API) that supports multi-platform Shared Memory Multiprocessing programming in C / C++ and Fortran on many architectures, including Unix and Microsoft Windows platforms. It consists of a set of Compiler Directive s, library routines, and Environment Variable s that influence run-time behavior.

Jointly defined by a group of major computer hardware and software vendors, OpenMP is a portable, scalable model that gives Programmer s a simple and flexible interface for developing parallel applications for platforms ranging from the desktop to the Supercomputer .

Often a so-called hybrid-model for Parallel Programming , programming in Computer Cluster s can be done using both OpenMP and MPI ( Message Passing Interface ) simultaneously.


INTRODUCTION

where the master thead forks off a number of threads which execute blocks of code in parallel]]
OpenMP is an implementation of Multithreading , a method of parallelization whereby the master "thread" (a series of instructions executed consecutively) "forks" a specified number of slave "threads" and a task is divided among them. The threads then run concurrently, with the Runtime Environment allocating threads to different processors.

The section of code that is meant to run in parallel is marked accordingly, with a Preprocessor Directive that will cause the threads to form before the section is executed. Each thread has an "id" attached to it which can be obtained using a Function (called omp_get_thread_num() in C / C++ and OMP_GET_THREAD_NUM() in FORTRAN ). The thread id is an integer, and the master thread has an id of "0". After the execution of the parallelized code, the threads "join" back into the master thread, which continues onward to the end of the program.

By default, each thread executes the parallelized section of code independently. "Work-sharing constructs" can be used to divide a task among the threads so that each thread executes its allocated part of the code. Both Task Parallelism and Data Parallelism can be achieved using OpenMP in this way.

The runtime environment allocates threads to processors depending on usage, machine load and other factors. The number of threads can be assigned by the runtime environment based on Environment Variables or in code using functions. The OpenMP functions are included in a Header File labelled "omp.h" in C / C++ .


HISTORY

The OpenMP Architecture Review Board (ARB) published its first standard, OpenMP for FORTRAN 1.0, in October of 1997. October the following year they released the C/C++ standard. 2000 saw version 2.0 of the FORTRAN standard with version 2.0 of the C/C++ standard being released in 2002. The current version is 2.5. It is a combined C/C++/FORTRAN standard, which was released in 2005.


THE CORE ELEMENTS


The core elements of OpenMP are the constructs for thread creation, work load distribution (work sharing), data environment management, thread synchronization, user level runtime routines and environment variables.

A compiler directive in C/C++ is called a ''pragma'' (pragmatic information). It is a Preprocessor Directive so is declared with a hash (#). Compiler directives specific to OpenMP in C/C++ are written in codes as follows:

#pragma omp

The OpenMP specific pragmas are listed below:


Thread creation

''omp parallel''. It is used to fork additional threads to carry out the work enclosed in the construct in parallel. The original process will be denoted as master thread with thread ID 0.

Example: Display "Hello, world" using multiple threads.


#pragma omp parallel
printf("Hello, world.
");
return 0;
}


Work-sharing constructs

used to specify how to assign independent work to one or all of the threads.
  • ''omp for'' or ''omp do'': used to split up loop iterations among the threads

  • ''sections'': assigning consecutive but independent code blocks to different threads

  • ''single'': specifying a code block that is executed by only one thread, a barrier is implied in the end

  • ''master'': similar to single, but the code block will be executed by the master thread only and no barrier implied in the end.

  • Example: initialize the value of a large array in parallel, using each thread to do a portion of the work


#define N 100000

int i, a {Link without Title} ;
#pragma omp parallel for
for (i=0;i
  • i;

  • return 0;

}


OpenMP Clauses

Since OpenMP is a shared memory programming model, most variables in OpenMP code are visible to all threads by default. But sometimes private variables are necessary to avoid Race Condition and there is a need to pass values between the sequential part and the parallel region (the code block executed in parallel), so data environment management is introduced as ''data clauses'' by appending them to the OpenMP directive. The different types of clauses are


Data Scoping Clauses

  • ''shared'': the data within a parallel region is shared, which means visible and accessible by all threads simultaneously. By default, all variables in the work sharing region are shared except the loop iteration counter.

  • ''private'': the data within a parallel region is private to each thread, which means each thread will have a local copy and use it as a temporary variable. A private variable is not initialized and the value is not maintained for use outside the parallel region. By default, the loop iteration counter in the work-sharing region (if any) is private.

  • ''default'': allows the programmer to state that the default data scoping within a parallel region will be either ''shared'', ''private'', or ''none''. The ''none'' option forces the programmer to declare each variable in the parallel region as either shared or private.



Synchronization clauses

  • ''critical section'': the enclosed code block will be executed by all threads but only one thread at a time, not simultaneously executed. It is often used to protect shared data from Race Condition .

  • ''atomic'': similar to ''critical section'', but advise the compiler to use special hardware instructions for better performance. Compilers may choose to ignore this suggestion from users and use ''critical section'' instead.

  • ''ordered'': the structure block is executed in the order in which iterations would be executed in a sequential loop

  • ''barrier'': each thread waits until all of the other threads of a team have reached this point. A work-sharing construct has an implicit barrier synchronization at the end.

  • ''nowait'': specifies that threads completing assigned work can proceed. In the absence of this clause, threads would encounter a barrier synchronization at the end of the work sharing construct by default.



Scheduling clauses

  • ''schedule(type, chunk)'':This is useful if the work sharing construct is a do-loop or for-loop. The iteration(s) in the work sharing construct are allocated to threads. The scheduling of the threads are controlled by this clause. The three types of scheduling are:

  • #''static'': Here,all the threads are allocated iterations before they execute the loop iterations. The iterations are divided among threads equally by default.However, specifying an integer for the parameter "chunk" will allocate "chunk" number of contiguous iterations to a particular thread.

#''dynamic'':Here,some of the iterations are allocated to a smaller number of threads.Once a particular thread finishes its allocated iteration, it returns to get another one from the iterations that are left. The parameter "chunk" defines the number of contiguous iterations that are allocated to a thread at a time.
#''guided'':A large chunk of contiguous iterations are allocated to each thread dynamically (as above). The chunk size decreases exponentially with each successive allocation to a minimum size specified in the parameter "chunk"

IF control

  • ''if'': This will cause the threads to parallelize the task only if a condition is met. Otherwise the code block executes serially.



Initialization

  • ''firstprivate'': the data is private to each thread, but initialized using the value of the variable using the same name from the master thread.

  • ''lastprivate'': the data is private to each thread. The value of this private data will be copied to a global variable using the same name outside the parallel region if current iteration is the last iteration in the parallelized loop. A variable can be both ''firstprivate'' and ''lastprivate''.

  • ''threadprivate'': The data is a global data, but it is private in each parallel region during the runtime. The difference between ''threadprivate'' and ''private'' is the global scope associated with threadprivate and the preserved value across parallel regions.

  • Data copying

  • ''copyin'': similar to ''firstprivate'' for ''private'' variables, ''threadprivate'' variables are not initialized, unless using ''copyin'' to pass the value from the corresponding global variables. No ''copyout'' is needed because the value of a threadprivate variable is maintained throughout the execution of the whole program.

  • ''copyprivate'': used with ''single'' to support the copying of data values from private objects on one thread (the ''single'' thread) to the corresponding objects on other threads in the team.



Reduction