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