source: CIVL/examples/omp/dotProduct_critical.cvl@ a94cd4f

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since a94cd4f was 10670fd, checked in by Stephen Siegel <siegel@…>, 12 years ago

Adding a couple simple translations (by hand) of OpenMP examples.

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

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/* CIVL model of dotProduct_critical.c. To verify:
2 * civl verify dotProduct_critical.cvl
3 */
4#include <civlc.h>
5#include <stdio.h>
6#include <stdlib.h>
7
8#define N 10
9#define THREAD_MAX 4
10/* Does thread t own iteration i in loop with n iterations? */
11#define CIVL_owns(t, n, i) ((i)%(n)==(t))
12
13_Bool critical_lock = $false;
14
15int main (int argc, char *argv[]) {
16 double a[N], b[N];
17 double sum = 0.0;
18 int nthreads;
19
20 // translation of parallel construct
21 // #pragma omp parallel shared(a,b,sum) private(i, localsum)
22 // note: tid should be private. corrected here.
23 {
24 int barrier = 0;
25 void thread(int _tid, int _nthreads) {
26 int i, tid;
27 double localsum;
28
29 tid = _tid;
30 if (_tid == 0) {
31 nthreads =_nthreads;
32 printf("Number of threads = %d\n", nthreads);
33 }
34 for (i=0; i < N; i++) {
35 if (CIVL_owns(_tid, _nthreads, i)) {
36 a[i] = b[i] = (double)i;
37 }
38 }
39 barrier++;
40 $when (barrier==_nthreads);
41 localsum = 0;
42 for (i=0; i < N; i++) {
43 if (CIVL_owns(_tid, _nthreads, i)) {
44 localsum = localsum + (a[i] * b[i]);
45 }
46 }
47 $when (!critical_lock) critical_lock = $true;
48 sum = sum + localsum;
49 critical_lock = $false;
50 }
51 int _nthreads = 1+$choose_int(THREAD_MAX);
52 $proc threads[_nthreads];
53
54 for (int i=0; i<_nthreads; i++)
55 threads[i] = $spawn thread(i, _nthreads);
56 for (int i=0; i<_nthreads; i++)
57 $wait(threads[i]);
58 }
59 printf(" Sum = %2.1f\n",sum);
60}
Note: See TracBrowser for help on using the repository browser.