source: CIVL/examples/verifyThisProblems/parallelGCD.c@ adcb43a

1.23 2.0 main test-branch
Last change on this file since adcb43a was 41eb9b6, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

format changing

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@3144 fb995dde-84ed-4084-dfe6-e5aef3e2452c

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