B11 - orphaned directive
void work() {
#pragma omp for
for (int i = 0; i < 100; i++) {
// Work
}
}
int main() {
#pragma omp parallel
{
work();
}
}
-
here,
#pragma omp forinsidework()is called an orphaned directive, because it isn't lexically enclosed by aparalleldirective in the source code -
when the function executes, it is still inside the active parallel region
-
static or lexical extent is the code physically enclosed by the parallel directive
-
dynamic extent includes the code executed through function calls made from the parallel region
// dynamic extent
void work() {
#pragma omp for
for (int i = 0; i < 100; i++) {
// Work
}
}
// static extent
#pragma omp parallel
{
work();
}
#include <stdio.h>
#include <omp.h>
void work() {
#pragma omp for
for (int i = 0; i < 8; i++) {
printf("Thread %d executes i=%d\n",
omp_get_thread_num(), i);
}
}
int main() {
#pragma omp parallel num_threads(4)
{
work();
}
return 0;
}
-
all four threads call
work(), but OpenMP distributes the eight loop iterations among them -
the function is called four times, but the loop is not executed four times in full
-
if the function is called from outside any parallel region, the loop executes using the initial team of one thread
-
only if called from within a parallel construct, threads are created