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

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

format changes

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

  • Property mode set to 100644
File size: 1.3 KB
RevLine 
[260762f]1/*
[0ae124a]2author: Yihao
[f3368e99]3
4Link(challenge 2): http://etaps2015.verifythis.org/challenges
[260762f]5
6problem description:
7Various parallel GCD algorithms exist. In this challenge, we consider a
8simple Euclid-like algorithm with two parallel threads. One thread
9subtracts in one direction, the other thread subtracts in the other
10direction, and eventually this procedure converges on GCD.
11
[f3368e99]12command: civl verify ParallelGCD_2015_2.c
[260762f]13*/
14
[f3368e99]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
[0ae124a]25int myGCD(int a, int b) {
[260762f]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 }
[0ae124a]38 void worker2() {
39 while(a != b) {
40 if(b>a) {
[260762f]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
[0ae124a]54int seqGCD(int a, int b) {
55 while(a != b) {
[260762f]56 if(a > b)
57 a = a-b;
58 if(b > a)
59 b = b-a;
60 }
61 return a;
62}
63
[0ae124a]64void main() {
[260762f]65 int result1 = myGCD(A, B);
66 int minAB = A < B ? A : B;
[f3368e99]67
[260762f]68 $assert($forall {i = (result1+1) .. (minAB)} (A%i != 0 || B%i != 0));
[0ae124a]69 $assert(A%result1 == 0 && B%result1 == 0);
[260762f]70}
Note: See TracBrowser for help on using the repository browser.