Rookie HPC

Teaming

C | FORTRAN

num_threads

Definition

The num_threads clause is one of the three ways to specify the number of threads to use when creating parallel regions. The other two are the environment variable OMP_NUM_THREADS and the function omp_set_num_threads. The num_threads clause specifies, during execution, the number of threads to use when creating the parallel region from the directive it is part of. It overwrites, for that specific parallel region only, any value that could have been specified by the environment variable OMP_NUM_THREADS and the function omp_set_num_threads. If none of the environment variable OMP_NUM_THREADS, the function omp_set_num_threads or the num_threads clause is used, the number of threads used when creating parallel regions is implementation defined. To see the number of threads used when creating a parallel region, one can call omp_get_num_threads inside the parallel region created.

Copy

Feedback

num_threads(n)

Parameters

n

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to use the num_threads clause.
 * @details This code generates three parallel regions. The number of threads to
 * use for all parallel regions has been set to 4 by the omp_set_num_threads
 * function. This value will be overwritten for the second parallel region only,
 * using the num_threads clause.
 **/
int main(int argc, char* argv[])
{
    // Tell OpenMP to use 4 threads in parallel regions from now on
    omp_set_num_threads(4);

    // Create a first parallel region, which therefore contains 4 threads
    #pragma omp parallel
    {
        // Each thread prints its identifier
        printf("Loop 1: We are %d threads, I am thread %d.\n", omp_get_num_threads(), omp_get_thread_num());
    }

    // Create a second parallel region, overwriting the number of threads to use with num_threads
    #pragma omp parallel num_threads(3)
    {
        // Each thread prints its identifier
        printf("Loop 2: We are %d threads, I am thread %d.\n", omp_get_num_threads(), omp_get_thread_num());
    }

    // Create a third parallel region, no overwriting from num_threads, so back to 4 threads again
    #pragma omp parallel
    {
        // Each thread prints its identifier
        printf("Loop 3: We are %d threads, I am thread %d.\n", omp_get_num_threads(), omp_get_thread_num());
    }

    return EXIT_SUCCESS;
}