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