openmp

Conditional parallel execution

Conditional clauses in OpenMP parallel regions

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

int main (void)
{
  int t = (0 == 0); // true value
  int f = (1 == 0); // false value

  #pragma omp parallel if (f)
  { printf ("FALSE: I am thread %d\n", omp_get_thread_num()); }

  #pragma omp parallel if (t)
  { printf ("TRUE : I am thread %d\n", omp_get_thread_num()); }

  return 0;
}

Its output is:

$ OMP_NUM_THREADS=4 ./test
FALSE: I am thread 0
TRUE : I am thread 0
TRUE : I am thread 1
TRUE : I am thread 3
TRUE : I am thread 2

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow