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