assign.cc 19.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <LEDA/graph_alg.h>
#include <LEDA/graphwin.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "testbed.h"

16 17
#include "phys.h"
topology *topo = NULL;
18
tbgraph PG(1,1);
19 20 21
/* How can we chop things up? */
#define PARTITION_BY_ANNEALING 0

22 23
#define MAX_DELAYS 64

24
int nparts = 3;     /* DEFAULTS */
25
int *intercap = NULL;
David G Andersen's avatar
David G Andersen committed
26
int *nodecap = NULL;
27 28 29 30 31
int better_heuristic = 0;
int accepts = 0;
int nnodes = 0;
int partition_mechanism;
int on_line = 0;
32
int cycles_to_best = 0;
Christopher Alfeld's avatar
Christopher Alfeld committed
33
int batch_mode = 0;
34 35 36 37 38 39 40 41 42 43

float sensitivity = .1;

static const int initial_temperature = 100;
static const int temp_prob = 130;

int refreshed = 0;

tbgraph G(1, 1);
node_array<int> bestnodes, absnodes;
44
node_array<toponode*> physnodes,absphys;
45 46 47
float                       bestscore, absbest;

float *interlinks;
48
int *numnodes;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

/*
 * Basic simulated annealing parameters:
 *
 * Make changes proportional to T
 * Accept worse solution with p = e^(change/Temperature*sensitivity)
 *
 */

inline int accept(float change, float temperature)
{
	float p;
	int r;

	if (change == 0) {
		p = 1000 * temperature / temp_prob;
	} else {
		p = expf(change/(temperature*sensitivity)) * 1000;
	}
	r = random() % 1000;
	if (r < p) {
		accepts++;
		return 1;
	}
	return 0;
}

David G Andersen's avatar
David G Andersen committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
/*
 * score performs two functions at the moment.  First, it actually
 * performs an assignment of the virtual nodes in a switch to
 * physical nodes in that switch.  Then it returns a numeric evaluation
 * of the current mapping.
 *
 * There are several problems it solves:
 *
 *  a)  Assigning generic vnodes to nodes.
 *
 *      This is performed by searching a list of the nodes and assigning
 *      the virtual node to a node with the minimum necessary number
 *      of interfaces.
 *
 *  b)  Assigning delay vnodes to delay nodes:
 *
 *      Since a delay node may support many delay vnodes, this is a
 *      unit weight knapsack problem.  I take the easy out here
 *      by simply assigning the delay vnodes to nodes as they
 *      come along in the list.  This works for finding a feasible
 *      solution, but does not optimize the use of physical nodes.
 *
 *  c)  Scoring
 *
 *      Scoring is handled by:
 *       i)  add 1 for each unassigned node (excess nodes or too many
 *           interfaces
 *      ii)  Add 1 for each link across switches in excess of the
 *           capacity
 *     iii)  Add .1 for each switch used to try to minimize the number
 *           of switches involved
 *      iv)  Add .1 for each unit of bandwidth between switches to try
 *           to minimize interswitch bandwidth consumption
 *
 *
 *  The score function is the big bottleneck of the program right
 *  now.  Unlike the earlier versions in which only changed nodes
 *  were updated, score recomputes the entire solution each time
 *  it's called.  This is very inefficient and should be fixed
 *  by having the scupdate function update only the parts of the
 *  topology which changed.  This needs to be looked at more,
 *  but should probably be delayed until the rest of the features
 *  have been added.
 */

121 122 123 124 125 126
float score()
{

	float sc = 0;

	for (int i = 0; i < nparts; i++) {
127 128 129 130 131 132 133 134 135 136 137 138 139
		/* XXX:  THIS MUST BE OPTIMIZED */
		
		/* Experimental:  Collapse delay nodes together
		 * and handle node fanout restrictions */

		/* Mark all nodes unused */
		for (int j = 0; j < topo->switches[i]->numnodes(); j++) {
			topo->switches[i]->nodes[j].used = 0;
		}
		
		int numdelays = 0;
		int assigned = 0;
		node n;
140
		node delays[MAX_DELAYS];
141 142 143
		forall_nodes(n, G) {
		    if (G[n].partition() == i) {
		        if (G[n].type() == testnode::TYPE_DELAY) {
144
				delays[numdelays++]=n;
145 146 147 148 149 150 151 152
			} else {
			    assigned = 0;
			    /* Assign to an available node */
			    for (int j = 0;
				 j < topo->switches[i]->numnodes();
				 j++) {
				    if (topo->switches[i]->nodes[j].used == 0 && (topo->switches[i]->nodes[j].ints >= G.degree(n))) {
					    topo->switches[i]->nodes[j].used = 1;
153
					    physnodes[n]=&(topo->switches[i]->nodes[j]);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
					    assigned = 1;
					    break;
				    }
			    }
			    if (!assigned) {
				    sc += 1;
			    }
			}
		    }
		}
		int numn = numnodes[i];

		numn -= numdelays;
		/* Now turn normal nodes into delay nodes as needed */
		/* XXX:  THIS IS NOT OPTIMAL.  We should improve on this
		 * so it uses the right size nodes a bit... but oh well */
		int maxnodes = topo->switches[i]->numnodes();
		int j = 0;
		while (numdelays > 0 && j < maxnodes) {
173
			if (topo->switches[i]->nodes[j].used) { j++; continue; }
174 175 176
			for (int z=0;numdelays>0 && z<topo->switches[i]->nodes[j].ints/2;++z) {
				physnodes[delays[--numdelays]] = &(topo->switches[i]->nodes[j]);
			}
177 178 179 180 181 182 183 184 185
			topo->switches[i]->nodes[j].used = 1;
			j++;
		}

		/* Add in the unsatisfied delay nodes */
		if (numdelays > 0) {
			sc += numdelays;
		}

David G Andersen's avatar
David G Andersen committed
186 187 188 189 190 191 192 193
		/* Try to minimize the number of switches used */
		/* This is likely NOT an effective way to do it! */
		if (numnodes[i] > 0) {
			sc += .1;
		}
		/* Try to minimize the bandwidth used... also probably
		   not effective */
		if (interlinks[i] > 0) {
194 195 196 197 198 199
			sc += .001 * interlinks[i];
		}
		/* Have we violated bandwidth between switches? */
		
		if (interlinks[i] > intercap[i]) {
			sc += (interlinks[i]-intercap[i])/100;
200 201 202 203 204
		}
	}
	return sc;
}

David G Andersen's avatar
David G Andersen committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*
 * This is a completely bogus function.  It's a straight copy of the
 * score() function, but instead of incrementing a score counter,
 * it prints out a list of the constraints which are violated.
 *
 * In the future, this function should return something allowing us
 * to determine if a critical resource (nodes, interfaces, etc.) is
 * violated, or if a non-critical resource (interswitch bandwidth)
 * has been violated, so we can allow the user to proceed with a
 * potentially bad configuration if they so desire.
 *
 * ... and the "coding by copy" junk should be eliminated.  Jeez. :)
 */

219 220 221 222
void violated()
{
	for (int i = 0; i < nparts; i++) {
		/* Have we violated bandwidth between switches? */
223
		if (interlinks[i] > intercap[i]) {
224 225 226 227 228 229 230 231 232 233 234
			cout << "violated:  switch " << i << " bandwidth"
			     << endl;
		}
		for (int j = 0; j < topo->switches[i]->numnodes(); j++) {
			topo->switches[i]->nodes[j].used = 0;
		}
		
		int numdelays = 0;
		int assigned = 0;
		int unassigned = 0;
		node n;
235
		node delays[MAX_DELAYS];
236 237 238
		forall_nodes(n, G) {
		    if (G[n].partition() == i) {
		        if (G[n].type() == testnode::TYPE_DELAY) {
239
				delays[numdelays++]=n;
240 241 242 243 244 245 246 247 248
			} else {
			    /* Assign to an available node */
			    assigned = 0;
			    for (int j = 0;
				 j < topo->switches[i]->numnodes();
				 j++) {
				    if ((topo->switches[i]->nodes[j].used == 0) && (topo->switches[i]->nodes[j].ints >= G.degree(n))) {
					    topo->switches[i]->nodes[j].used = 1;
					    assigned = 1;
249
	  				    physnodes[n]=&(topo->switches[i]->nodes[j]);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
					    break;
				    }
			    }
			    if (!assigned) {
				    unassigned++;
			    }
			}
		    }
		}
		if (unassigned > 0) {
			cout << "violated:  switch " << i << " had "
			     << unassigned << " unassigned nodes" << endl;
		}
		int numn = numnodes[i];

		numn -= numdelays;
		/* Now turn normal nodes into delay nodes as needed */
		/* XXX:  THIS IS NOT OPTIMAL.  We should improve on this
		 * so it uses the right size nodes a bit... but oh well */
		int maxnodes = topo->switches[i]->numnodes();
		int j = 0;
		while (numdelays > 0 && j < maxnodes) {
272
			if (topo->switches[i]->nodes[j].used) { j++; continue; }
273 274 275 276
			for (int z=0;numdelays>0 && z<topo->switches[i]->nodes[j].ints/2;++z) {
				// XXX: ugh
				physnodes[delays[--numdelays]] = &(topo->switches[i]->nodes[j]);
			}
277 278 279 280 281 282 283 284 285 286 287 288 289 290
			topo->switches[i]->nodes[j].used = 1;
			j++;
		}

		/* Add in the unsatisfied delay nodes */
		if (numdelays > 0) {
			cout << "violated:  switch " << i << " had "
			     << numdelays << " unassigned delay nodes"
			     << endl;
		}
	}
}


David G Andersen's avatar
David G Andersen committed
291 292 293 294 295
/*
 * Reset the interlinks and numnodes arrays to an accurate
 * value.  Requires an inspection of all nodes and edges.
 */

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
void screset() {
	edge e;
	node n;
	for (int i = 0; i < nparts; i++) {
		interlinks[i] = numnodes[i] = 0;
	}
    
	forall_nodes(n, G) {
		numnodes[G[n].partition()]++;
	}
	forall_edges(e, G) {
		node v = G.source(e);
		node w = G.target(e);
	
		if (G[v].partition() != G[w].partition()) {
311 312
			interlinks[G[v].partition()] += G[e].capacity();
			interlinks[G[w].partition()] += G[e].capacity();
313 314 315 316
		}
	}
}

David G Andersen's avatar
David G Andersen committed
317 318 319 320 321 322 323 324 325 326 327
/*
 * Move a node 'n' from its current switch to a new one, indicated by
 * newpos.
 *
 * Right now, this function performs the update logic to change the
 * node counts and the interswitch bandwidth, but does not cope
 * with the rest of the score.  In the future, it should perform
 * the incremental score update.  See the comments for the score()
 * function for more details.
 */

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
void scupdate(node n, int newpos)
{
	int prevpos;

	AdjIt it(G, n);
	prevpos = G[n].partition();
	if (newpos == prevpos) return;

	numnodes[prevpos]--;
	numnodes[newpos]++;

	while (it.eol() == false) {
		edge e = it.get_edge();
		node n1 = G.source(e);
		node n2 = G.target(e);
		/* Ensure that n2 points to the stationary node */
		if (n2 == n) n2 = n1;

		/* They were not in the same bucket to start with */
		/* So both contributed to their interlinks */
		if (G[n2].partition() != prevpos) {
349
			interlinks[prevpos] -= G[e].capacity();
350 351 352

			/* If they're together now, there's no interlink */
			if (G[n2].partition() == newpos) {
353
				interlinks[G[n2].partition()] -= G[e].capacity();
354
			} else { /* Otherwise, move the interlink */
355
				interlinks[newpos] += G[e].capacity();
356 357 358 359 360
			}
		}
		else /* They were in the same bucket.  They aren't anymore,
		      * or we would have exited earlier */
		{
361 362
			interlinks[G[n2].partition()] += G[e].capacity();
			interlinks[newpos] += G[e].capacity();
363 364 365 366 367 368 369
		}
		++it;
	}

	G[n].partition(newpos);
}

David G Andersen's avatar
David G Andersen committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383
/*
 * 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().
 */

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
int assign()
{
	float newscore, bestscore, absbest;
	node n;
	int iters = 0;

	float timestart = used_time();
	float timeend;
	float scorediff;

	nnodes = G.number_of_nodes();
 
	float cycles = 120.0*(float)(nnodes + G.number_of_edges());

	int mintrans = (int)cycles;
	int trans;
	int naccepts = 40*nnodes;
	int accepts = 0;
	int oldpos;

	float temp = initial_temperature;
  
	/* Set up the initial counts */
	screset();

	bestscore = score();
	absbest = bestscore;

	if (bestscore == 0) {
#ifdef VERBOSE
		cout << "Problem started optimal\n";
#endif
		return 1;
	}
  
	while (temp >= 2) {
#ifdef VERBOSE
		cout << "Temperature:  " << temp << endl;
#endif
		trans = 0;
		accepts = 0;
      
		while (trans < mintrans && accepts < naccepts) {
			int newpos;
			trans++;
			iters++;
			n = G.choose_node();
			oldpos = G[n].partition();

			newpos = oldpos;
			/* XXX:  Room for improvement. :-) */
			while (newpos == oldpos)
				newpos = random() % nparts;
			scupdate(n, newpos);
			newscore = score();
			if (newscore < 0.1f) {
				timeend = used_time(timestart);
				cout << "OPTIMAL (0.0) in "
				     << iters << " iters, "
				     << timeend << " seconds" << endl;
				return 1;
			}
			/* So it's negative if bad */
			scorediff = bestscore - newscore;

			if (newscore < bestscore || accept(scorediff, temp)) {
				bestnodes[n] = G[n].partition();
				bestscore = newscore;
				accepts++;
				if (newscore < absbest) {
					node n2;
					forall_nodes(n2, G) {
						absnodes[n2] = G[n2].partition();
457
						absphys[n2] = physnodes[n2];
458 459
					}
					absbest = newscore;
460
					cycles_to_best = iters;
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
				}
			} else { /* Reject this change */
				scupdate(n, oldpos);
			}
		}
      
		temp *= .9;
	}
	forall_nodes(n, G) {
		bestnodes[n] = absnodes[n];
	}
	bestscore = absbest;

	forall_nodes(n, G) {
		G[n].partition(absnodes[n]);
	}
	timeend = used_time(timestart);
	cout << "   BEST SCORE:  " << score() << " in "
	     << iters << " iters and " << timeend << " seconds" << endl;
	cout << "With " << accepts << " accepts of increases\n";
481
	cout << "Iters to find best score:  " << cycles_to_best << endl;
482
#if 0
483
	for (int i = 0; i < nparts; i++) {
David G Andersen's avatar
David G Andersen committed
484
		if (numnodes[i] > nodecap[i]) {
485 486 487 488 489 490 491 492 493 494 495 496 497
			cout << "node " << i << " has "
			     << numnodes[i] << " nodes" << endl;
		}
		if (interlinks[i] > intercap) {
			cout << "node " << i << " has "
			     << interlinks[i] << " links" << endl;
		}
	}
	if (score() < 0.0001) {
		return 1; /* Optimal enough */
	} else {
		return 0;
	}
498 499
#endif
	return 0;
500 501
}

David G Andersen's avatar
David G Andersen committed
502 503 504 505 506 507 508 509
/*
 * 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.
 *
 */

510 511 512 513 514 515 516 517 518 519
void loopassign()
{
	node_array<int> nodestorage;
	int optimal = 0;
	float timestart = used_time();
	float totaltime;

	nodestorage.init(G, 0);
	bestnodes.init(G, 0);
	absnodes.init(G, 0);
520 521
	physnodes.init(G, 0);
	absphys.init(G, 0);
522 523 524 525
    
	nnodes = G.number_of_nodes();
	optimal = assign();
	totaltime = used_time(timestart);
526
	violated();
527 528 529 530
	cout << "Total time to find solution "
	     << totaltime << " seconds" << endl;
}

David G Andersen's avatar
David G Andersen committed
531 532 533 534 535
/*
 * If we have more ways of partitioning the graph other than just
 * simulated annealing, throw them in here.
 */

Christopher Alfeld's avatar
Christopher Alfeld committed
536
void chopgraph() {
537 538 539 540 541 542 543 544 545 546 547 548 549 550
	node n;
	forall_nodes(n, G) {
		G[n].partition(0);
	}
	switch(partition_mechanism) {
	case PARTITION_BY_ANNEALING:
		loopassign();
		break;
	default:
		cerr << "Unknown partition mechanism.  eeeek." << endl;
		exit(-1);
	}
}

David G Andersen's avatar
David G Andersen committed
551 552 553 554 555 556 557 558
/*
 * Something in the graph has changed!  Better redisplay.
 *
 * Performs the color assignment for whichever switch the
 * node belongs to and shows the inter-switch links as
 * dashed lines.
 */

559 560 561 562 563 564 565 566 567 568
void display_scc(GraphWin& gw)
{
	edge e;
	node n;
	
	if (!refreshed) {
		forall_nodes(n, G) {
			G[n].partition(0);
		}
		if (on_line)
Christopher Alfeld's avatar
Christopher Alfeld committed
569
			chopgraph();
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	}
	
	refreshed = 0;
	
	/* Now color them according to their partition */
	forall_nodes(n, G) {
		switch(G[n].partition()) {
		case 0:
			gw.set_color(n, black);
			break;
		case 1:
			gw.set_color(n, blue);
			break;
		case 2:
			gw.set_color(n, green);
			break;
		case 3:
			gw.set_color(n, red);
			break;
		case 4:
			gw.set_color(n, yellow);
			break;
		case 5:
			gw.set_color(n, violet);
			break;
		case 6:
			gw.set_color(n, cyan);
			break;
		case 7:
			gw.set_color(n, brown);
			break;
		case 8:
			gw.set_color(n, pink);
			break;
		case 9:
			gw.set_color(n, orange);
			break;
		case 10:
			gw.set_color(n, grey1);
			break;
		case 11:
			gw.set_color(n, grey3);
			break;
		}
	}
	
	forall_edges(e, G) {
		node v = G.source(e);
		node w = G.target(e);
		if (G[v].partition() == G[w].partition()) {
			gw.set_style(e, solid_edge);
		} else {
			gw.set_style(e, dashed_edge);
		}
	}
	gw.redraw();
}

David G Andersen's avatar
David G Andersen committed
628 629 630 631 632
/*
 * Someone clicked on the "reassign" button.
 * Reset and redisplay.
 */

633 634 635 636 637 638 639 640
void reassign(GraphWin& gw)
{
	node n;
	forall_nodes(n, G) {
		G[n].partition(0);
	}
	bestnodes.init(G, 0);
	absnodes.init(G, 0);
641 642
	physnodes.init(G, 0);
	absphys.init(G, 0);
Christopher Alfeld's avatar
Christopher Alfeld committed
643
	chopgraph();
644 645 646 647
	refreshed = 1;
	display_scc(gw);
}

Christopher Alfeld's avatar
Christopher Alfeld committed
648 649 650 651 652 653 654 655 656 657 658 659 660
// XXX : another code by copy
void batch()
{
	node n;
	forall_nodes(n, G) {
		G[n].partition(0);
	}
	bestnodes.init(G, 0);
	absnodes.init(G, 0);
	physnodes.init(G, 0);
	absphys.init(G, 0);
	chopgraph();
}
661 662 663 664 665 666 667 668

void new_edge_handler(GraphWin& gw, edge)  { display_scc(gw); }
void del_edge_handler(GraphWin& gw)        { display_scc(gw); }
void new_node_handler(GraphWin& gw, node)  { display_scc(gw); }
void del_node_handler(GraphWin& gw)        { display_scc(gw); }

void usage() {
	fprintf(stderr,
Christopher Alfeld's avatar
Christopher Alfeld committed
669
		"usage:  assign [-h] [-bao] [-s <switches>] [-n nodes/switch] [-c cap] [file]\n"
670 671 672 673 674
		"           -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"
675
		"           -t <file> Input topology desc. from <file>\n"
Christopher Alfeld's avatar
Christopher Alfeld committed
676
		"           -b ...... batch mode (no gui)\n"
677 678 679
		);
}

680 681 682 683 684 685 686 687
void print_solution()
{
	node n;
	cout << "Best solution: " << absbest << endl;
	forall_nodes(n,G) {
		if (!absphys[n]) {
			cout << "unassigned: " << G[n].name() << endl;
		} else {
688
			cout << G[n].name() << " " << topo->switches[absnodes[n]]->name << " " << PG[absphys[n]->n].name() << endl;
689 690 691 692 693
		}
	}
	cout << "End solution" << endl;
}

694 695 696 697 698
int main(int argc, char **argv)
{
	int h_menu;
	extern char *optarg;
	extern int optind;
699
	char *topofile = NULL;
700 701 702 703 704
    
	int ch;

	partition_mechanism = PARTITION_BY_ANNEALING;
    
Christopher Alfeld's avatar
Christopher Alfeld committed
705
	while ((ch = getopt(argc, argv, "boas:n:t:h")) != -1)
706 707 708 709 710
		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;
711
		case 't': topofile = optarg; break;
Christopher Alfeld's avatar
Christopher Alfeld committed
712
		case 'b': batch_mode = 1; break;
713 714 715 716 717 718 719
		default: usage(); exit(-1);
		}

	argc -= optind;
	argv += optind;
    
	interlinks = new float[nparts];
720
	numnodes = new int[nparts];
721 722 723 724 725 726
	for (int i = 0; i < nparts; i++) {
		interlinks[i] = 0;
		numnodes[i] = 0;
	}
    
	srandom(time(NULL) + getpid());
David G Andersen's avatar
David G Andersen committed
727 728 729 730 731 732

	/*
	 * Set up the LEDA graph window environment.  Whenever
	 * the user does anything to the graph, call the
	 * proper handler.
	 */
Christopher Alfeld's avatar
Christopher Alfeld committed
733 734
	// XXX: alas, we need this because all the GW stuff is not in
	// the same spot.
735
	GraphWin gw(G, "Flux Testbed:  Simulated Annealing");
Christopher Alfeld's avatar
Christopher Alfeld committed
736 737 738 739 740 741
	if (! batch_mode) {
		gw.set_init_graph_handler(del_edge_handler);
		gw.set_new_edge_handler(new_edge_handler);
		gw.set_del_edge_handler(del_edge_handler);
		gw.set_new_node_handler(new_node_handler);
		gw.set_del_node_handler(del_node_handler);
742
    
Christopher Alfeld's avatar
Christopher Alfeld committed
743 744 745
		gw.set_node_width(24);
		gw.set_node_height(24);
	}
David G Andersen's avatar
David G Andersen committed
746 747 748 749
	/*
	 * Allow the user to specify a topology in ".top" format.
	 */

750 751 752
	if (argc == 1) {
		ifstream infile;
		infile.open(argv[0]);
753
		if (!infile || !infile.good()) {
754 755
		  cerr << "Error opening file: " << argv[0] << endl;
		  exit(-11);
756
		}
757
		parse_top(G, infile);
Christopher Alfeld's avatar
Christopher Alfeld committed
758 759 760 761 762 763 764 765 766 767
		if (! batch_mode) {
			gw.update_graph();
			node n;
			forall_nodes(n, G) {
				if (G[n].name() == NULL) {
					G[n].name("");
				}
				gw.set_label(n, G[n].name());
				gw.set_position(n,
						point(random() % 200, random() % 200));
David G Andersen's avatar
David G Andersen committed
768
			}
769 770
		}
	}
771

David G Andersen's avatar
David G Andersen committed
772 773 774 775 776 777
	/*
	 * 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.
	 */
	
778
	if (topofile != NULL) {
779 780 781 782 783 784 785 786 787
		cout << "Parsing ptop\n";
		ifstream ptopfile;
		ptopfile.open(topofile);
		if (!ptopfile || !ptopfile.good()) {
		  cerr << "Error opening file: " << topofile << endl;
		  exit(-1);
		}
		parse_ptop(PG,ptopfile);
		topo=ptop_to_phys(PG);
788
		if (!topo) {
789
			cerr << "Could not convert ptop to phys: "
790 791 792 793
			     << topofile << endl;
			exit(-1);
		}
		nparts = topo->switchcount;
David G Andersen's avatar
David G Andersen committed
794 795
		cout << "Nparts: " << nparts << endl;
		nodecap = new int[nparts];
796
		intercap = new int[nparts];
David G Andersen's avatar
David G Andersen committed
797 798 799
		for (int i = 0; i < nparts; i++) {
			nodecap[i] = topo->switches[i]->numnodes();
		}
800 801 802
		for (int i = 0; i < nparts; i++) {
			intercap[i] = topo->switches[i]->bw;
		}
803
		topo->print_topo();
804
	}
805

Christopher Alfeld's avatar
Christopher Alfeld committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
	if (! batch_mode) {
		gw.display();
		
		gw.set_directed(false);
		
		gw.set_node_shape(circle_node);
		gw.set_node_label_type(user_label);
		
		h_menu = gw.get_menu("Layout");
		gw_add_simple_call(gw, reassign, "Reassign", h_menu);
		
		/* Run until the user quits.  Everything is handled by callbacks
		 * from LEDA's event loop from here on.                           */
		
		gw.edit();
	} else {
		batch();
	}
824 825

	print_solution();
826 827 828
    
	return 0;
}