001/*
002 * $Id$
003 */
004
005package edu.jas.gb;
006
007
008import java.io.IOException;
009import java.io.Reader;
010import java.io.StringReader;
011import java.util.ArrayList;
012import java.util.List;
013
014import junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017import mpi.Comm;
018
019import edu.jas.arith.BigRational;
020import edu.jas.kern.ComputerThreads;
021import edu.jas.kern.MPJEngine;
022import edu.jas.poly.GenPolynomial;
023import edu.jas.poly.GenPolynomialRing;
024import edu.jas.poly.GenPolynomialTokenizer;
025import edu.jas.poly.PolynomialList;
026
027
028/**
029 * Distributed GroebnerBase MPJ tests with JUnit.
030 * @author Heinz Kredel
031 */
032
033public class GroebnerBaseDistMPJTest extends TestCase {
034
035
036    protected static Comm engine;
037
038
039    boolean mpjBug = true; // bug after cancel recv
040
041
042    /**
043     * main
044     */
045    public static void main(String[] args) throws IOException {
046        engine = MPJEngine.getCommunicator(args);
047        junit.textui.TestRunner.run(suite());
048        engine.Barrier();
049        MPJEngine.terminate();
050        //ComputerThreads.terminate();
051    }
052
053
054    /**
055     * Constructs a <CODE>GroebnerBaseDistMPJTest</CODE> object.
056     * @param name String.
057     */
058    public GroebnerBaseDistMPJTest(String name) {
059        super(name);
060    }
061
062
063    /**
064     * suite.
065     */
066    public static Test suite() {
067        TestSuite suite = new TestSuite(GroebnerBaseDistMPJTest.class);
068        return suite;
069    }
070
071
072    int port = 4711;
073
074
075    String host = "localhost";
076
077
078    GenPolynomialRing<BigRational> fac;
079
080
081    List<GenPolynomial<BigRational>> L;
082
083
084    PolynomialList<BigRational> F;
085
086
087    List<GenPolynomial<BigRational>> G;
088
089
090    GroebnerBase<BigRational> bbseq;
091
092
093    GroebnerBaseDistributedMPJ<BigRational> bbdist;
094
095
096    GroebnerBaseDistributedMPJ<BigRational> bbdists;
097
098
099    GenPolynomial<BigRational> a;
100
101
102    GenPolynomial<BigRational> b;
103
104
105    GenPolynomial<BigRational> c;
106
107
108    GenPolynomial<BigRational> d;
109
110
111    GenPolynomial<BigRational> e;
112
113
114    int rl = 3; //4; //3; 
115
116
117    int kl = 4;
118
119
120    int ll = 7;
121
122
123    int el = 3;
124
125
126    float q = 0.2f; //0.4f
127
128
129    int threads;
130
131
132    @Override
133    protected void setUp() {
134        try {
135            threads = engine.Size();
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            bbdists = new GroebnerBaseDistributedMPJ<BigRational>(threads);
141            bbdist = new GroebnerBaseDistributedMPJ<BigRational>(threads,
142                            new OrderedSyzPairlist<BigRational>());
143        } catch (IOException e) {
144            e.printStackTrace();
145        }
146    }
147
148
149    @Override
150    protected void tearDown() {
151        a = b = c = d = e = null;
152        fac = null;
153        bbseq = null;
154        bbdist.terminate();
155        bbdist = null;
156        bbdists.terminate();
157        bbdists = null;
158        ComputerThreads.terminate();
159    }
160
161
162    /**
163     * Test distributed GBase.
164     */
165    public void testDistributedGBase() {
166        L = new ArrayList<GenPolynomial<BigRational>>();
167        if (engine.Rank() == 0) {
168            a = fac.random(kl, ll, el, q);
169            b = fac.random(kl, ll, el, q);
170            c = fac.random(kl, ll, el, q);
171            d = fac.random(kl, ll, el, q);
172            e = d; //fac.random(kl, ll, el, q );
173        }
174        if (engine.Rank() == 0) {
175            assertTrue("not isZERO( a )", !a.isZERO());
176            L.add(a);
177        }
178
179        L = bbdist.GB(L);
180        if (engine.Rank() == 0) {
181            assertTrue("isGB( { a } )", bbseq.isGB(L));
182            assertTrue("not isZERO( b )", !b.isZERO());
183            L.add(b);
184            //System.out.println("L = " + L.size() );
185        }
186        if (mpjBug) {
187            return;
188        }
189
190        L = bbdist.GB(L);
191        if (engine.Rank() == 0) {
192            assertTrue("isGB( { a, b } )", bbseq.isGB(L));
193            assertTrue("not isZERO( c )", !c.isZERO());
194            L.add(c);
195        }
196
197        L = bbdist.GB(L);
198        if (engine.Rank() == 0) {
199            assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
200            assertTrue("not isZERO( d )", !d.isZERO());
201            L.add(d);
202        }
203        L = bbdist.GB(L);
204        if (engine.Rank() == 0) {
205            assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
206            assertTrue("not isZERO( e )", !e.isZERO());
207            L.add(e);
208        }
209        L = bbdist.GB(L);
210        if (engine.Rank() == 0) {
211            assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
212        }
213    }
214
215
216    /**
217     * Test Trinks7 GBase.
218     */
219    @SuppressWarnings("unchecked")
220    public void testTrinks7GBase() {
221        List<GenPolynomial<BigRational>> Fl;
222        long t = 0;
223        if (engine.Rank() == 0) {
224            String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
225                            + "( 35 P + 40 Z + 25 T - 27 S ), "
226                            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
227                            + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
228                            + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
229            Reader source = new StringReader(exam);
230            GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
231            try {
232                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
233            } catch (IOException e) {
234                fail("" + e);
235            }
236            System.out.println("F = " + F);
237            Fl = F.list;
238            t = System.currentTimeMillis();
239        } else {
240            Fl = null;
241        }
242
243        G = bbdists.GB(Fl);
244
245        if (engine.Rank() == 0) {
246            t = System.currentTimeMillis() - t;
247            assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
248            assertEquals("#GB(Trinks7) == 6", 6, G.size());
249            //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
250            System.out.println("G = " + G);
251            System.out.println("executed in " + t + " milliseconds");
252        }
253    }
254
255}