001/* 002 * $Id: RunSGB.java 5861 2018-07-20 10:09:03Z kredel $ 003 */ 004 005package edu.jas.application; 006 007 008import java.io.FileNotFoundException; 009import java.io.FileReader; 010import java.io.StringReader; 011import java.io.IOException; 012import java.io.FileInputStream; 013import java.io.InputStreamReader; 014import java.io.Reader; 015import java.io.BufferedReader; 016import java.nio.charset.Charset; 017import java.util.List; 018import java.util.Arrays; 019 020import edu.jas.gb.SolvableGroebnerBase; 021import edu.jas.gb.SolvableGroebnerBaseAbstract; 022import edu.jas.gb.SolvableGroebnerBaseParallel; 023import edu.jas.gb.SolvableGroebnerBaseSeq; 024import edu.jas.gb.SolvableGroebnerBaseSeqPairParallel; 025import edu.jas.gb.SolvableReduction; 026import edu.jas.gb.SolvableReductionPar; 027import edu.jas.gb.SolvableReductionSeq; 028import edu.jas.kern.ComputerThreads; 029import edu.jas.poly.GenPolynomialTokenizer; 030import edu.jas.poly.GenSolvablePolynomial; 031import edu.jas.poly.GenSolvablePolynomialRing; 032import edu.jas.poly.PolynomialList; 033import edu.jas.util.CatReader; 034 035 036/** 037 * Simple setup to run a solvable GB example. <br> Usage: RunSGB 038 * [seq|par|par+] [irr|left|right|two] <file> #procs 039 * @author Heinz Kredel 040 */ 041public class RunSGB { 042 043 /** 044 * Check result GB if it is a GB. 045 */ 046 static boolean doCheck = false; 047 048 049 /** 050 * main method to be called from commandline <br> Usage: RunSGB 051 * [seq|seq+|par|par+] [irr|left|right|two] <file> #procs 052 */ 053 @SuppressWarnings("unchecked") 054 public static void main(String[] args) { 055 056 String[] allkinds = new String[] { "seq", "seq+", 057 "par", "par+", 058 //"dist", "dist+", , 059 //"disthyb", "disthyb+", 060 //"cli" 061 }; // must be last 062 String[] allmeth = new String[] { "irr", "left", "right", "two" }; 063 064 String usage = "Usage: RunGB [ " 065 + join(allkinds, " | ") 066 //+ "[port] ] " 067 + " ] [" 068 + join(allmeth, " | ") 069 + "] <file> " 070 + "#threads " 071 //+ "#procs/#threadsPerNode " 072 //+ "[machinefile] "; 073 + "[check] "; 074 075 if (args.length < 3) { 076 System.out.println("args: " + Arrays.toString(args)); 077 System.out.println(usage); 078 return; 079 } 080 081 boolean plusextra = false; 082 String kind = args[0]; 083 boolean sup = false; 084 int k = -1; 085 for (int i = 0; i < args.length; i++) { 086 int j = indexOf(allkinds, args[i]); 087 if (j < 0) { 088 continue; 089 } 090 sup = true; 091 k = i; 092 kind = args[k]; 093 break; 094 } 095 if (!sup) { 096 System.out.println("args(sup): " + Arrays.toString(args)); 097 System.out.println(usage); 098 return; 099 } 100 if (kind.indexOf("+") >= 0) { 101 plusextra = true; 102 } 103 System.out.println("kind: " + kind + ", k = " + k); 104 105 String action = args[k + 1]; 106 sup = false; 107 int j = indexOf(allmeth, action); 108 if (j < 0) { 109 System.out.println(usage); 110 return; 111 } 112 113 String filename = args[k + 2]; 114 115 int threads = 0; 116 if (kind.startsWith("par")) { 117 if (args.length < 4) { 118 System.out.println("args(par): " + Arrays.toString(args)); 119 System.out.println(usage); 120 return; 121 } 122 String tup = args[k + 3]; 123 String t = tup; 124 try { 125 threads = Integer.parseInt(t); 126 } catch (NumberFormatException e) { 127 e.printStackTrace(); 128 System.out.println("args(threads): " + Arrays.toString(args)); 129 System.out.println(usage); 130 return; 131 } 132 if (threads < 1) { 133 threads = 1; 134 } 135 } 136 j = indexOf(args, "check"); 137 if (j >= 0) { 138 doCheck = true; 139 } 140 141 Reader problem = RunGB.getReader(filename); 142 if (problem == null) { 143 System.out.println("args(file): " + filename); 144 System.out.println("args(file): examples.jar(" + filename + ")"); 145 System.out.println("args(file): " + Arrays.toString(args)); 146 System.out.println(usage); 147 return; 148 } 149 RingFactoryTokenizer rftok = new RingFactoryTokenizer(problem); 150 GenSolvablePolynomialRing spfac = null; 151 try { 152 spfac = rftok.nextSolvablePolynomialRing(); 153 rftok = null; 154 } catch (IOException e) { 155 e.printStackTrace(); 156 return; 157 } 158 Reader polyreader = new CatReader(new StringReader("("),problem); // ( has gone 159 //Reader polyreader = problem; 160 GenPolynomialTokenizer tok = new GenPolynomialTokenizer(spfac,polyreader); 161 PolynomialList S = null; 162 try { 163 S = new PolynomialList(spfac,tok.nextSolvablePolynomialList()); 164 } catch (IOException e) { 165 e.printStackTrace(); 166 return; 167 } 168 System.out.println("S =\n" + S); 169 170 if (kind.startsWith("seq")) { 171 runSequential(S, action, plusextra); 172 } else if (kind.startsWith("par")) { 173 runParallel(S, threads, action, plusextra); 174 } 175 ComputerThreads.terminate(); 176 } 177 178 179 /** 180 * run Sequential. 181 * @param S polynomial list. 182 * @param action what to to. 183 */ 184 @SuppressWarnings("unchecked") 185 static void runSequential(PolynomialList S, String action, boolean plusextra) { 186 List<GenSolvablePolynomial> L = S.list; 187 List<GenSolvablePolynomial> G = null; 188 long t; 189 SolvableReduction sred = new SolvableReductionSeq(); 190 SolvableGroebnerBase sbb = null; 191 if (plusextra) { 192 //sbb = new SolvableGroebnerBaseSeqPlusextra(); 193 //System.out.println("SolvableGroebnerBaseSeqPlusextra not implemented using SolvableGroebnerBaseSeq"); 194 sbb = new SolvableGroebnerBaseSeq(sred); 195 } else { 196 sbb = new SolvableGroebnerBaseSeq(); 197 } 198 t = System.currentTimeMillis(); 199 System.out.println("\nSolvable GB [" + action + "] sequential ..."); 200 if (action.equals("irr")) { 201 G = sred.leftIrreducibleSet(L); 202 } 203 if (action.equals("left")) { 204 G = sbb.leftGB(L); 205 } 206 if (action.equals("right")) { 207 G = sbb.rightGB(L); 208 } 209 if (action.equals("two")) { 210 G = sbb.twosidedGB(L); 211 } 212 if (G == null) { 213 System.out.println("unknown action = " + action + "\n"); 214 return; 215 } 216 S = new PolynomialList(S.ring, G); 217 System.out.println("G =\n" + S); 218 System.out.println("G.size() = " + G.size()); 219 t = System.currentTimeMillis() - t; 220 if (plusextra) { 221 System.out.print("seq+, "); 222 } else { 223 System.out.print("seq, "); 224 } 225 System.out.println("time = " + t + " milliseconds"); 226 checkGB(S); 227 System.out.println(""); 228 } 229 230 231 /** 232 * run Parallel. 233 * @param S polynomial list. 234 * @param action what to to. 235 */ 236 @SuppressWarnings("unchecked") 237 static void runParallel(PolynomialList S, int threads, String action, boolean plusextra) { 238 List<GenSolvablePolynomial> L = S.list; 239 List<GenSolvablePolynomial> G = null; 240 long t; 241 SolvableReduction sred = new SolvableReductionPar(); 242 SolvableGroebnerBaseParallel sbb = null; 243 SolvableGroebnerBaseSeqPairParallel sbbs = null; 244 if (plusextra) { 245 sbbs = new SolvableGroebnerBaseSeqPairParallel(threads); 246 } else { 247 sbb = new SolvableGroebnerBaseParallel(threads); 248 } 249 250 t = System.currentTimeMillis(); 251 System.out.println("\nSolvable GB [" + action + "] parallel " + threads + " threads ..."); 252 if (action.equals("irr")) { 253 G = sred.leftIrreducibleSet(L); 254 } 255 if (action.equals("left")) { 256 if (plusextra) { 257 G = sbbs.leftGB(L); 258 } else { 259 G = sbb.leftGB(L); 260 } 261 } 262 if (action.equals("right")) { 263 if (plusextra) { 264 G = sbbs.rightGB(L); 265 } else { 266 G = sbb.rightGB(L); 267 } 268 } 269 if (action.equals("two")) { 270 if (plusextra) { 271 G = sbbs.twosidedGB(L); 272 } else { 273 G = sbb.twosidedGB(L); 274 } 275 } 276 if (G == null) { 277 System.out.println("unknown action = " + action + "\n"); 278 return; 279 } 280 if (G.size() > 0) { 281 S = new PolynomialList(G.get(0).ring, G); 282 } else { 283 S = new PolynomialList(S.ring, G); 284 } 285 System.out.println("G =\n" + S); 286 System.out.println("G.size() = " + G.size()); 287 t = System.currentTimeMillis() - t; 288 if (plusextra) { 289 System.out.print("p+ "); 290 } else { 291 System.out.print("p "); 292 } 293 System.out.println("= " + threads + ", time = " + t + " milliseconds"); 294 checkGB(S); 295 System.out.println(""); 296 if (plusextra) { 297 sbbs.terminate(); 298 } else { 299 sbb.terminate(); 300 } 301 } 302 303 304 @SuppressWarnings("unchecked") 305 static void checkGB(PolynomialList S) { 306 if (!doCheck) { 307 return; 308 } 309 SolvableGroebnerBaseAbstract sbb = new SolvableGroebnerBaseSeq(); 310 long t = System.currentTimeMillis(); 311 boolean chk = sbb.isLeftGB(S.list,false); 312 t = System.currentTimeMillis() - t; 313 System.out.println("check isGB = " + chk + " in " + t + " milliseconds"); 314 } 315 316 317 static int indexOf(String[] args, String s) { 318 for (int i = 0; i < args.length; i++) { 319 if (s.equals(args[i])) { 320 return i; 321 } 322 } 323 return -1; 324 } 325 326 327 static String join(String[] args, String d) { 328 StringBuffer sb = new StringBuffer(); 329 for (int i = 0; i < args.length; i++) { 330 if (i > 0) { 331 sb.append(d); 332 } 333 sb.append(args[i]); 334 } 335 return sb.toString(); 336 } 337 338}