Table of Contents

Overview (OpenMP)

Quick reference for OpenMP directives, functions, and environment variables.

Directives

#pragma omp parallel                         // fork a team of threads; join at closing brace
#pragma omp parallel for                     // distribute loop iterations across the team
#pragma omp parallel for reduction(+:s)     // loop with a parallel reduction
#pragma omp parallel sections                // distribute independent blocks across the team
#pragma omp section                          // one block inside a sections region
#pragma omp single                           // one thread runs the block; others wait at end
#pragma omp master                           // thread 0 only; no implicit barrier
#pragma omp task                             // package work for any idle thread to execute
#pragma omp taskwait                         // wait for all child tasks to finish
#pragma omp barrier                          // all threads wait until every thread arrives
#pragma omp critical                         // mutual exclusion: one thread at a time
#pragma omp atomic                           // single hardware-atomic read-modify-write
#pragma omp simd                             // assert the loop is safe to vectorise
#pragma omp flush                            // enforce memory visibility across threads

Functions

omp_get_thread_num()      // ID of the calling thread (0 … N-1)
omp_get_num_threads()     // number of threads in the current team
omp_get_max_threads()     // threads that would be used if a parallel region started now
omp_set_num_threads(n)    // set the default thread count at runtime
omp_get_num_procs()       // number of logical processors available to the program
omp_get_wtime()           // wall-clock time in seconds; use for timing parallel regions
omp_in_parallel()         // 1 if called from inside a parallel region, 0 otherwise

Environment variables

Variable Default Description
OMP_NUM_THREADS core count Number of threads to use in each parallel region
OMP_SCHEDULE static Default schedule kind and optional chunk size, e.g. dynamic,4
OMP_PROC_BIND false Thread-to-core affinity policy: close, spread, or master
OMP_PLACES (unset) Placement units for affinity: cores, threads, or sockets
OMP_MAX_ACTIVE_LEVELS 1 Maximum nesting depth of simultaneously active parallel regions
OMP_DISPLAY_ENV false Print OpenMP version and active settings at startup: TRUE or VERBOSE