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 |
|
|---|
| 15 | #include <civlc.cvh>
|
|---|
| 16 | #include <stdio.h>
|
|---|
| 17 |
|
|---|
| 18 | $input int A_BOUND=4;
|
|---|
| 19 | $input int B_BOUND=6;
|
|---|
| 20 | $input int A;
|
|---|
| 21 | $input int B;
|
|---|
| 22 | // assume the bound of A and B
|
|---|
| 23 | $assume (A>0 && B>0 && A<A_BOUND && B<B_BOUND);
|
|---|
| 24 |
|
|---|
| 25 | int myGCD(int a, int b) {
|
|---|
| 26 | $proc proc_a;
|
|---|
| 27 | $proc proc_b;
|
|---|
| 28 |
|
|---|
| 29 | void worker1() {
|
|---|
| 30 | while(a != b){
|
|---|
| 31 | if(a>b){
|
|---|
| 32 | int t1 = b;
|
|---|
| 33 | int t2 = a-t1;
|
|---|
| 34 | a = t2;
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | void worker2() {
|
|---|
| 39 | while(a != b) {
|
|---|
| 40 | if(b>a) {
|
|---|
| 41 | int t1 = a;
|
|---|
| 42 | int t2 = b-t1;
|
|---|
| 43 | b = t2;
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | proc_a= $spawn worker1();
|
|---|
| 48 | proc_b= $spawn worker2();
|
|---|
| 49 | $wait(proc_a);
|
|---|
| 50 | $wait(proc_b);
|
|---|
| 51 | return a;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | int seqGCD(int a, int b) {
|
|---|
| 55 | while(a != b) {
|
|---|
| 56 | if(a > b)
|
|---|
| 57 | a = a-b;
|
|---|
| 58 | if(b > a)
|
|---|
| 59 | b = b-a;
|
|---|
| 60 | }
|
|---|
| 61 | return a;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | void main() {
|
|---|
| 65 | int result1 = myGCD(A, B);
|
|---|
| 66 | int minAB = A < B ? A : B;
|
|---|
| 67 |
|
|---|
| 68 | $assert($forall {i = (result1+1) .. (minAB)} (A%i != 0 || B%i != 0));
|
|---|
| 69 | $assert(A%result1 == 0 && B%result1 == 0);
|
|---|
| 70 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.