source: CIVL/examples/concurrency/adder.cvl@ 23207c6

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since 23207c6 was 24ca21c, checked in by Manchun Zheng <zmanchun@…>, 13 years ago

improve output. Add atomic to some examples.

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

  • Property mode set to 100644
File size: 910 bytes
Line 
1/*
2 * civl verify -inputB=5 adder.cvl
3 * */
4#include <civlc.h>
5
6$input int B; // upper bound on array length
7$input int N; // length of array
8$assume 0<=N && N<=B;
9$input double a[N];
10
11double adderSeq(double *p, int n) {
12 double s = 0.0;
13
14 $atomic{
15 for (int i = 0; i < n; i++) {
16 s += p[i];
17 }}
18 return s;
19}
20
21double adderPar(double *p, int n) {
22 double s = 0.0; // sum shared by workers
23 int mutex = 0; // mutex shared by workers
24 $proc workers[n]; // one worker for each element!
25
26 void worker(int i) {
27 double t;
28
29 $atomic{
30 $when (mutex == 0) mutex = 1;
31 t = s;
32 t += p[i];
33 s = t;
34 mutex = 0;}
35 }
36
37 $atomic{
38 for (int j = 0; j < n; j++)
39 workers[j] = $spawn worker(j);}
40
41 $atomic{
42 for (int j = 0; j < n; j++)
43 $wait workers[j];}
44 return s;
45}
46
47void main() {
48 double seq = adderSeq(&a[0], N);
49 double par = adderPar(&a[0], N);
50
51 $assert seq == par;
52}
Note: See TracBrowser for help on using the repository browser.