0
|
1 /******************************************************************************
|
|
2 * FILE: omp_workshare1.c
|
|
3 * DESCRIPTION:
|
|
4 * OpenMP Example - Loop Work-sharing - C/C++ Version
|
|
5 * In this example, the iterations of a loop are scheduled dynamically
|
|
6 * across the team of threads. A thread will perform CHUNK iterations
|
|
7 * at a time before being scheduled for the next CHUNK of work.
|
|
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 CHUNKSIZE 10
|
|
15 #define N 100
|
|
16
|
|
17 int main (int argc, char *argv[]) {
|
|
18
|
|
19 int nthreads, tid, i, chunk;
|
|
20 float a[N], b[N], c[N];
|
|
21
|
|
22 /* Some initializations */
|
|
23 for (i=0; i < N; i++)
|
|
24 a[i] = b[i] = i * 1.0;
|
|
25 chunk = CHUNKSIZE;
|
|
26
|
|
27 #pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)
|
|
28 {
|
|
29 tid = omp_get_thread_num();
|
|
30 if (tid == 0)
|
|
31 {
|
|
32 nthreads = omp_get_num_threads();
|
|
33 printf("Number of threads = %d\n", nthreads);
|
|
34 }
|
|
35 printf("Thread %d starting...\n",tid);
|
|
36
|
|
37 #pragma omp for schedule(dynamic,chunk)
|
|
38 for (i=0; i<N; i++)
|
|
39 {
|
|
40 c[i] = a[i] + b[i];
|
|
41 printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
|
|
42 }
|
|
43
|
|
44 } /* end of parallel section */
|
|
45
|
|
46 return 0;
|
|
47 }
|