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