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