source: CIVL/examples/openMP/dotProduct_critical.cvl@ 82c2f5c

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

added the translation of two different implementations of dot product in OpenMP.

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

  • Property mode set to 100644
File size: 2.5 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#include <civlc.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include "omp.cvh"
14$input int N;
15
16int main (int argc, char *argv[]) {
17
18 double a[N], b[N];
19 double localsum, sum = 0.0;
20 int i, tid, nthreads;
21
22 /* helper variables shared by all threads*/
23 _Bool __critical_0 = $false; //default crticial region variable name
24
25 /* parallel procedure */
26 void __parallel_0(int __tid) {
27 #include "omp_thread.cvh"
28 double i, localsum; //private(i, localsum)
29
30 tid = omp_get_thread_num();
31
32 if(tid == 0) { //#pragma omp master
33 nthreads = omp_get_num_threads();
34 printf("Number of threads = %d\n", nthreads);
35 }
36
37 /* The procedure of each thread for the first for loop */
38 void __for_0(int __tid, int __start, int __end, int __extra){
39 for(int __i = __start; __i < __end; __i++) {
40 a[__i] = b[__i] = (double)__i;
41 }
42 if(__extra > 0) {
43 a[__extra] = b[__extra] = (double)__extra;
44 }
45 }
46
47 {//#pragma omp for
48 // for (i=0; i < N; i++)
49 // localsum = localsum + (a[i] * b[i]);
50 int __start = __for_start(__tid, N);
51 int __end = __for_end(__tid, N);
52 int __extra = __for_extra(__tid, N);
53
54 __for_0(__tid, __start, __end, __extra);
55 }
56
57 localsum = 0.0;
58
59 /* The procedure of each thread for the second for loop */
60 void __for_1(int __tid, int __start, int __end, int __extra){
61 for(int __i = __start; __i < __end; __i++) {
62 localsum = localsum + (a[__i] * b[__i]);
63 }
64 if(__extra > 0) {
65 localsum = localsum + (a[__extra] * b[__extra]);
66 }
67 }
68
69 {//#pragma omp for
70 // for (i=0; i < N; i++)
71 // localsum = localsum + (a[i] * b[i]);
72 int __start = __for_start(__tid, N);
73 int __end = __for_end(__tid, N);
74 int __extra = __for_extra(__tid, N);
75
76 __for_1(__tid, __start, __end, __extra);
77 }
78
79 {//#pragma omp critical
80 $when(!__critical_0) __critical_0 = $true;
81 sum = sum+localsum;
82 __critical_0 = $false;
83 }
84 }
85
86 $proc __parallel_0_procs[OMP_NUM_THREADS];
87
88 /* Create processes to conduct the parallel region */
89 for(i = 0; i < OMP_NUM_THREADS; i++) {
90 __parallel_0_procs[i] = $spawn __parallel_0(i);
91 }
92 for(i = 0; i < OMP_NUM_THREADS; i++) {
93 $wait(__parallel_0_procs[i]);
94 }
95 printf(" Sum = %f\n",sum);
96}
Note: See TracBrowser for help on using the repository browser.