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;
018import mpi.MPIException;
019
020import edu.jas.arith.BigRational;
021import edu.jas.kern.ComputerThreads;
022import edu.jas.kern.MPIEngine;
023import edu.jas.poly.GenPolynomial;
024import edu.jas.poly.GenPolynomialRing;
025import edu.jas.poly.GenPolynomialTokenizer;
026import edu.jas.poly.PolynomialList;
027
028
029/**
030 * Distributed GroebnerBase MPI tests with JUnit.
031 * @author Heinz Kredel
032 */
033
034public class GroebnerBaseDistHybridMPITest extends TestCase {
035
036
037    protected static Comm engine;
038
039
040    boolean mpjBug = true; // bug after cancel recv
041
042
043    /**
044     * main
045     */
046    public static void main(String[] args) throws IOException, MPIException {
047        engine = MPIEngine.getCommunicator(args);
048        junit.textui.TestRunner.run(suite());
049        MPIEngine.terminate();
050        //ComputerThreads.terminate();
051    }
052
053
054    /**
055     * Constructs a <CODE>GroebnerBaseDistHybridMPITest</CODE> object.
056     * @param name String.
057     */
058    public GroebnerBaseDistHybridMPITest(String name) {
059        super(name);
060    }
061
062
063    /**
064     * suite.
065     */
066    public static Test suite() {
067        TestSuite suite = new TestSuite(GroebnerBaseDistHybridMPITest.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    GroebnerBaseDistributedHybridMPI<BigRational> bbdist;
094
095
096    GroebnerBaseDistributedHybridMPI<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    int threadsPerNode = 3;
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            bbdist = new GroebnerBaseDistributedHybridMPI<BigRational>(threads, threadsPerNode);
144            //bbdists = new GroebnerBaseDistributedHybridMPI<BigRational>(threads,threadsPerNode, new OrderedSyzPairlist<BigRational>());
145        } catch (MPIException e) {
146            e.printStackTrace();
147        }
148    }
149
150
151    @Override
152    protected void tearDown() {
153        a = b = c = d = e = null;
154        fac = null;
155        bbseq = null;
156        bbdist.terminate();
157        bbdist = null;
158        //bbdists.terminate();
159        bbdists = null;
160        ComputerThreads.terminate();
161    }
162
163
164    /**
165     * Test distributed GBase.
166     */
167    public void onlyOnetestDistributedGBase() throws MPIException {
168        L = new ArrayList<GenPolynomial<BigRational>>();
169        if (engine.Rank() == 0) {
170            a = fac.random(kl, ll, el, q);
171            b = fac.random(kl, ll, el, q);
172            c = fac.random(kl, ll, el, q);
173            d = fac.random(kl, ll, el, q);
174            e = d; //fac.random(kl, ll, el, q );
175        }
176        if (engine.Rank() == 0) {
177            L.add(a);
178            L.add(b);
179            System.out.println("L = " + L);
180        }
181
182        L = bbdist.GB(L);
183        if (engine.Rank() == 0) {
184            System.out.println("L0 = " + L);
185            assertTrue("isGB( { a } )", bbseq.isGB(L));
186            L.add(b);
187        }
188        if (mpjBug) {
189            try {
190                Thread.sleep(100);
191            } catch (InterruptedException e) {
192                e.printStackTrace();
193            }
194            return;
195        }
196
197        L = bbdist.GB(L);
198        if (engine.Rank() == 0) {
199            System.out.println("L1 = " + L);
200            assertTrue("isGB( { a, b } )", bbseq.isGB(L));
201            L.add(c);
202        }
203
204        L = bbdist.GB(L);
205        if (engine.Rank() == 0) {
206            System.out.println("L2 = " + L);
207            assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
208            L.add(d);
209        }
210
211        L = bbdist.GB(L);
212        if (engine.Rank() == 0) {
213            System.out.println("L3 = " + L);
214            assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
215            L.add(e);
216        }
217        L = bbdist.GB(L);
218        if (engine.Rank() == 0) {
219            System.out.println("L4 = " + L);
220            assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
221        } else {
222            System.out.println("rank = " + engine.Rank());
223        }
224    }
225
226
227    /**
228     * Test Trinks7 GBase.
229     */
230    @SuppressWarnings("unchecked")
231    public void testTrinks7GBase() throws MPIException {
232        List<GenPolynomial<BigRational>> Fl;
233        long t = 0;
234        if (engine.Rank() == 0) {
235            String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
236                            + "( 35 P + 40 Z + 25 T - 27 S ), "
237                            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
238                            + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
239                            + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
240            Reader source = new StringReader(exam);
241            GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
242            try {
243                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
244            } catch (IOException e) {
245                fail("" + e);
246            }
247            System.out.println("F = " + F);
248            Fl = F.list;
249            t = System.currentTimeMillis();
250        } else {
251            Fl = null;
252        }
253
254        G = bbdist.GB(Fl);
255
256        if (engine.Rank() == 0) {
257            t = System.currentTimeMillis() - t;
258            assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
259            assertEquals("#GB(Trinks7) == 6", 6, G.size());
260            //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
261            System.out.println("G = " + G);
262            System.out.println("executed in " + t + " milliseconds");
263        } else {
264            assertTrue("G == null: ", G == null);
265        }
266    }
267
268}