testsuite/libgomp.c++/sections-1.C

Go to the documentation of this file.
00001 /******************************************************************************
00002 * FILE: omp_workshare2.c
00003 * DESCRIPTION:
00004 *   OpenMP Example - Sections Work-sharing - C/C++ Version
00005 *   In this example, the OpenMP SECTION directive is used to assign
00006 *   different array operations to threads that execute a SECTION. Each 
00007 *   thread receives its own copy of the result array to work with.
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 N     50
00015 
00016 int main (int argc, char *argv[]) {
00017 
00018 int i, nthreads, tid;
00019 float a[N], b[N], c[N];
00020 
00021 /* Some initializations */
00022 for (i=0; i<N; i++)
00023   a[i] = b[i] = i * 1.0;
00024 
00025 #pragma omp parallel shared(a,b,nthreads) private(c,i,tid)
00026   {
00027   tid = omp_get_thread_num();
00028   if (tid == 0)
00029     {
00030     nthreads = omp_get_num_threads();
00031     printf("Number of threads = %d\n", nthreads);
00032     }
00033   printf("Thread %d starting...\n",tid);
00034 
00035   #pragma omp sections nowait
00036     {
00037     #pragma omp section
00038       {
00039       printf("Thread %d doing section 1\n",tid);
00040       for (i=0; i<N; i++)
00041         {
00042         c[i] = a[i] + b[i];
00043         printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
00044         }
00045       }
00046 
00047     #pragma omp section
00048       {
00049       printf("Thread %d doing section 2\n",tid);
00050       for (i=0; i<N; i++)
00051         {
00052         c[i] = a[i] * b[i];
00053         printf("Thread %d: c[%d]= %f\n",tid,i,c[i]);
00054         }
00055       }
00056 
00057     }  /* end of sections */
00058 
00059     printf("Thread %d done.\n",tid); 
00060 
00061   }  /* end of parallel section */
00062 
00063   return 0;
00064 }

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