001 /*
002 * $Id: GBDistSP.java 3295 2010-08-26 17:01:10Z kredel $
003 */
004
005 package edu.jas.gb;
006
007
008 import java.io.IOException;
009 import java.util.List;
010
011 import edu.jas.poly.GenPolynomial;
012 import edu.jas.structure.RingElem;
013 import edu.jas.util.DistThreadPool;
014 import edu.jas.util.RemoteExecutable;
015
016
017 /**
018 * Setup to run a distributed GB example.
019 * @author Heinz Kredel
020 */
021
022 public 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, port);
070 dtp = new DistThreadPool(threads, mfile);
071 }
072
073
074 /**
075 * Execute a distributed GB example. Distribute clients and start master.
076 * Obsolete version.
077 * @param F list of polynomials
078 * @return GB(F) a Groebner base for F. public List<GenPolynomial<C>>
079 * executeOld(List<GenPolynomial<C>> F) { final int numc = threads;
080 * List<GenPolynomial<C>> G = null; ExecutableChannels ec = null;
081 * try { ec = new ExecutableChannels( mfile ); } catch
082 * (FileNotFoundException e) { e.printStackTrace(); return G; } try
083 * { ec.open(numc); } catch (IOException e) { e.printStackTrace();
084 * return G; } GBClient<C> gbc = new GBClient<C>(
085 * ec.getMasterHost(), ec.getMasterPort() ); try { for ( int i = 0;
086 * i < numc; i++ ) { ec.send( i, gbc ); } } catch (IOException e) {
087 * e.printStackTrace(); return G; } G = bbd.GB( F ); try { for ( int
088 * i = 0; i < numc; i++ ) { Object o = ec.receive( i ); } } catch
089 * (IOException e) { e.printStackTrace(); return G; } catch
090 * (ClassNotFoundException e) { e.printStackTrace(); return G; }
091 * ec.close(); bbd.terminate(); return G; }
092 */
093
094
095 /**
096 * Execute a distributed GB example. Distribute clients and start master.
097 * @param F list of polynomials
098 * @return GB(F) a Groebner base for F.
099 */
100 public List<GenPolynomial<C>> execute(List<GenPolynomial<C>> F) {
101 String master = dtp.getEC().getMasterHost();
102 int port = dtp.getEC().getMasterPort();
103 GBClientSP<C> gbc = new GBClientSP<C>(master, port);
104 for (int i = 0; i < threads; i++) {
105 // schedule remote clients
106 dtp.addJob(gbc);
107 }
108 // run master
109 List<GenPolynomial<C>> G = bbd.GB(F);
110 return G;
111 }
112
113
114 /**
115 * Terminates the distributed thread pools.
116 * @param shutDown true, if shut-down of the remote executable servers is
117 * requested, false, if remote executable servers stay alive.
118 */
119 public void terminate(boolean shutDown) {
120 bbd.terminate();
121 dtp.terminate(shutDown);
122 }
123
124 }
125
126
127 /**
128 * Objects of this class are to be send to a ExecutableServer.
129 */
130
131 class GBClientSP<C extends RingElem<C>> implements RemoteExecutable {
132
133
134 String host;
135
136
137 int port;
138
139
140 /**
141 * GBClient.
142 * @param host
143 * @param port
144 */
145 public GBClientSP(String host, int port) {
146 this.host = host;
147 this.port = port;
148 }
149
150
151 /**
152 * run.
153 */
154 public void run() {
155 GroebnerBaseSeqPairDistributed<C> bbd;
156 bbd = new GroebnerBaseSeqPairDistributed<C>(1, null, port);
157 try {
158 bbd.clientPart(host);
159 } catch (IOException ignored) {
160 }
161 bbd.terminate();
162 }
163
164 }