INTRODUCTION
This manual documents the usage of libgomp, the GNU implementation of the Openmp. Application Programming Interface (API) for multi-platform shared-memory parallel programming in C/C++ and Fortran.1.Enabling OpenMP
To activate the OpenMP extensions for C/C++ and Fortran, the compile-time flag -fopenmp must be specified. This enables the OpenMP directive
#pragma omp
in C/C++ and !$omp
directives in free form, c$omp
, *$omp
and !$omp
directives in fixed form, !$
conditional compilation sentinels in free form and c$
, *$
and !$
sentinels in fixed form, for Fortran. The flag also arranges for automatic linking of the OpenMP runtime library .
A complete description of all OpenMP directives accepted may be found in the OpenMP Application Program Interface manual, version 3.1.
2.Runtime Library Routines
The routines are structured in following three parts:
(i)Control threads, processors and the parallel environment.
Some of the functions are:-
opm_get_active_level- This function returns the nesting level for the active parallel blocks, which enclosing the calling call.
C/C++
Prototype:
int omp_get_active_level(void);
omp_get_level-
This function returns the nesting level for the parallel blocks, which enclose the calling call.
C/C++
Prototype:
int omp_get_level(void);
omp_get_max_threads-
Return the maximum number of threads used for the current parallel region that does not use the clause num_threads
.
C/C++
Prototype: | int omp_get_max_threads(void); |
omp_get_thread_num-
Returns a unique thread identification number within the current team. In a sequential parts of the program, omp_get_thread_num
always returns 0. In parallel regions the return value varies from 0 to omp_get_num_threads
-1 inclusive. The return value of the master thread of a team is always 0.
C/C++
Prototype: | int omp_get_thread_num(void); |
omp_in_parallel-
This function returns true
if currently running in parallel, false
otherwise. Here, true
and false
represent their language-specific counterparts.
C/C++
Prototype: | int omp_in_parallel(void); |
(ii)Initialize, set, test, unset and destroy simple and nested locks.
Some of the functions are:-
omp_init_lock-
Initialize a simple lock. After initialization, the lock is in an unlocked state.
C/C++
Prototype:
void omp_init_lock(omp_lock_t *lock);
omp_set_lock-
Before setting a simple lock, the lock variable must be initialized by omp_init_lock
. The calling thread is blocked until the lock is available. If the lock is already held by the current thread, a deadlock occurs.
C/C++
Prototype: void omp_set_lock(omp_lock_t *lock);
omp_test_lock-
Before setting a simple lock, the lock variable must be initialized by omp_init_lock
. Contrary to omp_set_lock
, omp_test_lock
does not block if the lock is not available. This function returns true
upon success, false
otherwise. Here, true
and false
represent their language-specific counterparts.
C/C++
Prototype: int omp_test_lock(omp_lock_t *lock);
omp_unset_lock-
A simple lock about to be unset must have been locked by omp_set_lock
or omp_test_lock
before. In addition, the lock must be held by the thread calling omp_unset_lock
. Then, the lock becomes unlocked. If one or more threads attempted to set the lock before, one of them is chosen to, again, set the lock to itself.
C/C++
Prototype:void omp_unset_lock(omp_lock_t *lock);
omp_destroy_lock-
Destroy a simple lock. In order to be destroyed, a simple lock must be in the unlocked state.
C/C++
Prototype: void omp_destroy_lock(omp_lock_t *lock);
(iii)Portable, thread-based, wall clock timer.
Functions are:-
omp_get_wtick-
Gets the timer precision, i.e., the number of seconds between two successive clock ticks.
C/C++
Prototype: | double omp_get_wtick(void); |
omp_get_wtime-
Elapsed wall clock time in seconds. The time is measured per thread, no guarantee can be made that two distinct threads measure the same time. Time is measured from some "time in the past", which is an arbitrary time guaranteed not to change during the execution of the program.
C/C++
Prototype: | double omp_get_wtime(void); |
3.Environment Variables
Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.
OMP_DYNAMIC – Enable or disable the dynamic adjustment of the number of threads within a team. The value of this environment variable shall be
TRUE
or FALSE
. If undefined, dynamic adjustment is disabled by default.
C/C++:
Prototype: | void omp_set_dynamic(int set); |
OMP_MAX_ACTIVE_LEVELS – Specifies the initial value for the maximum number of nested parallel regions. The value of this variable shall be a positive integer. If undefined, the number of active levels is unlimited.
C/C++
Prototype:
void omp_set_max_active_levels(int max_levels);
OMP_NESTED –Enable or disable nested parallel regions, i.e., whether team members are allowed to create new teams. The value of this environment variable shall be TRUE
or FALSE
. If undefined, nested parallel regions are disabled by default.
C/C++:
Prototype: | void omp_set_nested(int set); |
omp_set_num_threads-
Specifies the number of threads used by default in subsequent parallel sections, if those do not specify a num_threads
clause. The argument of omp_set_num_threads
shall be a positive integer.
C/C++:
Prototype: void omp_set_num_threads(int n);