Arkerone
Arkerone

Reputation: 2026

OpenMP and sections

i have the following code :

#pragma omp parallel sections num_threads(2) {
  #pragma omp section
   Function_1;
   #pragma omp section
   Function_2;
}

but within the Function_1 and Function_2, i have a parallel for but just one thread run it. So, how run the Function_1 and Function_2 in parallel and run several threads within these functions?

thx!

Upvotes: 2

Views: 227

Answers (1)

Hristo Iliev
Hristo Iliev

Reputation: 74495

Having one parallel region inside another is called nesting. By default nested regions are inactive, which means that they execute serially. In order to make them active, you can:

  • set the environment variable OMP_NESTED to true
  • insert the following call before the enclosing parallel region: omp_set_nested(1);

One can also limit the number of levels, where nested parallelism works, by:

  • setting the environment variable OMP_MAX_ACTIVE_LEVELS to num, or
  • calling omp_set_max_active_levels(num);

where num is the desired maximum active level, e.g. a value of 3 would render all parallel regions, nested more than 3 levels deep, inactive.

Upvotes: 3

Related Questions