source: CIVL/examples/concurrency/adderBad.cvl@ 4f22a92

1.23 2.0 main test-branch
Last change on this file since 4f22a92 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: 720 bytes
RevLine 
[72c01cc]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 $proc workers[n]; // one worker for each element!
20
21 void worker(int i) {
22 double t;
23
24 t = s;
25 t += p[i];
26 s = t;
27 }
28
29 for (int j = 0; j < n; j++)
30 workers[j] = $spawn worker(j);
31 for (int j = 0; j < n; j++)
32 $wait workers[j];
33 return s;
34}
35
36void main() {
37 double seq = adderSeq(&a[0], N);
38 double par = adderPar(&a[0], N);
39
40 $assert seq == par;
41}
Note: See TracBrowser for help on using the repository browser.