1*404b540aSrobert /******************************************************************************
2*404b540aSrobert * OpenMP Example - Combined Parallel Loop Work-sharing - C/C++ Version
3*404b540aSrobert * FILE: omp_workshare4.c
4*404b540aSrobert * DESCRIPTION:
5*404b540aSrobert * This is a corrected version of the omp_workshare3.c example. Corrections
6*404b540aSrobert * include removing all statements between the parallel for construct and
7*404b540aSrobert * the actual for loop, and introducing logic to preserve the ability to
8*404b540aSrobert * query a thread's id and print it from inside the for loop.
9*404b540aSrobert * SOURCE: Blaise Barney 5/99
10*404b540aSrobert * LAST REVISED: 03/03/2002
11*404b540aSrobert ******************************************************************************/
12*404b540aSrobert
13*404b540aSrobert #include <omp.h>
14*404b540aSrobert #include <stdio.h>
15*404b540aSrobert #define N 50
16*404b540aSrobert #define CHUNKSIZE 5
17*404b540aSrobert
main()18*404b540aSrobert main () {
19*404b540aSrobert
20*404b540aSrobert int i, chunk, tid;
21*404b540aSrobert float a[N], b[N], c[N];
22*404b540aSrobert char first_time;
23*404b540aSrobert
24*404b540aSrobert /* Some initializations */
25*404b540aSrobert for (i=0; i < N; i++)
26*404b540aSrobert a[i] = b[i] = i * 1.0;
27*404b540aSrobert chunk = CHUNKSIZE;
28*404b540aSrobert first_time = 'y';
29*404b540aSrobert
30*404b540aSrobert #pragma omp parallel for \
31*404b540aSrobert shared(a,b,c,chunk) \
32*404b540aSrobert private(i,tid) \
33*404b540aSrobert schedule(static,chunk) \
34*404b540aSrobert firstprivate(first_time)
35*404b540aSrobert
36*404b540aSrobert for (i=0; i < N; i++)
37*404b540aSrobert {
38*404b540aSrobert if (first_time == 'y')
39*404b540aSrobert {
40*404b540aSrobert tid = omp_get_thread_num();
41*404b540aSrobert first_time = 'n';
42*404b540aSrobert }
43*404b540aSrobert c[i] = a[i] + b[i];
44*404b540aSrobert printf("tid= %d i= %d c[i]= %f\n", tid, i, c[i]);
45*404b540aSrobert }
46*404b540aSrobert
47*404b540aSrobert return 0;
48*404b540aSrobert }
49