source:
CIVL/examples/concurrency/adder.cvl
| Last change on this file was ea777aa, checked in by , 3 years ago | |
|---|---|
|
|
| File size: 825 bytes | |
| Rev | Line | |
|---|---|---|
| [9fb69d3] | 1 | /* Commandline execution: |
| [e6b02c8] | 2 | * civl verify -inputB=5 adder.cvl |
| [24ca21c] | 3 | * */ |
| [e6b02c8] | 4 | #include <civlc.cvh> |
| [844ebd8] | 5 | |
| [0baeebd] | 6 | $input int B = 5; // upper bound on array length |
| 7 | $input int N; // length of array | |
| [bc8dbea] | 8 | $assume(1<=N && N<=B); |
| [72c01cc] | 9 | $input double a[N]; |
| 10 | ||
| 11 | double 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] | 20 | double adderPar(double *p, int n) { |
| 21 | double s = 0.0; // sum shared by workers | |
| 22 | int mutex = 0; // mutex shared by workers | |
| [a3da6fb] | 23 | $proc workers[n]; // one worker for each element! UNUSED |
| [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 | } |
| [385b2ad] | 34 | |
| [7b9eb4c] | 35 | $parfor (int j : 0 .. n-1) |
| [385b2ad] | 36 | worker(j); |
| [844ebd8] | 37 | return s; |
| 38 | } | |
| 39 | ||
| 40 | void main() { | |
| [72c01cc] | 41 | double seq = adderSeq(&a[0], N); |
| 42 | double par = adderPar(&a[0], N); | |
| 43 | ||
| [d980649] | 44 | $assert(seq == par); |
| [844ebd8] | 45 | } |
Note:
See TracBrowser
for help on using the repository browser.
