1 #pragma omp directive [clause list]
1 #pragma omp parallel [clause list] 2 /* structured block */ 3
|
#include <omp.h>
main () {
int var1, var2, var3;
Serial code
.
.
Beginning of parallel section. Fork a team of threads.
Specify variable scoping
#pragma omp parallel private(var1, var2) shared(var3)
{
Parallel section executed by all threads
.
.
All threads join master thread and disband
}
Resume serial code
.
.
}
************
#include <omp.h>
int a,b,num_threads;
int main()
{
printf("I am in sequential part.\n");
#pragma omp parallel num_threads (8) private (a) shared (b)
{
num_threads=omp_get_num_threads();
printf("I am openMP parellized part and thread %d \n",omp_get_thread_num());
}
}
Using the parallel directive
1 #pragma omp parallel if (is_parallel == 1) num_threads(8) \
2 private (a) shared (b) firstprivate(c)
3 {
4 /* structured block */
5 }
Here, if the value of the variable is_parallel equals one, eight threads are created. Each of these threads gets private copies of variables a and c, and shares a single value of variable b. Furthermore, the value of each copy of c is initialized to the value of c before the parallel directive.
+, *, -, &, |, ^, &&, and ||.
1 #pragma omp parallel reduction(+: sum) num_threads(8)
2 {
3 /* compute local sums here */
4 }
5 /* sum here contains sum of all local instances of sums */
In this example, each of the eight threads gets a copy of the variable sum. When the threads exit, the sum of all of these local copies is stored in the single copy of the variable (at the master thread).
1 /* ******************************************************
2 An OpenMP version of a threaded program to compute PI.
3 ****************************************************** */
4
5 #pragma omp parallel default(private) shared (npoints) \
6 reduction(+: sum) num_threads(8)
7 {
8 num_threads = omp_get_num_threads();
9 sample_points_per_thread = npoints / num_threads;
10 sum = 0;
11 for (i = 0; i < sample_points_per_thread; i++) {
12 rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1);
13 rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1);
14 if (((rand_no_x - 0.5) * (rand_no_x - 0.5) +
15 (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25)
16 sum ++;
17 }
18 }
Note that this program is much easier to write in terms of specifying creation and termination of threads compared to the corresponding POSIX threaded program.