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