source: CIVL/examples/verifyThisProblems/parallelGCD.c@ 6247c10

1.23 2.0 main test-branch
Last change on this file since 6247c10 was 76f18de, checked in by Yihao Yan <yihaoyan1@…>, 10 years ago

format changes

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

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/*
2Author: Yihao Yan
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:
25For any numbers A less than 5 and B less than 7, myGCD returns the greatest common
26divisor 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
38int 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
67int 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
77void 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.