001 /* 002 * $Id: GBDist.java 3652 2011-06-02 18:17:04Z 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 GBDist<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 GroebnerBaseDistributed<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 GBDist(int threads, String mfile, int port) { 062 this(threads, new OrderedPairlist<C>(), mfile, port); 063 } 064 065 /** 066 * Constructor. 067 * @param threads number of threads respectivly processes. 068 * @param pl pair selection strategy 069 * @param mfile name of the machine file. 070 * @param port for GB server. 071 */ 072 public GBDist(int threads, PairList<C> pl, String mfile, int port) { 073 this.threads = threads; 074 if (mfile == null || mfile.length() == 0) { 075 this.mfile = "../util/machines"; 076 } else { 077 this.mfile = mfile; 078 } 079 this.port = port; 080 bbd = new GroebnerBaseDistributed<C>(threads, pl, port); 081 dtp = new DistThreadPool(threads, mfile); 082 } 083 084 085 /** 086 * Execute a distributed GB example. Distribute clients and start master. 087 * Obsolete version. 088 * @param F list of polynomials 089 * @return GB(F) a Groebner base for F. public List<GenPolynomial<C>> 090 * executeOld(List<GenPolynomial<C>> F) { final int numc = threads; 091 * List<GenPolynomial<C>> G = null; ExecutableChannels ec = null; 092 * try { ec = new ExecutableChannels( mfile ); } catch 093 * (FileNotFoundException e) { e.printStackTrace(); return G; } try 094 * { ec.open(numc); } catch (IOException e) { e.printStackTrace(); 095 * return G; } GBClient<C> gbc = new GBClient<C>( 096 * ec.getMasterHost(), ec.getMasterPort() ); try { for ( int i = 0; 097 * i < numc; i++ ) { ec.send( i, gbc ); } } catch (IOException e) { 098 * e.printStackTrace(); return G; } G = bbd.GB( F ); try { for ( int 099 * i = 0; i < numc; i++ ) { Object o = ec.receive( i ); } } catch 100 * (IOException e) { e.printStackTrace(); return G; } catch 101 * (ClassNotFoundException e) { e.printStackTrace(); return G; } 102 * ec.close(); bbd.terminate(); return G; } 103 */ 104 105 106 /** 107 * Execute a distributed GB example. Distribute clients and start master. 108 * @param F list of polynomials 109 * @return GB(F) a Groebner base for F. 110 */ 111 public List<GenPolynomial<C>> execute(List<GenPolynomial<C>> F) { 112 String master = dtp.getEC().getMasterHost(); 113 int port = dtp.getEC().getMasterPort(); 114 GBClient<C> gbc = new GBClient<C>(master, port); 115 for (int i = 0; i < threads; i++) { 116 // schedule remote clients 117 dtp.addJob(gbc); 118 } 119 // run master 120 List<GenPolynomial<C>> G = bbd.GB(F); 121 return G; 122 } 123 124 125 /** 126 * Terminates the distributed thread pools. 127 * @param shutDown true, if shut-down of the remote executable servers is 128 * requested, false, if remote executable servers stay alive. 129 */ 130 public void terminate(boolean shutDown) { 131 bbd.terminate(); 132 dtp.terminate(shutDown); 133 } 134 135 } 136 137 138 /** 139 * Objects of this class are to be send to a ExecutableServer. 140 */ 141 142 class GBClient<C extends RingElem<C>> implements RemoteExecutable { 143 144 145 String host; 146 147 148 int port; 149 150 151 /** 152 * GBClient. 153 * @param host 154 * @param port 155 */ 156 public GBClient(String host, int port) { 157 this.host = host; 158 this.port = port; 159 } 160 161 162 /** 163 * run. 164 */ 165 public void run() { 166 GroebnerBaseDistributed<C> bbd; 167 bbd = new GroebnerBaseDistributed<C>(1, null, null, port); 168 try { 169 bbd.clientPart(host); 170 } catch (IOException ignored) { 171 } 172 bbd.terminate(); 173 } 174 175 }