001    /*
002     * $Id: RunGB.java 3655 2011-06-02 18:20:54Z kredel $
003     */
004    
005    package edu.jas.application;
006    
007    
008    import java.io.FileNotFoundException;
009    import java.io.FileReader;
010    import java.io.StringReader;
011    import java.io.IOException;
012    import java.io.Reader;
013    import java.util.List;
014    
015    import org.apache.log4j.BasicConfigurator;
016    
017    import edu.jas.kern.ComputerThreads;
018    import edu.jas.poly.GenPolynomialRing;
019    import edu.jas.poly.GenPolynomialTokenizer;
020    import edu.jas.poly.PolynomialList;
021    import edu.jas.util.ExecutableServer;
022    import edu.jas.gb.GBDist;
023    import edu.jas.gb.GBDistHybrid;
024    import edu.jas.gb.GroebnerBaseAbstract;
025    import edu.jas.gb.GroebnerBaseParallel;
026    import edu.jas.gb.GroebnerBaseSeq;
027    import edu.jas.gb.OrderedSyzPairlist;
028    import edu.jas.gb.ReductionPar;
029    import edu.jas.gb.ReductionSeq;
030    import edu.jas.gbufd.GBFactory;
031    import edu.jas.util.CatReader;
032    
033    
034    /**
035     * Simple setup to run a GB example. <br />
036     * Usage: RunGB [seq(+)|par(+)|dist(1)(+)|disthyb|cli] &lt;file&gt;
037     * #procs/#threadsPerNode [machinefile]
038     * @author Heinz Kredel
039     */
040    
041    public class RunGB {
042    
043    
044        /**
045         * Check result GB if it is a GB.
046         */
047        static boolean doCheck = true; //false;
048    
049    
050        /**
051         * main method to be called from commandline <br />
052         * Usage: RunGB [seq|par(+)|dist(1)(+)|disthyb|cli] &lt;file&gt;
053         * #procs/#threadsPerNode [machinefile]
054         */
055    
056        public static void main(java.lang.String[] args) {
057    
058            BasicConfigurator.configure();
059    
060            String usage = "Usage: RunGB "
061                + "[ seq | seq+ | par | par+ | dist | dist1 | dist+ | dist1+ | disthyb1 | cli [port] ] "
062                + "<file> " + "#procs/#threadsPerNode " + "[machinefile] <check>";
063            if (args.length < 1) {
064                System.out.println(usage);
065                return;
066            }
067    
068            boolean pairseq = false;
069            String kind = args[0];
070            String[] allkinds = new String[] { "seq", "seq+", "par", "par+", "dist", "dist1", "dist+", "dist1+",
071                                               "disthyb1", "cli" };
072            boolean sup = false;
073            for (int i = 0; i < allkinds.length; i++) {
074                if (kind.equals(allkinds[i])) {
075                    sup = true;
076                    if (kind.indexOf("+") >= 0) {
077                        pairseq = true;
078                    }
079                }
080            }
081            if (!sup) {
082                System.out.println(usage);
083                return;
084            }
085    
086            boolean once = false;
087            final int GB_SERVER_PORT = 7114;
088            //inal int EX_CLIENT_PORT = GB_SERVER_PORT + 1000; 
089            int port = GB_SERVER_PORT;
090    
091            if (kind.equals("cli")) {
092                if (args.length >= 2) {
093                    try {
094                        port = Integer.parseInt(args[1]);
095                    } catch (NumberFormatException e) {
096                        e.printStackTrace();
097                        System.out.println(usage);
098                        return;
099                    }
100                }
101                runClient(port);
102                return;
103            }
104    
105            String filename = null;
106            if (!kind.equals("cli")) {
107                if (args.length < 2) {
108                    System.out.println(usage);
109                    return;
110                }
111                filename = args[1];
112            }
113    
114            for ( int i = 0; i < args.length; i++ ) {
115                if ( args[i].equals("check") ) {
116                    doCheck = true;
117                }
118            }
119    
120            int threads = 0;
121            int threadsPerNode = 1;
122            if (kind.startsWith("par") || kind.startsWith("dist")) {
123                if (args.length < 3) {
124                    System.out.println(usage);
125                    return;
126                }
127                String tup = args[2];
128                String t = tup;
129                int i = tup.indexOf("/");
130                if (i >= 0) {
131                    t = tup.substring(0, i).trim();
132                    tup = tup.substring(i + 1).trim();
133                    try {
134                        threadsPerNode = Integer.parseInt(tup);
135                    } catch (NumberFormatException e) {
136                        e.printStackTrace();
137                        System.out.println(usage);
138                        return;
139                    }
140                }
141                try {
142                    threads = Integer.parseInt(t);
143                } catch (NumberFormatException e) {
144                    e.printStackTrace();
145                    System.out.println(usage);
146                    return;
147                }
148            }
149    
150            String mfile = null;
151            if (kind.startsWith("dist")) {
152                if (args.length >= 4) {
153                    mfile = args[3];
154                } else {
155                    mfile = "machines";
156                }
157            }
158    
159            Reader problem = null;
160            try {
161                problem = new FileReader(filename);
162            } catch (FileNotFoundException e) {
163                e.printStackTrace();
164                System.out.println(usage);
165                return;
166            }
167    
168            RingFactoryTokenizer rftok = new RingFactoryTokenizer(problem);
169            GenPolynomialRing pfac = null;
170            try {
171                pfac = rftok.nextPolynomialRing();
172                rftok = null;
173            } catch (IOException e) {
174                e.printStackTrace();
175                return;
176            }
177            Reader polyreader = new CatReader(new StringReader("("),problem); // ( has gone
178            GenPolynomialTokenizer tok = new GenPolynomialTokenizer(pfac,polyreader);
179            PolynomialList S = null;
180            try {
181                S = new PolynomialList(pfac,tok.nextPolynomialList());
182            } catch (IOException e) {
183                e.printStackTrace();
184                return;
185            }
186            System.out.println("S =\n" + S);
187    
188            if (kind.startsWith("seq")) {
189                runSequential(S, pairseq);
190            }
191            if (kind.startsWith("par")) {
192                runParallel(S, threads, pairseq);
193            }
194            if (kind.startsWith("dist1")) {
195                runMasterOnce(S, threads, mfile, port, pairseq);
196            } else if (kind.startsWith("disthyb1")) {
197                runMasterOnceHyb(S, threads, threadsPerNode, mfile, port, pairseq);
198            } else if (kind.startsWith("dist")) {
199                runMaster(S, threads, mfile, port, pairseq);
200            }
201            ComputerThreads.terminate();
202            //System.exit(0);
203        }
204    
205    
206        @SuppressWarnings("unchecked")
207        static void runMaster(PolynomialList S, int threads, String mfile, int port, boolean pairseq) {
208            List L = S.list;
209            List G = null;
210            long t, t1;
211    
212            t = System.currentTimeMillis();
213            System.out.println("\nGroebner base distributed (" + threads + ", " + mfile + ", " + port +  ") ...");
214            GBDist gbd = null;
215            GBDist gbds = null;
216            if (pairseq) {
217                //gbds = new GBDistSP(threads,mfile, port);
218                gbds = new GBDist(threads, new OrderedSyzPairlist(), mfile, port);
219            } else {
220                gbd = new GBDist(threads, mfile, port);
221            }
222            t1 = System.currentTimeMillis();
223            if (pairseq) {
224                G = gbds.execute(L);
225            } else {
226                G = gbd.execute(L);
227            }
228            t1 = System.currentTimeMillis() - t1;
229            if (pairseq) {
230                gbds.terminate(false);
231            } else {
232                gbd.terminate(false);
233            }
234            S = new PolynomialList(S.ring, G);
235            System.out.println("G =\n" + S);
236            System.out.println("G.size() = " + G.size());
237            t = System.currentTimeMillis() - t;
238            if (pairseq) {
239                System.out.print("d+ ");
240            } else {
241                System.out.print("d ");
242            }
243            System.out.println("= " + threads + ", time = " + t + " milliseconds, " + (t - t1) + " start-up");
244            checkGB(S);
245            System.out.println("");
246        }
247    
248    
249        @SuppressWarnings("unchecked")
250        static void runMasterOnce(PolynomialList S, int threads, String mfile, int port, boolean pairseq) {
251            List L = S.list;
252            List G = null;
253            long t, t1;
254    
255            t = System.currentTimeMillis();
256            System.out.println("\nGroebner base distributed[once] (" + threads + ", " + mfile + ", " + port +  ") ...");
257            GBDist gbd = null;
258            GBDist gbds = null;
259            if (pairseq) {
260                //gbds = new GBDistSP(threads, mfile, port);
261                gbds = new GBDist(threads, new OrderedSyzPairlist(), mfile, port);
262            } else {
263                gbd = new GBDist(threads, mfile, port);
264            }
265            t1 = System.currentTimeMillis();
266            if (pairseq) {
267                G = gbds.execute(L);
268            } else {
269                G = gbd.execute(L);
270            }
271            t1 = System.currentTimeMillis() - t1;
272            if (pairseq) {
273                gbds.terminate(true);
274            } else {
275                gbd.terminate(true);
276            }
277            S = new PolynomialList(S.ring, G);
278            System.out.println("G =\n" + S);
279            System.out.println("G.size() = " + G.size());
280            t = System.currentTimeMillis() - t;
281            if (pairseq) {
282                System.out.print("d+ ");
283            } else {
284                System.out.print("d ");
285            }
286            System.out.println("= " + threads + ", time = " + t + " milliseconds, " + (t - t1) + " start-up");
287            checkGB(S);
288            System.out.println("");
289        }
290    
291    
292        @SuppressWarnings("unchecked")
293        static void runMasterOnceHyb(PolynomialList S, int threads, int threadsPerNode, String mfile, int port,
294                                     boolean pairseq) {
295            List L = S.list;
296            List G = null;
297            long t, t1;
298    
299            t = System.currentTimeMillis();
300            System.out.println("\nGroebner base distributed hybrid[once] (" + threads + "/" + threadsPerNode + ", " + mfile + ", " + port +  ") ...");
301            GBDistHybrid gbd = null;
302            GBDistHybrid gbds = null; 
303            if (pairseq) {
304                //System.out.println("... not implemented.");
305                //return;
306                // gbds = new GBDistSP(threads, mfile, port);
307                gbds = new GBDistHybrid(threads, threadsPerNode, new OrderedSyzPairlist(), mfile, port);
308            } else {
309                gbd = new GBDistHybrid(threads, threadsPerNode, mfile, port);
310            }
311            t1 = System.currentTimeMillis();
312            if (pairseq) {
313                G = gbds.execute( L );
314            } else {
315                G = gbd.execute(L);
316            }
317            t1 = System.currentTimeMillis() - t1;
318            if (pairseq) {
319                //gbds.terminate(true);
320            } else {
321                //gbd.terminate(true);
322                gbd.terminate(false); // plus eventually killed by script
323            }
324            t = System.currentTimeMillis() - t;
325            S = new PolynomialList(S.ring, G);
326            System.out.println("G =\n" + S);
327            System.out.println("G.size() = " + G.size());
328            if (pairseq) {
329                System.out.print("d+ ");
330            } else {
331                System.out.print("d ");
332            }
333            System.out.println("= " + threads + ", ppn = " + threadsPerNode + ", time = " + t + " milliseconds, " + (t - t1) + " start-up");
334            checkGB(S);
335            System.out.println("");
336        }
337    
338    
339        static void runClient(int port) {
340            System.out.println("\nGroebner base distributed client (" + port +  ") ...");
341    
342            ExecutableServer es = new ExecutableServer(port);
343            es.init();
344        }
345    
346    
347        @SuppressWarnings("unchecked")
348        static void runParallel(PolynomialList S, int threads, boolean pairseq) {
349            List L = S.list;
350            List G;
351            long t;
352            GroebnerBaseAbstract bb = null;
353            GroebnerBaseAbstract bbs = null;
354            if (pairseq) {
355                //bbs = new GroebnerBaseSeqPairParallel(threads);
356                bbs = new GroebnerBaseParallel(threads,new ReductionPar(),new OrderedSyzPairlist());
357            } else {
358                bb = new GroebnerBaseParallel(threads);
359            }
360            t = System.currentTimeMillis();
361            System.out.println("\nGroebner base parallel (" + threads + ") ...");
362    
363            if (pairseq) {
364                G = bbs.GB(L);
365            } else {
366                G = bb.GB(L);
367            }
368            t = System.currentTimeMillis() - t;
369            S = new PolynomialList(S.ring, G);
370            System.out.println("G =\n" + S);
371            System.out.println("G.size() = " + G.size());
372    
373            if (pairseq) {
374                System.out.print("p+ ");
375            } else {
376                System.out.print("p ");
377            }
378            System.out.println("= " + threads + ", time = " + t + " milliseconds");
379            if (pairseq) {
380                bbs.terminate();
381            } else {
382                bb.terminate();
383            }
384            checkGB(S);
385            System.out.println("");
386        }
387    
388    
389        @SuppressWarnings("unchecked")
390        static void runSequential(PolynomialList S, boolean pairseq) {
391            List L = S.list;
392            List G;
393            long t;
394            GroebnerBaseAbstract bb = null;
395            if (pairseq) {
396                //bb = new GroebnerBaseSeqPairSeq();
397                bb = new GroebnerBaseSeq(new ReductionSeq(),new OrderedSyzPairlist());
398            } else {
399                bb = new GroebnerBaseSeq();
400            }
401            t = System.currentTimeMillis();
402            System.out.println("\nGroebner base sequential ...");
403            G = bb.GB(L);
404            t = System.currentTimeMillis() - t;
405            S = new PolynomialList(S.ring, G);
406            System.out.println("G =\n" + S);
407            System.out.println("G.size() = " + G.size());
408            if (pairseq) {
409                System.out.print("seq+, ");
410            } else {
411                System.out.print("seq, ");
412            }
413            System.out.println("time = " + t + " milliseconds");
414            checkGB(S);
415            System.out.println("");
416        }
417    
418    
419        static void checkGB(PolynomialList S) {
420            if ( !doCheck ) {
421                return;
422            }
423            GroebnerBaseAbstract bb = GBFactory.getImplementation(S.ring.coFac);
424            long t = System.currentTimeMillis();
425            boolean chk = bb.isGB(S.list);
426            t = System.currentTimeMillis() - t;
427            System.out.println("check isGB = " + chk + " in " + t + " milliseconds");
428        }
429    
430    }