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