source: CIVL/examples/concurrency/blockAdderBad.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: 1.2 KB
Line 
1/* Compare sequential and parallel sum of array by block partition
2 * Example run:
3 * civl verify -inputB=6 -inputW=3 blockAdders.cvl
4 */
5#include<civlc.h>
6
7$input int B; // upper bound on array length
8$input int W; // upper bound on number of workers
9
10$input int N; // array length
11$assume N>=1 && N<=B;
12$input int P; // number of workers
13$assume P>=1 && P<=W;
14$input double a[N];
15
16double adder_seq() {
17 double s = 0.0;
18
19 for (int i=0; i<N; i++)
20 s += a[i];
21 return s;
22}
23
24double adder_par() {
25 $proc workers[P]; // the workers
26 int lock = 0; // lock for accessing result
27 double result = 0.0; // shared by all workers
28
29 void run_worker(int id) {
30 int start = (id*N)/P;
31 int stop = ((id+1)*N)/P;
32 double localSum = 0.0;
33
34 for (int i=start; i<stop; i++)
35 localSum += a[i];
36 $when (lock==0) lock = 1;
37 result += localSum;
38 // lock = 0; // forgot to release the lock
39 }
40
41 for (int i=0; i<P; i++) ; // hack
42 for (int i=0; i<P; i++)
43 workers[i] = $spawn run_worker(i);
44 for (int i=0; i<P; i++)
45 $wait workers[i];
46 return result;
47}
48
49void main() {
50 double sum_seq = adder_seq();
51 double sum_par = adder_par();
52
53 $assert sum_seq == sum_par;
54}
Note: See TracBrowser for help on using the repository browser.