1.23
2.0
main
test-branch
|
Last change
on this file since f2ab81f was 09b9231b, checked in by Manchun Zheng <zmanchun@…>, 12 years ago |
|
implemented assert statements with printing functionality; since $assert is redefined as a function in civlc.h, the translation in FunctionTranslator is changed and all examples using $assert is updated accordingly; translates assert() which is defined in assert.h into an assert statement; fixed warnings in mpi executor.
git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@536 fb995dde-84ed-4084-dfe6-e5aef3e2452c
|
-
Property mode
set to
100644
|
|
File size:
943 bytes
|
| Line | |
|---|
| 1 | /* Commandline execution:
|
|---|
| 2 | * civl verify -inputB=5 adder.cvl
|
|---|
| 3 | * */
|
|---|
| 4 | #include <civlc.h>
|
|---|
| 5 |
|
|---|
| 6 | $input int B; // upper bound on array length
|
|---|
| 7 | $input int N; // length of array
|
|---|
| 8 | $assume 0<=N && N<=B;
|
|---|
| 9 | $input double a[N];
|
|---|
| 10 |
|
|---|
| 11 | double adderSeq(double *p, int n) {
|
|---|
| 12 | double s = 0.0;
|
|---|
| 13 |
|
|---|
| 14 | $atomic{
|
|---|
| 15 | for (int i = 0; i < n; i++) {
|
|---|
| 16 | s += p[i];
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 | return s;
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | double adderPar(double *p, int n) {
|
|---|
| 23 | double s = 0.0; // sum shared by workers
|
|---|
| 24 | int mutex = 0; // mutex shared by workers
|
|---|
| 25 | $proc workers[n]; // one worker for each element!
|
|---|
| 26 |
|
|---|
| 27 | void worker(int i) {
|
|---|
| 28 | double t;
|
|---|
| 29 |
|
|---|
| 30 | $when (mutex == 0) mutex = 1;
|
|---|
| 31 | t = s;
|
|---|
| 32 | t += p[i];
|
|---|
| 33 | s = t;
|
|---|
| 34 | mutex = 0;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | $atomic {
|
|---|
| 38 | for (int j = 0; j < n; j++)
|
|---|
| 39 | workers[j] = $spawn worker(j);
|
|---|
| 40 | }
|
|---|
| 41 | $atomic {
|
|---|
| 42 | for (int j = 0; j < n; j++)
|
|---|
| 43 | $wait workers[j];
|
|---|
| 44 | }
|
|---|
| 45 | return s;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void main() {
|
|---|
| 49 | double seq = adderSeq(&a[0], N);
|
|---|
| 50 | double par = adderPar(&a[0], N);
|
|---|
| 51 |
|
|---|
| 52 | $assert(seq == par);
|
|---|
| 53 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.