OpenMP* provides several run-time library routines to help you manage your program in parallel mode. Many of these run-time library routines have corresponding environment variables that can be set as defaults. The run-time library routines let you dynamically change these factors to assist in controlling your program. In all cases, a call to a run-time library routine overrides any corresponding environment variable.
This topic provides a summary of the OpenMP run-time library routines. See OpenMP* Support Overview for additional resources; refer to the OpenMP API Version 3.0 specification for detailed information about using these routines.
Include the appropriate declarations of the routines by adding a statement similar to the following in your source code:
Example |
---|
include "omp_lib.h" |
#include <omp.h> |
The header files are provided in the ../include (Linux* and Mac OS* X) or ..\include (Windows*) directory of your compiler installation.
The following tables specify the interfaces to these routines. (The names for the routines are in user name space.)
Use these routines to monitor and influence threads and the parallel environment.
Function |
Description |
---|---|
void omp_set_num_threads(int nthreads) |
Sets the number of threads to use for subsequent parallel regions created by the calling thread. |
int omp_get_num_threads(void) |
Returns the number of threads that are being used in the current parallel region. This function does not return the value inherited by the calling thread from the omp_set_num_threads() function. |
int omp_get_max_threads(void) |
Returns the number of threads available to subsequent parallel regions created by the calling thread. This function returns the value inherited by the calling thread from the omp_set_num_threads() function. |
int omp_get_thread_num(void) |
Returns the thread number of the calling thread, within the context of the current parallel region.. |
int omp_get_num_procs(void) |
Returns the number of processors available to the program. |
int omp_in_parallel(void) |
Returns TRUE (non zero) if called within the dynamic extent of a parallel region executing in parallel; otherwise returns FALSE (an integer value of zero). |
void omp_set_dynamic(intdynamic_threads) |
Enables or disables dynamic adjustment of the number of threads used to execute a parallel region. If dynamic_threads is TRUE, dynamic threads are enabled. If dynamic_threads is FALSE, dynamic threads are disabled. Dynamics threads are disabled by default. |
int omp_get_dynamic(void) |
Returns TRUE if dynamic thread adjustment is enabled, otherwise returns FALSE. |
void omp_set_nested(intnested) |
Enables or disables nested parallelism. If nested is TRUE, nested parallelism is enabled. If nested is FALSE, nested parallelism is disabled. Nested parallelism is disabled by default. |
int omp_get_nested(void) |
Returns TRUE if nested parallelism is enabled, otherwise returns FALSE |
void omp_set_schedule(omp_sched_t kind,int modifier) |
Determines the schedule of a worksharing loop that is applied when 'runtime' is used as schedule kind. |
void omp_get_schedule(omp_sched_t *kind,int *modifier) |
Returns the schedule of a worksharing loop that is applied when the 'runtime' schedule is used. |
int omp_get_thread_limit(void) |
Returns the maximum number of simultaneously executing threads in an OpenMP* program. |
void omp_set_max_active_levels(int max_active_levels) |
Limits the number of nested active parallel regions. The call is ignored if negative max_active_levels specified. |
int omp_get_max_active_levels(void) |
Returns the maximum number of nested active parallel regions. |
int omp_get_active_level(void) |
Returns the number of nested, active parallel regions enclosing the task that contains the call. |
int omp_get_level(void) |
Returns the number of nested parallel regions (whether active or inactive) enclosing the task that contains the call, not including the implicit parallel region. |
int omp_get_ancestor_thread_num(int level) |
Returns the thread number of the ancestor at a given nest level of the current thread. |
int omp_get_team_size(int level) |
Returns the size of the thread team to which the ancestor belongs. |
Use these routines to affect OpenMP locks.
Function |
Description |
---|---|
void omp_init_lock(omp_lock_t lock) |
Initializes the lock associated with lock for use in subsequent calls. |
void omp_destroy_lock(omp_lock_t lock) |
Causes the lock specified by lock to become undefined or uninitialized. |
void omp_set_lock(omp_lock_t lock) |
Forces the executing thread to wait until the lock associated with lock is available. The thread is granted ownership of the lock when it becomes available. |
void omp_unset_lock(omp_lock_t lock) |
Releases the executing thread from ownership of the lock associated with lock. The behavior is undefined if the executing thread does not own the lock associated with lock. |
int omp_test_lock(omp_lock_t lock) |
Attempts to set the lock associated with lock. If successful, returns TRUE, otherwise returns FALSE. |
void omp_init_nest_lock(omp_nest_lock_t lock) |
Initializes the nested lock associated with lock for use in the subsequent calls. |
void omp_destroy_nest_lock(omp_nest_lock_t lock) |
Causes the nested lock associated with lock to become undefined or uninitialized. |
void omp_set_nest_lock(omp_nest_lock_t lock) |
Forces the executing thread to wait until the nested lock associated with lock is available. The thread is granted ownership of the nested lock when it becomes available. If the thread already owns the lock, then the lock nesting count is incremented. |
void omp_unset_nest_lock(omp_nest_lock_t lock) |
Releases the executing thread from ownership of the nested lock associated with lock if the nesting count is zero; otherwise, the nesting count is decremented. Behavior is undefined if the executing thread does not own the nested lock associated with lock. |
int omp_test_nest_lock(omp_nest_lock_t lock) |
Attempts to set the nested lock specified by lock. If successful, returns the nesting count, otherwise returns zero. |
Function |
Description |
---|---|
double omp_get_wtime(void) |
Returns a double precision value equal to the elapsed wall clock time (in seconds) relative to an arbitrary reference time. The reference time does not change during program execution. |
double omp_get_wtick(void) |
Returns a double precision value equal to the number of seconds between successive clock ticks. |