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

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

updates CIVL to use the new ABC FrontEnd. tests updated accordingly when necessary.

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

  • Property mode set to 100644
File size: 885 bytes
RevLine 
[9fb69d3]1/* Commandline execution:
[e6b02c8]2 * civl verify -inputB=5 adder.cvl
[24ca21c]3 * */
[e6b02c8]4#include <civlc.cvh>
[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.