001/* 002 * $Id: RunSGB.java 4227 2012-09-30 11:20:24Z 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; 018 019import org.apache.log4j.BasicConfigurator; 020 021import edu.jas.gb.SolvableGroebnerBase; 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.poly.GenPolynomialTokenizer; 029import edu.jas.poly.GenSolvablePolynomial; 030import edu.jas.poly.GenSolvablePolynomialRing; 031import edu.jas.poly.PolynomialList; 032import edu.jas.util.CatReader; 033 034 035/** 036 * Simple setup to run a solvable GB example. <br /> Usage: RunSGB 037 * [seq|par|par+] [irr|left|right|two] <file> #procs 038 * @author Heinz Kredel 039 */ 040 041public class RunSGB { 042 043 044 /** 045 * main method to be called from commandline <br /> Usage: RunSGB 046 * [seq|par|par+] [irr|left|right|two] <file> #procs 047 */ 048 @SuppressWarnings("unchecked") 049 public static void main(java.lang.String[] args) { 050 051 BasicConfigurator.configure(); 052 053 String usage = "Usage: RunSGB " + "[ seq | par | par+ ] " 054 // + "[ seq | par | dist | cli [port] ] " 055 + "[ irr | left | right | two ] " + "<file> " + "#procs "; 056 // + "[machinefile]"; 057 if (args.length < 3) { 058 System.out.println(usage); 059 return; 060 } 061 062 boolean pairseq = false; 063 String kind = args[0]; 064 String[] allkinds = new String[] { "seq", "par", "par+" }; 065 // String[] allkinds = new String[] { "seq", "par", "dist", "cli" }; 066 boolean sup = false; 067 for (int i = 0; i < allkinds.length; i++) { 068 if (kind.equals(allkinds[i])) { 069 sup = true; 070 if (kind.indexOf("+") >= 0) { 071 pairseq = true; 072 } 073 } 074 } 075 if (!sup) { 076 System.out.println(usage); 077 return; 078 } 079 String[] allmeth = new String[] { "irr", "left", "right", "two" }; 080 String action = args[1]; 081 sup = false; 082 for (int i = 0; i < allmeth.length; i++) { 083 if (action.equals(allmeth[i])) { 084 sup = true; 085 } 086 } 087 if (!sup) { 088 System.out.println(usage); 089 return; 090 } 091 092 String filename = args[2]; 093 094 int threads = 0; 095 if (kind.startsWith("par")) { 096 if (args.length < 4) { 097 System.out.println(usage); 098 return; 099 } 100 String tup = args[3]; 101 String t = tup; 102 try { 103 threads = Integer.parseInt(t); 104 } catch (NumberFormatException e) { 105 e.printStackTrace(); 106 System.out.println(usage); 107 return; 108 } 109 if (threads < 1) { 110 threads = 1; 111 } 112 } 113 114 Reader problem = null; 115 try { 116 problem = new InputStreamReader(new FileInputStream(filename),Charset.forName("UTF8")); 117 problem = new BufferedReader(problem); 118 } catch (FileNotFoundException e) { 119 e.printStackTrace(); 120 System.out.println(usage); 121 return; 122 } 123 124 RingFactoryTokenizer rftok = new RingFactoryTokenizer(problem); 125 GenSolvablePolynomialRing spfac = null; 126 try { 127 spfac = rftok.nextSolvablePolynomialRing(); 128 rftok = null; 129 } catch (IOException e) { 130 e.printStackTrace(); 131 return; 132 } 133 Reader polyreader = new CatReader(new StringReader("("),problem); // ( has gone 134 GenPolynomialTokenizer tok = new GenPolynomialTokenizer(spfac,polyreader); 135 PolynomialList S = null; 136 try { 137 S = new PolynomialList(spfac,tok.nextSolvablePolynomialList()); 138 } catch (IOException e) { 139 e.printStackTrace(); 140 return; 141 } 142 System.out.println("S =\n" + S); 143 144 if (kind.startsWith("seq")) { 145 runSequential(S, action, pairseq); 146 } 147 148 if (kind.startsWith("par")) { 149 runParallel(S, threads, action, pairseq); 150 } 151 } 152 153 154 /** 155 * run Sequential. 156 * @param S polynomial list. 157 * @param action what to to. 158 */ 159 @SuppressWarnings("unchecked") 160 static void runSequential(PolynomialList S, String action, boolean pairseq) { 161 List<GenSolvablePolynomial> L = S.list; 162 List<GenSolvablePolynomial> G = null; 163 long t; 164 SolvableReduction sred = new SolvableReductionSeq(); 165 SolvableGroebnerBase sbb = null; 166 if (pairseq) { 167 //sbb = new SolvableGroebnerBaseSeqPairSeq(); 168 System.out 169 .println("SolvableGroebnerBaseSeqPairSeq not implemented using SolvableGroebnerBaseSeq"); 170 sbb = new SolvableGroebnerBaseSeq(); 171 } else { 172 sbb = new SolvableGroebnerBaseSeq(); 173 } 174 t = System.currentTimeMillis(); 175 System.out.println("\nSolvable GB [" + action + "] sequential ..."); 176 if (action.equals("irr")) { 177 G = sred.leftIrreducibleSet(L); 178 } 179 if (action.equals("left")) { 180 G = sbb.leftGB(L); 181 } 182 if (action.equals("right")) { 183 G = sbb.rightGB(L); 184 } 185 if (action.equals("two")) { 186 G = sbb.twosidedGB(L); 187 } 188 if (G == null) { 189 System.out.println("unknown action = " + action + "\n"); 190 return; 191 } 192 S = new PolynomialList(S.ring, G); 193 System.out.println("G =\n" + S); 194 System.out.println("G.size() = " + G.size()); 195 t = System.currentTimeMillis() - t; 196 if (pairseq) { 197 System.out.print("seq+, "); 198 } else { 199 System.out.print("seq, "); 200 } 201 System.out.println("time = " + t + " milliseconds"); 202 System.out.println(""); 203 } 204 205 206 /** 207 * run Parallel. 208 * @param S polynomial list. 209 * @param action what to to. 210 */ 211 @SuppressWarnings("unchecked") 212 static void runParallel(PolynomialList S, int threads, String action, boolean pairseq) { 213 List<GenSolvablePolynomial> L = S.list; 214 List<GenSolvablePolynomial> G = null; 215 long t; 216 SolvableReduction sred = new SolvableReductionPar(); 217 SolvableGroebnerBaseParallel sbb = null; 218 SolvableGroebnerBaseSeqPairParallel sbbs = null; 219 if (pairseq) { 220 sbbs = new SolvableGroebnerBaseSeqPairParallel(threads); 221 } else { 222 sbb = new SolvableGroebnerBaseParallel(threads); 223 } 224 225 t = System.currentTimeMillis(); 226 System.out.println("\nSolvable GB [" + action + "] parallel " + threads + " threads ..."); 227 if (action.equals("irr")) { 228 G = sred.leftIrreducibleSet(L); 229 } 230 if (action.equals("left")) { 231 if (pairseq) { 232 G = sbbs.leftGB(L); 233 } else { 234 G = sbb.leftGB(L); 235 } 236 } 237 if (action.equals("right")) { 238 if (pairseq) { 239 G = sbbs.rightGB(L); 240 } else { 241 G = sbb.rightGB(L); 242 } 243 } 244 if (action.equals("two")) { 245 if (pairseq) { 246 G = sbbs.twosidedGB(L); 247 } else { 248 G = sbb.twosidedGB(L); 249 } 250 } 251 if (G == null) { 252 System.out.println("unknown action = " + action + "\n"); 253 return; 254 } 255 if (G.size() > 0) { 256 S = new PolynomialList(G.get(0).ring, G); 257 } else { 258 S = new PolynomialList(S.ring, G); 259 } 260 System.out.println("G =\n" + S); 261 System.out.println("G.size() = " + G.size()); 262 t = System.currentTimeMillis() - t; 263 if (pairseq) { 264 System.out.print("p+ "); 265 } else { 266 System.out.print("p "); 267 } 268 System.out.println("= " + threads + ", time = " + t + " milliseconds"); 269 System.out.println(""); 270 if (pairseq) { 271 sbbs.terminate(); 272 } else { 273 sbb.terminate(); 274 } 275 } 276 277}