1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | Author: Yihao Yan
|
|---|
| 3 |
|
|---|
| 4 | See challenge 2 of: http://etaps2015.verifythis.org/challenges
|
|---|
| 5 |
|
|---|
| 6 | -----------------
|
|---|
| 7 | Problem description:
|
|---|
| 8 |
|
|---|
| 9 | Various parallel GCD algorithms exist. In this challenge, we consider a
|
|---|
| 10 | simple Euclid-like algorithm with two parallel threads. One thread
|
|---|
| 11 | subtracts in one direction, the other thread subtracts in the other
|
|---|
| 12 | direction, and eventually this procedure converges on GCD.
|
|---|
| 13 |
|
|---|
| 14 | Verification tasks
|
|---|
| 15 | ------------------
|
|---|
| 16 |
|
|---|
| 17 | Specify and verify the following behaviour of this parallel GCD algorithm:
|
|---|
| 18 |
|
|---|
| 19 | Input: two positive integers a and b
|
|---|
| 20 | Output: a positive number that is the greatest common divisor of a and b
|
|---|
| 21 |
|
|---|
| 22 | command: civl verify parallelGCD.c
|
|---|
| 23 |
|
|---|
| 24 | result:
|
|---|
| 25 | For any numbers A less than 5 and B less than 7, myGCD returns the greatest common
|
|---|
| 26 | divisor of them.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <civlc.cvh>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 |
|
|---|
| 32 | $input int A_BOUND=4;
|
|---|
| 33 | $input int B_BOUND=6;
|
|---|
| 34 | $input int A;
|
|---|
| 35 | $input int B;
|
|---|
| 36 | $assume (A>0 && B>0 && A<A_BOUND && B<B_BOUND);
|
|---|
| 37 |
|
|---|
| 38 | int myGCD(int a, int b) {
|
|---|
| 39 | $proc proc_a;
|
|---|
| 40 | $proc proc_b;
|
|---|
| 41 |
|
|---|
| 42 | void worker1() {
|
|---|
| 43 | while(a != b){
|
|---|
| 44 | if(a>b){
|
|---|
| 45 | int t1 = b;
|
|---|
| 46 | int t2 = a-t1;
|
|---|
| 47 | a = t2;
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | void worker2() {
|
|---|
| 52 | while(a != b) {
|
|---|
| 53 | if(b>a) {
|
|---|
| 54 | int t1 = a;
|
|---|
| 55 | int t2 = b-t1;
|
|---|
| 56 | b = t2;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | proc_a= $spawn worker1();
|
|---|
| 61 | proc_b= $spawn worker2();
|
|---|
| 62 | $wait(proc_a);
|
|---|
| 63 | $wait(proc_b);
|
|---|
| 64 | return a;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | int seqGCD(int a, int b) {
|
|---|
| 68 | while(a != b) {
|
|---|
| 69 | if(a > b)
|
|---|
| 70 | a = a-b;
|
|---|
| 71 | if(b > a)
|
|---|
| 72 | b = b-a;
|
|---|
| 73 | }
|
|---|
| 74 | return a;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void main() {
|
|---|
| 78 | int result1 = myGCD(A, B);
|
|---|
| 79 | int minAB = A < B ? A : B;
|
|---|
| 80 |
|
|---|
| 81 | $assert($forall {i = (result1+1) .. (minAB)} (A%i != 0 || B%i != 0));
|
|---|
| 82 | $assert(A%result1 == 0 && B%result1 == 0);
|
|---|
| 83 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.