001/*
002 * $Id: RunMPJGB.java 5796 2018-03-26 09:24:00Z kredel $
003 */
004
005package edu.jas.application;
006
007
008import java.io.BufferedReader;
009import java.io.FileInputStream;
010import java.io.FileNotFoundException;
011import java.io.IOException;
012import java.io.InputStreamReader;
013import java.io.Reader;
014import java.io.StringReader;
015import java.nio.charset.Charset;
016import java.util.Arrays;
017import java.util.List;
018
019import org.apache.log4j.BasicConfigurator;
020
021import edu.jas.gb.GroebnerBaseAbstract;
022import edu.jas.gb.GroebnerBaseDistributedEC;
023import edu.jas.gb.GroebnerBaseDistributedHybridEC;
024import edu.jas.gb.GroebnerBaseDistributedHybridMPJ;
025import edu.jas.gb.GroebnerBaseDistributedMPJ;
026import edu.jas.gb.GroebnerBaseParallel;
027import edu.jas.gb.GroebnerBaseSeq;
028import edu.jas.gb.OrderedSyzPairlist;
029import edu.jas.gb.ReductionPar;
030import edu.jas.gb.ReductionSeq;
031import edu.jas.gbufd.GBFactory;
032import edu.jas.kern.ComputerThreads;
033import edu.jas.kern.MPJEngine;
034import edu.jas.poly.GenPolynomialRing;
035import edu.jas.poly.GenPolynomialTokenizer;
036import edu.jas.poly.PolynomialList;
037import edu.jas.util.CatReader;
038import edu.jas.util.ExecutableServer;
039
040
041/**
042 * Simple setup to run a GB example in MPJ environment. <br>
043 * Usage: RunGB [seq(+)|par(+)|dist(1)(+)|disthyb|cli] &lt;file&gt;
044 * #procs/#threadsPerNode [machinefile]
045 * @author Heinz Kredel
046 */
047
048public class RunMPJGB {
049
050
051    /**
052     * Check result GB if it is a GB.
053     */
054    static boolean doCheck = false;
055
056
057    /**
058     * Enable logging.
059     */
060    static boolean doLog = true;
061
062
063    /**
064     * main method to be called from commandline <br>
065     * Usage: RunMPJGB [seq|par(+)|dist(1)(+)|disthyb|cli] &lt;file&gt;
066     * #procs/#threadsPerNode [machinefile]
067     */
068    @SuppressWarnings("unchecked")
069    public static void main(String[] args) throws IOException {
070
071        MPJEngine.setCommandLine(args); //args = MPI.Init(args);
072
073        String[] allkinds = new String[] { "seq", "seq+", 
074                                           "par", "par+", 
075                                           "dist", "dist+", 
076                                           "disthyb", "disthyb+", 
077                                           "distmpj", "distmpj+", 
078                                           "disthybmpj", "disthybmpj+", 
079                                           "cli" };
080
081        String usage = "Usage: RunGB [ "
082                        + RunGB.join(allkinds, " | ") 
083                        + "[port] ] " 
084                        + "<file> " 
085                        + "#procs/#threadsPerNode " 
086                        + "[machinefile] " 
087                        + "[check] [nolog]";
088
089        if (args.length < 1) {
090            System.out.println("args: " + Arrays.toString(args));
091            System.out.println(usage);
092            return;
093        }
094
095        boolean plusextra = false;
096        String kind = args[0];
097        boolean sup = false;
098        int k = -1;
099        for (int i = 0; i < args.length; i++) {
100            int j = RunGB.indexOf(allkinds, args[i]);
101            if (j < 0) {
102                continue;
103            }
104            sup = true;
105            k = i;
106            kind = args[k];
107            break;
108        }
109        if (!sup) {
110            System.out.println("args(sup): " + Arrays.toString(args));
111            System.out.println(usage);
112            return;
113        }
114        if (kind.indexOf("+") >= 0) {
115            plusextra = true;
116        }
117        System.out.println("kind: " + kind + ", k = " + k);
118
119        final int GB_SERVER_PORT = 7114;
120        int port = GB_SERVER_PORT;
121
122        if (kind.equals("cli")) {
123            if (args.length - k >= 2) {
124                try {
125                    port = Integer.parseInt(args[k + 1]);
126                } catch (NumberFormatException e) {
127                    e.printStackTrace();
128                    System.out.println("args(port): " + Arrays.toString(args));
129                    System.out.println(usage);
130                    return;
131                }
132            }
133            RunGB.runClient(port);
134            return;
135        }
136
137        String filename = null;
138        if (!kind.equals("cli")) {
139            if (args.length - k < 2) {
140                System.out.println("args(file): " + Arrays.toString(args));
141                System.out.println(usage);
142                return;
143            }
144            filename = args[k + 1];
145        }
146
147        int j = RunGB.indexOf(args, "check");
148        if (j >= 0) {
149            doCheck = true;
150            RunGB.doCheck = true;
151        }
152        j = RunGB.indexOf(args, "nolog");
153        if (j >= 0) {
154            doLog = false;
155        }
156
157        int threads = 0;
158        int threadsPerNode = 1;
159        if (kind.startsWith("par") || kind.startsWith("dist")) {
160            if (args.length - k < 3) {
161                System.out.println("args(par|dist): " + Arrays.toString(args));
162                System.out.println(usage);
163                return;
164            }
165            String tup = args[k + 2];
166            String t = tup;
167            int i = tup.indexOf("/");
168            if (i >= 0) {
169                t = tup.substring(0, i).trim();
170                tup = tup.substring(i + 1).trim();
171                try {
172                    threadsPerNode = Integer.parseInt(tup);
173                } catch (NumberFormatException e) {
174                    e.printStackTrace();
175                    System.out.println("args(threadsPerNode): " + Arrays.toString(args));
176                    System.out.println(usage);
177                    return;
178                }
179            }
180            try {
181                threads = Integer.parseInt(t);
182            } catch (NumberFormatException e) {
183                e.printStackTrace();
184                System.out.println("args(threads): " + Arrays.toString(args));
185                System.out.println(usage);
186                return;
187            }
188        }
189
190        String mfile = null;
191        if (kind.startsWith("dist")) {
192            if (args.length - k >= 4) {
193                mfile = args[k + 3];
194            } else {
195                mfile = "machines";
196            }
197        }
198
199        Reader problem = RunGB.getReader(filename);
200        if (problem == null) {
201            System.out.println("args(file): " + filename);
202            System.out.println("args(file): examples.jar(" + filename + ")");
203            System.out.println("args(file): " + Arrays.toString(args));
204            System.out.println(usage);
205            return;
206        }
207        RingFactoryTokenizer rftok = new RingFactoryTokenizer(problem);
208        GenPolynomialRing pfac = null;
209        try {
210            pfac = rftok.nextPolynomialRing();
211            rftok = null;
212        } catch (IOException e) {
213            e.printStackTrace();
214            return;
215        }
216        Reader polyreader = new CatReader(new StringReader("("), problem); // ( has gone
217        //Reader polyreader = problem;
218        GenPolynomialTokenizer tok = new GenPolynomialTokenizer(pfac, polyreader);
219        PolynomialList S = null;
220        try {
221            S = new PolynomialList(pfac, tok.nextPolynomialList());
222        } catch (IOException e) {
223            e.printStackTrace();
224            return;
225        }
226        System.out.println("S =\n" + S);
227
228        if (doLog) {
229            BasicConfigurator.configure();
230        }
231
232        if (kind.startsWith("seq")) {
233            RunGB.runSequential(S, plusextra);
234        } else if (kind.startsWith("par")) {
235            RunGB.runParallel(S, threads, plusextra);
236        } else if (kind.startsWith("distmpj")) {
237            runMpj(S, threads, mfile, port, plusextra);
238        } else if (kind.startsWith("disthybmpj")) {
239            runHybridMpj(S, threads, threadsPerNode, mfile, port, plusextra);
240        } else if (kind.startsWith("disthyb")) {
241            RunGB.runMasterHyb(S, threads, threadsPerNode, mfile, port, plusextra);
242        } else if (kind.startsWith("dist")) {
243            RunGB.runMaster(S, threads, mfile, port, plusextra);
244        }
245        ComputerThreads.terminate();
246        //System.exit(0);
247    }
248
249
250    @SuppressWarnings("unchecked")
251    static void runMpj(PolynomialList S, int threads, String mfile, int port, boolean plusextra)
252                    throws IOException {
253        List L = S.list;
254        List G = null;
255        long t, t1;
256
257        t = System.currentTimeMillis();
258        System.out.println("\nGroebner base distributed MPJ (" + threads + ", " + mfile + ", " + port
259                        + ") ...");
260        GroebnerBaseDistributedMPJ gbd = null;
261        GroebnerBaseDistributedMPJ gbds = null;
262        if (plusextra) {
263            gbds = new GroebnerBaseDistributedMPJ(threads, new OrderedSyzPairlist());
264        } else {
265            gbd = new GroebnerBaseDistributedMPJ(threads);
266        }
267        t1 = System.currentTimeMillis();
268        if (plusextra) {
269            G = gbds.GB(L);
270        } else {
271            G = gbd.GB(L);
272        }
273        t1 = System.currentTimeMillis() - t1;
274        if (plusextra) {
275            gbds.terminate();
276        } else {
277            gbd.terminate();
278        }
279        MPJEngine.terminate();
280        if (G == null) {
281            return; // mpi.rank != 0
282        }
283        S = new PolynomialList(S.ring, G);
284        System.out.println("G =\n" + S);
285        System.out.println("G.size() = " + G.size());
286        t = System.currentTimeMillis() - t;
287        if (plusextra) {
288            System.out.print("m+ ");
289        } else {
290            System.out.print("m ");
291        }
292        System.out.println("= " + threads + ", time = " + t1 + " milliseconds, " + (t - t1) + " start-up "
293                        + ", total = " + t);
294        RunGB.checkGB(S);
295        System.out.println("");
296    }
297
298
299    @SuppressWarnings("unchecked")
300    static void runHybridMpj(PolynomialList S, int threads, int threadsPerNode, String mfile, int port,
301                    boolean plusextra) throws IOException {
302        List L = S.list;
303        List G = null;
304        long t, t1;
305
306        t = System.currentTimeMillis();
307        System.out.println("\nGroebner base distributed hybrid MPJ (" + threads + "/" + threadsPerNode + ", "
308                        + mfile + ", " + port + ") ...");
309        GroebnerBaseDistributedHybridMPJ gbd = null;
310        GroebnerBaseDistributedHybridMPJ gbds = null;
311        if (plusextra) {
312            gbds = new GroebnerBaseDistributedHybridMPJ(threads, threadsPerNode, new OrderedSyzPairlist());
313        } else {
314            gbd = new GroebnerBaseDistributedHybridMPJ(threads, threadsPerNode);
315        }
316        t1 = System.currentTimeMillis();
317        if (plusextra) {
318            G = gbds.GB(L);
319        } else {
320            G = gbd.GB(L);
321        }
322        t1 = System.currentTimeMillis() - t1;
323        if (plusextra) {
324            gbds.terminate();
325        } else {
326            gbd.terminate();
327        }
328        MPJEngine.terminate();
329        if (G == null) {
330            return; // mpi.rank != 0
331        }
332        S = new PolynomialList(S.ring, G);
333        System.out.println("G =\n" + S);
334        System.out.println("G.size() = " + G.size());
335        t = System.currentTimeMillis() - t;
336        if (plusextra) {
337            System.out.print("m+ ");
338        } else {
339            System.out.print("m ");
340        }
341        System.out.println("= " + threads + ", ppn = " + threadsPerNode + ", time = " + t1
342                        + " milliseconds, " + (t - t1) + " start-up " + ", total = " + t);
343        RunGB.checkGB(S);
344        System.out.println("");
345    }
346
347}