001/*
002 * $Id: GroebnerBaseDistHybridMPITest.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 GroebnerBaseDistHybridMPITest 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>GroebnerBaseDistHybridMPITest</CODE> object.
059     * @param name String.
060     */
061    public GroebnerBaseDistHybridMPITest(String name) {
062        super(name);
063    }
064
065
066    /**
067     * suite.
068     */
069    public static Test suite() {
070        TestSuite suite = new TestSuite(GroebnerBaseDistHybridMPITest.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    GroebnerBaseDistributedHybridMPI<BigRational> bbdist;
097
098
099    GroebnerBaseDistributedHybridMPI<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() throws IOException {
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 GroebnerBaseDistributedHybridMPI<BigRational>(threads, threadsPerNode);
147            //bbdists = new GroebnerBaseDistributedHybridMPI<BigRational>(threads,threadsPerNode, new OrderedSyzPairlist<BigRational>());
148        } catch (MPIException e) {
149            e.printStackTrace();
150        }
151    }
152
153
154    @Override
155    protected void tearDown() {
156        a = b = c = d = e = null;
157        fac = null;
158        bbseq = null;
159        bbdist.terminate();
160        bbdist = null;
161        //bbdists.terminate();
162        bbdists = null;
163        ComputerThreads.terminate();
164    }
165
166
167    /**
168     * Test distributed GBase.
169     */
170    public void onlyOnetestDistributedGBase() throws MPIException {
171        L = new ArrayList<GenPolynomial<BigRational>>();
172        if (engine.Rank() == 0) {
173            a = fac.random(kl, ll, el, q);
174            b = fac.random(kl, ll, el, q);
175            c = fac.random(kl, ll, el, q);
176            d = fac.random(kl, ll, el, q);
177            e = d; //fac.random(kl, ll, el, q );
178        }
179        if (engine.Rank() == 0) {
180            L.add(a);
181            L.add(b);
182            System.out.println("L = " + L);
183        }
184
185        L = bbdist.GB(L);
186        if (engine.Rank() == 0) {
187            System.out.println("L0 = " + L);
188            assertTrue("isGB( { a } )", bbseq.isGB(L));
189            L.add(b);
190        }
191        if (mpjBug) {
192            try {
193                Thread.sleep(100);
194            } catch (InterruptedException e) {
195                e.printStackTrace();
196            }
197            return;
198        }
199
200        L = bbdist.GB(L);
201        if (engine.Rank() == 0) {
202            System.out.println("L1 = " + L);
203            assertTrue("isGB( { a, b } )", bbseq.isGB(L));
204            L.add(c);
205        }
206
207        L = bbdist.GB(L);
208        if (engine.Rank() == 0) {
209            System.out.println("L2 = " + L);
210            assertTrue("isGB( { a, b, c } )", bbseq.isGB(L));
211            L.add(d);
212        }
213
214        L = bbdist.GB(L);
215        if (engine.Rank() == 0) {
216            System.out.println("L3 = " + L);
217            assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L));
218            L.add(e);
219        }
220        L = bbdist.GB(L);
221        if (engine.Rank() == 0) {
222            System.out.println("L4 = " + L);
223            assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L));
224        } else {
225            System.out.println("rank = " + engine.Rank());
226        }
227    }
228
229
230    /**
231     * Test Trinks7 GBase.
232     */
233    @SuppressWarnings("unchecked")
234    public void testTrinks7GBase() throws MPIException {
235        List<GenPolynomial<BigRational>> Fl;
236        long t = 0;
237        if (engine.Rank() == 0) {
238            String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), "
239                            + "( 35 P + 40 Z + 25 T - 27 S ), "
240                            + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), "
241                            + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), "
242                            + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") ";
243            Reader source = new StringReader(exam);
244            GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source);
245            try {
246                F = (PolynomialList<BigRational>) parser.nextPolynomialSet();
247            } catch (IOException e) {
248                fail("" + e);
249            }
250            System.out.println("F = " + F);
251            Fl = F.list;
252            t = System.currentTimeMillis();
253        } else {
254            Fl = null;
255        }
256
257        G = bbdist.GB(Fl);
258
259        if (engine.Rank() == 0) {
260            t = System.currentTimeMillis() - t;
261            assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G));
262            assertEquals("#GB(Trinks7) == 6", 6, G.size());
263            //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G);
264            System.out.println("G = " + G);
265            System.out.println("executed in " + t + " milliseconds");
266        } else {
267            assertTrue("G == null: ", G == null);
268        }
269    }
270
271}