00001
00002
00003 #include <omp.h>
00004 #include <stdio.h>
00005
00006 extern void abort (void);
00007
00008 #define NUMBER_OF_THREADS 4
00009
00010 int synch[NUMBER_OF_THREADS];
00011 int work[NUMBER_OF_THREADS];
00012 int result[NUMBER_OF_THREADS];
00013 int
00014 fn1 (int i)
00015 {
00016 return i * 2;
00017 }
00018
00019 int
00020 fn2 (int a, int b)
00021 {
00022 return a + b;
00023 }
00024
00025 int
00026 main ()
00027 {
00028 int i, iam, neighbor;
00029 omp_set_num_threads (NUMBER_OF_THREADS);
00030 #pragma omp parallel private(iam,neighbor) shared(work,synch)
00031 {
00032 iam = omp_get_thread_num ();
00033 synch[iam] = 0;
00034 #pragma omp barrier
00035
00036 work[iam] = fn1 (iam);
00037
00038
00039
00040
00041 #pragma omp flush(work,synch)
00042 synch[iam] = 1;
00043 #pragma omp flush(synch)
00044
00045
00046
00047
00048
00049 neighbor = (iam > 0 ? iam : omp_get_num_threads ()) - 1;
00050 while (synch[neighbor] == 0)
00051 {
00052 #pragma omp flush(synch)
00053 }
00054 #pragma omp flush(work,synch)
00055
00056 result[iam] = fn2 (work[neighbor], work[iam]);
00057 }
00058
00059 for (i = 0; i < NUMBER_OF_THREADS; i++)
00060 {
00061 neighbor = (i > 0 ? i : NUMBER_OF_THREADS) - 1;
00062 if (result[i] != i * 2 + neighbor * 2)
00063 abort ();
00064 }
00065
00066 return 0;
00067 }