
edu.jas.gb.SolvableGroebnerBaseSeqPairParallel Maven / Gradle / Ivy
The newest version!
/*
* $Id: SolvableGroebnerBaseSeqPairParallel.java 3990 2012-07-14 12:46:08Z
* kredel $
*/
package edu.jas.gb;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.Semaphore;
import org.apache.log4j.Logger;
import edu.jas.poly.ExpVector;
import edu.jas.poly.GenSolvablePolynomial;
import edu.jas.poly.GenSolvablePolynomialRing;
import edu.jas.structure.RingElem;
import edu.jas.util.Terminator;
import edu.jas.util.ThreadPool;
/**
* Solvable Groebner Base parallel algorithm. Makes some effort to produce the
* same sequence of critical pairs as in the sequential version. However already
* reduced pairs are not rereduced if new polynomials appear. Implements a
* shared memory parallel version of Groebner bases. Threads maintain pairlist.
* @param coefficient type
* @author Heinz Kredel
*/
public class SolvableGroebnerBaseSeqPairParallel> extends
SolvableGroebnerBaseAbstract {
private static final Logger logger = Logger.getLogger(SolvableGroebnerBaseSeqPairParallel.class);
//private static final boolean debug = logger.isDebugEnabled();
/**
* Number of threads to use.
*/
protected final int threads;
/**
* Pool of threads to use.
*/
protected transient final ThreadPool pool;
/**
* Constructor.
*/
public SolvableGroebnerBaseSeqPairParallel() {
this(2);
}
/**
* Constructor.
* @param threads number of threads to use.
*/
public SolvableGroebnerBaseSeqPairParallel(int threads) {
this(threads, new ThreadPool(threads));
}
/**
* Constructor.
* @param threads number of threads to use.
* @param pool ThreadPool to use.
*/
public SolvableGroebnerBaseSeqPairParallel(int threads, ThreadPool pool) {
this(threads, pool, new SolvableReductionPar());
}
/**
* Constructor.
* @param threads number of threads to use.
* @param red parallelism aware reduction engine
*/
public SolvableGroebnerBaseSeqPairParallel(int threads, SolvableReduction red) {
this(threads, new ThreadPool(threads), red);
}
/**
* Constructor.
* @param threads number of threads to use.
* @param pool ThreadPool to use.
* @param sred parallelism aware reduction engine
*/
public SolvableGroebnerBaseSeqPairParallel(int threads, ThreadPool pool, SolvableReduction sred) {
super(sred);
if (!(sred instanceof SolvableReductionPar)) {
logger.warn("parallel GB should use parallel aware reduction");
}
if (threads < 1) {
threads = 1;
}
this.threads = threads;
this.pool = pool;
}
/**
* Cleanup and terminate ThreadPool.
*/
public void terminate() {
if (pool == null) {
return;
}
pool.terminate();
}
/**
* Parallel Groebner base using sequential pair order class. Threads
* maintain pairlist.
* @param modv number of module variables.
* @param F polynomial list.
* @return GB(F) a Groebner base of F.
*/
public List> leftGB(int modv, List> F) {
GenSolvablePolynomial p;
List> G = new ArrayList>();
CriticalPairList pairlist = null;
int l = F.size();
ListIterator> it = F.listIterator();
while (it.hasNext()) {
p = it.next();
if (p.length() > 0) {
p = (GenSolvablePolynomial) p.monic();
if (p.isONE()) {
G.clear();
G.add(p);
return G; // since no threads activated jet
}
G.add(p);
if (pairlist == null) {
pairlist = new CriticalPairList(modv, p.ring);
if (!p.ring.coFac.isField()) {
throw new IllegalArgumentException("coefficients not from a field");
}
}
// putOne not required
pairlist.put(p);
} else {
l--;
}
}
if (l <= 1) {
return G; // since no threads activated jet
}
Terminator fin = new Terminator(threads);
LeftSolvableReducerSeqPair R;
for (int i = 0; i < threads; i++) {
R = new LeftSolvableReducerSeqPair(fin, G, pairlist);
pool.addJob(R);
}
fin.waitDone();
logger.debug("#parallel list = " + G.size());
G = leftMinimalGB(G);
// not in this context // pool.terminate();
logger.info("" + pairlist);
return G;
}
/**
* Minimal ordered groebner basis, parallel.
* @param Fp a Groebner base.
* @return minimalGB(F) a minimal Groebner base of Fp.
*/
@Override
public List> leftMinimalGB(List> Fp) {
GenSolvablePolynomial a;
ArrayList> G;
G = new ArrayList>(Fp.size());
ListIterator> it = Fp.listIterator();
while (it.hasNext()) {
a = it.next();
if (a.length() != 0) { // always true
// already monic a = a.monic();
G.add(a);
}
}
if (G.size() <= 1) {
return G;
}
ExpVector e;
ExpVector f;
GenSolvablePolynomial p;
ArrayList> F;
F = new ArrayList>(G.size());
boolean mt;
while (G.size() > 0) {
a = G.remove(0);
e = a.leadingExpVector();
it = G.listIterator();
mt = false;
while (it.hasNext() && !mt) {
p = it.next();
f = p.leadingExpVector();
mt = e.multipleOf(f);
}
it = F.listIterator();
while (it.hasNext() && !mt) {
p = it.next();
f = p.leadingExpVector();
mt = e.multipleOf(f);
}
if (!mt) {
F.add(a); // no thread at this point
} else {
// System.out.println("dropped " + a.length());
}
}
G = F;
if (G.size() <= 1) {
return G;
}
SolvableMiReducerSeqPair[] mirs = (SolvableMiReducerSeqPair[]) new SolvableMiReducerSeqPair[G
.size()];
int i = 0;
F = new ArrayList>(G.size());
while (G.size() > 0) {
a = G.remove(0);
// System.out.println("doing " + a.length());
List> R = new ArrayList>(G.size() + F.size());
R.addAll(G);
R.addAll(F);
mirs[i] = new SolvableMiReducerSeqPair(R, a);
pool.addJob(mirs[i]);
i++;
F.add(a);
}
G = F;
F = new ArrayList>(G.size());
for (i = 0; i < mirs.length; i++) {
a = mirs[i].getNF();
F.add(a);
}
return F;
}
/**
* Solvable Extended Groebner base using critical pair class.
* @param modv module variable number.
* @param F solvable polynomial list.
* @return a container for an extended left Groebner base of F.
*/
public SolvableExtendedGB extLeftGB(int modv, List> F) {
throw new UnsupportedOperationException("parallel extLeftGB not implemented");
}
/**
* Twosided Groebner base using pairlist class.
* @param modv number of module variables.
* @param Fp solvable polynomial list.
* @return tsGB(Fp) a twosided Groebner base of F.
*/
public List> twosidedGB(int modv, List> Fp) {
if (Fp == null || Fp.size() == 0) { // 0 not 1
return new ArrayList>();
}
GenSolvablePolynomialRing fac = Fp.get(0).ring; // assert != null
//List> X = generateUnivar( modv, Fp );
List> X = fac.univariateList(modv);
//System.out.println("X univ = " + X);
List> F = new ArrayList>(Fp.size() * (1 + X.size()));
F.addAll(Fp);
GenSolvablePolynomial p, x, q;
for (int i = 0; i < Fp.size(); i++) {
p = Fp.get(i);
for (int j = 0; j < X.size(); j++) {
x = X.get(j);
q = p.multiply(x);
q = sred.leftNormalform(F, q);
if (!q.isZERO()) {
F.add(q);
}
}
}
//System.out.println("F generated = " + F);
List> G = new ArrayList>();
CriticalPairList pairlist = null;
int l = F.size();
ListIterator> it = F.listIterator();
while (it.hasNext()) {
p = it.next();
if (p.length() > 0) {
p = (GenSolvablePolynomial) p.monic();
if (p.isONE()) {
G.clear();
G.add(p);
return G; // since no threads are activated
}
G.add(p);
if (pairlist == null) {
pairlist = new CriticalPairList(modv, p.ring);
if (!p.ring.coFac.isField()) {
throw new IllegalArgumentException("coefficients not from a field");
}
}
// putOne not required
pairlist.put(p);
} else {
l--;
}
}
//System.out.println("G to check = " + G);
if (l <= 1) { // 1 ok
return G; // since no threads are activated
}
Terminator fin = new Terminator(threads);
TwosidedSolvableReducerSeqPair R;
for (int i = 0; i < threads; i++) {
R = new TwosidedSolvableReducerSeqPair(fin, X, G, pairlist);
pool.addJob(R);
}
fin.waitDone();
logger.debug("#parallel list = " + G.size());
G = leftMinimalGB(G);
// not in this context // pool.terminate();
logger.info("" + pairlist);
return G;
}
}
/**
* Reducing left worker threads.
* @param coefficient type
*/
class LeftSolvableReducerSeqPair> implements Runnable {
private final List> G;
private final CriticalPairList pairlist;
private final Terminator pool;
private final SolvableReductionPar sred;
private static final Logger logger = Logger.getLogger(LeftSolvableReducerSeqPair.class);
private static final boolean debug = logger.isDebugEnabled();
LeftSolvableReducerSeqPair(Terminator fin, List> G, CriticalPairList L) {
pool = fin;
this.G = G;
pairlist = L;
sred = new SolvableReductionPar();
}
@SuppressWarnings("unchecked")
public void run() {
CriticalPair pair;
GenSolvablePolynomial S;
GenSolvablePolynomial H;
boolean set = false;
int reduction = 0;
int sleeps = 0;
while (pairlist.hasNext() || pool.hasJobs()) {
while (!pairlist.hasNext()) {
pairlist.update();
// wait
pool.beIdle();
set = true;
try {
sleeps++;
if (sleeps % 10 == 0) {
logger.info(" reducer is sleeping");
} else {
logger.debug("r");
}
Thread.sleep(100);
} catch (InterruptedException e) {
break;
}
if (!pool.hasJobs()) {
break;
}
}
if (!pairlist.hasNext() && !pool.hasJobs()) {
break;
}
if (set) {
pool.notIdle();
set = false;
}
pair = pairlist.getNext();
if (pair == null) {
pairlist.update();
continue;
}
if (debug) {
logger.debug("pi = " + pair.pi);
logger.debug("pj = " + pair.pj);
}
S = sred.leftSPolynomial((GenSolvablePolynomial) pair.pi, (GenSolvablePolynomial) pair.pj);
if (S.isZERO()) {
pairlist.record(pair, S);
continue;
}
if (debug) {
logger.debug("ht(S) = " + S.leadingExpVector());
}
H = sred.leftNormalform(G, S); //mod
reduction++;
if (H.isZERO()) {
pairlist.record(pair, H);
continue;
}
if (debug) {
logger.debug("ht(H) = " + H.leadingExpVector());
}
H = (GenSolvablePolynomial) H.monic();
// System.out.println("H = " + H);
if (H.isONE()) {
// pairlist.update( pair, H );
pairlist.putOne(); // not really required
synchronized (G) {
G.clear();
G.add(H);
}
pool.allIdle();
return;
}
if (debug) {
logger.debug("H = " + H);
}
synchronized (G) {
G.add(H);
}
pairlist.update(pair, H);
//pairlist.record( pair, H );
//pairlist.update();
}
logger.info("terminated, done " + reduction + " reductions");
}
}
/**
* Reducing twosided worker threads.
* @param coefficient type
*/
class TwosidedSolvableReducerSeqPair> implements Runnable {
private final List> X;
private final List> G;
private final CriticalPairList pairlist;
private final Terminator pool;
private final SolvableReductionPar sred;
private static final Logger logger = Logger.getLogger(TwosidedSolvableReducerSeqPair.class);
private static final boolean debug = logger.isDebugEnabled();
TwosidedSolvableReducerSeqPair(Terminator fin, List> X,
List> G, CriticalPairList L) {
pool = fin;
this.X = X;
this.G = G;
pairlist = L;
sred = new SolvableReductionPar();
}
public void run() {
GenSolvablePolynomial p, x;
CriticalPair pair;
GenSolvablePolynomial S;
GenSolvablePolynomial H;
boolean set = false;
int reduction = 0;
int sleeps = 0;
while (pairlist.hasNext() || pool.hasJobs()) {
while (!pairlist.hasNext()) {
pairlist.update();
// wait
pool.beIdle();
set = true;
try {
sleeps++;
if (sleeps % 10 == 0) {
logger.info(" reducer is sleeping");
} else {
logger.debug("r");
}
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
if (!pool.hasJobs()) {
break;
}
}
if (!pairlist.hasNext() && !pool.hasJobs()) {
break;
}
if (set) {
pool.notIdle();
set = false;
}
pair = pairlist.getNext();
if (pair == null) {
pairlist.update();
continue;
}
if (debug) {
logger.debug("pi = " + pair.pi);
logger.debug("pj = " + pair.pj);
}
S = sred.leftSPolynomial((GenSolvablePolynomial) pair.pi, (GenSolvablePolynomial) pair.pj);
if (S.isZERO()) {
pairlist.record(pair, S);
continue;
}
if (debug) {
logger.debug("ht(S) = " + S.leadingExpVector());
}
H = sred.leftNormalform(G, S); //mod
reduction++;
if (H.isZERO()) {
pairlist.record(pair, H);
continue;
}
if (debug) {
logger.debug("ht(H) = " + H.leadingExpVector());
}
H = (GenSolvablePolynomial) H.monic();
// System.out.println("H = " + H);
if (H.isONE()) {
// pairlist.update( pair, H );
pairlist.putOne(); // not really required
synchronized (G) {
G.clear();
G.add(H);
}
pool.allIdle();
return;
}
if (debug) {
logger.debug("H = " + H);
}
synchronized (G) {
G.add(H);
}
pairlist.update(pair, H);
for (int j = 0; j < X.size(); j++) {
x = X.get(j);
p = H.multiply(x);
p = sred.leftNormalform(G, p);
if (!p.isZERO()) {
p = (GenSolvablePolynomial) p.monic();
if (p.isONE()) {
synchronized (G) {
G.clear();
G.add(p);
}
pool.allIdle();
return;
}
synchronized (G) {
G.add(p);
}
pairlist.put(p);
}
}
}
logger.info("terminated, done " + reduction + " reductions");
}
}
/**
* Reducing worker threads for minimal GB.
* @param coefficient type
*/
class SolvableMiReducerSeqPair> implements Runnable {
private final List> G;
private GenSolvablePolynomial H;
private final SolvableReductionPar sred;
private final Semaphore done = new Semaphore(0);
private static final Logger logger = Logger.getLogger(SolvableMiReducerSeqPair.class);
private static final boolean debug = logger.isDebugEnabled();
SolvableMiReducerSeqPair(List> G, GenSolvablePolynomial p) {
this.G = G;
H = p;
sred = new SolvableReductionPar();
}
/**
* getNF. Blocks until the normal form is computed.
* @return the computed normal form.
*/
public GenSolvablePolynomial getNF() {
try {
done.acquire(); //done.P();
} catch (InterruptedException e) {
}
return H;
}
public void run() {
if (debug) {
logger.debug("ht(H) = " + H.leadingExpVector());
}
H = sred.leftNormalform(G, H); //mod
done.release(); //done.V();
if (debug) {
logger.debug("ht(H) = " + H.leadingExpVector());
}
// H = H.monic();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy