source: CIVL/examples/concurrency/blockAdderBad.cvl

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[72c01cc]1/* Compare sequential and parallel sum of array by block partition
2 * Example run:
[9fb69d3]3 * civl verify -inputB=6 -inputW=3 blockAdderBad.cvl
4 * or (if you want to find the minimal counterexample)
5 * civl verify -inputB=6 -inputW=3 blockAdderBad.cvl -min
[72c01cc]6 */
[e6b02c8]7#include<civlc.cvh>
[72c01cc]8
[0baeebd]9$input int B = 6; // upper bound on array length
10$input int W = 3; // upper bound on number of workers
[72c01cc]11
12$input int N; // array length
[3ff27cf]13$assume(N>=1 && N<=B);
[72c01cc]14$input int P; // number of workers
[3ff27cf]15$assume(P>=1 && P<=W);
[72c01cc]16$input double a[N];
17
18double adder_seq() {
19 double s = 0.0;
20
21 for (int i=0; i<N; i++)
22 s += a[i];
23 return s;
24}
25
26double adder_par() {
27 $proc workers[P]; // the workers
28 int lock = 0; // lock for accessing result
29 double result = 0.0; // shared by all workers
30
31 void run_worker(int id) {
32 int start = (id*N)/P;
33 int stop = ((id+1)*N)/P;
34 double localSum = 0.0;
35
36 for (int i=start; i<stop; i++)
37 localSum += a[i];
38 $when (lock==0) lock = 1;
39 result += localSum;
40 // lock = 0; // forgot to release the lock
41 }
42
43 for (int i=0; i<P; i++) ; // hack
44 for (int i=0; i<P; i++)
45 workers[i] = $spawn run_worker(i);
[3ff27cf]46 $waitall(workers, P);
[72c01cc]47 return result;
48}
49
50void main() {
51 double sum_seq = adder_seq();
52 double sum_par = adder_par();
53
[3ff27cf]54 $assert((sum_seq == sum_par));
[72c01cc]55}
Note: See TracBrowser for help on using the repository browser.