source: CIVL/examples/concurrency/blockAdder.cvl@ dccd621

1.23 2.0 main test-branch
Last change on this file since dccd621 was 0baeebd, checked in by Ziqing Luo <ziqing@…>, 11 years ago

cleaned up examples

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@1776 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 blockAdder.cvl
4 */
5#include<civlc.cvh>
6
7$input int B = 6; // upper bound on array length
8$input int W = 3; // 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 $atomic {
35 for (int i=start; i<stop; i++)
36 localSum += a[i];
37 }
38 $when (lock==0) lock = 1;
39 result += localSum;
40 lock = 0;
41 }
42
43 $atomic {
44 for (int i=0; i<P; i++) ; // hack
45 for (int i=0; i<P; i++)
46 workers[i] = $spawn run_worker(i);
47 }
48 $atomic {
49 for (int i=0; i<P; i++)
50 $wait(workers[i]);
51 }
52 return result;
53}
54
55void main() {
56 double sum_seq = adder_seq();
57 double sum_par = adder_par();
58
59 $assert(sum_seq == sum_par);
60}
Note: See TracBrowser for help on using the repository browser.