score.cc 20 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * ASSUMPTIONS:
 *  1. Any switch can get to any other switch either directly
 *     or via at most one other switch (star formation).
 */

// Note on variable names: LEDA has generic 'edge' and 'node'.  When
// these are translated to 'tb_*' structures the variables end in
// r.  I.e. dst -> dstr.  dst is a node, and dstr is a tb_pnode or similar.

// Not sure we need all these LEDA includes.
#include <LEDA/graph_alg.h>
13
#include <LEDA/ugraph.h>
Mac Newbold's avatar
Mac Newbold committed
14 15 16 17
#include <LEDA/graphwin.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
18
#include <LEDA/sortseq.h>
Mac Newbold's avatar
Mac Newbold committed
19 20 21 22 23 24 25 26 27
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>

#include "common.h"
28
#include "vclass.h"
Mac Newbold's avatar
Mac Newbold committed
29 30
#include "virtual.h"
#include "physical.h"
Christopher Alfeld's avatar
 
Christopher Alfeld committed
31 32 33
#include "pclass.h"
#include "score.h"

Mac Newbold's avatar
Mac Newbold committed
34 35 36

#include "assert.h"

37 38 39
typedef node_array<edge> switch_pred_array;
extern node_array<switch_pred_array> switch_preds;

Mac Newbold's avatar
Mac Newbold committed
40 41 42 43 44 45 46 47 48 49
float score;			// The score of the current mapping
int violated;			// How many times the restrictions
				// have been violated.
node pnodes[MAX_PNODES];	// int->node map
				// pnodes[0] == NULL

violated_info vinfo;		// specific info on violations

extern tb_vgraph G;		// virtual graph
extern tb_pgraph PG;		// physical grpaph
50
extern tb_sgraph SG;		// switch fabric
Mac Newbold's avatar
Mac Newbold committed
51 52 53 54 55 56

edge direct_link(node a,node b);
void score_link(edge e,edge v,bool interswitch);
void unscore_link(edge e,edge v,bool interswitch);
edge find_link_to_switch(node n);
int find_intraswitch_path(node src,node dst,edge *first,edge *second);
57
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L);
Mac Newbold's avatar
Mac Newbold committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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

#ifdef SCORE_DEBUG_MORE
#define SADD(amount) fprintf(stderr,"SADD: %s = %.2f\n",#amount,amount);score+=amount
#define SSUB(amount) fprintf(stderr,"SSUB: %s = %.2f\n",#amount,amount);score-=amount
#else
#define SADD(amount) score += amount
#define SSUB(amount) score -= amount
#endif

/*
 * score()
 * Returns the score.
 */
float get_score() {return score;}

/*
 * init_score()
 * This initialized the scoring system.  It also clears all
 * assignments.
 */
void init_score()
{
#ifdef SCORE_DEBUG
  fprintf(stderr,"SCORE: Initializing\n");
#endif
  score=0;
  violated=0;
  vinfo.unassigned = vinfo.pnode_load = 0;
  vinfo.no_connection = vinfo.link_users = vinfo.bandwidth = 0;

  node n;
  edge e;
  forall_nodes(n,G) {
    tb_vnode &vn=G[n];
    vn.posistion=0;
    vn.no_connections=0;
    SADD(SCORE_UNASSIGNED);
    vinfo.unassigned++;
    violated++;
  }
  forall_edges(e,G) {
    tb_vlink &ve=G[e];
    ve.type=tb_vlink::LINK_UNKNOWN;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
101
    ve.plink=NULL;
Mac Newbold's avatar
Mac Newbold committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  }
  forall_nodes(n,PG) {
    tb_pnode &pn=PG[n];
    pn.typed=false;
    pn.current_load=0;
    pn.pnodes_used=0;
  }
  forall_edges(e,PG) {
    tb_plink &pe=PG[e];
    pe.bw_used=0;
    pe.users=0;
  }

  assert(pnodes[0] == NULL);
#ifdef SCORE_DEBUG
  fprintf(stderr,"  score = %.2f violated = %d\n",score,violated);
#endif
}

/*
 * void remove_node(node node)
 * This removes a virtual node from the assignments, adjusting
 * the score appropriately.
 */
void remove_node(node n)
{
  /* Find pnode assigned to */
  node pnode;

  tb_vnode &vnoder = G[n];
  pnode = pnodes[vnoder.posistion];
  tb_pnode &pnoder = PG[pnode];

#ifdef SCORE_DEBUG
136
  cerr <<  "SCORE: remove_node(" << vnoder.name << ")\n";
Mac Newbold's avatar
Mac Newbold committed
137 138 139 140 141
  fprintf(stderr,"       no_connections = %d\n",vnoder.no_connections);
#endif

  assert(pnode != NULL);

Christopher Alfeld's avatar
 
Christopher Alfeld committed
142 143 144 145 146
  pclass_unset(pnoder);

  // pclass
  if (pnoder.my_class->used == 0) {
#ifdef SCORE_DEBUG
147
    cerr << "  freeing pclass\n";
Christopher Alfeld's avatar
 
Christopher Alfeld committed
148 149 150
#endif
    SSUB(SCORE_PCLASS);
  }
151 152 153 154 155 156 157 158 159 160 161 162 163 164

  // vclass
  if (vnoder.vclass != NULL) {
    double score_delta = vnoder.vclass->unassign_node(vnoder.type);
#ifdef SCORE_DEBUG
    cerr << "  vclass unassign " << score_delta << endl;
#endif
    
    if (score_delta <= -1) {
      violated--;
      vinfo.vclass--;
    }
    SSUB(-score_delta*SCORE_VCLASS);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
165
  
Mac Newbold's avatar
Mac Newbold committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  edge e;
  tb_vlink *vlink;
  node vdst;
  tb_vnode *vdstr;
  node pdst;
  tb_pnode *pdstr;

  // remove the scores associated with each edge
  forall_inout_edges(e,n) {
    vlink=&G[e];
    vdst=G.target(e);
    if (vdst == n)
      vdst = G.source(e);
    vdstr=&G[vdst];
#ifdef SCORE_DEBUG
181
    cerr << "  edge to " << vdstr->name << endl;
Mac Newbold's avatar
Mac Newbold committed
182 183 184 185 186 187 188 189 190 191 192 193 194
#endif
    if (vdstr->posistion == 0) continue;
    if (vlink->type == tb_vlink::LINK_DIRECT) {
      // DIRECT LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   direct link\n");
#endif
      unscore_link(vlink->plink,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTERSWITCH) {
      // INTERSWITCH LINK
      pdst=pnodes[vdstr->posistion];
      pdstr=&PG[pdst];
#ifdef SCORE_DEBUG
195
      cerr << "  interswitch link\n";
Mac Newbold's avatar
Mac Newbold committed
196 197
#endif

198 199 200 201 202 203 204
      edge cur;      
      forall(cur,G[e].path) {
	SSUB(SCORE_INTERSWITCH_LINK);
	unscore_link(cur,e,true);
      }
      G[e].path.clear();
      
Mac Newbold's avatar
Mac Newbold committed
205 206 207 208 209 210 211 212 213 214 215
      unscore_link(vlink->plink_local_one,e,false);
      unscore_link(vlink->plink_local_two,e,false);
    } else if (vlink->type == tb_vlink::LINK_INTRASWITCH) {
      // INTRASWITCH LINK
#ifdef SCORE_DEBUG
      fprintf(stderr,"   intraswitch link\n");
#endif
      SSUB(SCORE_INTRASWITCH_LINK);

      unscore_link(vlink->plink,e,false);
      unscore_link(vlink->plink_two,e,false); 
216
    } else if (vlink->type != tb_vlink::LINK_TRIVIAL) {
Mac Newbold's avatar
Mac Newbold committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      // No link
      fprintf(stderr,"Internal error - no link\n");
      abort();
    }
  }

  // remove scores associated with the node
  SSUB(SCORE_NO_CONNECTION*vnoder.no_connections);
  violated -= vnoder.no_connections;
  vinfo.no_connection -= vnoder.no_connections;

  // adjust pnode scores
  pnoder.current_load--;
  vnoder.posistion = 0;
  if (pnoder.current_load == 0) {
    // release pnode
#ifdef SCORE_DEBUG
    fprintf(stderr,"  releasing pnode\n");
#endif
    SSUB(SCORE_PNODE);
    node the_switch=pnoder.the_switch;
    if (the_switch) {
      if ((--PG[the_switch].pnodes_used) == 0) {
#ifdef SCORE_DEBUG
241
	cerr << "  releasing switch " << PG[the_switch].name << endl;
Mac Newbold's avatar
Mac Newbold committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
#endif
	// release switch
	SSUB(SCORE_SWITCH);
      }
    }
    // revert pnode type
    pnoder.typed=false;
  } else if (pnoder.current_load >= pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  reducing penalty, new load = %d (>= %d)\n",pnoder.current_load,pnoder.max_load);
#endif
    SSUB(SCORE_PNODE_PENALTY);
    vinfo.pnode_load--;
    violated--;
  }
  // add score for unassigned node
  SADD(SCORE_UNASSIGNED);
  vinfo.unassigned++;
  violated++;
261 262

  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
263 264 265 266 267
  int fd_violated;
  double fds=fd_score(vnoder,pnoder,&fd_violated);
  SSUB(fds);
  violated -= fd_violated;
  vinfo.desires -= fd_violated;
268
  
Mac Newbold's avatar
Mac Newbold committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
#ifdef SCORE_DEBUG
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
}

/*
 * int add_node(node node,int ploc)
 * Add a mapping of node to ploc and adjust score appropriately.
 * Returns 1 in the case of an incompatible mapping.  This should
 * never happen as the same checks should be in place in a higher
 * level.  (Optimization?)
 */
int add_node(node n,int ploc)
{
  tb_vnode &vnoder=G[n];
  node pnode = pnodes[ploc];
  tb_pnode &pnoder=PG[pnode];

#ifdef SCORE_DEBUG
288
  cerr << "SCORE: add_node(" << vnoder.name << "," << pnoder.name << "[" << ploc << "])\n";
Mac Newbold's avatar
Mac Newbold committed
289 290
  fprintf(stderr,"  vnode type = ");
  cerr << vnoder.type << " pnode switch = ";
291 292 293 294 295
  if (pnoder.the_switch) {
    cerr << PG[pnoder.the_switch].name;
  } else {
    cerr << "No switch";
  }
Mac Newbold's avatar
Mac Newbold committed
296 297 298 299 300 301 302 303
  cerr << endl;
#endif
  
  // set up pnode
  // figure out type
  if (!pnoder.typed) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  virgin pnode\n");
304
    cerr << "    vtype = " << vnoder.type << "\n";
Mac Newbold's avatar
Mac Newbold committed
305 306 307 308 309
#endif
    
    // Remove check assuming at higher level?
    // Remove higher level checks?
    pnoder.max_load=0;
310 311
    if (pnoder.types.lookup(vnoder.type) != nil)
      pnoder.max_load = pnoder.types.access(vnoder.type);
Mac Newbold's avatar
Mac Newbold committed
312 313 314 315 316 317 318 319
    
    if (pnoder.max_load == 0) {
      // didn't find a type
#ifdef SCORE_DEBUG
      fprintf(stderr,"  no matching type\n");
#endif
      return 1;
    }
320 321 322 323

    pnoder.current_type=vnoder.type;
    pnoder.typed=true;

Mac Newbold's avatar
Mac Newbold committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
#ifdef SCORE_DEBUG
    fprintf(stderr,"  matching type found (");
    cerr << pnoder.current_type << ", max = " << pnoder.max_load;
    cerr << ")" << endl;
#endif
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  pnode already has type\n");
#endif
    if (pnoder.current_type != vnoder.type) {
#ifdef SCORE_DEBUG      
      fprintf(stderr,"  incompatible types\n");
#endif
      return 1;
    } else {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  comaptible types\n");
#endif
      if (pnoder.current_load == pnoder.max_load) {
343 344 345
	/* XXX - We could ignore this check and let the code
	   at the end of the routine penalize for going over
	   load.  Failing here seems to work better though. */
Mac Newbold's avatar
Mac Newbold committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
#ifdef SCORE_DEBUG
	fprintf(stderr,"  node is full\n");
#endif
	return 1;
      }
      
    }
  }

  // set up links
  vnoder.no_connections=0;
  edge e;
  node dst;
  tb_vlink *er;
  tb_plink *pl;
  edge pedge;
  forall_inout_edges(e,n) {
    dst=G.source(e);
    er=&G[e];
    if (dst == n) {
      dst=G.target(e);
    }
    tb_vnode &dstr=G[dst];
    assert(dst != n);
    assert(&dstr != &vnoder);
#ifdef SCORE_DEBUG
372
    cerr << "  edge to " << dstr.name << endl;
Mac Newbold's avatar
Mac Newbold committed
373 374 375 376 377 378 379 380
#endif

    if (dstr.posistion != 0) {
      // dstr is assigned
      node dpnode=pnodes[dstr.posistion];
      tb_pnode &dpnoder=PG[dpnode];

#ifdef SCORE_DEBUG
381
      cerr << "   goes to " << dpnoder.name << endl;
Mac Newbold's avatar
Mac Newbold committed
382 383
#endif

384 385 386 387 388 389
      if (dpnode == pnode) {
#ifdef SCORE_DEBUG
	fprintf(stderr,"  trivial link\n");
#endif SCORE_DEBUG
	er->type = tb_vlink::LINK_TRIVIAL;
      } else if ((pedge=direct_link(dpnode,pnode)) != NULL) {
Mac Newbold's avatar
Mac Newbold committed
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
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found direct link = %p\n",pedge);
#endif
	pl = &PG[pedge];

	// direct
	er->type = tb_vlink::LINK_DIRECT;
	er->plink = pedge;

	score_link(pedge,e,false);
      } else if (pnoder.the_switch &&
		 (pnoder.the_switch == dpnoder.the_switch)) {
	// intraswitch
	assert(pnoder.the_switch == dpnoder.the_switch);
	edge first,second;
	if (find_intraswitch_path(pnode,dpnode,&first,&second) == 1) {
	  fprintf(stderr,"Internal error: Could not find intraswitch link!\n");
	  abort();
	}

	assert(first != NULL);
	assert(second != NULL);
	
#ifdef SCORE_DEBUG
	fprintf(stderr,"   found intraswitch link (%p,%p)\n",first,second);
#endif
	er->type = tb_vlink::LINK_INTRASWITCH;
	er->plink = first;
	er->plink_two = second;
	SADD(SCORE_INTRASWITCH_LINK);

	// check users and bandwidth
	score_link(first,e,false);
	score_link(second,e,false);
      } else {
	// try to find interswitch
#ifdef SCORE_DEBUG
427
	cerr << "   looking for interswitch link " <<
428 429
	  (pnoder.the_switch != nil?PG[pnoder.the_switch].name:string("No Switch")) << " " <<
	  (dpnoder.the_switch != nil?PG[dpnoder.the_switch].name:string("No Switch")) << endl;
Mac Newbold's avatar
Mac Newbold committed
430 431
#endif
	if (find_interswitch_path(pnoder.the_switch,dpnoder.the_switch,
432
				  er->bandwidth,er->path) == 0) {
Mac Newbold's avatar
Mac Newbold committed
433 434 435
#ifdef SCORE_DEBUG
	  fprintf(stderr,"   could not find path - no connection\n");
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

	  // Need to free up all links already made and abort
	  forall_inout_edges(e,n) {
	    tb_vlink &vlink = G[e];
	    if (vlink.type == tb_vlink::LINK_DIRECT) {
	      unscore_link(vlink.plink,e,false);
	    } else if (vlink.type == tb_vlink::LINK_INTRASWITCH) {
	      SSUB(SCORE_INTRASWITCH_LINK);
	      unscore_link(vlink.plink,e,false);
	      unscore_link(vlink.plink_two,e,false);
	    } else if (vlink.type == tb_vlink::LINK_INTERSWITCH) {
	      edge cur;
	      forall(cur,G[e].path) {
		SSUB(SCORE_INTERSWITCH_LINK);
		unscore_link(cur,e,true);
	      }
	      G[e].path.clear();
	      unscore_link(vlink.plink_local_one,e,false);
	      unscore_link(vlink.plink_local_two,e,false);
	    } // else LINK_UNKNOWN i.e. unassigned.
	  }

	  // Reset to be retyped next time and abort.  This is a
	  // fatal error.
Mac Newbold's avatar
Mac Newbold committed
460 461 462 463
	  pnoder.typed = false;
	  return 1;
 	} else {
#ifdef SCORE_DEBUG
464
	  fprintf(stderr,"   found interswitch link\n");
Mac Newbold's avatar
Mac Newbold committed
465 466 467
#endif
	  er->type=tb_vlink::LINK_INTERSWITCH;

468 469 470 471 472 473 474 475
	  edge cur;
	  forall(cur,er->path) {
	    SADD(SCORE_INTERSWITCH_LINK);
#ifdef SCORE_DEBUG
	    cerr << "     " << PG[cur].name << endl;
#endif
	    score_link(cur,e,true);
	  }
Mac Newbold's avatar
Mac Newbold committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

	  er->plink_local_one = find_link_to_switch(pnode);
	  assert(er->plink_local_one != NULL);
	  er->plink_local_two = find_link_to_switch(dpnode);
	  assert(er->plink_local_two != NULL);

	  score_link(er->plink_local_one,e,false);
	  score_link(er->plink_local_two,e,false);
	}
      }
    }
  }
    
  // finish setting up pnode
  pnoder.current_load++;
  vnoder.posistion = ploc;
  if (pnoder.current_load > pnoder.max_load) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load to high - penalty (%d)\n",pnoder.current_load);
#endif
    SADD(SCORE_PNODE_PENALTY);
    vinfo.pnode_load++;
    violated++;
  } else {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  load is fine\n");
#endif
  }
  if (pnoder.current_load == 1) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"  new pnode\n");
#endif
    SADD(SCORE_PNODE);
    if (pnoder.the_switch &&
	(++PG[pnoder.the_switch].pnodes_used) == 1) {
#ifdef SCORE_DEBUG
      fprintf(stderr,"  new switch\n");
#endif
      SADD(SCORE_SWITCH);
    }
  }

  // node no longer unassigned
  SSUB(SCORE_UNASSIGNED);
  vinfo.unassigned--;
  violated--;

523
  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
524 525 526 527 528 529 530 531
  int fd_violated;
  double fds = fd_score(vnoder,pnoder,&fd_violated);
  SADD(fds);
  violated += fd_violated;
  vinfo.desires += fd_violated;

  // pclass
  if (pnoder.my_class->used == 0) {
532
#ifdef SCORE_DEBUG
533
    cerr << "  new pclass\n";
534
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
535
    SADD(SCORE_PCLASS);
536
  }
537 538 539 540 541 542 543 544 545 546 547 548 549 550

  // vclass
  if (vnoder.vclass != NULL) {
    double score_delta = vnoder.vclass->assign_node(vnoder.type);
#ifdef SCORE_DEBUG
    cerr << "  vclass assign " << score_delta << endl;
#endif
    SADD(score_delta*SCORE_VCLASS);
    if (score_delta >= 1) {
      violated++;
      vinfo.vclass++;
    }
  }

Mac Newbold's avatar
Mac Newbold committed
551 552 553 554
#ifdef SCORE_DEBUG
  fprintf(stderr,"  posistion = %d\n",vnoder.posistion);
  fprintf(stderr,"  new score = %.2f  new violated = %d\n",score,violated);
#endif
Christopher Alfeld's avatar
 
Christopher Alfeld committed
555 556 557

  pclass_set(vnoder,pnoder);
  
Mac Newbold's avatar
Mac Newbold committed
558 559 560 561 562 563 564 565 566 567 568 569 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 628 629 630 631 632 633 634 635 636
  return 0;
}

// returns "best" direct link between a and b.
// best = less users
//        break ties with minimum bw_used
edge direct_link(node a,node b)
{
  node dst;
  edge e;
  edge best = NULL;
  tb_plink *pl;
  tb_plink *bestpl = NULL;
  forall_inout_edges(e,a) {
    dst=PG.target(e);
    if (dst == a)
      dst=PG.source(e);
    if (dst == b) {
      pl = &PG[e];
      if (! bestpl ||
	  (pl->users < bestpl->users ||
	   (pl->users == bestpl->users &&
	    pl->bw_used < bestpl->bw_used))) {
	best = e;
	bestpl = pl;
      }
    }
  }
  return best;
}

edge find_link_to_switch(node n)
{
  tb_pnode &nr = PG[n];

  edge e;
  node edst;
  float best_bw=1000.0;
  int best_users = 1000;
  float bw;
  edge best=NULL;
  forall_inout_edges(e,n) {
    edst = PG.target(e);
    if (edst == n)
      edst = PG.source(e);
    if (edst == nr.the_switch) {
      tb_plink &er = PG[e];
      bw = er.bw_used / er.bandwidth;
      if ((er.users < best_users) ||
	  ((er.users == best_users) && (bw < best_bw))) {
	best = e;
	best_bw = bw;
	best_users = er.users;
      }
    }
  }
  
  return best;
}

// this looks for the best links between the src and the switch
// and the dst and the switch.
int find_intraswitch_path(node src,node dst,edge *first,edge *second)
{
  tb_pnode &srcr=PG[src];
  tb_pnode &dstr=PG[dst];

  assert(srcr.the_switch == dstr.the_switch);
  assert(srcr.the_switch != NULL);
  assert(src != dst);

  *first = find_link_to_switch(src);
  *second = find_link_to_switch(dst);

  if ((*first != NULL) &&
      (*second != NULL)) return 0;
  return 1;
}

637 638 639 640 641
// this uses the shortest paths calculated over the switch graph to
// find a path between src and dst.  It passes out list<edge>, a list
// of the edges used. (assumed to be empty to begin with).
// Returns 0 if no path exists and 1 otherwise.
int find_interswitch_path(node src,node dst,int bandwidth,list<edge> &L)
Mac Newbold's avatar
Mac Newbold committed
642
{
643 644 645 646 647 648 649
  // We know the shortest path from src to node already.  It's stored
  // in switch_preds[src] and is a node_array<edge>.  Let P be this
  // array.  We can trace our shortest path by starting at the end and
  // following the pred edges back until we reach src.  We need to be
  // careful though because the switch_preds deals with elements of SG
  // and we have elements of PG.
  if ((src == nil) || (dst == nil)) {return 0;}
Mac Newbold's avatar
Mac Newbold committed
650
  
651 652 653 654 655 656 657
  node sg_src = PG[src].sgraph_switch;
  node sg_dst = PG[dst].sgraph_switch;
  edge sg_ed;
  node sg_cur=sg_dst;
  switch_pred_array &preds = switch_preds[sg_src];
  if (preds[sg_dst] == nil) {
    // unreachable
Mac Newbold's avatar
Mac Newbold committed
658 659
    return 0;
  }
660 661 662 663 664 665 666 667 668 669
  while (sg_cur != sg_src) {
    sg_ed = preds[sg_cur];
    L.push(SG[sg_ed].mate);
    if (SG.source(sg_ed) == sg_cur) {
      sg_cur = SG.target(sg_ed);
    } else {
      sg_cur = SG.source(sg_ed);
    }
  }
  return 1;
Mac Newbold's avatar
Mac Newbold committed
670 671 672 673 674 675 676 677 678
}

// this does scoring for over users and over bandwidth on edges.
void score_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
679
  cerr << "  score_link(" << e << ") - " << pl.name << " / " << er.name << endl;
Mac Newbold's avatar
Mac Newbold committed
680 681 682
#endif

  if (! interswitch) {
683 684 685
    if (! er.emulated) {
      pl.users++;
      if (pl.users == 1) {
Mac Newbold's avatar
Mac Newbold committed
686
#ifdef SCORE_DEBUG
687
	fprintf(stderr,"    first user\n");
Mac Newbold's avatar
Mac Newbold committed
688
#endif
689 690 691 692 693 694 695 696 697
	SADD(SCORE_DIRECT_LINK);
      } else {
#ifdef SCORE_DEBUG
	fprintf(stderr,"    not first user - penalty\n");
#endif
	SADD(SCORE_DIRECT_LINK_PENALTY);
	vinfo.link_users++;
	violated++;
      }
Mac Newbold's avatar
Mac Newbold committed
698
    } else {
699 700 701
      pl.users++;
      SADD(SCORE_EMULATED_LINK);
      if (pl.users == 1) {
Mac Newbold's avatar
Mac Newbold committed
702
#ifdef SCORE_DEBUG
703
	fprintf(stderr,"    emulated - first user\n");
Mac Newbold's avatar
Mac Newbold committed
704
#endif
705 706
	SADD(SCORE_DIRECT_LINK);
      }
Mac Newbold's avatar
Mac Newbold committed
707
    }
708
  } 
Mac Newbold's avatar
Mac Newbold committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
  
  // bandwidth
  int prev_bw = pl.bw_used;
  pl.bw_used += er.bandwidth;
  if ((pl.bw_used > pl.bandwidth) &&
      (prev_bw <= pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"    went over bandwidth (%d > %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated++;
    vinfo.bandwidth++;
    SADD(SCORE_OVER_BANDWIDTH);
  }
}

void unscore_link(edge e,edge v,bool interswitch)
{
  tb_plink &pl = PG[e];
  tb_vlink &er = G[v];

#ifdef SCORE_DEBUG
  fprintf(stderr,"  unscore_link(%p)\n",e);
#endif

  if (!interswitch) {
735 736 737 738
    if (! er.emulated) {
      pl.users--;
      if (pl.users == 0) {
	// link no longer used
Mac Newbold's avatar
Mac Newbold committed
739
#ifdef SCORE_DEBUG
740
	fprintf(stderr,"   freeing link\n");
Mac Newbold's avatar
Mac Newbold committed
741
#endif
742 743 744 745 746 747 748 749 750 751
	SSUB(SCORE_DIRECT_LINK);
      } else {
	// getting close to no violations
#ifdef SCORE_DEBUG
	fprintf(stderr,"   reducing users\n");
#endif
	SSUB(SCORE_DIRECT_LINK_PENALTY);
	vinfo.link_users--;
	violated--;
      }
Mac Newbold's avatar
Mac Newbold committed
752
    } else {
753 754 755 756
      pl.users--;
      SSUB(SCORE_EMULATED_LINK);
      if (pl.users == 0) {
	// link no longer used
Mac Newbold's avatar
Mac Newbold committed
757
#ifdef SCORE_DEBUG
758
	fprintf(stderr,"   emulated - freeing link\n");
Mac Newbold's avatar
Mac Newbold committed
759
#endif
760 761
	SSUB(SCORE_DIRECT_LINK);
      } 
Mac Newbold's avatar
Mac Newbold committed
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
    }
  }
  
  // bandwidth check
  int prev_bw = pl.bw_used;
  pl.bw_used -= er.bandwidth;
  if ((pl.bw_used <= pl.bandwidth) &&
      (prev_bw > pl.bandwidth)) {
#ifdef SCORE_DEBUG
    fprintf(stderr,"   went under bandwidth (%d <= %d)\n",
	    pl.bw_used,pl.bandwidth);
#endif
    violated--;
    vinfo.bandwidth--;
    SSUB(SCORE_OVER_BANDWIDTH);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

  er.type = tb_vlink::LINK_UNKNOWN;
}

double fd_score(tb_vnode &vnoder,tb_pnode &pnoder,int *fd_violated)
{
  double fd_score=0;
  (*fd_violated)=0;
  
  seq_item desire;
  seq_item feature;
  double value;
  for (desire = vnoder.desires.min_item();desire;
       desire = vnoder.desires.succ(desire)) {
    feature = pnoder.features.lookup(vnoder.desires.key(desire));
#ifdef SCORE_DEBUG
    cerr << "  desire = " << vnoder.desires.key(desire) \
	 << " " << vnoder.desires.inf(desire) << "\n";
#endif
    if (!feature) {
      // Unmatched desire.  Add cost.
#ifdef SCORE_DEBUG
      cerr << "    unmatched\n";
#endif
      value = vnoder.desires.inf(desire);
      fd_score += SCORE_DESIRE*value;
      if (value >= 1) {
	(*fd_violated)++;
      }
    }
  }
  for (feature = pnoder.features.min_item();feature;
       feature = pnoder.features.succ(feature)) {
    desire = vnoder.desires.lookup(pnoder.features.key(feature));
#ifdef SCORE_DEBUG
    cerr << "  feature = " << pnoder.features.key(feature) \
	 << " " << pnoder.features.inf(feature) << "\n";
#endif
    if (! desire) {
      // Unused feature.  Add weight
#ifdef SCORE_DEBUG
      cerr << "    unused\n";
#endif
      value = pnoder.features.inf(feature);
      fd_score+=SCORE_FEATURE*value;
    }
  }

  return fd_score;
Mac Newbold's avatar
Mac Newbold committed
827
}