assign.cc 18.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2000-2002 University of Utah and the Flux Group.
 * All rights reserved.
 */

#include <LEDA/graph_alg.h>
#include <LEDA/graphwin.h>
#include <LEDA/ugraph.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
#include <LEDA/node_pq.h>
#include <LEDA/sortseq.h>
Mac Newbold's avatar
Mac Newbold committed
15
#include <iostream.h>
16
#include <stdio.h>
Mac Newbold's avatar
Mac Newbold committed
17 18
#include <stdlib.h>
#include <sys/types.h>
19
#include <unistd.h>
Mac Newbold's avatar
Mac Newbold committed
20
#include <sys/time.h>
21 22
#include <string.h>
#include <assert.h>
Mac Newbold's avatar
Mac Newbold committed
23 24 25

#include "common.h"
#include "physical.h"
26
#include "vclass.h"
27
#include "virtual.h"
28
#include "score.h"
29 30 31 32 33 34 35
#include "pclass.h"

void parse_options(char **argv, struct config_param options[], int nopt);
int config_parse(char **args, struct config_param cparams[], int nparams);
void dump_options(const char *str, struct config_param cparams[], int nparams);

// Purely heuristic
36

37
#ifdef USE_OPTIMAL
Mac Newbold's avatar
Mac Newbold committed
38
#define OPTIMAL_SCORE(edges,nodes) (nodes*SCORE_PNODE + \
39
                                    nodes/opt_nodes_per_sw*SCORE_SWITCH + \
40 41
                                    edges*((SCORE_INTRASWITCH_LINK+ \
                                    SCORE_DIRECT_LINK*2)*4+\
42
                                    SCORE_INTERSWITCH_LINK)/opt_nodes_per_sw)
43
#else
44
#define OPTIMAL_SCORE(edges,nodes) 0
45
#endif
Mac Newbold's avatar
Mac Newbold committed
46

47
tb_sgraph SG;
48 49 50
edge_array<int> edge_costs;
typedef node_array<int> switch_distance_array;
typedef node_array<edge> switch_pred_array;
Mac Newbold's avatar
Mac Newbold committed
51

52 53 54 55 56 57
node_array<switch_distance_array> switch_distances;
node_array<switch_pred_array> switch_preds;
tb_pgraph PG;
tb_vgraph G;
dictionary<tb_pnode*,node> pnode2node;
dictionary<tb_pnode*,int> pnode2posistion;
58 59
pclass_list pclasses;
pclass_types type_table;
60

61 62 63
dictionary<string,node> pname2node;
dictionary<string,node> vname2node;
dictionary<string,string> fixed_nodes;
64

65 66
/* How can we chop things up? */
#define PARTITION_BY_ANNEALING 0
67

68
#define MAX_DELAYS 64
69

70 71 72 73 74 75 76
int nparts = 0;     /* DEFAULTS */
int accepts = 0;
int nnodes = 0;
int partition_mechanism;
int on_line = 0;
int cycles_to_best = 0;
int batch_mode = 0;
77

78
float sensitivity = .1;
79

80
int refreshed = 0;
81

82 83 84
node_array<int> absnodes;
node_array<string> abstypes;
float bestscore, absbest;
85

86 87 88
extern node pnodes[MAX_PNODES];
extern node_array<int> switch_index;
node_pq<int> unassigned_nodes(G);
89

90 91
int parse_top(tb_vgraph &G, istream& i);
int parse_ptop(tb_pgraph &PG, tb_sgraph &SG, istream& i);
Mac Newbold's avatar
Mac Newbold committed
92

93 94 95 96 97 98 99 100 101 102
/* The following two sets hold all the virtual and physical types.  These
 * are compared to make sure that every member of vtypes is in ptypes.
 * Both are filled by the parse_* routines.  I'd love to use LEDA sets
 * to implement this but LEDA, an otherwise profession work, did a lousy
 * job when it came to sets.  They clash with graph iterators!  Since
 * we only use these once we'll use a much less efficient linked list
 * <shudder>
 */
list<string> vtypes;
list<string> ptypes;
103

104
// Makes LEDA happy
105

106 107 108 109 110 111
int compare(tb_pnode *const &a, tb_pnode *const &b)
{
  if (a==b) return 0;
  if (a < b) return -1;
  return 1;
}
112

113 114 115 116 117 118 119
/*
 * Basic simulated annealing parameters:
 *
 * Make changes proportional to T
 * Accept worse solution with p = e^(change/Temperature*sensitivity)
 *
 */
120

121
inline int accept(float change, float temperature)
Mac Newbold's avatar
Mac Newbold committed
122
{
123
  float p;
Mac Newbold's avatar
Mac Newbold committed
124 125 126 127 128 129 130
  int r;

  if (change == 0) {
    p = 1000 * temperature / temp_prob;
  } else {
    p = expf(change/(temperature*sensitivity)) * 1000;
  }
131
  r = random() % 1000;
Mac Newbold's avatar
Mac Newbold committed
132
  if (r < p) {
133
    accepts++;
Mac Newbold's avatar
Mac Newbold committed
134 135 136 137 138
    return 1;
  }
  return 0;
}

139 140 141 142 143
// This routine chooses randomly chooses a pclass based on the weights
// and *removes that pclass from the weights*.  Total is the total of
// all weights and is adjusted when a pclass is removed.
tb_pnode *choose_pnode(dictionary<tb_pclass*,double> &weights,double &total,
		       string vtype)
144
{
145 146
  tb_pnode *pnode;
  dic_item dit=nil;
147

148 149 150 151
  double r = random()/(double)RAND_MAX*total;
  forall_items(dit,weights) {
    r -= weights.inf(dit);
    if (r <= 0) break;
152
  }
153
  if (dit == nil) return NULL;
154

155
  tb_pclass *chosen_class = weights.key(dit);
156

157 158
  // Take the first node of the correct type from the class.
  pnode = chosen_class->members.access(vtype)->front();
159
  
160 161
  total -= weights.inf(dit);
  weights.del_item(dit);
162
  
163 164 165
#ifdef PCLASS_DEBUG_MORE
  cout << "choose_pnode = [" << chosen_class->name << "] = "
       << ((pnode == NULL) ? string("NULL"):pnode->name) << endl;
166
#endif
167 168
  
  return pnode;
169
}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/*
 * The workhorse of our program.
 *
 * Assign performs an assignment of the virtual nodes (vnodes) to
 * nodes in the physical topology.
 *
 * The input virtual topology is the graph G (global)
 * the input physical topology is the topology topo (global).
 *
 * The simulated annealing logic is contained herein,
 * except for the "accept a bad change" computation,
 * which is performed in accept().
 */

int assign()
185
{
186 187 188
  float newscore, bestscore;
  node n;
  int iters = 0;
Mac Newbold's avatar
Mac Newbold committed
189

190 191 192
  float timestart = used_time();
  float timeend;
  float scorediff;
Mac Newbold's avatar
Mac Newbold committed
193

194 195 196 197 198
  nnodes = G.number_of_nodes();
 
  float cycles = CYCLES*(float)(nnodes + G.number_of_edges());

  float optimal = OPTIMAL_SCORE(G.number_of_edges(),nnodes);
Mac Newbold's avatar
Mac Newbold committed
199 200 201
#ifdef STATS
  cout << "STATS_OPTIMAL = " << optimal << endl;
#endif
202
  
Mac Newbold's avatar
Mac Newbold committed
203 204
  int mintrans = (int)cycles;
  int trans;
205 206 207 208
  int naccepts = 20*nnodes;
  int accepts = 0;
  int oldpos;

Mac Newbold's avatar
Mac Newbold committed
209
  int bestviolated;
210 211
  int absbestv;
  
212
  int num_fixed=0;
213 214

  float temp = init_temp;
215 216 217

#ifdef VERBOSE
  cout << "Initialized to cycles="<<cycles<<" optimal="<<optimal<<" mintrans="
218
       << mintrans<<" naccepts="<<naccepts<<" nnodes="<<nnodes<<"\n";
219
#endif
220 221
  
  
Mac Newbold's avatar
Mac Newbold committed
222 223 224
  /* Set up the initial counts */
  init_score();

225
  /* Set up fixed nodes */
226 227 228 229 230 231
  dic_item fixed_it;
  
  forall_items(fixed_it,fixed_nodes) {
    if (vname2node.lookup(fixed_nodes.key(fixed_it)) == nil) {
      cerr << "Fixed node: " << fixed_nodes.key(fixed_it)
	   << " does not exist.\n",
232 233
      exit(1);
    }
234 235 236 237
    node vn = vname2node.access(fixed_nodes.key(fixed_it));
    if (pname2node.lookup(fixed_nodes.inf(fixed_it)) == nil) {
      cerr << "Fixed node: " << fixed_nodes.inf(fixed_it)
	   << " does not exist.\n",
238 239
      exit(1);
    }
240 241 242 243
    node pn = pname2node.access(fixed_nodes.inf(fixed_it));
    int ppos = pnode2posistion.access(&PG[pn]);
    if (G[vn].vclass != NULL) {
      cerr << "Can not have fixed nodes be in a vclass!\n";
244 245
      exit(1);
    }
246 247 248
    if (add_node(vn,ppos) == 1) {
      cerr << "Fixed node: Could not map " << fixed_nodes.key(fixed_it)
	   << " to " << fixed_nodes.inf(fixed_it) << ".\n";
249 250
      exit(1);
    }
251 252
    unassigned_nodes.del(vn);
    G[vn].fixed=true;
253
    num_fixed++;
254
  }
255
  
Mac Newbold's avatar
Mac Newbold committed
256 257
  bestscore = get_score();
  bestviolated = violated;
258 259
#ifdef VERBOSE
  cout << "Problem started with score "<<bestscore<<" and "<< violated
260
       << " violations.\n";
261
#endif
Mac Newbold's avatar
Mac Newbold committed
262
  absbest = bestscore;
263 264 265 266 267
  absbestv = bestviolated;
  node n3;
  forall_nodes(n3, G) {
    absnodes[n3] = G[n3].posistion;
    abstypes[n3] = G[n3].type;
268 269 270 271 272 273
  }


  if (num_fixed == nnodes) {
    cout << "All nodes are fixed.  No annealing." << endl;
    goto DONE;
Mac Newbold's avatar
Mac Newbold committed
274 275
  }

276
  while (temp >= temp_stop) {
Mac Newbold's avatar
Mac Newbold committed
277
#ifdef VERBOSE
278
    cout << "Temperature:  " << temp << " AbsBest: " << absbest << " (" << absbestv << ")" << endl;
Mac Newbold's avatar
Mac Newbold committed
279 280 281 282
#endif
    trans = 0;
    accepts = 0;

283
    while (trans < mintrans && accepts < naccepts) {
Mac Newbold's avatar
Mac Newbold committed
284 285 286
#ifdef STATS
      cout << "STATS temp:" << temp << " score:" << get_score() <<
	" violated:" << violated << " trans:" << trans <<
287 288 289
	" accepts:" << accepts << endl;
#endif STATS
      int newpos=0;
Mac Newbold's avatar
Mac Newbold committed
290 291 292
      trans++;
      iters++;

293 294 295 296
      n = unassigned_nodes.find_min();
      while (n == nil) {
	n = G.choose_node();
	if (G[n].fixed) n=nil;
297
      }
298

299 300 301
      // Note: we have a lot of +1's here because of the first
      // node loc in pnodes is 1 not 0.
      oldpos = G[n].posistion;
302

303 304 305 306
      if (oldpos != 0) {
	remove_node(n);
	unassigned_nodes.insert(n,random());
      }
307

308 309 310 311 312 313 314
      tb_vnode &vn=G[n];

      if (vn.vclass != NULL) {
	vn.type = vn.vclass->choose_type();
#ifdef SCORE_DEBUG
	cerr << "vclass " << vn.vclass->name  << ": choose type = "
	     << vn.type << " dominant = " << vn.vclass->dominant << endl;
315
#endif
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
      }
      
      tt_entry tt = type_table.access(vn.type);
      int num_types = tt.first();
      pclass_array &acceptable_types = *(tt.second());

      // Loop will break eventually.
      tb_pnode *newpnode;
      int i = random()%num_types;
      int first = i;
      bool found_pclass = true;
      for (;;) {		// breaks loop in a number of places
	i = (i+1)%num_types;
	newpnode = acceptable_types[i]->members.access(vn.type)->front();
#ifdef PCLASS_DEBUG
	cerr << "Found pclass: " <<
	  acceptable_types[i]->name << " and node " <<
	  (newpnode == NULL ? string("NULL") : newpnode->name) << "\n";
#endif	
	if (newpnode != NULL) {
	  newpos = pnode2posistion.access(newpnode);
	  if (add_node(n,newpos) == 0) break; // main exit condition
Mac Newbold's avatar
Mac Newbold committed
338
	}
339 340 341 342 343 344 345 346 347 348 349 350 351
	
	if (i == first) {
	  // no available nodes
	  // need to free up a node and recalculate weights.
	  int pos = 0;
	  node ntor;
	  while (pos == 0) {
	    ntor=nil;
	    while (ntor == nil) {
	      ntor = G.choose_node();
	      if (G[ntor].fixed) ntor=nil;
	    }
	    pos = G[ntor].posistion;
352
	  }
353 354 355 356
	  remove_node(ntor);
	  unassigned_nodes.insert(ntor,random());
	  found_pclass = false;
	  break;
357
	}
358
      } 
Mac Newbold's avatar
Mac Newbold committed
359

360 361
      // This occurs when no pclass could be found.
      if (found_pclass == false) continue;
Mac Newbold's avatar
Mac Newbold committed
362

363 364
      unassigned_nodes.del(n);
      
365 366 367
      newscore = get_score();
      
      // Negative means bad
Mac Newbold's avatar
Mac Newbold committed
368
      scorediff = bestscore - newscore;
369
      
370 371 372 373
      // tinkering aournd witht his.
      if ((newscore < optimal) || (violated < bestviolated) ||
	  ((violated == bestviolated) && (newscore < bestscore)) ||
	  accept(scorediff*((bestviolated - violated)/2), temp)) {
Mac Newbold's avatar
Mac Newbold committed
374 375 376
	bestscore = newscore;
	bestviolated = violated;
	accepts++;
377 378
	if ((violated < absbestv) ||
	    ((violated == absbestv) &&
Mac Newbold's avatar
Mac Newbold committed
379
	     (newscore < absbest))) {
380 381 382 383
	  node n2;
	  forall_nodes(n2, G) {
	    absnodes[n2] = G[n2].posistion;
	    abstypes[n2] = G[n2].type;
Mac Newbold's avatar
Mac Newbold committed
384 385
	  }
	  absbest = newscore;
386 387
	  absbestv = violated;
	  cycles_to_best = iters;
Mac Newbold's avatar
Mac Newbold committed
388 389
	}
	if (newscore < optimal) {
390 391 392 393
	  timeend = used_time(timestart);
	  cout << "OPTIMAL ( " << optimal << ") in "
	       << iters << " iters, "
	       << timeend << " seconds" << endl;
Mac Newbold's avatar
Mac Newbold committed
394 395
	  goto DONE;
	}
396 397 398
	// Accept change
      } else {
	// Reject change
399 400 401
	remove_node(n);
	if (oldpos != 0) {
	  add_node(n,oldpos);
Mac Newbold's avatar
Mac Newbold committed
402 403 404
	}
      }
    }
405 406 407 408 409 410
    temp *= temp_rate;
  }
  cout << "Done.\n";
  
 DONE:
  bestscore = absbest;
Mac Newbold's avatar
Mac Newbold committed
411

412 413 414 415 416 417 418 419 420
  forall_nodes(n, G) {
    if (G[n].posistion != 0)
      remove_node(n);
  }
	
  forall_nodes(n, G) {
    if (absnodes[n] != 0) {
      if (G[n].vclass != NULL) {
	G[n].type = abstypes[n];
421
      }
422 423 424
      assert(G[n].type == abstypes[n]);
      if (add_node(n,absnodes[n]) != 0) {
	cerr << "Invalid assumption.  Tell calfeld that reseting to best configuration doesn't work" << endl;
425 426
      }
    } else {
427
      cout << "Unassigned node: " << G[n].name << endl;
Mac Newbold's avatar
Mac Newbold committed
428
    }
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  }
  
  timeend = used_time(timestart);
  printf("   BEST SCORE:  %.2f",get_score());
  cout << " in " << iters << " iters and " << timeend << " seconds" << endl;
  cout << "With " << violated << " violations" << endl;
  cout << "With " << accepts << " accepts of increases\n";
  cout << "Iters to find best score:  " << cycles_to_best << endl;
  cout << "Violations: " << violated << endl;
  cout << "  unassigned: " << vinfo.unassigned << endl;
  cout << "  pnode_load: " << vinfo.pnode_load << endl;
  cout << "  no_connect: " << vinfo.no_connection << endl;
  cout << "  link_users: " << vinfo.link_users << endl;
  cout << "  bandwidth:  " << vinfo.bandwidth << endl;
  cout << "  desires:    " << vinfo.desires << endl;
Mac Newbold's avatar
Mac Newbold committed
444

445 446
  return 0;
}
Mac Newbold's avatar
Mac Newbold committed
447

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*
 * A legacy function from a less general version of the program.
 *
 * Now simply resets the node assignment, performs a new assignment,
 * and prints out the results.
 *
 */
void loopassign()
{
  node_array<int> nodestorage;
  int optimal = 0;
  float timestart = used_time();
  float totaltime;

  nodestorage.init(G, 0);
  absnodes.init(G, 0);
  abstypes.init(G, "");
Mac Newbold's avatar
Mac Newbold committed
465
    
466 467 468
  nnodes = G.number_of_nodes();
  optimal = assign();
  totaltime = used_time(timestart);
Mac Newbold's avatar
Mac Newbold committed
469

470 471 472 473 474 475
  if (violated > 0) {
    cout << "violations: " << violated << endl;
  }
  cout << "Total time to find solution "
       << totaltime << " seconds" << endl;
}
Mac Newbold's avatar
Mac Newbold committed
476

477 478 479 480
/*
 * If we have more ways of partitioning the graph other than just
 * simulated annealing, throw them in here.
 */
Mac Newbold's avatar
Mac Newbold committed
481

482 483 484 485 486 487 488 489 490 491
void chopgraph() {
  switch(partition_mechanism) {
  case PARTITION_BY_ANNEALING:
    loopassign();
    break;
  default:
    cerr << "Unknown partition mechanism.  eeeek." << endl;
    exit(-1);
  }
}
492

493 494 495 496 497 498
void batch()
{
  absnodes.init(G, 0);
  abstypes.init(G, "");
  chopgraph();
}
499 500


501 502 503 504 505 506 507 508 509 510 511 512
void usage() {
  fprintf(stderr,
	  "usage:  assign [-h] [-bao] [-s <switches>] [-n nodes/switch] [-c cap] [file]\n"
	  "           -h ...... brief help listing\n"
	  //	  "           -s #  ... number of switches in cluster\n"
	  //	  "           -n #  ... number of nodes per switch\n"
	  "           -a ...... Use simulated annealing (default)\n"
	  "           -o ...... Update on-line (vs batch, default)\n"
	  "           -t <file> Input topology desc. from <file>\n"
	  "           -b ...... batch mode (no gui)\n"
	  );
}
513

514 515 516 517 518 519 520 521 522
int mst_comp(const edge &A,const edge &B)
{
  edge pA,pB;
  pA = SG[A].mate;
  pB = SG[B].mate;
  // Highbandwidth = low score
  if (PG[pA].bandwidth > PG[pB].bandwidth) return -1;
  if (PG[pA].bandwidth < PG[pB].bandwidth) return 1;
  return 0;
523 524
}

Mac Newbold's avatar
Mac Newbold committed
525 526
void print_solution()
{
527 528
  node n;
  cout << "Best solution: " << absbest << endl;
Mac Newbold's avatar
Mac Newbold committed
529
  cout << "Nodes:" << endl;
530 531 532
  forall_nodes(n,G) {
    if (!G[n].posistion) {
      cout << "unassigned: " << G[n].name << endl;
Mac Newbold's avatar
Mac Newbold committed
533
    } else {
534 535 536 537 538 539 540 541 542
      node pnode = pnodes[G[n].posistion];
      tb_pnode &pnoder = PG[pnode];
      cout << G[n].name << " ";
      if (pnoder.the_switch) {
	cout << PG[pnoder.the_switch].name;
      } else {
	cout << "NO_SWITCH";
      }
      cout << " " << pnoder.name << endl;
Mac Newbold's avatar
Mac Newbold committed
543 544 545 546
    }
  }
  cout << "End Nodes" << endl;
  cout << "Edges:" << endl;
547 548 549 550 551 552 553 554 555 556 557 558 559 560
  edge e;
  forall_edges(e,G) {
    tb_vlink &v = G[e];
    cout << G[e].name;
    if (v.type == tb_vlink::LINK_DIRECT) {
      tb_plink &p = PG[v.plink];
      cout << " direct " << p.name << " (" <<
	p.srcmac << "," << p.dstmac << ")" << endl;
    } else if (v.type == tb_vlink::LINK_INTRASWITCH) {
      tb_plink &p = PG[v.plink];
      tb_plink &p2 = PG[v.plink_two];
      cout << " intraswitch " << p.name << " (" <<
	p.srcmac << "," << p.dstmac << ") " <<
	p2.name << " (" << p2.srcmac << "," << p2.dstmac <<
Mac Newbold's avatar
Mac Newbold committed
561
	")" << endl;
562
    } else if (v.type == tb_vlink::LINK_INTERSWITCH) {
563
      cout << " interswitch ";
564 565 566 567 568 569 570
      edge e;
      tb_plink &lp = PG[v.plink_local_one];
      tb_plink &lp2 = PG[v.plink_local_two];
      cout << lp.name << " (" << lp.srcmac << "," << lp.dstmac << ")";
      forall(e,v.path) {
	tb_plink &p = PG[e];
	cout << " " << p.name << " (" << p.srcmac << "," << p.dstmac << ")";
Mac Newbold's avatar
Mac Newbold committed
571
      }
572 573 574
      cout << " " << lp2.name << " (" << lp2.srcmac << "," <<
	lp2.dstmac << ")" << endl;
    } else if (v.type == tb_vlink::LINK_TRIVIAL) {
575
      cout << " trivial" << endl;
Mac Newbold's avatar
Mac Newbold committed
576
    } else {
577
      cout << "Unknown link type" << endl;
Mac Newbold's avatar
Mac Newbold committed
578 579 580 581 582 583
    }
  }
  cout << "End Edges" << endl;
  cout << "End solution" << endl;
}

584 585 586 587 588 589 590
int main(int argc, char **argv)
{
  extern char *optarg;
  extern int optind;
  char *topofile = NULL;
    
  int ch;
591

592
  partition_mechanism = PARTITION_BY_ANNEALING;
Christopher Alfeld's avatar
Christopher Alfeld committed
593

594 595 596 597 598 599 600 601 602
  while ((ch = getopt(argc, argv, "boas:n:t:h")) != -1)
    switch(ch) {
    case 'h': usage(); exit(0);
      //		case 's': nparts = atoi(optarg); break;
    case 'a': partition_mechanism = PARTITION_BY_ANNEALING; break;
    case 'o': on_line = 1; break;
    case 't': topofile = optarg; break;
    case 'b': batch_mode = 1; break;
    default: usage(); exit(-1);
Mac Newbold's avatar
Mac Newbold committed
603
    }
604

Mac Newbold's avatar
Mac Newbold committed
605 606 607
  argc -= optind;
  argv += optind;

608 609 610 611 612
  /* newbold@cs
     These relate to the globals defined in common.h
     It reads in all the parameters for the program that were formerly
     all hardcoded constants.
  */
613 614
  parse_options(argv, options, noptions);
#ifdef SCORE_DEBUG
615
  dump_options("Configuration options:", options, noptions);
616 617
#endif

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  int seed;
  if (getenv("ASSIGN_SEED") != NULL) {
    sscanf(getenv("ASSIGN_SEED"),"%d",&seed);
  } else {
    seed = time(NULL)+getpid();
  }
  printf("seed = %d\n",seed);
  srandom(seed);

  /*
   * Allow the user to specify a topology in ".top" format.
   */

  if (argc >= 1) {
    ifstream infile;
    infile.open(argv[0]);
    if (!infile || !infile.good()) {
      cerr << "Error opening file: " << argv[0] << endl;
      exit(-11);
    }
    cout << "Parsing top\n";
    parse_top(G, infile);
  }

  /*
   * Allow the user to specify a physical topology
   * in .phys format.  Fills in the "topo" global variable.
   * Make no mistake:  This is actually mandatory now.
   */
  if (topofile != NULL) {
    cout << "Parsing ptop\n";
    ifstream ptopfile;
    ptopfile.open(topofile);
    if (!ptopfile || !ptopfile.good()) {
      cerr << "Error opening file: " << topofile << endl;
      exit(-1);
    }
    nparts = parse_ptop(PG,SG,ptopfile);
    cout << "Nparts: " << nparts << endl;

    cout << "Type Precheck" << endl;
    string curtype;
    int ok=1;
    forall (curtype,vtypes) {
      if (ptypes.search(curtype) == nil) {
	cout << "  No physical nodes of type " << curtype << endl;
	ok=0;
      }
    }
    if (! ok) exit(-1);
    
    cout << "Initializing data structures." << endl;
    edge_costs.init(SG);
    switch_distances.init(SG);
    switch_preds.init(SG);
    cout << "Calculating shortest paths on switch fabric." << endl;
    edge ed;
    forall_edges(ed,SG) {
      edge_costs[ed] = 100000000-PG[SG[ed].mate].bandwidth;
#ifdef SCORE_DEBUG
      cerr << "  " << PG[SG[ed].mate].name << " " << edge_costs[ed] << endl;
679
#endif
680 681

    }
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
    node sw;
    forall_nodes(sw,SG) {
      switch_distances[sw].init(SG);
      switch_preds[sw].init(SG);
      DIJKSTRA_T(SG,sw,edge_costs,
		 switch_distances[sw],switch_preds[sw]);
#ifdef SCORE_DEBUG
      cerr << "Source " << PG[SG[sw].mate].name << endl;
      node dsw;
      forall_nodes(dsw,SG) {
	cerr << "  " << PG[SG[dsw].mate].name;
	int dist = switch_distances[sw][dsw];
	cerr << "  dist " << dist;
	edge de = switch_preds[sw][dsw];
	if (de == nil) {
	  cerr << "  pred nil" << endl;
	} else {
	  cerr << "  pred " << PG[SG[de].mate].name << endl;
	}
701 702
      }
#endif
703
    }
704
  }
705

706 707 708
  node pn;
  forall_nodes(pn,PG) {
    pnode2node.insert(&PG[pn],pn);
709
  }
710 711 712 713
  for (int i=0;i<MAX_PNODES;++i) {
    if (pnodes[i] != nil) {
      pnode2posistion.insert(&PG[pnodes[i]],i);
    }
714
  }
Mac Newbold's avatar
Mac Newbold committed
715

716 717 718
  cout << "Generating physical classes\n";
  generate_pclasses(PG);
  cout << "Nclasses: " << pclasses.length() << endl;
719

720 721 722
#ifdef PCLASS_DEBUG
  pclass_debug();
#endif
723
  
724 725
  cout << "Annealing!" << endl;
  batch();
726

727 728 729 730
  print_solution();
    
  return 0;
}