source:
CIVL/examples/concurrency/adder.cvl@
dccd621
| Last change on this file since dccd621 was 0baeebd, checked in by , 11 years ago | |
|---|---|
|
|
| File size: 828 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 | |
| [72c01cc] | 8 | $assume 0<=N && N<=B; |
| 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 | |
| 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 | } |
| [385b2ad] | 34 | |
| 35 | $parfor (int j : ($domain){0 .. n-1}) | |
| 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 | ||
| [09b9231b] | 44 | $assert(seq == par); |
| [844ebd8] | 45 | } |
Note:
See TracBrowser
for help on using the repository browser.
