001/*
002 * $Id: GBDistSP.java 4334 2012-12-28 11:49:57Z kredel $
003 */
004
005package edu.jas.gb;
006
007
008import java.io.IOException;
009import java.util.List;
010
011import edu.jas.poly.GenPolynomial;
012import edu.jas.structure.RingElem;
013import edu.jas.util.DistThreadPool;
014import edu.jas.util.RemoteExecutable;
015
016
017/**
018 * Setup to run a distributed GB example.
019 * @author Heinz Kredel
020 * TODO: to deprecate
021 */
022
023public class GBDistSP<C extends RingElem<C>> {
024
025
026    /**
027     * machine file to use.
028     */
029    private final String mfile;
030
031
032    /**
033     * Number of threads to use.
034     */
035    protected final int threads;
036
037
038    /**
039     * Server port to use.
040     */
041    protected final int port;
042
043
044    /**
045     * GB algorithm to use.
046     */
047    private final GroebnerBaseSeqPairDistributed<C> bbd;
048
049
050    /**
051     * Distributed thread pool to use.
052     */
053    private final DistThreadPool dtp;
054
055
056    /**
057     * Constructor.
058     * @param threads number of threads respectivly processes.
059     * @param mfile name of the machine file.
060     * @param port for GB server.
061     */
062    public GBDistSP(int threads, String mfile, int port) {
063        this.threads = threads;
064        if (mfile == null || mfile.length() == 0) {
065            this.mfile = "../util/machines";
066        } else {
067            this.mfile = mfile;
068        }
069        this.port = port;
070        bbd = new GroebnerBaseSeqPairDistributed<C>(threads, this.port);
071        dtp = new DistThreadPool(threads, this.mfile);
072    }
073
074
075    /**
076     * Execute a distributed GB example. Distribute clients and start master.
077     * @param F list of polynomials
078     * @return GB(F) a Groebner base for F.
079     */
080    public List<GenPolynomial<C>> execute(List<GenPolynomial<C>> F) {
081        String master = dtp.getEC().getMasterHost();
082        int port = dtp.getEC().getMasterPort();
083        GBClientSP<C> gbc = new GBClientSP<C>(master, port);
084        for (int i = 0; i < threads; i++) {
085            // schedule remote clients
086            dtp.addJob(gbc);
087        }
088        // run master
089        List<GenPolynomial<C>> G = bbd.GB(F);
090        return G;
091    }
092
093
094    /**
095     * Terminates the distributed thread pools.
096     * @param shutDown true, if shut-down of the remote executable servers is
097     *            requested, false, if remote executable servers stay alive.
098     */
099    public void terminate(boolean shutDown) {
100        bbd.terminate();
101        dtp.terminate(shutDown);
102    }
103
104}
105
106
107/**
108 * Objects of this class are to be send to a ExecutableServer.
109 */
110
111class GBClientSP<C extends RingElem<C>> implements RemoteExecutable {
112
113
114    String host;
115
116
117    int port;
118
119
120    /**
121     * GBClient.
122     * @param host
123     * @param port
124     */
125    public GBClientSP(String host, int port) {
126        this.host = host;
127        this.port = port;
128    }
129
130
131    /**
132     * run.
133     */
134    public void run() {
135        GroebnerBaseSeqPairDistributed<C> bbd;
136        bbd = new GroebnerBaseSeqPairDistributed<C>(1, null, port);
137        try {
138            bbd.clientPart(host);
139        } catch (IOException ignored) {
140        }
141        bbd.terminate();
142    }
143
144}