Unsafe use of OpenMP* threading intrinsic functions

An OpenMP intrinsic function such as omp_set_num_threads was called within a parallel region.

OpenMP provides various intrinsic functions that affect the behavior of the OpenMP run time system. One such function is omp_set_num_threads, which determines the number of threads available to execute parallel constructs.

Generally speaking, it is best to set up the parameters of the run time system before entering a parallel region. Since the decision about what threads will team up to execute a parallel region is made before the parallel region is entered, a call to an intrinsic function inside that region will not affect the threading of that region. This can lead to unexpected results.

Formally speaking, this usage pattern is legal. However, the intrinsic function call will only affect the threading of subsequent parallel regions.

ID

Observation

Description

1

OpenMP usage error

The place the OpenMP intrinsic function was invoked

Examples

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

int main(int argc, char **argv)
{
    int i;

#pragma omp parallel for ordered
    for (i = 0; i < 6; i++) {
    
        // bad: call inside parallel region is not recommended
        omp_set_num_threads(3);
#pragma omp ordered
        printf("%d\n", i);
        
    }
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.