Rookie HPC

About

Docs

Tools

Tests

OMP_NUM_THREADS

Definition

OMP_NUM_THREADS is the environment variable that specifies the number of threads to use in OpenMP parallel regions. This value can be overwritten during program execution with the OpenMP routine omp_set_num_threads.

Example

Copy

Feedback

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

/**
 * @brief Illustrates the effect of the OMP_NUM_THREADS environment variable.
 * @details Creates a parallel region and prints how many threads are in this region.
 **/
int main(int argc, char* argv[])
{
    #pragma omp parallel
    {
        #pragma omp single
        {
            printf("From the environment variable OMP_NUM_THREADS, parallel regions contain %d threads.\n", omp_get_num_threads());
        }
    }

    return EXIT_SUCCESS;
}