if
Collapse the table of content
Expand the table of content
This documentation is archived and is not being maintained.

if (OpenMP) 

Specifies whether a loop should be executed in parallel or in serial.


if(expression)

where,

expression

An integral expression that, if it evaluates to true (nonzero), causes the code in the parallel region to execute in parallel. If the expression evaluates to false (zero), the parallel region is executed in serial (by a single thread).

if applies to the following directives:

For more information, see 2.3 parallel Construct.

// omp_if.cpp
// compile with: /openmp 
#include <stdio.h>
#include <omp.h>

void test(int val) 
{
    #pragma omp parallel if (val) num_threads(val)
    if (omp_in_parallel())
        #pragma omp single
        printf_s("val = %d, parallelized with %d threads\n",
                 val, omp_get_num_threads());
    else
        printf_s("val = %d, serialized\n", val);
}

int main( ) 
{
    test(0);
    test(2);
}

Output

val = 0, serialized
val = 2, parallelized with 2 threads
Show:
© 2016 Microsoft