testsuite/libgomp.c/omp_workshare1.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 * FILE: omp_workshare1.c
00003 * DESCRIPTION:
00004 *   OpenMP Example - Loop Work-sharing - C/C++ Version
00005 *   In this example, the iterations of a loop are scheduled dynamically
00006 *   across the team of threads.  A thread will perform CHUNK iterations
00007 *   at a time before being scheduled for the next CHUNK of work.
00008 * AUTHOR: Blaise Barney  5/99
00009 * LAST REVISED: 04/06/05
00010 ******************************************************************************/
00011 #include <omp.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #define CHUNKSIZE   10
00015 #define N       100
00016 
00017 int main (int argc, char *argv[]) {
00018 
00019 int nthreads, tid, i, chunk;
00020 float a[N], b[N], c[N];
00021 
00022 /* Some initializations */
00023 for (i=0; i < N; i++)
00024   a[i] = b[i] = i * 1.0;
00025 chunk = CHUNKSIZE;
00026 
00027 #pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)
00028   {
00029   tid = omp_get_thread_num();
00030   if (tid == 0)
00031     {
00032     nthreads = omp_get_num_threads();
00033     printf("Number of threads = %d\n", nthreads);
00034     }
00035   printf("Thread %d starting...\n",tid);
00036 
00037   #pragma omp for schedule(dynamic,chunk)
00038   for (i=0; i<N; i++)
00039     {
00040     c[i] = a[i] + b[i];
00041     printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
00042     }
00043 
00044   }  /* end of parallel section */
00045 
00046   return 0;
00047 }

Generated on Fri Apr 5 05:38:10 2013 for Libgomp by  doxygen 1.4.7