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 GroebnerBaseDistHybridMPJTest 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>GroebnerBaseDistHybridMPJTest</CODE> object.
056     * @param name String.
057     */
058    public GroebnerBaseDistHybridMPJTest(String name) {
059        super(name);
060    }
061
062
063    /**
064     * suite.
065     */
066    public static Test suite() {
067        TestSuite suite = new TestSuite(GroebnerBaseDistHybridMPJTest.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    GroebnerBaseDistributedHybridMPJ<BigRational> bbdist;
094
095
096    GroebnerBaseDistributedHybridMPJ<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() {
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 GroebnerBaseDistributedHybridMPJ<BigRational>(threads, threadsPerNode);
144            //bbdists = new GroebnerBaseDistributedHybridMPJ<BigRational>(threads,threadsPerNode, new OrderedSyzPairlist<BigRational>());
145        } catch (IOException e) {
146            e.printStackTrace();
147        }
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 onlyOnetestDistributedGBase() {
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            L.add(a);
179            L.add(b);
180            System.out.println("L = " + L);
181        }
182
183        L = bbdist.GB(L);
184        if (engine.Rank() == 0) {
185            System.out.println("L0 = " + L);
186            assertTrue("isGB( { a } )", bbseq.isGB(L));
187            L.add(b);
188        }
189        if (mpjBug) {
190            try {
191                Thread.sleep(100);
192            } catch (InterruptedException e) {
193                e.printStackTrace();
194            }
195            return;
196        }
197
198        L = bbdist.GB(L);
199        if (engine.Rank() == 0) {
200            System.out.println("L1 = " + L);
201            assertTrue("isGB( { a, b } )", bbseq.isGB(L));
202            L.add(c);
203        }
204
205        L = bbdist.GB(L);
206        if (engine.Rank() == 0) {
207            System.out.println("L2 = " + L);
208            assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
209            L.add(d);
210        }
211
212        L = bbdist.GB(L);
213        if (engine.Rank() == 0) {
214            System.out.println("L3 = " + L);
215            assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
216            L.add(e);
217        }
218        L = bbdist.GB(L);
219        if (engine.Rank() == 0) {
220            System.out.println("L4 = " + L);
221            assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
222        } else {
223            System.out.println("rank = " + engine.Rank());
224        }
225    }
226
227
228    /**
229     * Test Trinks7 GBase.
230     */
231    @SuppressWarnings("unchecked")
232    public void testTrinks7GBase() {
233        List<GenPolynomial<BigRational>> Fl;
234        long t = 0;
235        if (engine.Rank() == 0) {
236            String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
237                            + "( 35 P + 40 Z + 25 T - 27 S ), "
238                            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
239                            + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
240                            + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
241            Reader source = new StringReader(exam);
242            GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
243            try {
244                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
245            } catch (IOException e) {
246                fail("" + e);
247            }
248            System.out.println("F = " + F);
249            Fl = F.list;
250            t = System.currentTimeMillis();
251        } else {
252            Fl = null;
253        }
254
255        G = bbdist.GB(Fl);
256
257        if (engine.Rank() == 0) {
258            t = System.currentTimeMillis() - t;
259            assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
260            assertEquals("#GB(Trinks7) == 6", 6, G.size());
261            //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
262            System.out.println("G = " + G);
263            System.out.println("executed in " + t + " milliseconds");
264        } else {
265            assertTrue("G == null: ", G == null);
266        }
267    }
268
269}