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