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