001/* 002 * $Id: GroebnerBaseDistTest.java 5048 2014-12-30 17:45:01Z 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.arith.BigRational; 023import edu.jas.kern.ComputerThreads; 024import edu.jas.poly.GenPolynomial; 025import edu.jas.poly.GenPolynomialRing; 026import edu.jas.poly.GenPolynomialTokenizer; 027import edu.jas.poly.PolynomialList; 028import edu.jas.structure.RingElem; 029 030 031/** 032 * Distributed GroebnerBase tests with JUnit. 033 * @author Heinz Kredel 034 */ 035 036public class GroebnerBaseDistTest extends TestCase { 037 038 039 //private static final Logger logger = Logger.getLogger(GroebnerBaseDistTest.class); 040 041 /** 042 * main 043 */ 044 public static void main(String[] args) { 045 BasicConfigurator.configure(); 046 junit.textui.TestRunner.run(suite()); 047 //ComputerThreads.terminate(); 048 } 049 050 051 /** 052 * Constructs a <CODE>GroebnerBaseDistTest</CODE> object. 053 * @param name String. 054 */ 055 public GroebnerBaseDistTest(String name) { 056 super(name); 057 } 058 059 060 /** 061 * suite. 062 */ 063 public static Test suite() { 064 TestSuite suite = new TestSuite(GroebnerBaseDistTest.class); 065 return suite; 066 } 067 068 069 int port = 4711; 070 071 072 String host = "localhost"; 073 074 075 GenPolynomialRing<BigRational> fac; 076 077 078 List<GenPolynomial<BigRational>> L; 079 080 081 PolynomialList<BigRational> F; 082 083 084 List<GenPolynomial<BigRational>> G; 085 086 087 GroebnerBase<BigRational> bbseq; 088 089 090 GroebnerBaseDistributed<BigRational> bbdist; 091 092 093 GroebnerBaseDistributed<BigRational> bbdists; 094 095 096 GenPolynomial<BigRational> a; 097 098 099 GenPolynomial<BigRational> b; 100 101 102 GenPolynomial<BigRational> c; 103 104 105 GenPolynomial<BigRational> d; 106 107 108 GenPolynomial<BigRational> e; 109 110 111 int rl = 3; //4; //3; 112 113 114 int kl = 4; 115 116 117 int ll = 7; 118 119 120 int el = 3; 121 122 123 float q = 0.2f; //0.4f 124 125 126 int threads = 2; 127 128 129 @Override 130 protected void setUp() { 131 BigRational coeff = new BigRational(9); 132 fac = new GenPolynomialRing<BigRational>(coeff, rl); 133 a = b = c = d = e = null; 134 bbseq = new GroebnerBaseSeq<BigRational>(); 135 bbdists = new GroebnerBaseDistributed<BigRational>(threads, port); 136 bbdist = new GroebnerBaseDistributed<BigRational>(threads, new OrderedSyzPairlist<BigRational>(), 137 port); 138 } 139 140 141 @Override 142 protected void tearDown() { 143 a = b = c = d = e = null; 144 fac = null; 145 bbseq = null; 146 bbdist.terminate(); 147 bbdist = null; 148 bbdists.terminate(); 149 bbdists = null; 150 ComputerThreads.terminate(); 151 } 152 153 154 /** 155 * Helper method to start threads with distributed clients. 156 * 157 */ 158 Thread[] startThreads() { 159 Thread[] clients = new Thread[threads]; 160 for (int t = 0; t < threads; t++) { 161 clients[t] = new Thread(new JunitClient(host, port)); 162 clients[t].start(); 163 } 164 return clients; 165 } 166 167 168 /** 169 * Helper method to stop threads with distributed clients. 170 * 171 */ 172 void stopThreads(Thread[] clients) { 173 for (int t = 0; t < threads; t++) { 174 try { 175 clients[t].join(); 176 } catch (InterruptedException e) { 177 } 178 } 179 } 180 181 182 /** 183 * Test distributed GBase. 184 * 185 */ 186 public void testDistributedGBase() { 187 Thread[] clients; 188 L = new ArrayList<GenPolynomial<BigRational>>(); 189 190 a = fac.random(kl, ll, el, q); 191 b = fac.random(kl, ll, el, q); 192 c = fac.random(kl, ll, el, q); 193 d = fac.random(kl, ll, el, q); 194 e = d; //fac.random(kl, ll, el, q ); 195 196 L.add(a); 197 clients = startThreads(); 198 L = bbdist.GB(L); 199 stopThreads(clients); 200 assertTrue("isGB( { a } )", bbseq.isGB(L)); 201 202 L.add(b); 203 //System.out.println("L = " + L.size() ); 204 clients = startThreads(); 205 L = bbdist.GB(L); 206 stopThreads(clients); 207 assertTrue("isGB( { a, b } )", bbseq.isGB(L)); 208 209 L.add(c); 210 clients = startThreads(); 211 L = bbdist.GB(L); 212 stopThreads(clients); 213 assertTrue("isGB( { a, b, c } )", bbseq.isGB(L)); 214 215 L.add(d); 216 clients = startThreads(); 217 L = bbdist.GB(L); 218 stopThreads(clients); 219 assertTrue("isGB( { a, b, c, d } )", bbseq.isGB(L)); 220 221 L.add(e); 222 clients = startThreads(); 223 L = bbdist.GB(L); 224 stopThreads(clients); 225 assertTrue("isGB( { a, b, c, d, e } )", bbseq.isGB(L)); 226 } 227 228 229 /** 230 * Test compare sequential with distributed GBase. 231 * 232 */ 233 public void testSequentialDistributedGBase() { 234 Thread[] clients; 235 List<GenPolynomial<BigRational>> Gs, Gp = null; 236 L = new ArrayList<GenPolynomial<BigRational>>(); 237 238 a = fac.random(kl, ll, el, q); 239 b = fac.random(kl, ll, el, q); 240 c = fac.random(kl, ll, el, q); 241 d = fac.random(kl, ll, el, q); 242 e = d; //fac.random(kl, ll, el, q ); 243 244 L.add(a); 245 Gs = bbseq.GB(L); 246 clients = startThreads(); 247 Gp = bbdist.GB(L); 248 stopThreads(clients); 249 250 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 251 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 252 253 L = Gs; 254 L.add(b); 255 Gs = bbseq.GB(L); 256 clients = startThreads(); 257 Gp = bbdist.GB(L); 258 stopThreads(clients); 259 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 260 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 261 262 L = Gs; 263 L.add(c); 264 Gs = bbseq.GB(L); 265 clients = startThreads(); 266 Gp = bbdist.GB(L); 267 stopThreads(clients); 268 269 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 270 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 271 272 L = Gs; 273 L.add(d); 274 Gs = bbseq.GB(L); 275 clients = startThreads(); 276 Gp = bbdist.GB(L); 277 stopThreads(clients); 278 279 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 280 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 281 282 L = Gs; 283 L.add(e); 284 Gs = bbseq.GB(L); 285 clients = startThreads(); 286 Gp = bbdist.GB(L); 287 stopThreads(clients); 288 289 assertTrue("Gs.containsAll(Gp)" + Gs + ", " + Gp + ", " + L, Gs.containsAll(Gp)); 290 assertTrue("Gp.containsAll(Gs)" + Gs + ", " + Gp + ", " + L, Gp.containsAll(Gs)); 291 } 292 293 294 /** 295 * Test Trinks7 GBase. 296 * 297 */ 298 @SuppressWarnings("cast") 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}