00001
00002
00003
00004
00005
00006
00007
00008
00009
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
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 }
00058
00059 printf("Thread %d done.\n",tid);
00060
00061 }
00062
00063 return 0;
00064 }