# OpenMP* Run-time Library Routines

OpenMP* provides 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.

### CAUTION

Running OpenMP runtime library routines may initialize the OpenMP runtime environment, which might cause a situation where subsequent programmatic setting of OpenMP environment variables has no effect. To avoid this situation, you can use the Intel extension routine `kmp_set_defaults()` to set OpenMP environment variables.

The compiler supports all the OpenMP run-time library routines. Refer to the OpenMP API specification for detailed information about using these routines.

Include the appropriate declarations of the routines in your source code by adding a statement similar to the following:

    #include <omp.h>

The header files are provided in the ../include (Linux* and macOS) or ..\include (Windows*) directory of your compiler installation.

## Thread Team Routines

Routines that affect and monitor thread teams in the current contention group.

Routine | 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 necessarily 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.
`int omp_get_thread_num(void)` | Returns the thread number of the calling thread, within the context of the current parallel region.
`int omp_in_parallel(void)` | Returns TRUE if called within the dynamic extent of a parallel region executing in parallel; otherwise returns FALSE.
`void omp_set_dynamic(int dynamic_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. Dynamic threads are disabled by default.
`int omp_get_dynamic(void)` | Returns TRUE if dynamic thread adjustment is enabled, otherwise returns FALSE.
`int omp_get_cancellation(void)` | Returns TRUE if cancellation is enabled, otherwise returns FALSE. This routine can be affected by the setting for environment variable `OMP_CANCELLATION`.
`void omp_set_nested(int nested)` ### Note This has been deprecated. | 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)` ### Note This has been deprecated. | Returns TRUE if nested parallelism is enabled, otherwise returns FALSE.
`void omp_set_schedule(omp_sched_t kind,int chunk_size)` | Determines the schedule of a worksharing loop that is applied when '`runtime`' is used as the schedule kind.
`void omp_get_schedule(omp_sched_kind *kind,int *chunk_size)` | 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.
`int omp_get_supported_active_levels(void)` | Returns the number of active levels of parallelism supported by the implementation.
`void omp_set_max_active_levels(int max_active_levels)` | Limits the number of nested active parallel regions. The value of `max_active_levels` must evaluate to a non-negative integer.
`int omp_get_max_active_levels(void)` | Returns the maximum number of nested active parallel regions.
`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 or the current thread belongs for a given nested level.
`int omp_get_active_level(void)` | Returns the number of nested, active parallel regions enclosing the task that contains the call.

## Thread Affinity Routines

Routines that affect and access thread affinity policies that are in effect.

Function | Description
--- | ---
`omp_proc_bind_t omp_get_proc_bind(void)` | Returns the currently active thread affinity policy, which can be initialized by the environment variable OMP_PROC_BIND. This policy is used for subsequent nested parallel regions.
`int omp_get_num_places(void)` | Returns the number of places available to the execution environment in the place list of the initial task, usually threads, cores, or sockets.
`int omp_get_place_num_procs(int place_num)` | Returns the number of processors associated with the place numbered `place_num`. The routine returns zero when `place_num` is negative or is greater than or equal to omp_get_num_places().
`void omp_get_place_proc_ids(int place_num, int *ids)` | Returns the numerical identifiers of each processor associated with the place numbered `place_num`. The numerical identifiers are non-negative and their meaning is implementation defined. The numerical identifiers are returned in the array `ids` and their order in the array is implementation defined.The array `ids` must be sufficiently large to contain `omp_get_place_num_procs(place_num)` elements. The routine has no effect when `place_num` is negative or greater than or equal to omp_get_num_places().
`int omp_get_place_num(void)` | Returns the place number of the place to which the encountering thread is bound. The returned value is between 0 and omp_get_num_places() - 1, inclusive. When the encountering thread is not bound to a place, the routine returns -1.
`int omp_get_partition_num_places(void)` | Returns the number of places in the place partition of the innermost implicit task.
`void omp_get_partition_place_nums(int *place_nums)` | Returns the list of place numbers corresponding to the places in the place-partition-var ICV of the innermost implicit task. The array `place_nums` must be sufficiently large to contain `omp_get_partition_num_places()` elements.
`void omp_set_affinity_format(const char *format)` | Sets the affinity format to be used on the device by setting the value of the affinity-format-var ICV.
`size_t omp_get_affinity_format(char *buffer, size_t size)` | Returns the value of the affinity-format-var ICV on the device.
`void omp_display_affinity(const char *format)` | Prints the OpenMP thread affinity information using the format specification provided.
`size_t omp_capture_affinity(char *buffer, size_t size, const char *format)` | Prints the OpenMP thread affinity information into a buffer using the format specification provided.

## Teams Region Routines

Routines that affect and monitor the league of teams that may execute a teams region.

Function | Description
--- | ---
`int omp_get_num_teams(void)` | Returns the number of initial teams in the current teams region.
`int omp_get_team_num(void)` | Returns the initial team number of the calling thread.
`void omp_set_num_teams(int num_teams)` | Affects the number of threads to be used for subsequent teams regions that do not specify a `num_teams` clause.
`int omp_get_max_teams(void)` | Returns an upper bound on the number of teams that could be created by a teams construct without a `num_teams` clause that is encountered after execution returns from this routine.
`void omp_set_teams_thread_limit(int thread_limit)` | Defines the maximum number of OpenMP threads that can participate in each contention group created by a teams construct.
`int omp_get_teams_thread_limit(void)` | Returns the maximum number of OpenMP threads available to participate in each contention group created by a teams construct.

## Tasking Routines

Routines that pertain to OpenMP explicit tasks.

Function | Description
--- | ---
`int omp_get_max_task_priority(void)` | Returns the maximum value that can be specified in the priority clause.
`int omp_in_explicit_task(void)` | Returns TRUE if called within an explicit task region; otherwise returns FALSE.
`int omp_in_final(void)` | Returns TRUE if called within a final task region; otherwise returns FALSE.

## Resource Relinquishing Routines

Routines that relinquish resources used by the OpenMP runtime. These routines are only effective on the host device.

Function | Description
--- | ---
`int omp_pause_resource(omp_pause_resource_t kind, int device_num)` | Allows the runtime to relinquish resources used by OpenMP on the specified device. The routine returns zero in case of success, and non-zero otherwise.
`int omp_pause_resource_all(omp_pause_resource_t kind)` | Allows the runtime to relinquish resources used by OpenMP on all devices. The routine returns zero in case of success, and non-zero otherwise.

## Device Information Routines

Routines that pertain to the set of devices that are accessible to an OpenMP program.

Function | Description
--- | ---
`int omp_get_num_procs(void)` | Returns the number of processors available to the program.
`void omp_set_default_device(int device_number)` | Sets the default device number.
`int omp_get_default_device(void)` | Returns the default device number.
`int omp_get_num_devices(void)` | Returns the number of target devices.
`int omp_get_device_num(void)` | Returns the device number of the device on which the calling thread is executing.
`int omp_is_initial_device(void)` | Returns TRUE if the current task is running on the host device; otherwise, FALSE.
`int omp_get_initial_device(void)` | Returns the device number of the host device. The value of the device number is implementation defined. If it is between 0 and omp_get_num_devices()-1, then it is valid in all device constructs and routines; if it is outside that range, then it is only valid in the device memory routines and not in the device clause.

## Lock Routines

Use these routines to affect OpenMP locks.

Function | Description
--- | ---
`void omp_init_lock(omp_lock_t *lock)` | Initializes the lock to the unlocked state.
`void omp_init_nest_lock(omp_nest_lock_t *lock)` | Initializes the nested lock to the unlocked state. The nesting count for the nested lock is set to zero.
`void omp_init_lock_with_hint(omp_lock_t *lock, omp_sync_hint_t hint)` | Initializes the lock to the unlocked state, optionally choosing a specific lock implementation based on `hint`. See the OpenMP specification for the available hints.
`void omp_init_nest_lock_with_hint(omp_nest_lock_t *lock, omp_sync_hint_t hint)` | Initializes the nested lock to the unlocked state, optionally choosing a specific lock implementation based on `hint`. The nesting count for the nested lock is set to zero. See the OpenMP specification for the available hints.
`void omp_destroy_lock(omp_lock_t *lock)` | Changes the state of the lock to uninitialized.
`void omp_destroy_nest_lock(omp_nest_lock_t *lock)` | Changes the state of the nested lock to uninitialized.
`void omp_set_lock(omp_lock_t *lock)` | Forces the executing thread to wait until the lock is available. The thread is granted ownership of the lock when it becomes available.
`void omp_set_nest_lock(omp_nest_lock_t *lock)` | Forces the executing thread to wait until the nested lock is available. If the thread already owns the lock, then the lock nesting count is incremented.
`void omp_unset_lock(omp_lock_t *lock)` | Releases the executing thread from ownership of the lock. The behavior is undefined if the executing thread does not own the lock.
`void omp_unset_nest_lock(omp_nest_lock_t *lock)` | Decrements the nesting count for the nested lock and releases the executing thread from ownership of the nested lock if the resulting nesting count is zero. Behavior is undefined if the executing thread does not own the nested lock.
`int omp_test_lock(omp_lock_t *lock)` | Attempts to set the lock. If successful, returns TRUE, otherwise returns FALSE.
`int omp_test_nest_lock(omp_nest_lock_t *lock)` | Attempts to set the nested lock. If successful, returns the nesting count, otherwise returns zero.

## Timing Routines

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.

## Event Routines

Function | Description
--- | ---
`void omp_fulfill_event(omp_event_handle_t event)` | Fulfills the event associated with the event handle `event` and destroys the event.

## Memory Management Routines

Function | Description
--- | ---
`omp_allocator_handle_t omp_init_allocator(omp_memspace_handle_t memspace, int ntraits, const omp_alloctrait_t traits[])` | Creates a new allocator that is associated with the `memspace` memory space and returns a handle to it.
`void omp_destroy_allocator(omp_allocator_handle_t allocator)` | Releases all resources used to implement the allocator handle.
`void omp_set_default_allocator(omp_allocator_handle_t allocator)` | Sets the default memory allocator to be used by allocation calls, allocate directives and allocate clauses that do not specify an allocator.
`omp_allocator_handle_t omp_get_default_allocator(void)` | Returns a handle to the memory allocator to be used by allocation calls, allocate directives and allocate clauses that do not specify an allocator.
`void *omp_alloc(size_t size, omp_allocator_handle_t allocator)` | Requests a memory allocation of size bytes from the specified memory allocator.
`void *omp_aligned_alloc(size_t alignment, size_t size, omp_allocator_handle_t allocator)` | Requests a memory allocation of `size` bytes from the specified memory allocator. Memory allocated by `omp_aligned_alloc` will be byte-aligned to at least the maximum of the alignment required by malloc, the alignment trait of the allocator and the alignment argument value.
`void omp_free(void *ptr, omp_allocator_handle_t allocator)` | Deallocates the memory to which `ptr` points. The `ptr` argument must have been returned by an OpenMP allocation routine.
`void *omp_calloc(size_t nmemb, size_t size, omp_allocator_handle_t allocator)` | Requests a memory allocation from the specified memory allocator for an array of `nmemb` elements each of which has a size of `size` bytes.
`void *omp_aligned_calloc(size_t alignment, size_t nmemb, size_t size, omp_allocator_handle_t allocator)` | Requests a memory allocation from the specified memory allocator for an array of `nmemb` elements each of which has a size of `size` bytes. Memory allocated by `omp_aligned_calloc` will be byte-aligned to at least the maximum of the alignment required by malloc, the alignment trait of the allocator and the alignment argument value.
`void *omp_realloc(void *ptr, size_t size, omp_allocator_handle_t allocator, omp_allocator_handle_t free_allocator)` | Deallocates the memory to which `ptr` points and requests a new memory allocation of `size` bytes from the specified memory allocator. Upon success it returns a pointer to the allocated memory and the contents of the new object shall be the same as that of the old object prior to deallocation up to the minimum size of old allocated size and `size` argument.

## Tool Control Routines

Function | Description
--- | ---
`int omp_control_tool(int command, int modifier, void *arg)` | Enables a program to pass commands to an active tool.

## Environment Display Routines

Function | Description
--- | ---
`void omp_display_env(int verbose)` | Displays the OpenMP version number and the initial values of ICVs associated with the environment variables.

Parent topic: OpenMP* Library Support

## See Also

Intel Extension Routines to OpenMP*