OpenMP Runtime Library

by

in

The OpenMP runtime library provides functions to query and control parallel execution at runtime. These functions enable dynamic thread management, timing measurements, and environment queries. Understanding the runtime library is essential for writing flexible, portable parallel programs that can adjust few parameters based on different execution environments and provide performance feedback.

Core Concept

The OpenMP runtime library, accessed through omp.h, offers functions in four main categories:

Query Functions:

  • omp_get_thread_num() – Returns current thread ID
  • omp_get_num_threads() – Returns number of active threads
  • omp_get_max_threads() – Returns maximum available threads
  • omp_get_num_procs() – Returns number of processors

Control Functions:

  • omp_set_num_threads(int) – Sets thread count for next parallel region
  • omp_set_dynamic(int) – Enables/disables dynamic thread adjustment
  • omp_set_nested(int) – Enables/disables nested parallelism

Timing Functions:

  • omp_get_wtime() – Wall-clock time in seconds (high precision)
  • omp_get_wtick() – Timer precision in seconds

Lock Functions:

  • omp_init_lock(), omp_set_lock(), omp_unset_lock(), omp_destroy_lock()

Code Example

Demonstrate key runtime library functions for thread information and timing

OpenMP Implementation:

#include <stdio.h>
#include <omp.h>

int main() {
    double start, end;

    // Query system capabilities
    printf("Available processors: %d\n", omp_get_num_procs());
    printf("Max threads: %d\n", omp_get_max_threads());

    // Set threads for next parallel region
    omp_set_num_threads(4);

    // Start timing
    start = omp_get_wtime();

    #pragma omp parallel
    {
        int tid = omp_get_thread_num();
        int nthreads = omp_get_num_threads();

        #pragma omp master
        printf("Number of threads: %d\n", nthreads);

        printf("Thread %d: Hello!\n", tid);
    }

    // End timing
    end = omp_get_wtime();
    printf("Parallel region took %.6f seconds\n", end - start);
    printf("Timer precision: %.9f seconds\n", omp_get_wtick());

    return 0;
}

Expected Output:

Available processors: 8
Max threads: 8
Number of threads: 4
Thread 0: Hello!
Thread 1: Hello!
Thread 2: Hello!
Thread 3: Hello!
Parallel region took 0.000123 seconds
Timer precision: 0.000000001 seconds

Usage & Best Practices

When to Use

  • Performance Measurement: Use omp_get_wtime() for accurate timing
  • Dynamic Configuration: Query system at runtime instead of hardcoding values
  • Debugging: Print thread information to understand parallel execution
  • Adaptive Parallelism: Adjust thread count based on problem size

Best Practices

  • Always call omp_get_wtime() twice and compute difference for elapsed time
  • Use omp_get_num_procs() to determine available hardware parallelism
  • Call control functions outside parallel regions for predictable behavior
  • Check timer precision with omp_get_wtick() for measurement reliability

Common Mistakes

  • Avoid: Calling omp_get_num_threads() outside parallel regions (returns 1)
  • Avoid: Excessive calls to runtime functions in hot loops (performance overhead)

Key Takeaways

Summary:

  • OpenMP runtime library (omp.h) provides query, control, timing, and lock functions
  • omp_get_wtime() is the standard method for parallel performance measurement
  • Query functions provide thread and system information at runtime
  • Control functions enable dynamic configuration without recompilation
  • Understanding runtime functions is essential for portable, adaptable parallel programs

Quick Reference

Compilation:

gcc -fopenmp -o runtime_demo runtime_demo.c

References:

Go back to OpenMP tutorials.


Mandar Gurav Avatar

Mandar Gurav

Parallel Programmer, Trainer and Mentor


If you are new to Parallel Programming you can start here.



Beginner CUDA Fortran Hello World Message Passing Interface MPI Nvidia Nsight Systems NVPROF OpenACC OpenACC Fortran OpenMP PGI Fortran Compiler Profiling Vector Addition


Popular Categories