Open MP3
Open MP3
CS3006
Lecture 10
OpenMP-III
6th April 2022
Private
firstprivate, lastprivate
Shared
Default
private, shared, none
Reduction
If clause
Schedule
Static, dynamic, guided, runtime
nowait
11.667
+ 3.765
11.667
15.432 + 3.563
15.230
OMP_NUM_THREADS
OMP_DYNAMIC
OMP_SCHEDULE
OMP_NESTED
OMP_DYNAMIC
when set to TRUE, allows the number of threads to be
controlled at runtime. It means Openmp will use its
dynamic adjustment algorithm to create number of
threads that may optimize system performance
Incase of TRUE , total number of threads generated may not
be equal to the threads requested by using the omp_set_num
threads() function or the num_threads clause.
Incase of FALSE, usually total no. of generated threads in a
parallel region become as requested by the num_threads
clause
OpenMP routines for setting/getting dynamic status:
void omp_set_dynamic (int flag); //disables if flag=0
Should be called from outside of a parallel region
int omp_get_dynamic (); //return value of dynamic status
CS5009 - Advanced Operating Systems
Environment Variables in OpenMP
OMP_DYNAMIC[dynamic.c]
omp_set_dynamic(0);
omp_set_num_threads(8);
printf("total number of requested when dynamic is false
are:%d\n", 8);
#pragma omp parallel
{
#pragma omp single nowait
printf("total threads in parallel region2=%d:\n",
omp_get_num_threads());
#pragma omp for
for (i = 0; i < mult; i++)
{a = complex_func();}
}
OMP_SCHEDULE
OMP_NESTED
Default value is FALSE
While using nested parallel pragma inside another, the
nested one is executed by the original team instead of
making new thread team.
When TRUE
Enables nested parallelism
While using nested parallel pragma code inside another, it
makes a new team of threads for executing the nested
one.
Use omp_set_nested(int val) with non-zero value to
set this variable to TRUE.
When called with ‘0’ as argument, it set the variable to
FALSE
CS5009 - Advanced Operating Systems
Environment Variables in OpenMP
OMP_NESTED[nested.c]
omp_set_nested(0);
#pragma omp parallel num_threads(2)
{
#pragma omp single
printf("Level 1: number of threads in the team : %d\n",
omp_get_num_threads());
Preliminary Idea:
points in circle
Pi = 4 x ( )
points in square
Steps
For all the random points
1. Calculate total points in the circle
2. Divide points in the circle to the points in the square
Total number of points are also the total number of points inside the
square
3. Multiply this fraction with 4
count=0;
for (i=0;i<chunk_size; i++)
{
//get random points
x = (double)rand_r(&seed)/(double)RAND_MAX;
y = (double)rand_r(&seed)/(double)RAND_MAX;
z = ((x-0.5)*(x-0.5))+((y-0.5)*(y-0.5));
//check to see if point is in unit circle
if (z<0.25)
{
++count;
}
}
}
pi = ((double)count/(double)niter)*4.0;
CS5009 - Advanced Operating Systems
Parallelizing linked lists
p =head;
while(p != NULL){
Parray[i]=p;
p=p->next;
}
current = current->ptr;
}
}
}
Total threads=4
CS5009 - Advanced Operating Systems
Total complex itters= 100 Million
List size= 10 nodes
Parallelizing linked lists
[omp task illustration]