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.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 |
|
|---|
| 16 | double 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 |
|
|---|
| 24 | double 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 | $waitall(workers, P);
|
|---|
| 49 | return result;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void main() {
|
|---|
| 53 | double sum_seq = adder_seq();
|
|---|
| 54 | double sum_par = adder_par();
|
|---|
| 55 |
|
|---|
| 56 | $assert((sum_seq == sum_par));
|
|---|
| 57 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.