001/*
002 * $Id: GroebnerBaseDistHybridTest.java 5048 2014-12-30 17:45:01Z kredel $
003 */
004
005package edu.jas.gb;
006
007
008// import edu.jas.poly.GroebnerBase;
009
010import java.io.IOException;
011import java.io.Reader;
012import java.io.StringReader;
013import java.util.ArrayList;
014import java.util.List;
015
016import junit.framework.Test;
017import junit.framework.TestCase;
018import junit.framework.TestSuite;
019
020import org.apache.log4j.BasicConfigurator; // import org.apache.log4j.Logger;
021
022import edu.jas.arith.BigRational;
023import edu.jas.kern.ComputerThreads;
024import edu.jas.poly.GenPolynomial;
025import edu.jas.poly.GenPolynomialRing;
026import edu.jas.poly.GenPolynomialTokenizer;
027import edu.jas.poly.PolynomialList;
028import edu.jas.structure.RingElem;
029
030
031/**
032 * Distributed hybrid architecture GroebnerBase tests with JUnit.
033 * @author Heinz Kredel
034 */
035
036public class GroebnerBaseDistHybridTest extends TestCase {
037
038
039    //private static final Logger logger = Logger.getLogger(GroebnerBaseDistHybridTest.class);
040
041    /**
042     * main
043     */
044    public static void main(String[] args) {
045        BasicConfigurator.configure();
046        junit.textui.TestRunner.run(suite());
047        //ComputerThreads.terminate();
048    }
049
050
051    /**
052     * Constructs a <CODE>GroebnerBaseDistHybridTest</CODE> object.
053     * @param name String.
054     */
055    public GroebnerBaseDistHybridTest(String name) {
056        super(name);
057    }
058
059
060    /**
061     * suite.
062     */
063    public static Test suite() {
064        TestSuite suite = new TestSuite(GroebnerBaseDistHybridTest.class);
065        return suite;
066    }
067
068
069    int port = 4711;
070
071
072    String host = "localhost";
073
074
075    GenPolynomialRing<BigRational> fac;
076
077
078    List<GenPolynomial<BigRational>> L;
079
080
081    PolynomialList<BigRational> F;
082
083
084    List<GenPolynomial<BigRational>> G;
085
086
087    GroebnerBase<BigRational> bbseq;
088
089
090    GroebnerBaseDistributedHybrid<BigRational> bbdisthybs;
091
092
093    GroebnerBaseDistributedHybrid<BigRational> bbdisthyb;
094
095
096    GenPolynomial<BigRational> a;
097
098
099    GenPolynomial<BigRational> b;
100
101
102    GenPolynomial<BigRational> c;
103
104
105    GenPolynomial<BigRational> d;
106
107
108    GenPolynomial<BigRational> e;
109
110
111    int rl = 3; //4; //3; 
112
113
114    int kl = 4;
115
116
117    int ll = 7;
118
119
120    int el = 3;
121
122
123    float q = 0.2f; //0.4f
124
125
126    int threads = 2;
127
128
129    int threadsPerNode = 2;
130
131
132    @Override
133    protected void setUp() {
134        BigRational coeff = new BigRational(9);
135        fac = new GenPolynomialRing<BigRational>(coeff, rl);
136        a = b = c = d = e = null;
137        bbseq = new GroebnerBaseSeq<BigRational>();
138        bbdisthybs = null; //new GroebnerBaseDistributed<BigRational>(threads, port);
139        bbdisthyb = null; //new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
140    }
141
142
143    @Override
144    protected void tearDown() {
145        a = b = c = d = e = null;
146        fac = null;
147        bbseq = null;
148        bbdisthybs.terminate();
149        bbdisthybs = null;
150        bbdisthyb.terminate();
151        bbdisthyb = null;
152        ComputerThreads.terminate();
153    }
154
155
156    /**
157     * Helper method to start threads with distributed clients.
158     * 
159     */
160    Thread[] startThreads() {
161        Thread[] clients = new Thread[threads];
162        for (int t = 0; t < threads; t++) {
163            clients[t] = new Thread(new JunitClientHybrid(threadsPerNode, host, port));
164            clients[t].start();
165        }
166        return clients;
167    }
168
169
170    /**
171     * Helper method to stop threads with distributed clients.
172     * 
173     */
174    void stopThreads(Thread[] clients) {
175        for (int t = 0; t < threads; t++) {
176            try {
177                while (clients[t].isAlive()) {
178                    clients[t].interrupt();
179                    clients[t].join(100);
180                }
181            } catch (InterruptedException e) {
182            }
183        }
184    }
185
186
187    /**
188     * Test distributed hybrid GBase.
189     * 
190     */
191    public void testDistributedHybridGBase() {
192
193        bbdisthybs = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
194        bbdisthyb = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode,
195                        new OrderedSyzPairlist<BigRational>(), port);
196
197        Thread[] clients;
198
199        L = new ArrayList<GenPolynomial<BigRational>>();
200
201        a = fac.random(kl, ll, el, q);
202        b = fac.random(kl, ll, el, q);
203        c = fac.random(kl, ll, el, q);
204        d = fac.random(kl, ll, el, q);
205        e = d; //fac.random(kl, ll, el, q );
206
207        assertTrue("not isZERO( a )", !a.isZERO());
208        L.add(a);
209
210        clients = startThreads();
211        L = bbdisthyb.GB(L);
212        stopThreads(clients);
213        assertTrue("isGB( { a } )", bbseq.isGB(L));
214        //System.out.println("L = " + L.size() );
215
216        assertTrue("not isZERO( b )", !b.isZERO());
217        L.add(b);
218        //System.out.println("L = " + L.size() );
219
220        clients = startThreads();
221        L = bbdisthyb.GB(L);
222        stopThreads(clients);
223        assertTrue("isGB( { a, b } )", bbseq.isGB(L));
224        //System.out.println("L = " + L.size() );
225
226        assertTrue("not isZERO( c )", !c.isZERO());
227        L.add(c);
228
229        clients = startThreads();
230        L = bbdisthyb.GB(L);
231        stopThreads(clients);
232        assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
233        //System.out.println("L = " + L.size() );
234
235        assertTrue("not isZERO( d )", !d.isZERO());
236        L.add(d);
237
238        clients = startThreads();
239        L = bbdisthyb.GB(L);
240        stopThreads(clients);
241        assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
242        //System.out.println("L = " + L.size() );
243
244        assertTrue("not isZERO( e )", !e.isZERO());
245        L.add(e);
246
247        clients = startThreads();
248        L = bbdisthyb.GB(L);
249        stopThreads(clients);
250        assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
251        //System.out.println("L = " + L.size() );
252    }
253
254
255    /**
256     * Test Trinks7 GBase.
257     * 
258     */
259    @SuppressWarnings("cast")
260    public void testTrinks7GBase() {
261        bbdisthybs = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
262        bbdisthyb = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode,
263                        new OrderedSyzPairlist<BigRational>(), port);
264        Thread[] clients;
265        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
266                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
267                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
268                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
269        Reader source = new StringReader(exam);
270        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
271        try {
272            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
273        } catch (IOException e) {
274            fail("" + e);
275        }
276        //System.out.println("F = " + F);
277
278        clients = startThreads();
279        G = bbdisthyb.GB(F.list);
280        stopThreads(clients);
281
282        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
283        assertEquals("#GB(Trinks7) == 6", 6, G.size());
284        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
285        //System.out.println("G = " + trinks);
286
287    }
288
289
290    /**
291     * Test Trinks7 GBase.
292     * 
293     */
294    @SuppressWarnings("cast")
295    public void testTrinks7GBase_t1_p4() {
296        //bbdisthyb.terminate();
297        threads = 1;
298        threadsPerNode = 4;
299        bbdisthybs = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
300        bbdisthyb = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode,
301                        new OrderedSyzPairlist<BigRational>(), port);
302
303        Thread[] clients;
304        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
305                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
306                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
307                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
308        Reader source = new StringReader(exam);
309        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
310        try {
311            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
312        } catch (IOException e) {
313            fail("" + e);
314        }
315        //System.out.println("F = " + F);
316
317        clients = startThreads();
318        G = bbdisthyb.GB(F.list);
319        stopThreads(clients);
320
321        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
322        assertEquals("#GB(Trinks7) == 6", 6, G.size());
323        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
324        //System.out.println("G = " + trinks);
325    }
326
327
328    /**
329     * Test Trinks7 GBase.
330     * 
331     */
332    @SuppressWarnings("cast")
333    public void testTrinks7GBase_t4_p1() {
334        //bbdisthyb.terminate();
335        threads = 4;
336        threadsPerNode = 1;
337        bbdisthybs = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
338        bbdisthyb = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode,
339                        new OrderedSyzPairlist<BigRational>(), port);
340
341        Thread[] clients;
342        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
343                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
344                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
345                        + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
346        Reader source = new StringReader(exam);
347        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
348        try {
349            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
350        } catch (IOException e) {
351            fail("" + e);
352        }
353        //System.out.println("F = " + F);
354
355        clients = startThreads();
356        G = bbdisthyb.GB(F.list);
357        stopThreads(clients);
358
359        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
360        assertEquals("#GB(Trinks7) == 6", 6, G.size());
361        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
362        //System.out.println("G = " + trinks);
363    }
364
365
366    /**
367     * Test Trinks7 GBase.
368     * 
369     */
370    @SuppressWarnings("cast")
371    public void testTrinks7GBase_t2_p2() {
372
373        //bbdisthyb.terminate();
374        threads = 2;
375        threadsPerNode = 4;
376        bbdisthybs = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode, port);
377        bbdisthyb = new GroebnerBaseDistributedHybrid<BigRational>(threads, threadsPerNode,
378                        new OrderedSyzPairlist<BigRational>(), port);
379
380        Thread[] clients;
381        String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
382                        + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
383                        + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
384                        + "( 99 W - 11 B S + 3 B**2 ), " + ") ";
385        //      + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
386        Reader source = new StringReader(exam);
387        GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
388        try {
389            F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
390        } catch (IOException e) {
391            fail("" + e);
392        }
393        //System.out.println("F = " + F);
394
395        clients = startThreads();
396        G = bbdisthyb.GB(F.list);
397        stopThreads(clients);
398
399        assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
400        assertEquals("#GB(Trinks7) == 6", 6, G.size());
401        //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
402        //System.out.println("G = " + trinks);
403    }
404
405}
406
407
408/**
409 * Unit Test client to be executed by test threads.
410 */
411
412class JunitClientHybrid<C extends RingElem<C>> implements Runnable {
413
414
415    private final int threadsPerNode;
416
417
418    private final String host;
419
420
421    private final int port;
422
423
424    JunitClientHybrid(int threadsPerNode, String host, int port) {
425        this.threadsPerNode = threadsPerNode;
426        this.host = host;
427        this.port = port;
428    }
429
430
431    public void run() {
432        GroebnerBaseDistributedHybrid<C> bbd;
433        bbd = new GroebnerBaseDistributedHybrid<C>(1, threadsPerNode, null, null, port);
434        try {
435            bbd.clientPart(host);
436        } catch (IOException ignored) {
437        }
438        bbd.terminate();
439    }
440}