Nested
Parallelism
The OpenMP API also supports nested
parallelism. Here, a parallel region is encountered inside another parallel
region. This can be a useful way of attaining increased parallelism by having tasks or parallel sections that
provide course-grained parallelism and then using a parallel
for to gain further parallelism within the task or
region. Nested parallelism is also useful in situations where the algorithm is
recursive in nature.
Listing 7.37 shows an example of nested parallelism
where two parallel sections con-tain parallel for directives that initialize two arrays of values. Support for nested
par-allelism can be enabled through the environment variable OMP_NESTED or through a call to the routine omp_set_nested() with a value other than zero. However, support of omp_set_nested()
is optional. It can be determined whether nested
parallelism is enabled or not through a call to omp_get_nested().
Listing 7.37 Using
Nested Parallelism to Perform Array Set Up in Parallel
#include <stdlib.h> #include <omp.h>
int main()
{
double
* array1, *array2;
omp_set_nested( 1 );
#pragma omp
parallel sections shared( array1, array2 )
{
#pragma omp
section
{
array1 = (double*)malloc( sizeof(double)*1024*1024 );
#pragma omp
parallel for shared(array1)
for ( int i=0; i<1024*1024; i++ )
{
array1[i] = i;
}
}
#pragma omp
section
{
array2 = (double*)malloc( sizeof(double)*1024*512 );
#pragma omp
parallel for shared(array2)
for ( int i=0; i<1024*512; i++ )
{
array2[i] = i;
}
}
}
}
However, nested parallelism is complex, so detailed
discussion is left to specialist texts on the topic of OpenMP.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.