[Pntool-developers] SF.net SVN: pntool:[254] examples
Brought to you by:
compaqdrew,
miordache
From: <mio...@us...> - 2011-07-21 14:49:32
|
Revision: 254 http://pntool.svn.sourceforge.net/pntool/?rev=254&view=rev Author: miordache Date: 2011-07-21 14:49:26 +0000 (Thu, 21 Jul 2011) Log Message: ----------- Added Paths: ----------- examples/README examples/inteq.sp Added: examples/README =================================================================== --- examples/README (rev 0) +++ examples/README 2011-07-21 14:49:26 UTC (rev 254) @@ -0,0 +1,4 @@ +Here is the list of examples: + +inteq.sp: a simple example illustrating how to write concurrent program specifications. It specifies a concurrent program for finding integer solutions to the equation w^2 = x^2 + y^2 + z^2. + Added: examples/inteq.sp =================================================================== --- examples/inteq.sp (rev 0) +++ examples/inteq.sp 2011-07-21 14:49:26 UTC (rev 254) @@ -0,0 +1,119 @@ +// inteq.hl -- a concurrent program for finding integer solutions to the +// equation w^2 = x^2 + y^2 + z^2 +// Fastest if built with the option -s2 + +thread t_one { + + /* + Let's create the following Petri net + + t3 + --->| + | + p0 t0 p1| t1 p2 + O-->|--->O-->|-->O + | | + | | + ---|<---- + t2 + */ + + places: p0 p1 p2 p3 + transitions: t0 t1 t2 t3 + + (p0, t0, p1); (p1, t3) { i > N }; (p1, t1, p2) { i <= N }; (p2, t2, p1); +} + + +thread t_calc { // The thread group used to perform the calculations + + // The PN structure used here is simpler: + + // |-->O-->|-->O-->| + // t0 p1 t1 p2 t2 + + places: p1 p2 + transitions: t0 t1 t2 + + (t0, p1); (p1, t1, p2); (p2, t2) +} + + +sync t_one.t1 t_calc.t0 +sync t_one.t2 t_calc.t1 + +initialize: t_one(p0:1) + +// Here is the code + +t_one.include { +int lo, hi, id, mx; // these are global variables +} + +t_one.main { +// here are local variables +int max = -1; +int N = -1; +int i, j; +} + +t_one.p0 { + +while(1) { + fprintf(stderr,"Enter the number of threads: "); + scanf("%d", &N); + if(N < 1 || N > 20) + fprintf(stderr,"The number must be between 1 and 20.\n"); + else + break; +} +while(1) { + fprintf(stderr,"Enter the maximum value of the variables: "); + scanf("%d", &max); + if(max < N) + fprintf(stderr,"The maximum value should be greater than %d\n", N); + else + break; +} + +j = max/N; +i = 0; mx = max; + +} // here is the end of the t_one.p0 code + + +t_one.p1 { + lo = i*j; + i++; + if(i < N) hi = i*j; + else hi = max; + id = i; +} + + +t_calc.include { +#include<math.h> +extern id, lo, hi, mx; +} + +t_calc.main { + int i, x, y, z, w, u, tid, t_lo, t_hi; +} + +t_calc.p1 { + // get parameters from t_one + tid = id; t_lo = lo; t_hi = hi; + fprintf(stderr, "Thread %d has began\n", id); fflush(0); +} + +t_calc.p2 { + for(x = t_lo; x < t_hi; x++) + for(y = x; y <= mx; y++) + for(z = y; z <= mx; z++) { + u = x*x + y*y + z*z; + w = (int) sqrt((double) u); + if(w*w == u) { + fprintf(stdout, "%d^2 = %d^2 + %d^2 + %d^2\n", w, x, y, z);fflush(0); + } + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |