B2 - parallel regions and work sharing
parallel region
-
the parallel directive defines a region of code executed on all threads parallelly
-
the number of threads can be determined at runtime
-
the default value is the number of processors (usually CPU cores)
-
this can be overridden by:
- setting the environment variable
OMP_NUM_THREADS - calling
omp_set_num_threads(P) - using the modifier
num_threads(P)when declaring the parallel region
- setting the environment variable
-
the parallel directive creates necessary parallelism but does not share the work
-
eg: in the following block of code, for
P = 12, there will be 120 messages:
#pragma omp parallel private(i)
for (i = 0; i < 10; i++) {
printf("Hello world %d\n", i);
}
- so, instead of sharing, the work is being replicated on each thread
work sharing
- the parallel for directive is a work-sharing construct
- it complements the parallel construct
#pragma omp parallel for
for (i = 0; i < 10; i++) {
printf("Hello world %d\n", i);
}
-
now, for
P = 12, 10 messages will be printed -
different iterations execute concurrently on
threads -
iis private by default in a for directive -
conditional compilation can be useful to compile statements only if OpenMP support is enabled:
#ifdef _OPENMP
... statememnts ...
#endif