source: CIVL/examples/concurrency/adder.cvl@ 9705dfd

1.23 2.0 main test-branch
Last change on this file since 9705dfd was 72c01cc, checked in by Stephen Siegel <siegel@…>, 13 years ago

Continuing to clean up examples, but just realized the minimal
depth search is not quite right, it won't necessarily find
the minimum, as revealed by dining philosophers.

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

  • Property mode set to 100644
File size: 813 bytes
Line 
1#include <civlc.h>
2
3$input int B; // upper bound on array length
4$input int N; // length of array
5$assume 0<=N && N<=B;
6$input double a[N];
7
8double adderSeq(double *p, int n) {
9 double s = 0.0;
10
11 for (int i = 0; i < n; i++) {
12 s += p[i];
13 }
14 return s;
15}
16
17double adderPar(double *p, int n) {
18 double s = 0.0; // sum shared by workers
19 int mutex = 0; // mutex shared by workers
20 $proc workers[n]; // one worker for each element!
21
22 void worker(int i) {
23 double t;
24
25 $when (mutex == 0) mutex = 1;
26 t = s;
27 t += p[i];
28 s = t;
29 mutex = 0;
30 }
31
32 for (int j = 0; j < n; j++)
33 workers[j] = $spawn worker(j);
34 for (int j = 0; j < n; j++)
35 $wait workers[j];
36 return s;
37}
38
39void main() {
40 double seq = adderSeq(&a[0], N);
41 double par = adderPar(&a[0], N);
42
43 $assert seq == par;
44}
Note: See TracBrowser for help on using the repository browser.