#pragma omp directive [clause list] |
#pragma omp parallel [clause list] /* structured block */ |
|
|
| 1 | Conditional Parallelization: The clause if (scalar expression) determines whether the parallel construct results in creation of threads. |
| 2 | Degree of Concurrency: The clause num_threads (integer expression) specifies the number of threads that are created by the parallel directive. |
| 3 | Data Handling: The clause private (variable list) indicates that the set of variables specified is local to each thread.
|
| FORK | Master thread then creates a team of parallel threads. |
| Statements in program that are enclosed by the parallel region construct are executed in parallel among the various threads. | |
| JOIN | When the team threads complete the statements in the parallel region construct, they synchronize and terminate, leaving only the master thread. |
1 #include <stdio.h>
2 #include <omp.h>
3 #include <unistd.h>
4 int a,b,x,y,num_threads,thread_num;
5 int main()
6 {
7 printf("I am in sequential part.\n");
8 #pragma omp parallel num_threads (8) private (a) shared (b)
9 {
10 num_threads=omp_get_num_threads();
11 thread_num=omp_get_thread_num();
12 x=thread_num;
13 // sleep(1);
14 y=x+1;
15 printf("I am openMP parellized part and thread %d. \n X and Y values are %d and %d. \n",omp_get_thread_num(),x,y);
16 }
17 printf("I am in sequential part again.\n");
18 }
|
1
2 pragma omp parallel if (is_parallel == 1) num_threads(8) private (a) shared (b) firstprivate(c)
3 {
4 /* structured block */
5 }
|
+ * - & | ^ && ||
1
2 #pragma omp parallel reduction(+: sum) num_threads(8)
3 {
4 /* compute local sums here */
5 }
6 /* sum here contains sum of all local instances of sums */
|
|
|
Example:
|
|