2018-01-22 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / libgomp / testsuite / libgomp.c++ / sections-1.C
blob32c93dbdef69c5634ca76f9863da6ba41fa9121f
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
16 int main (int argc, char *argv[]) {
18 int i, nthreads, tid;
19 float a[N], b[N], c[N];
21 /* Some initializations */
22 for (i=0; i<N; i++)
23   a[i] = b[i] = i * 1.0;
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);
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       }
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       }
57     }  /* end of sections */
59     printf("Thread %d done.\n",tid); 
61   }  /* end of parallel section */
63   return 0;