001 /*
002 * $Id: GBDistHybrid.java 3391 2010-12-05 13:18:42Z 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 GBDistHybrid<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 * Number of threads per node to use.
039 */
040 protected final int threadsPerNode;
041
042
043 /**
044 * Server port to use.
045 */
046 protected final int port;
047
048
049 /**
050 * GB algorithm to use.
051 */
052 private final GroebnerBaseDistributedHybrid<C> bbd;
053
054
055 /**
056 * Distributed thread pool to use.
057 */
058 private final DistThreadPool dtp;
059
060
061 /**
062 * Constructor.
063 * @param threads number of threads respectivly processes.
064 * @param threadsPerNode number of threads per node to use.
065 * @param mfile name of the machine file.
066 * @param port for GB server.
067 */
068 public GBDistHybrid(int threads, int threadsPerNode, String mfile, int port) {
069 this(threads, threadsPerNode, new OrderedPairlist<C>(), mfile, port);
070 }
071
072
073 /**
074 * Constructor.
075 * @param threads number of threads respectivly processes.
076 * @param threadsPerNode number of threads per node to use.
077 * @param pl pair selection strategy
078 * @param mfile name of the machine file.
079 * @param port for GB server.
080 */
081 public GBDistHybrid(int threads, int threadsPerNode, PairList<C> pl, String mfile, int port) {
082 this.threads = threads;
083 this.threadsPerNode = threadsPerNode;
084 if (mfile == null || mfile.length() == 0) {
085 this.mfile = "../util/machines";
086 } else {
087 this.mfile = mfile;
088 }
089 this.port = port;
090 bbd = new GroebnerBaseDistributedHybrid<C>(threads, threadsPerNode, pl, port);
091 dtp = new DistThreadPool(threads, mfile);
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 GBClientHybrid<C> gbc = new GBClientHybrid<C>(threadsPerNode, 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 GBClientHybrid<C extends RingElem<C>> implements RemoteExecutable {
132
133
134 String host;
135
136 int port;
137
138 //int threads;
139
140 int threadsPerNode;
141
142
143 /**
144 * GBClientHybrid.
145 * @param threadsPerNode
146 * @param host master
147 * @param port
148 */
149 public GBClientHybrid(int threadsPerNode, String host, int port) {
150 //this.threads = threads;
151 this.threadsPerNode = threadsPerNode;
152 this.host = host;
153 this.port = port;
154 }
155
156
157 /** Get the String representation.
158 * @see java.lang.Object#toString()
159 */
160 @Override
161 public String toString() {
162 return "GBClientHybrid(" + threadsPerNode + ", " + host + ":" + port + " )";
163 }
164
165
166 /**
167 * run.
168 */
169 public void run() {
170 GroebnerBaseDistributedHybrid<C> bbd;
171 bbd = new GroebnerBaseDistributedHybrid<C>(1, threadsPerNode, null, null, port);
172 try {
173 bbd.clientPart(host);
174 } catch (IOException e) {
175 System.out.println("clientPart, exception " + e);
176 } catch (Exception e) {
177 System.out.println("clientPart, exception " + e);
178 }
179 bbd.terminate();
180 }
181
182 }