0
|
1 /******************************************************************************
|
|
2 * FILE: omp_workshare2.c
|
|
3 * DESCRIPTION:
|
|
4 * OpenMP Example - Sections Work-sharing - C/C++ Version
|
|
5 * In this example, the OpenMP SECTION directive is used to assign
|
|
6 * different array operations to threads that execute a SECTION. Each
|
|
7 * thread receives its own copy of the result array to work with.
|
|
8 * AUTHOR: Blaise Barney 5/99
|
|
9 * LAST REVISED: 04/06/05
|
|
10 ******************************************************************************/
|
|
11 #include <omp.h>
|
|
12 #include <stdio.h>
|
|
13 #include <stdlib.h>
|
|
14 #define N 50
|
|
15
|
|
16 int main (int argc, char *argv[]) {
|
|
17
|
|
18 int i, nthreads, tid;
|
|
19 float a[N], b[N], c[N];
|
|
20
|
|
21 /* Some initializations */
|
|
22 for (i=0; i<N; i++)
|
|
23 a[i] = b[i] = i * 1.0;
|
|
24
|
|
25 #pragma omp parallel shared(a,b,nthreads) private(c,i,tid)
|
|
26 {
|
|
27 tid = omp_get_thread_num();
|
|
28 if (tid == 0)
|
|
29 {
|
|
30 nthreads = omp_get_num_threads();
|
|
31 printf("Number of threads = %d\n", nthreads);
|
|
32 }
|
|
33 printf("Thread %d starting...\n",tid);
|
|
34
|
|
35 #pragma omp sections nowait
|
|
36 {
|
|
37 #pragma omp section
|
|
38 {
|
|
39 printf("Thread %d doing section 1\n",tid);
|
|
40 for (i=0; i<N; i++)
|
|
41 {
|
|
42 c[i] = a[i] + b[i];
|
|
43 printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
|
|
44 }
|
|
45 }
|
|
46
|
|
47 #pragma omp section
|
|
48 {
|
|
49 printf("Thread %d doing section 2\n",tid);
|
|
50 for (i=0; i<N; i++)
|
|
51 {
|
|
52 c[i] = a[i] * b[i];
|
|
53 printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
|
|
54 }
|
|
55 }
|
|
56
|
|
57 } /* end of sections */
|
|
58
|
|
59 printf("Thread %d done.\n",tid);
|
|
60
|
|
61 } /* end of parallel section */
|
|
62
|
|
63 return 0;
|
|
64 }
|