source: CIVL/examples/translation/openmp/dotProduct_critical.cvl@ bfebb46

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since bfebb46 was a9d7f9f, checked in by Manchun Zheng <zmanchun@…>, 12 years ago

renamed openmp folder

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@812 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 3.1 KB
Line 
1// dot product implementation with critical region.
2
3/*
4 OpenMP example program which computes the dot product of two arrays a and b
5 (that is sum(a[i]*b[i]) ) using explicit synchronization with a critical region.
6 Compile with gcc -O3 -fopenmp omp_critical.c -o omp_critical
7*/
8// Online source: http://users.abo.fi/mats/PP2012/examples/OpenMP/omp_critical.c
9/*
10 * civl verify -inputNTHREADS=2 -inputN=8 dotProduct_critical.cvl
11 * */
12#include <civlc.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include "omp.cvh"
16$input int NTHREADS;
17$input int N;
18
19int main (int argc, char *argv[]) {
20 /* implicitly initializing the number of threads */
21 omp_set_num_threads(NTHREADS);
22
23 double a[N], b[N];
24 double localsum, sum = 0.0;
25 int i, tid, nthreads;
26
27 /* helper variables shared by all threads*/
28 _Bool __critical_0 = $false; //default crticial region variable name
29
30 /* parallel procedure */
31 void __parallel_0(int __tid) {
32 #include "omp_thread.cvh"
33 int __i;
34 double __localsum; //private(i, localsum)
35
36 tid = omp_get_thread_num();
37 if(tid == 0) { //#pragma omp master
38 nthreads = omp_get_num_threads();
39 printf("Number of threads = %d\n", nthreads);
40 }
41
42 /* The procedure of each thread for the first for loop */
43 void __for_0(int __tid, int __start, int __end, int __extra){
44 for(__i = __start; __i < __end; __i++) {
45 a[__i] = b[__i] = (double)__i;
46 }
47 if(__extra > 0) {
48 a[__extra] = b[__extra] = (double)__extra;
49 }
50 /* implicit barrier at the end of the for construct */
51 __barrier(__tid);
52 }
53
54 { //#pragma omp for
55 // for (i=0; i < N; i++)
56 // localsum = localsum + (a[i] * b[i]);
57 int __start = __for_start(__tid, N);
58 int __end = __for_end(__tid, N);
59 int __extra = __for_extra(__tid, N);
60
61 __for_0(__tid, __start, __end, __extra);
62 }
63
64 __localsum = 0.0;
65
66 /* The procedure of each thread for the second for loop */
67 void __for_1(int __tid, int __start, int __end, int __extra){
68 for(__i = __start; __i < __end; __i++) {
69 __localsum = __localsum + (a[__i] * b[__i]);
70 }
71 if(__extra > 0) {
72 __localsum = __localsum + (a[__extra] * b[__extra]);
73 }
74 /* implicit barrier at the end of the for construct */
75 __barrier(__tid);
76 }
77
78 {//#pragma omp for
79 // for (i=0; i < N; i++)
80 // localsum = localsum + (a[i] * b[i]);
81 int __start = __for_start(__tid, N);
82 int __end = __for_end(__tid, N);
83 int __extra = __for_extra(__tid, N);
84
85 __for_1(__tid, __start, __end, __extra);
86 }
87
88 {//#pragma omp critical
89 $when(!__critical_0) __critical_0 = $true;
90 sum = sum+__localsum;
91 __critical_0 = $false;
92 }
93 /* implicit barrier at the end of the parallel construct */
94 __barrier(__tid);
95 }
96
97 $proc __parallel_0_procs[OMP_NUM_THREADS];
98
99 __barrier_init();
100 /* Create processes to conduct the parallel region */
101 for(i = 0; i < OMP_NUM_THREADS; i++) {
102 __parallel_0_procs[i] = $spawn __parallel_0(i);
103 }
104 for(i = 0; i < OMP_NUM_THREADS; i++) {
105 $wait(__parallel_0_procs[i]);
106 }
107 printf(" Sum = %f\n",sum);
108}
Note: See TracBrowser for help on using the repository browser.