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

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

$wait is re-implemented as a system function now; examples are updated accordingly.

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

  • Property mode set to 100644
File size: 892 bytes
RevLine 
[9fb69d3]1/* Commandline execution:
[08d3117]2 * civl verify -por=new -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
[08d3117]14 for (int i = 0; i < n; i++) {
15 s += p[i];
[8b354468]16 }
[844ebd8]17 return s;
18}
19
[72c01cc]20double adderPar(double *p, int n) {
21 double s = 0.0; // sum shared by workers
22 int mutex = 0; // mutex shared by workers
23 $proc workers[n]; // one worker for each element!
[5b49b89]24
[844ebd8]25 void worker(int i) {
[5b49b89]26 double t;
27
[b50c660]28 $when (mutex == 0) mutex = 1;
29 t = s;
30 t += p[i];
31 s = t;
32 mutex = 0;
[844ebd8]33 }
34
[08d3117]35 for (int j = 0; j < n; j++)
36 workers[j] = $spawn worker(j);
37 for (int j = 0; j < n; j++)
[a82987f]38 $wait(workers[j]);
[844ebd8]39 return s;
40}
41
42void main() {
[72c01cc]43 double seq = adderSeq(&a[0], N);
44 double par = adderPar(&a[0], N);
45
[09b9231b]46 $assert(seq == par);
[844ebd8]47}
Note: See TracBrowser for help on using the repository browser.