source:
CIVL/examples/concurrency/adder.cvl@
23207c6
| Last change on this file since 23207c6 was 24ca21c, checked in by , 13 years ago | |
|---|---|
|
|
| File size: 910 bytes | |
| Rev | Line | |
|---|---|---|
| [24ca21c] | 1 | /* |
| 2 | * civl verify -inputB=5 adder.cvl | |
| 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 | ||
| 11 | double adderSeq(double *p, int n) { | |
| [844ebd8] | 12 | double s = 0.0; |
| 13 | ||
| [24ca21c] | 14 | $atomic{ |
| [844ebd8] | 15 | for (int i = 0; i < n; i++) { |
| [72c01cc] | 16 | s += p[i]; |
| [24ca21c] | 17 | }} |
| [844ebd8] | 18 | return s; |
| 19 | } | |
| 20 | ||
| [72c01cc] | 21 | double adderPar(double *p, int n) { |
| 22 | double s = 0.0; // sum shared by workers | |
| 23 | int mutex = 0; // mutex shared by workers | |
| 24 | $proc workers[n]; // one worker for each element! | |
| [5b49b89] | 25 | |
| [844ebd8] | 26 | void worker(int i) { |
| [5b49b89] | 27 | double t; |
| 28 | ||
| [24ca21c] | 29 | $atomic{ |
| [f28d814] | 30 | $when (mutex == 0) mutex = 1; |
| [5b49b89] | 31 | t = s; |
| [72c01cc] | 32 | t += p[i]; |
| [5b49b89] | 33 | s = t; |
| [24ca21c] | 34 | mutex = 0;} |
| [844ebd8] | 35 | } |
| 36 | ||
| [24ca21c] | 37 | $atomic{ |
| [72c01cc] | 38 | for (int j = 0; j < n; j++) |
| [24ca21c] | 39 | workers[j] = $spawn worker(j);} |
| 40 | ||
| 41 | $atomic{ | |
| [72c01cc] | 42 | for (int j = 0; j < n; j++) |
| [24ca21c] | 43 | $wait workers[j];} |
| [844ebd8] | 44 | return s; |
| 45 | } | |
| 46 | ||
| 47 | void main() { | |
| [72c01cc] | 48 | double seq = adderSeq(&a[0], N); |
| 49 | double par = adderPar(&a[0], N); | |
| 50 | ||
| [f28d814] | 51 | $assert seq == par; |
| [844ebd8] | 52 | } |
Note:
See TracBrowser
for help on using the repository browser.
