001/* 002 * $Id: GBDistSP.java 6013 2020-04-29 17:04:16Z 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 * @see edu.jas.application.RunGB 021 * @see edu.jas.application.RunSGB 022 * TODO: to deprecate 023 */ 024 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 respectivly 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 */ 112 113class GBClientSP<C extends RingElem<C>> implements RemoteExecutable { 114 115 116 String host; 117 118 119 int port; 120 121 122 /** 123 * GBClient. 124 * @param host 125 * @param port 126 */ 127 public GBClientSP(String host, int port) { 128 this.host = host; 129 this.port = port; 130 } 131 132 133 /** 134 * run. 135 */ 136 public void run() { 137 GroebnerBaseSeqPairDistributed<C> bbd; 138 bbd = new GroebnerBaseSeqPairDistributed<C>(1, null, port); 139 try { 140 bbd.clientPart(host); 141 } catch (IOException ignored) { 142 } 143 bbd.terminate(); 144 } 145 146}