source: CIVL/examples/concurrency/adder.cvl@ c0885ed8

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

removed wrong atomic blocks.

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

  • Property mode set to 100644
File size: 942 bytes
RevLine 
[9fb69d3]1/* Commandline execution:
2 * civl verify -inputB=5 adder.cvl
[24ca21c]3 * */
[f28d814]4#include <civlc.h>
[844ebd8]5
[72c01cc]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) {
[844ebd8]12 double s = 0.0;
13
[24ca21c]14 $atomic{
[8b354468]15 for (int i = 0; i < n; i++) {
16 s += p[i];
17 }
18 }
[844ebd8]19 return s;
20}
21
[72c01cc]22double adderPar(double *p, int n) {
23 double s = 0.0; // sum shared by workers
24 int mutex = 0; // mutex shared by workers
25 $proc workers[n]; // one worker for each element!
[5b49b89]26
[844ebd8]27 void worker(int i) {
[5b49b89]28 double t;
29
[b50c660]30 $when (mutex == 0) mutex = 1;
31 t = s;
32 t += p[i];
33 s = t;
34 mutex = 0;
[844ebd8]35 }
36
[8b354468]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 }
[844ebd8]45 return s;
46}
47
48void main() {
[72c01cc]49 double seq = adderSeq(&a[0], N);
50 double par = adderPar(&a[0], N);
51
[f28d814]52 $assert seq == par;
[844ebd8]53}
Note: See TracBrowser for help on using the repository browser.