001/* 002 * $Id: GroebnerBaseDistTest.java 4230 2012-10-03 17:46:52Z kredel $ 003 */ 004 005package edu.jas.gb; 006 007 008//import edu.jas.poly.GroebnerBase; 009 010import java.io.IOException; 011import java.io.Reader; 012import java.io.StringReader; 013import java.util.ArrayList; 014import java.util.List; 015 016import junit.framework.Test; 017import junit.framework.TestCase; 018import junit.framework.TestSuite; 019 020import org.apache.log4j.BasicConfigurator; //import org.apache.log4j.Logger; 021 022import edu.jas.kern.ComputerThreads; 023import edu.jas.arith.BigRational; 024 025import edu.jas.poly.GenPolynomial; 026import edu.jas.poly.GenPolynomialRing; 027import edu.jas.poly.GenPolynomialTokenizer; 028import edu.jas.poly.PolynomialList; 029 030import edu.jas.structure.RingElem; 031 032 033/** 034 * Distributed GroebnerBase tests with JUnit. 035 * @author Heinz Kredel 036 */ 037 038public class GroebnerBaseDistTest extends TestCase { 039 040 041 //private static final Logger logger = Logger.getLogger(GroebnerBaseDistTest.class); 042 043 /** 044 * main 045 */ 046 public static void main(String[] args) { 047 BasicConfigurator.configure(); 048 junit.textui.TestRunner.run(suite()); 049 //ComputerThreads.terminate(); 050 } 051 052 053 /** 054 * Constructs a <CODE>GroebnerBaseDistTest</CODE> object. 055 * @param name String. 056 */ 057 public GroebnerBaseDistTest(String name) { 058 super(name); 059 } 060 061 062 /** 063 * suite. 064 */ 065 public static Test suite() { 066 TestSuite suite = new TestSuite(GroebnerBaseDistTest.class); 067 return suite; 068 } 069 070 071 int port = 4711; 072 073 074 String host = "localhost"; 075 076 077 GenPolynomialRing<BigRational> fac; 078 079 080 List<GenPolynomial<BigRational>> L; 081 082 083 PolynomialList<BigRational> F; 084 085 086 List<GenPolynomial<BigRational>> G; 087 088 089 GroebnerBase<BigRational> bbseq; 090 091 092 GroebnerBaseDistributed<BigRational> bbdist; 093 094 095 GroebnerBaseDistributed<BigRational> bbdists; 096 097 098 GenPolynomial<BigRational> a; 099 100 101 GenPolynomial<BigRational> b; 102 103 104 GenPolynomial<BigRational> c; 105 106 107 GenPolynomial<BigRational> d; 108 109 110 GenPolynomial<BigRational> e; 111 112 113 int rl = 3; //4; //3; 114 115 116 int kl = 4; 117 118 119 int ll = 7; 120 121 122 int el = 3; 123 124 125 float q = 0.2f; //0.4f 126 127 128 int threads = 2; 129 130 131 @Override 132 protected void setUp() { 133 BigRational coeff = new BigRational(9); 134 fac = new GenPolynomialRing<BigRational>(coeff, rl); 135 a = b = c = d = e = null; 136 bbseq = new GroebnerBaseSeq<BigRational>(); 137 bbdists = new GroebnerBaseDistributed<BigRational>(threads, port); 138 bbdist = new GroebnerBaseDistributed<BigRational>(threads, new OrderedSyzPairlist<BigRational>(), port); 139 } 140 141 142 @Override 143 protected void tearDown() { 144 a = b = c = d = e = null; 145 fac = null; 146 bbseq = null; 147 bbdist.terminate(); 148 bbdist = null; 149 bbdists.terminate(); 150 bbdists = null; 151 ComputerThreads.terminate(); 152 } 153 154 155 /** 156 * Helper method to start threads with distributed clients. 157 * 158 */ 159 Thread[] startThreads() { 160 Thread[] clients = new Thread[threads]; 161 for (int t = 0; t < threads; t++) { 162 clients[t] = new Thread(new JunitClient(host, port)); 163 clients[t].start(); 164 } 165 return clients; 166 } 167 168 169 /** 170 * Helper method to stop threads with distributed clients. 171 * 172 */ 173 void stopThreads(Thread[] clients) { 174 for (int t = 0; t < threads; t++) { 175 try { 176 clients[t].join(); 177 } catch (InterruptedException e) { 178 } 179 } 180 } 181 182 183 /** 184 * Test distributed GBase. 185 * 186 */ 187 public void testDistributedGBase() { 188 Thread[] clients; 189 L = new ArrayList<GenPolynomial<BigRational>>(); 190 191 a = fac.random(kl, ll, el, q); 192 b = fac.random(kl, ll, el, q); 193 c = fac.random(kl, ll, el, q); 194 d = fac.random(kl, ll, el, q); 195 e = d; //fac.random(kl, ll, el, q ); 196 197 L.add(a); 198 clients = startThreads(); 199 L = bbdist.GB(L); 200 stopThreads(clients); 201 assertTrue("isGB( { a } )", bbseq.isGB(L)); 202 203 L.add(b); 204 //System.out.println("L = " + L.size() ); 205 clients = startThreads(); 206 L = bbdist.GB(L); 207 stopThreads(clients); 208 assertTrue("isGB( { a, b } )", bbseq.isGB(L)); 209 210 L.add(c); 211 clients = startThreads(); 212 L = bbdist.GB(L); 213 stopThreads(clients); 214 assertTrue("isGB( { a, b, c } )", bbseq.isGB(L)); 215 216 L.add(d); 217 clients = startThreads(); 218 L = bbdist.GB(L); 219 stopThreads(clients); 220 assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L)); 221 222 L.add(e); 223 clients = startThreads(); 224 L = bbdist.GB(L); 225 stopThreads(clients); 226 assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L)); 227 } 228 229 230 /** 231 * Test compare sequential with distributed GBase. 232 * 233 */ 234 public void testSequentialDistributedGBase() { 235 Thread[] clients; 236 List<GenPolynomial<BigRational>> Gs, Gp = null; 237 L = new ArrayList<GenPolynomial<BigRational>>(); 238 239 a = fac.random(kl, ll, el, q); 240 b = fac.random(kl, ll, el, q); 241 c = fac.random(kl, ll, el, q); 242 d = fac.random(kl, ll, el, q); 243 e = d; //fac.random(kl, ll, el, q ); 244 245 L.add(a); 246 Gs = bbseq.GB(L); 247 clients = startThreads(); 248 Gp = bbdist.GB(L); 249 stopThreads(clients); 250 251 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 252 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 253 254 L = Gs; 255 L.add(b); 256 Gs = bbseq.GB(L); 257 clients = startThreads(); 258 Gp = bbdist.GB(L); 259 stopThreads(clients); 260 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 261 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 262 263 L = Gs; 264 L.add(c); 265 Gs = bbseq.GB(L); 266 clients = startThreads(); 267 Gp = bbdist.GB(L); 268 stopThreads(clients); 269 270 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 271 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 272 273 L = Gs; 274 L.add(d); 275 Gs = bbseq.GB(L); 276 clients = startThreads(); 277 Gp = bbdist.GB(L); 278 stopThreads(clients); 279 280 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 281 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 282 283 L = Gs; 284 L.add(e); 285 Gs = bbseq.GB(L); 286 clients = startThreads(); 287 Gp = bbdist.GB(L); 288 stopThreads(clients); 289 290 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 291 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 292 } 293 294 295 /** 296 * Test Trinks7 GBase. 297 * 298 */ 299 public void testTrinks7GBase() { 300 Thread[] clients; 301 String exam = "(B,S,T,Z,P,W) L " + "( " + "( 45 P + 35 S - 165 B - 36 ), " 302 + "( 35 P + 40 Z + 25 T - 27 S ), " + "( 15 W + 25 S P + 30 Z - 18 T - 165 B**2 ), " 303 + "( - 9 W + 15 T P + 20 S Z ), " + "( P W + 2 T Z - 11 B**3 ), " 304 + "( 99 W - 11 B S + 3 B**2 ), " + "( B**2 + 33/50 B + 2673/10000 ) " + ") "; 305 Reader source = new StringReader(exam); 306 GenPolynomialTokenizer parser = new GenPolynomialTokenizer(source); 307 try { 308 F = (PolynomialList<BigRational>) parser.nextPolynomialSet(); 309 } catch (IOException e) { 310 fail("" + e); 311 } 312 //System.out.println("F = " + F); 313 314 clients = startThreads(); 315 G = bbdist.GB(F.list); 316 stopThreads(clients); 317 318 assertTrue("isGB( GB(Trinks7) )", bbseq.isGB(G)); 319 assertEquals("#GB(Trinks7) == 6", 6, G.size()); 320 //PolynomialList<BigRational> trinks = new PolynomialList<BigRational>(F.ring, G); 321 //System.out.println("G = " + trinks); 322 323 } 324 325} 326 327 328/** 329 * Unit Test client to be executed by test threads. 330 */ 331 332class JunitClient<C extends RingElem<C>> implements Runnable { 333 334 335 private final String host; 336 337 338 private final int port; 339 340 341 JunitClient(String host, int port) { 342 this.host = host; 343 this.port = port; 344 } 345 346 347 public void run() { 348 GroebnerBaseDistributed<C> bbd; 349 bbd = new GroebnerBaseDistributed<C>(1, null, null, port); 350 try { 351 bbd.clientPart(host); 352 } catch (IOException ignored) { 353 } 354 bbd.terminate(); 355 } 356}