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