1 cananian 1.4.2.4 // Main.java, created Mon Nov 16 23:33:21 1998 by mfoltz
  2 cananian 1.4.2.4 // Copyright (C) 1998 Mark A. Foltz <mfoltz@ai.mit.edu>
  3 cananian 1.4.2.4 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.4.2.4 
  5 mfoltz   1.1     // -*-Mode: Java-*- 
  6 mfoltz   1.1     // Main.java -- 
  7 mfoltz   1.1     // Author: Mark Foltz <mfoltz@ai.mit.edu> 
  8 mfoltz   1.1     // Maintainer: Mark Foltz <mfoltz@ai.mit.edu> 
  9 mfoltz   1.1     // Version: 
 10 mfoltz   1.1     // Created: <Sun Oct 25 12:37:16 1998> 
 11 mfoltz   1.5     // Time-stamp: <1998-11-27 14:38:58 mfoltz> 
 12 mfoltz   1.1     // Keywords: 
 13 mfoltz   1.1     
 14 mfoltz   1.2     package harpoon.Analysis.Partition;
 15 mfoltz   1.1     import java.util.Observer;
 16 mfoltz   1.1     import java.util.Observable;
 17 mfoltz   1.1     import java.util.Properties;
 18 mfoltz   1.1     import java.util.Hashtable;
 19 mfoltz   1.1     import java.util.Enumeration;
 20 mfoltz   1.1     import java.util.StringTokenizer;
 21 mfoltz   1.1     import java.io.FileInputStream;
 22 mfoltz   1.1     import java.io.BufferedReader;
 23 mfoltz   1.1     import java.io.FileReader;
 24 mfoltz   1.1     
 25 mfoltz   1.1     /**
 26 mfoltz   1.1      * 
 27 mfoltz   1.1      * @author  Mark A. Foltz <mfoltz@ai.mit.edu>
 28 cananian 1.7      * @version $Id: Main.java,v 1.7 2003/07/28 22:33:36 cananian Exp $
 29 mfoltz   1.1      */
 30 mfoltz   1.1     
 31 mfoltz   1.1     public class Main  {
 32 mfoltz   1.1     
 33 mfoltz   1.1     //   public static void main(String args[]) {
 34 mfoltz   1.1     
 35 mfoltz   1.1     //     Properties p = new Properties();
 36 mfoltz   1.1     
 37 mfoltz   1.1     //     try {
 38 mfoltz   1.1     //       p.load(new FileInputStream("/home/ai/mfoltz/class/6892/src/harpoon/Analyze/Partition/properties"));
 39 mfoltz   1.1     
 40 mfoltz   1.1     //       WeightedGraph pt[] = new WeightedGraph[2];
 41 mfoltz   1.1     //       WeightedGraph g = 
 42 mfoltz   1.1     //      WeightedGraph.createRandomGraph(Integer.parseInt(p.getProperty("nodes")), 
 43 mfoltz   1.1     //                                      Integer.parseInt(p.getProperty("mean")), 
 44 mfoltz   1.1     //                                      Integer.parseInt(p.getProperty("variance")),
 45 mfoltz   1.1     //                                      Integer.parseInt(p.getProperty("maxweight")));
 46 mfoltz   1.1     //       Partition.initialPartition(g, 2, pt);
 47 mfoltz   1.1     //       System.err.println("Edge sum:  "+Partition.computeEdgeSum(pt[0], pt[1]));
 48 mfoltz   1.1     //       Callback cb = new Callback(pt);
 49 mfoltz   1.1     //       if (p.getProperty("viewerstyle").equals("ONE_CIRCLE")) {
 50 mfoltz   1.1     //      PartitionGraphViewer pgv = 
 51 mfoltz   1.1     //        new PartitionGraphViewer(pt, 640, 660, cb, PartitionGraphViewer.ONE_CIRCLE);
 52 mfoltz   1.1     //       } else {
 53 mfoltz   1.1     //      PartitionGraphViewer pgv = 
 54 mfoltz   1.1     //        new PartitionGraphViewer(pt, 640, 660, cb, PartitionGraphViewer.MULTI_CIRCLE);
 55 mfoltz   1.1     //       }
 56 mfoltz   1.1     //     } catch (Exception e) {
 57 mfoltz   1.1     //       System.err.println(e);
 58 mfoltz   1.1     //     }
 59 mfoltz   1.1           
 60 mfoltz   1.1     //   }
 61 mfoltz   1.1     
 62 mfoltz   1.1       
 63 mfoltz   1.1       public static void main(String args[]) {
 64 mfoltz   1.1     
 65 mfoltz   1.1         try {
 66 mfoltz   1.1     
 67 mfoltz   1.3           FileReader fr;
 68 mfoltz   1.3           BufferedReader br;
 69 mfoltz   1.3           int i;
 70 mfoltz   1.3           WeightedGraph g = new WeightedGraph();
 71 mfoltz   1.1           WeightedGraph pt[] = new WeightedGraph[2];
 72 mfoltz   1.3           WGNode pseudo_nodes[] = new WGNode[2];
 73 mfoltz   1.1           Properties p = new Properties();
 74 mfoltz   1.1           
 75 mfoltz   1.1           p.load(new FileInputStream("/home/mfoltz/Harpoon/Code/Analysis/Partition/properties"));
 76 mfoltz   1.3     
 77 mfoltz   1.3           // add pseudo-nodes that allow bindings of object creation sites to nodes
 78 mfoltz   1.3           for (i = 0; i < 2; i++) {
 79 mfoltz   1.3             pseudo_nodes[i] = new WGNode("Machine_"+i,null);
 80 mfoltz   1.3             pseudo_nodes[i]._dummy = true;
 81 mfoltz   1.3           }
 82 mfoltz   1.3     
 83 mfoltz   1.3           for (i = 0; i < args.length; i++) {
 84 mfoltz   1.3             fr = new FileReader(args[i]);
 85 mfoltz   1.3             br = new BufferedReader(fr);
 86 mfoltz   1.3             parseProfile(g,br,pseudo_nodes);
 87 mfoltz   1.3             br.close();
 88 mfoltz   1.3             fr.close();
 89 mfoltz   1.3           }
 90 mfoltz   1.3     
 91 mfoltz   1.1           g.addDummies(g.size());
 92 mfoltz   1.3     
 93 mfoltz   1.1           Partition.initialPartition(g, 2, pt);
 94 mfoltz   1.3     
 95 mfoltz   1.3           // add pseudo-nodes that allow bindings of object creation sites to nodes
 96 mfoltz   1.3           for (i = 0; i < 2; i++) 
 97 mfoltz   1.3             pt[i].addNode(pseudo_nodes[i]);
 98 mfoltz   1.5             
 99 mfoltz   1.3     
100 mfoltz   1.1           System.err.println("Edge sum:  "+Partition.computeEdgeSum(pt[0], pt[1]));
101 mfoltz   1.1           Callback cb = new Callback(pt);
102 mfoltz   1.1           if (p.getProperty("viewerstyle").equals("ONE_CIRCLE")) {
103 mfoltz   1.1             PartitionGraphViewer pgv = 
104 mfoltz   1.1               new PartitionGraphViewer(pt, 640, 660, cb, PartitionGraphViewer.ONE_CIRCLE);
105 mfoltz   1.1           } else {
106 mfoltz   1.1             PartitionGraphViewer pgv = 
107 mfoltz   1.1               new PartitionGraphViewer(pt, 640, 660, cb, PartitionGraphViewer.MULTI_CIRCLE);
108 mfoltz   1.1           }
109 mfoltz   1.1     
110 mfoltz   1.1          } catch (Exception e) {
111 mfoltz   1.1            System.err.println(e);
112 mfoltz   1.4            e.printStackTrace();
113 mfoltz   1.1          }
114 mfoltz   1.1           
115 mfoltz   1.1       }
116 mfoltz   1.1         
117 mfoltz   1.1       static class Callback implements Observer {
118 mfoltz   1.1     
119 mfoltz   1.1         private WeightedGraph[] _partition;
120 mfoltz   1.1     
121 mfoltz   1.1         public Callback(WeightedGraph[] partition) {
122 mfoltz   1.1           _partition = partition;
123 mfoltz   1.1         }
124 mfoltz   1.1     
125 mfoltz   1.1         public void update(Observable o, Object arg) {
126 mfoltz   1.4     
127 mfoltz   1.4           try {
128 mfoltz   1.4             long gain;
129 mfoltz   1.4             gain = Partition.exchange(_partition[0], _partition[1]);
130 mfoltz   1.4             System.err.println("Edge sum:  "+Partition.computeEdgeSum(_partition[0], _partition[1]));
131 mfoltz   1.4             WGNode node;
132 cananian 1.7             Enumeration _enum_ = _partition[0].getNodes();
133 cananian 1.7             while (_enum_.hasMoreElements()) {
134 cananian 1.7               node = (WGNode) _enum_.nextElement();
135 mfoltz   1.5               if (!node._dummy) System.err.println("MACHINE "+node._value+" 0");
136 mfoltz   1.4             }
137 cananian 1.7             _enum_ = _partition[1].getNodes();
138 cananian 1.7             while (_enum_.hasMoreElements()) {
139 cananian 1.7               node = (WGNode) _enum_.nextElement();
140 mfoltz   1.5               if (!node._dummy) System.err.println("MACHINE "+node._value+" 1");
141 mfoltz   1.4             }
142 mfoltz   1.4             if (gain == 0) {
143 mfoltz   1.4               System.err.println("Phase-1 optimal partition found.");
144 mfoltz   1.4               System.exit(0);
145 mfoltz   1.4             }
146 mfoltz   1.4           } catch (Exception e) {
147 mfoltz   1.4             System.err.println(e);
148 mfoltz   1.4             e.printStackTrace();
149 mfoltz   1.1           }
150 mfoltz   1.4           
151 mfoltz   1.4         }
152 mfoltz   1.1     
153 mfoltz   1.1       }
154 mfoltz   1.1     
155 mfoltz   1.3       public static WeightedGraph parseProfile(WeightedGraph g, BufferedReader in, WGNode[] pseudo_nodes) {
156 mfoltz   1.1     
157 mfoltz   1.1         Hashtable creation_sites = new Hashtable();
158 mfoltz   1.1         long creator, created;
159 mfoltz   1.3         int site_id, machine_no;
160 mfoltz   1.1         StringTokenizer st;
161 mfoltz   1.1         NEW creation_site;
162 mfoltz   1.1         WGNode node, source, target;
163 mfoltz   1.1     
164 mfoltz   1.1         try {
165 mfoltz   1.1     
166 mfoltz   1.3           String line = in.readLine(), token, creator_method, creator_class, created_class, node_name;
167 mfoltz   1.1     
168 mfoltz   1.1           while (line != null) {
169 mfoltz   1.1             st = new StringTokenizer(line," ");
170 mfoltz   1.4             if (st.countTokens() == 0) break;
171 mfoltz   1.1             token = st.nextToken();
172 mfoltz   1.1             if (token.startsWith("NEW")) {
173 mfoltz   1.1               creator = Long.parseLong(st.nextToken());
174 mfoltz   1.1               creator_method = st.nextToken();
175 mfoltz   1.1               creator_class = st.nextToken();
176 mfoltz   1.1               created = Long.parseLong(st.nextToken());
177 mfoltz   1.1               created_class = st.nextToken();
178 mfoltz   1.1               site_id = Integer.parseInt(st.nextToken());
179 mfoltz   1.3               node_name = creator_class+":"+creator_method+":"+site_id;
180 mfoltz   1.3               node = g.getNode(node_name);
181 mfoltz   1.3               if (node == null) {
182 mfoltz   1.3                 creation_site = new NEW(creator, creator_method, creator_class, created, created_class, site_id);
183 mfoltz   1.3                 node = new WGNode(node_name,creation_site);
184 mfoltz   1.3                 g.addNode(node);
185 mfoltz   1.3               }
186 mfoltz   1.1               creation_sites.put(new Long(created), node);
187 mfoltz   1.1             } else if (token.startsWith("CALL")) {
188 mfoltz   1.1               source = (WGNode) creation_sites.get(new Long(Long.parseLong(st.nextToken())));
189 mfoltz   1.1               st.nextToken();
190 mfoltz   1.1               target = (WGNode) creation_sites.get(new Long(Long.parseLong(st.nextToken())));
191 mfoltz   1.1               if (source == null || target == null || source == target) {
192 mfoltz   1.1                 System.err.println("IGNORING: "+line);
193 mfoltz   1.1               } else {
194 mfoltz   1.1                 WeightedGraph.addToEdge(source,target,1);
195 mfoltz   1.3               }
196 mfoltz   1.3             } else if (token.startsWith("MAYCALL")) {
197 mfoltz   1.3               // do nothing
198 mfoltz   1.3             } else if (token.startsWith("BIND")) {
199 mfoltz   1.3               node_name = st.nextToken();
200 mfoltz   1.3               machine_no = Integer.parseInt(st.nextToken());
201 mfoltz   1.3               if (machine_no < 0 || machine_no >= pseudo_nodes.length)
202 mfoltz   1.3                 source = null;
203 mfoltz   1.3               else source = pseudo_nodes[machine_no];
204 mfoltz   1.3               target = g.getNode(node_name);
205 mfoltz   1.3               if (source == null || target == null || source == target) {
206 mfoltz   1.3                 System.err.println("IGNORING: "+line);
207 mfoltz   1.3               } else {
208 mfoltz   1.4                 WeightedGraph.setEdge(source,target,Long.MAX_VALUE);
209 mfoltz   1.5                 target._binding = machine_no;
210 mfoltz   1.5               }
211 mfoltz   1.5             } else if (token.startsWith("MACHINE")) {
212 mfoltz   1.5               node_name = st.nextToken();
213 mfoltz   1.5               machine_no = Integer.parseInt(st.nextToken());
214 mfoltz   1.5               target = g.getNode(node_name);
215 mfoltz   1.5               if (target == null) {
216 mfoltz   1.5                 System.err.println("IGNORING: "+line);
217 mfoltz   1.5               } else {
218 mfoltz   1.5                 target._binding = machine_no;
219 mfoltz   1.1               }
220 mfoltz   1.1             }
221 mfoltz   1.1             line = in.readLine();
222 mfoltz   1.1           }
223 mfoltz   1.1     
224 mfoltz   1.1         } catch (Exception e) {
225 mfoltz   1.1           System.err.println(e);
226 mfoltz   1.4           e.printStackTrace();
227 mfoltz   1.1         }
228 mfoltz   1.1     
229 mfoltz   1.1         return g;
230 mfoltz   1.1     
231 mfoltz   1.1       }
232 mfoltz   1.1     
233 mfoltz   1.1       static private class NEW {
234 mfoltz   1.1     
235 mfoltz   1.1         public long _creator;
236 mfoltz   1.1         public String _creator_method;
237 mfoltz   1.1         public String _creator_class;
238 mfoltz   1.1         public long _created;
239 mfoltz   1.1         public String _created_class;
240 mfoltz   1.1         public int _site_id;
241 mfoltz   1.1     
242 mfoltz   1.1         public NEW(long creator, String creator_method, String creator_class,
243 mfoltz   1.1                    long created, String created_class, int site_id) {
244 mfoltz   1.1           _creator = creator;
245 mfoltz   1.1           _creator_method = creator_method;
246 mfoltz   1.1           _creator_class = creator_class;
247 mfoltz   1.1           _created = created;
248 mfoltz   1.1           _created_class = created_class;
249 mfoltz   1.1           _site_id = site_id;
250 mfoltz   1.1         }
251 mfoltz   1.1         
252 mfoltz   1.1         public String toString() {
253 mfoltz   1.1           return _creator_class+":"+_creator_method+":"+_site_id;
254 mfoltz   1.1         }
255 mfoltz   1.1     
256 mfoltz   1.1       }
257 mfoltz   1.1         
258 mfoltz   1.1     }
259 mfoltz   1.1     
260 mfoltz   1.1