score.cc 39.6 KB
Newer Older
1
#include "port.h"
Mac Newbold's avatar
Mac Newbold committed
2 3

#include <iostream.h>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <float.h>

#include <hash_map>
#include <rope>
#include <queue>
#include <slist>
#include <hash_set>

#include <boost/config.hpp>
#include <boost/utility.hpp>
#include <boost/property_map.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;
Mac Newbold's avatar
Mac Newbold committed
19 20

#include "common.h"
21
#include "vclass.h"
22
#include "delay.h"
Mac Newbold's avatar
Mac Newbold committed
23
#include "physical.h"
24
#include "virtual.h"
Christopher Alfeld's avatar
 
Christopher Alfeld committed
25 26 27
#include "pclass.h"
#include "score.h"

28
#include "math.h"
Mac Newbold's avatar
Mac Newbold committed
29

30
extern switch_pred_map_map switch_preds;
31

32
double score;			// The score of the current mapping
Mac Newbold's avatar
Mac Newbold committed
33 34 35 36 37
int violated;			// How many times the restrictions
				// have been violated.

violated_info vinfo;		// specific info on violations

38
extern tb_vgraph VG;		// virtual graph
Mac Newbold's avatar
Mac Newbold committed
39
extern tb_pgraph PG;		// physical grpaph
40
extern tb_sgraph SG;		// switch fabric
Mac Newbold's avatar
Mac Newbold committed
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
bool direct_link(pvertex a,pvertex b,tb_vlink *vlink,pedge &edge);
#ifdef FIX_PLINK_ENDPOINTS
void score_link(pedge pe,vedge ve,tb_pnode *src_pnode,tb_pnode *dst_pnode);
void unscore_link(pedge pe,vedge ve,tb_pnode *src_pnode,tb_pnode *dst_pnode);
#else
void score_link(pedge pe,vedge ve);
void unscore_link(pedge pe,vedge ve);
#endif
bool find_link_to_switch(pvertex pv,pvertex switch_pv,tb_vlink *vlink,
			 pedge &out_edge);
int find_interswitch_path(pvertex src_pv,pvertex dest_pv,
			  int bandwidth,pedge_path &out_path,
			  pvertex_list &out_switches);
double fd_score(tb_vnode *vnode,tb_pnode *pnoder,int &out_fd_violated);
#ifdef FIX_PLINK_ENDPOINTS
void score_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode);
void unscore_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode);
#else
void score_link_info(vedge ve);
void unscore_link_info(vedge ve);
#endif

#ifdef FIX_PLINK_ENDPOINTS
void score_link_endpoints(pedge pe);
#endif
Mac Newbold's avatar
Mac Newbold committed
67 68

#ifdef SCORE_DEBUG_MORE
69 70
#define SADD(amount) cerr << "SADD: " << #amount << "=" << amount << " from " << score;score+=amount;cerr << " to " << score << endl
#define SSUB(amount)  cerr << "SSUB: " << #amount << "=" << amount << " from " << score;score-=amount;cerr << " to " << score << endl
Mac Newbold's avatar
Mac Newbold committed
71 72 73 74 75
#else
#define SADD(amount) score += amount
#define SSUB(amount) score -= amount
#endif

76 77 78 79 80 81
#ifdef SCORE_DEBUG
#define SDEBUG(a) a
#else
#define SDEBUG(a)
#endif

Mac Newbold's avatar
Mac Newbold committed
82 83 84 85
/*
 * score()
 * Returns the score.
 */
86
double get_score() {return score;}
Mac Newbold's avatar
Mac Newbold committed
87 88 89 90 91 92 93 94

/*
 * init_score()
 * This initialized the scoring system.  It also clears all
 * assignments.
 */
void init_score()
{
95
  SDEBUG(cerr << "SCORE: Initializing" << endl);
Mac Newbold's avatar
Mac Newbold committed
96 97 98 99
  score=0;
  violated=0;
  vinfo.unassigned = vinfo.pnode_load = 0;
  vinfo.no_connection = vinfo.link_users = vinfo.bandwidth = 0;
100 101 102
#ifdef FIX_PLINK_ENDPOINTS
  vinfo.incorrect_endpoints = 0;
#endif
Mac Newbold's avatar
Mac Newbold committed
103

104 105 106 107 108
  vvertex_iterator vvertex_it,end_vvertex_it;
  tie(vvertex_it,end_vvertex_it) = vertices(VG);
  for (;vvertex_it!=end_vvertex_it;++vvertex_it) {
    tb_vnode *vnode=get(vvertex_pmap,*vvertex_it);
    vnode->assigned = false;
Mac Newbold's avatar
Mac Newbold committed
109
    SADD(SCORE_UNASSIGNED);
110
    //cout << "Init: Counting " << vnode->name << endl;
Mac Newbold's avatar
Mac Newbold committed
111 112 113
    vinfo.unassigned++;
    violated++;
  }
114 115 116 117 118 119
  vedge_iterator vedge_it,end_vedge_it;
  tie(vedge_it,end_vedge_it) = edges(VG);
  for (;vedge_it!=end_vedge_it;++vedge_it) {
    tb_vlink *vlink=get(vedge_pmap,*vedge_it);
    vlink->link_info.type=tb_link_info::LINK_UNKNOWN;
    vlink->no_connection=false;
Mac Newbold's avatar
Mac Newbold committed
120
  }
121 122 123 124 125 126 127 128
  pvertex_iterator pvertex_it,end_pvertex_it;
  tie(pvertex_it,end_pvertex_it) = vertices(PG);
  for (;pvertex_it!=end_pvertex_it;++pvertex_it) {
    tb_pnode *pn=get(pvertex_pmap,*pvertex_it);
    pn->typed=false;
    pn->current_load=0;
    pn->pnodes_used=0;
    pn->switch_used_links=0;
Mac Newbold's avatar
Mac Newbold committed
129
  }
130 131 132 133 134 135 136
  pedge_iterator pedge_it,end_pedge_it;
  tie(pedge_it,end_pedge_it) = edges(PG);
  for (;pedge_it!=end_pedge_it;++pedge_it) {
    tb_plink *plink=get(pedge_pmap,*pedge_it);
    plink->bw_used=0;
    plink->emulated=0;
    plink->nonemulated=0;
Mac Newbold's avatar
Mac Newbold committed
137 138
  }

139
  SDEBUG(cerr << "  score=" << score << " violated=" << violated << endl);
Mac Newbold's avatar
Mac Newbold committed
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/* unscore_link_info(vedge ve)
 * This routine is the highest level link scorer.  It handles all
 * scoring that depends on the link_info of vlink.
 */
#ifdef FIX_PLINK_ENDPOINTS
void unscore_link_info(vedge ve,tb_pnode *src_pnode,tb_pnode *dst_pnode)
#else
void unscore_link_info(vedge ve)
#endif
{
  tb_vlink *vlink = get(vedge_pmap,ve);
  if (vlink->link_info.type == tb_link_info::LINK_DIRECT) {
    // DIRECT LINK
    SDEBUG(cerr << "   direct link" << endl);
#ifdef FIX_PLINK_ENDPOINTS
    unscore_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
#else
    unscore_link(vlink->link_info.plinks.front(),ve);
#endif
    vlink->link_info.plinks.clear();
  } else if (vlink->link_info.type == tb_link_info::LINK_INTERSWITCH) {
    // INTERSWITCH LINK
    SDEBUG(cerr << "  interswitch link" << endl);
    
    pedge_path &path = vlink->link_info.plinks;
#ifndef INTERSWITCH_LENGTH
    SSUB(SCORE_INTERSWITCH_LINK);
#endif
    // XXX: Potentially bogus;
    int numinterlinks;
    numinterlinks = -2;
    for (pedge_path::iterator it=path.begin();
	 it != path.end();++it) {
#ifdef FIX_PLINK_ENDPOINTS
      unscore_link(*it,ve,src_pnode,dst_pnode);
#else
      unscore_link(*it,ve);
#endif
      numinterlinks++;
    }
#ifdef INTERSWITCH_LENGTH
    for (int i = 1; i <= numinterlinks; i++) {
      SSUB(SCORE_INTERSWITCH_LINK);
    }
#endif

    path.clear();
    for (pvertex_list::iterator it = vlink->link_info.switches.begin();
	 it != vlink->link_info.switches.end();++it) {
      tb_pnode *the_switch = get(pvertex_pmap,*it);
      if (--the_switch->switch_used_links == 0) {
	SDEBUG(cerr << "  releasing switch" << endl);
	SSUB(SCORE_SWITCH);
      }
    }
    vlink->link_info.switches.clear();
  } else if (vlink->link_info.type == tb_link_info::LINK_INTRASWITCH) {
    // INTRASWITCH LINK
    SDEBUG(cerr << "   intraswitch link" << endl);
    SSUB(SCORE_INTRASWITCH_LINK);
    
#ifdef FIX_PLINK_ENDPOINTS
    unscore_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    unscore_link(vlink->link_info.plinks.back(),ve,src_pnode,dst_pnode);
#else
    unscore_link(vlink->link_info.plinks.front(),ve);
    unscore_link(vlink->link_info.plinks.back(),ve);
#endif
    vlink->link_info.plinks.clear();
    tb_pnode *the_switch = get(pvertex_pmap,
			       vlink->link_info.switches.front());
    if (--the_switch->switch_used_links == 0) {
      SDEBUG(cerr << "  releasing switch" << endl);
      SSUB(SCORE_SWITCH);
    }
    vlink->link_info.switches.clear();
  }
}
Mac Newbold's avatar
Mac Newbold committed
220 221
/*
 * This removes a virtual node from the assignments, adjusting
222 223
 * the score appropriately.  */
void remove_node(vvertex vv)
Mac Newbold's avatar
Mac Newbold committed
224 225
{
  /* Find pnode assigned to */
226 227 228 229 230 231 232 233 234 235 236
  tb_vnode *vnode = get(vvertex_pmap,vv);
  assert(vnode->assigned);
  pvertex pv = vnode->assignment;
  tb_pnode *pnode = get(pvertex_pmap,pv);
  //cerr << "remove_node: " << vnode->name << endl;

  SDEBUG(cerr <<  "SCORE: remove_node(" << vnode->name << ")" << endl);
  SDEBUG(cerr <<  "  assignment=" << pnode->name << endl);
#ifdef SCORE_DEBUG_LOTS
  cerr << *vnode;
  cerr << *pnode;
Mac Newbold's avatar
Mac Newbold committed
237 238 239 240
#endif

  assert(pnode != NULL);

241 242 243
  if (pnode->my_class) {
    pclass_unset(pnode);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
244

245 246
#ifdef SMART_UNMAP
  pnode->assigned_nodes.erase(vnode);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
247
#endif
248 249 250 251

  // pclass
  if (pnode->my_class && (pnode->my_class->used == 0)) {
    SDEBUG(cerr << "  freeing pclass" << endl);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
252 253
    SSUB(SCORE_PCLASS);
  }
254 255

  // vclass
256 257 258
  if (vnode->vclass != NULL) {
    double score_delta = vnode->vclass->unassign_node(vnode->type);
    SDEBUG(cerr << "  vclass unassign " << score_delta << endl);
259 260 261 262 263 264 265
    
    if (score_delta <= -1) {
      violated--;
      vinfo.vclass--;
    }
    SSUB(-score_delta*SCORE_VCLASS);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
266
  
Mac Newbold's avatar
Mac Newbold committed
267
  // remove the scores associated with each edge
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  voedge_iterator vedge_it,end_vedge_it;
  tie(vedge_it,end_vedge_it) = out_edges(vv,VG);
  for (;vedge_it!=end_vedge_it;++vedge_it) {
    tb_vlink *vlink = get(vedge_pmap,*vedge_it);
    vvertex dest_vv = target(*vedge_it,VG);
    if (dest_vv == vv)
      dest_vv = source(*vedge_it,VG);
    tb_vnode *dest_vnode = get(vvertex_pmap,dest_vv);
    SDEBUG(cerr << "  edge to " << dest_vnode->name << endl);

    if (vlink->no_connection) {
      SDEBUG(cerr << "  link no longer in violation.\n";)
      SSUB(SCORE_NO_CONNECTION);
      vlink->no_connection=false;
      vinfo.no_connection--;
      violated--;
Mac Newbold's avatar
Mac Newbold committed
284
    }
285 286 287 288 289 290 291 292 293 294
    
    if (! dest_vnode->assigned) continue;
    
#ifdef FIX_PLINK_ENDPOINTS
    pvertex dest_pv = dest_vnode->assignment;
    tb_pnode *dest_pnode = get(pvertex_pmap,dest_pv);
    unscore_link_info(*vedge_it,pnode,dest_pnode);
#else
    unscore_link_info(*vedge_it);
#endif
Mac Newbold's avatar
Mac Newbold committed
295
  }
296 297 298 299 300 301
 
#ifdef PENALIZE_UNUSED_INTERFACES
  SSUB((pnode->total_interfaces - pnode->used_interfaces) * SCORE_UNUSED_INTERFACE);
  pnode->used_interfaces = 0;
#endif
 
Mac Newbold's avatar
Mac Newbold committed
302
  // adjust pnode scores
303 304 305 306 307 308 309 310
  pnode->current_load--;
  vnode->assigned = false;
#ifdef LOAD_BALANCE
  //SADD(SCORE_PNODE * (1.0/pnode->max_load));
  //SSUB(SCORE_PNODE * (1+powf(((pnode->current_load+1) * 1.0)/pnode->max_load,2)));
  //SADD(SCORE_PNODE * (1+powf(pnode->current_load * 1.0/pnode->max_load,2)));
  SSUB(SCORE_PNODE * (powf(1+ ((pnode->current_load+1) * 1.0)/pnode->max_load,2)));
  SADD(SCORE_PNODE * (powf(1+ pnode->current_load * 1.0/pnode->max_load,2)));
Mac Newbold's avatar
Mac Newbold committed
311
#endif
312 313 314
  if (pnode->current_load == 0) {
    // release pnode
    SDEBUG(cerr << "  releasing pnode" << endl);
Mac Newbold's avatar
Mac Newbold committed
315
    SSUB(SCORE_PNODE);
316 317
#ifdef LOAD_BALANCE
    //SSUB(SCORE_PNODE);
Mac Newbold's avatar
Mac Newbold committed
318
#endif
319 320
  
    
Mac Newbold's avatar
Mac Newbold committed
321
    // revert pnode type
322 323 324 325
    pnode->typed=false;
  } else if (pnode->current_load >= pnode->max_load) {
    SDEBUG(cerr << "  reducing penalty, new load=" << pnode->current_load <<
	   " (>= " << pnode->max_load << ")" << endl);
Mac Newbold's avatar
Mac Newbold committed
326 327 328 329
    SSUB(SCORE_PNODE_PENALTY);
    vinfo.pnode_load--;
    violated--;
  }
330

Mac Newbold's avatar
Mac Newbold committed
331 332 333 334
  // add score for unassigned node
  SADD(SCORE_UNASSIGNED);
  vinfo.unassigned++;
  violated++;
335 336

  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
337
  int fd_violated;
338
  double fds=fd_score(vnode,pnode,fd_violated);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
339 340 341
  SSUB(fds);
  violated -= fd_violated;
  vinfo.desires -= fd_violated;
342 343 344 345 346 347

  // remove lan node if necessary
  if (vnode->type.compare("lan") == 0) {
    SDEBUG(cerr << "Deleting lan node." << endl);
    delete_lan_node(pv);
  }
348
  
349
  SDEBUG(cerr << "  new score = " << score << " new violated = " << violated << endl);
Mac Newbold's avatar
Mac Newbold committed
350 351
}

352 353 354
/* score_link_info(vedge ve)
 * This routine is the highest level link scorer.  It handles all
 * scoring that depends on the link_info of vlink.
Mac Newbold's avatar
Mac Newbold committed
355
 */
356 357 358 359 360
#ifdef FIX_PLINK_ENDPOINTS
void score_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode)
#else
void score_link_info(vedge ve)
#endif
Mac Newbold's avatar
Mac Newbold committed
361
{
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 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
  tb_vlink *vlink = get(vedge_pmap,ve);
  tb_pnode *the_switch;
  switch (vlink->link_info.type) {
  case tb_link_info::LINK_DIRECT:
    SADD(SCORE_DIRECT_LINK);
#ifdef FIX_PLINK_ENDPOINTS
    score_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
#else
    score_link(vlink->link_info.plinks.front(),ve);
#endif
    break;
  case tb_link_info::LINK_INTRASWITCH:
    SADD(SCORE_INTRASWITCH_LINK);
#ifdef FIX_PLINK_ENDPOINTS
    score_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    score_link(vlink->link_info.plinks.back(),ve,src_pnode,dst_pnode);
#else
    score_link(vlink->link_info.plinks.front(),ve);
    score_link(vlink->link_info.plinks.back(),ve);
#endif
    the_switch = get(pvertex_pmap,
		     vlink->link_info.switches.front());
    if (++the_switch->switch_used_links == 1) {
      SDEBUG(cerr << "  new switch" << endl);
      SADD(SCORE_SWITCH);
    }
    break;
  case tb_link_info::LINK_INTERSWITCH:
#ifndef INTERSWITCH_LENGTH
    SADD(SCORE_INTERSWITCH_LINK);
#endif
    // XXX: Potentially bogus!
    int numinterlinks;
    numinterlinks = -2;
    for (pedge_path::iterator plink_It = vlink->link_info.plinks.begin();
	 plink_It != vlink->link_info.plinks.end();
	 ++plink_It) {
#ifdef FIX_PLINK_ENDPOINTS
	score_link(*plink_It,ve,src_pnode,dst_pnode);
#else
	score_link(*plink_It,ve);
#endif
	numinterlinks++;
    }
#ifdef INTERSWITCH_LENGTH
    for (int i = 1; i <= numinterlinks; i++) {
	SADD(SCORE_INTERSWITCH_LINK);
    }
#endif
Mac Newbold's avatar
Mac Newbold committed
411

412 413 414 415 416 417 418 419 420 421 422 423 424 425
    for (pvertex_list::iterator switch_it = vlink->link_info.switches.begin();
	 switch_it != vlink->link_info.switches.end();++switch_it) {
      the_switch = get(pvertex_pmap,*switch_it);
      if (++the_switch->switch_used_links == 1) {
	SDEBUG(cerr << "  new switch" << endl);
	SADD(SCORE_SWITCH);
      }
    }
    break;
  case tb_link_info::LINK_UNKNOWN:
  case tb_link_info::LINK_TRIVIAL:
    cerr << "Internal error: Should not be here either." << endl;
    exit(1);
    break;
426
  }
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
}

/*
 * int add_node(vvertex vv,pvertex pv,bool deterministic)
 * Add a mapping of vv to pv and adjust score appropriately.
 * Returns 1 in the case of an incompatible mapping.  If determinisitic
 * is true then it deterministically solves the link problem for best
 * score.  Note: deterministic takes considerably longer.
 */
int add_node(vvertex vv,pvertex pv, bool deterministic)
{
  tb_vnode *vnode = get(vvertex_pmap,vv);
  tb_pnode *pnode = get(pvertex_pmap,pv);
  //cerr << "add_node: " << vnode->name << endl;
  assert(!vnode->assigned);

  SDEBUG(cerr << "SCORE: add_node(" << vnode->name << "," <<
	 pnode->name << ")" << endl);
#ifdef SCORE_DEBUG_LOTS
  cerr << *vnode;
  cerr << *pnode;
Mac Newbold's avatar
Mac Newbold committed
448
#endif
449
  SDEBUG(cerr << "  vnode type = " << vnode->type << endl);
Mac Newbold's avatar
Mac Newbold committed
450 451
  
  // set up pnode
452

Mac Newbold's avatar
Mac Newbold committed
453
  // figure out type
454 455 456 457
  if (!pnode->typed) {
    SDEBUG(cerr << "  virgin pnode" << endl);
    SDEBUG(cerr << "    vtype = " << vnode->type << endl);

Mac Newbold's avatar
Mac Newbold committed
458 459
    // Remove check assuming at higher level?
    // Remove higher level checks?
460 461 462 463 464
    pnode->max_load=0;
    if (pnode->types.find(vnode->type) != pnode->types.end()) {
      pnode->max_load = pnode->types[vnode->type];
    }
    if (pnode->max_load == 0) {
Mac Newbold's avatar
Mac Newbold committed
465
      // didn't find a type
466 467
      SDEBUG(cerr << "  no matching type" << endl);
      //cerr << "add_node FAILED! (no matching type)" << endl;
Mac Newbold's avatar
Mac Newbold committed
468 469
      return 1;
    }
470 471 472
    
    pnode->current_type=vnode->type;
    pnode->typed=true;
473

474 475
    SDEBUG(cerr << "  matching type found (" <<pnode->current_type <<
	   ", max = " << pnode->max_load << ")" << endl);
Mac Newbold's avatar
Mac Newbold committed
476
  } else {
477 478 479 480
    SDEBUG(cerr << "  pnode already has type" << endl);
    if (pnode->current_type != vnode->type) {
      SDEBUG(cerr << "  incompatible types" << endl);
      //cerr << "add_node FAILED! (incompatible types)" << endl;
Mac Newbold's avatar
Mac Newbold committed
481 482
      return 1;
    } else {
483 484
      SDEBUG(cerr << "  compatible types" << endl);
      if (pnode->current_load == pnode->max_load) {
485 486 487
	/* 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. */
488 489 490 491 492

	// XXX is this a bug?  do we need to revert the pnode/vnode to
	// it's initial state.
	SDEBUG(cerr << "  node is full" << endl);
	//cerr << "add_node FAILED! (node is full)" << endl;
Mac Newbold's avatar
Mac Newbold committed
493 494 495 496 497
	return 1;
      }
    }
  }

498 499
#ifdef PENALIZE_UNUSED_INTERFACES
  pnode->used_interfaces = 0;
Mac Newbold's avatar
Mac Newbold committed
500
#endif
501 502 503
 
#ifdef SMART_UNMAP
  pnode->assigned_nodes.insert(vnode);
Mac Newbold's avatar
Mac Newbold committed
504
#endif
505 506 507 508 509 510 511 512 513 514 515 516 517 518
 
  // set up links
  voedge_iterator vedge_it,end_vedge_it;
  tie(vedge_it,end_vedge_it) = out_edges(vv,VG);	    
  for (;vedge_it!=end_vedge_it;++vedge_it) {
    tb_vlink *vlink = get(vedge_pmap,*vedge_it);
    vvertex dest_vv = target(*vedge_it,VG);
    if (dest_vv == vv)
      dest_vv = source(*vedge_it,VG);
    tb_vnode *dest_vnode = get(vvertex_pmap,dest_vv);

    pedge pe;
    
    SDEBUG(cerr << "  edge to " << dest_vnode->name << endl);
Mac Newbold's avatar
Mac Newbold committed
519

520 521 522
    if (dest_vnode->assigned) {
      pvertex dest_pv = dest_vnode->assignment;
      tb_pnode *dest_pnode = get(pvertex_pmap,dest_pv);
Mac Newbold's avatar
Mac Newbold committed
523

524
      SDEBUG(cerr << "   goes to " << dest_pnode->name << endl);
Mac Newbold's avatar
Mac Newbold committed
525

526 527
      if (dest_pv == pv) {
	SDEBUG(cerr << "  trivial link" << endl);
528 529 530 531 532 533
	if (!allow_trivial_links) {
	    SADD(SCORE_NO_CONNECTION);
	    vlink->no_connection=true;
	    vinfo.no_connection++;
	    violated++;
	}
534
	vlink->link_info.type = tb_link_info::LINK_TRIVIAL;
Mac Newbold's avatar
Mac Newbold committed
535
      } else {
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	SDEBUG(cerr << "   finding link resolutions" << endl);
	// We need to calculate all possible link resolutions, stick them
	// in a nice datastructure along with their weights, and then
	// select one randomly.
	typedef vector<tb_link_info> resolution_vector;
	typedef vector<pvertex_list> switchlist_vector;

	resolution_vector resolutions(10);
	int resolution_index = 0;
	float total_weight = 0;

	// Direct link
	if (direct_link(dest_pv,pv,vlink,pe)) {
	  resolutions[resolution_index].type = tb_link_info::LINK_DIRECT;
	  resolutions[resolution_index].plinks.push_back(pe);
	  resolution_index++;
	  total_weight += LINK_RESOLVE_DIRECT;
	  SDEBUG(cerr << "    direct_link " << pe << endl);
	}
	// Intraswitch link
	pedge first,second;
	for (pvertex_set::iterator switch_it = pnode->switches.begin();
	     switch_it != pnode->switches.end();++switch_it) {
	  if (dest_pnode->switches.find(*switch_it) != dest_pnode->switches.end()) {
#ifdef FIX_SHARED_INTERFACES
	      if ((!find_link_to_switch(pv,*switch_it,vlink,first)) || 
		      (!find_link_to_switch(dest_pv,*switch_it,vlink,second))) {
		  //cerr << "No acceptable links" << endl;
		  continue;
	      }
#else
	    find_link_to_switch(pv,*switch_it,vlink,first);
	    find_link_to_switch(dest_pv,*switch_it,vlink,second);
Mac Newbold's avatar
Mac Newbold committed
569
#endif
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
	    resolutions[resolution_index].type = tb_link_info::LINK_INTRASWITCH;
	    resolutions[resolution_index].plinks.push_back(first);
	    resolutions[resolution_index].plinks.push_back(second);
	    resolutions[resolution_index].switches.push_front(*switch_it);
	    resolution_index++;
	    total_weight += LINK_RESOLVE_INTRASWITCH;
	    SDEBUG(cerr << "    intraswitch " << first << " and " << second << endl);
	  }
	}
	// Interswitch paths
	//cout << "Source switches list has " << pnode->switches.size() <<
	 //   " entries" << endl;
	for (pvertex_set::iterator source_switch_it = pnode->switches.begin();
	     source_switch_it != pnode->switches.end();
	     ++source_switch_it) {
	  //cout << "Source switch: " << get(pvertex_pmap,*source_switch_it)->name << endl;
	  //cout << "Dest switches list has " << dest_pnode->switches.size() <<
	   //   " entries" << endl;
	  //cout << "Dest pnode is " << dest_pnode->name << endl;
	  int tmp = 0;
	  for (pvertex_set::iterator dest_switch_it = dest_pnode->switches.begin();
	       dest_switch_it != dest_pnode->switches.end();
	       ++dest_switch_it) {
	    //cout << "Dest switch number " << ++tmp << endl;
	    //cout << "Dest switch: " << get(pvertex_pmap,*dest_switch_it)->name << endl;
	    if (*source_switch_it == *dest_switch_it) continue;
	    if (find_interswitch_path(*source_switch_it,*dest_switch_it,vlink->delay_info.bandwidth,
				      resolutions[resolution_index].plinks,
				      resolutions[resolution_index].switches) != 0) {
#ifdef FIX_SHARED_INTERFACES
	      if ((!find_link_to_switch(pv,*source_switch_it,vlink,first)) || 
		      (!find_link_to_switch(dest_pv,*dest_switch_it,vlink,second))) {
		  //cerr << "No acceptable links" << endl;
		  continue;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
604
	      }
605 606 607 608 609 610 611 612 613 614 615 616 617 618
#else
	      find_link_to_switch(pv,*source_switch_it,vlink,first);
	      find_link_to_switch(dest_pv,*dest_switch_it,vlink,second);
#endif

	      resolutions[resolution_index].type = tb_link_info::LINK_INTERSWITCH;
	      resolutions[resolution_index].plinks.push_front(first);
	      resolutions[resolution_index].plinks.push_back(second);
	      resolution_index++;
	      total_weight += LINK_RESOLVE_INTERSWITCH;
	      SDEBUG(cerr << "    interswitch " <<
		     get(pvertex_pmap,*source_switch_it)->name << " and " <<
		     get(pvertex_pmap,*dest_switch_it)->name << endl);
	    }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
619
	  }
620
	}
Christopher Alfeld's avatar
 
Christopher Alfeld committed
621

622 623 624 625 626 627 628 629 630 631 632 633 634
	// check for no link
	if (resolution_index == 0) {
	    //cerr << "No resolutions at all" << endl;
	  SDEBUG(cerr << "  Could not find any resolutions. Trying delay." <<
		 endl);

#if 0
	  // Create virtual delay node and link with special free vlinks.
	  vvertex delayv = make_delay_node(vlink);
	  tb_pnode *delaypnode = find_pnode(get(vvertex_pmap,delayv));
	  
	  // Assign delay node
	  if (add_node(delayv,pnode2vertex[delaypnode],false) == 1) {
Mac Newbold's avatar
Mac Newbold committed
635
#endif
636 637 638 639 640 641 642
	    SDEBUG(cerr << "Failed to delay." << endl);
	    SADD(SCORE_NO_CONNECTION);
	    vlink->no_connection=true;
	    vinfo.no_connection++;
	    violated++;
#if 0
	  }
643
#endif
644 645 646 647 648 649 650 651 652
	} else {
	    //cerr << "Some resolutions" << endl;
	  // Check to see if we are fixing a violation
	  if (vlink->no_connection) {
	    SDEBUG(cerr << "  Fixing previous violations." << endl);
	    SSUB(SCORE_NO_CONNECTION);
	    vlink->no_connection=false;
	    vinfo.no_connection--;
	    violated--;
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 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
	  
	  // Choose a link
	  int index;
	  if (!deterministic) {
	    float choice = std::random()%(int)total_weight;
	    for (index = 0;index < resolution_index;++index) {
	      switch (resolutions[index].type) {
	      case tb_link_info::LINK_DIRECT:
		choice -= LINK_RESOLVE_DIRECT; break;
	      case tb_link_info::LINK_INTRASWITCH:
		choice -= LINK_RESOLVE_INTRASWITCH; break;
	      case tb_link_info::LINK_INTERSWITCH:
		choice -= LINK_RESOLVE_INTERSWITCH; break;
	      case tb_link_info::LINK_UNKNOWN:
	      case tb_link_info::LINK_TRIVIAL:
		cerr << "Internal error: Should not be here." << endl;
		exit(1);
		break;
	      }
	      if (choice < 0) break;
	    }
	  } else {
	    // Deterministic
	    int bestindex;
	    int bestviolated = 10000;
	    double bestscore=10000.0;
	    int i;
	    for (i=0;i<resolution_index;++i) {
	      vlink->link_info = resolutions[i];
#ifdef FIX_PLINK_ENDPOINTS
	      score_link_info(*vedge_it,pnode,dest_pnode);
#else
	      score_link_info(*vedge_it);
#endif
	      if ((score <= bestscore) &&
		  (violated <= bestviolated)) {
		bestscore = score;
		bestviolated = violated;
		bestindex = i;
	      }
#ifdef FIX_PLINK_ENDPOINTS
	      unscore_link_info(*vedge_it,pnode,dest_pnode);
#else
	      unscore_link_info(*vedge_it);
#endif
	    }
	    index = bestindex;
	  }
#ifdef PENALIZE_UNUSED_INTERFACES
	  pnode->used_interfaces++;
#endif
	  vlink->link_info = resolutions[index];
	  SDEBUG(cerr << "  choice:" << vlink->link_info);
#ifdef FIX_PLINK_ENDPOINTS
	  score_link_info(*vedge_it,pnode,dest_pnode);
#else
	  score_link_info(*vedge_it);
#endif
Mac Newbold's avatar
Mac Newbold committed
712 713
	}
      }
714 715 716 717 718 719 720 721 722
#ifdef AUTO_MIGRATE
      if (dest_vnode->type.compare("lan") == 0) {
	  //cout << "Auto-migrating LAN " << dest_vnode->name << " (because of "
	   //   << vnode->name << ")" << endl;
	  remove_node(dest_vv);
	  pvertex lanpv = make_lan_node(dest_vv);
	  add_node(dest_vv,lanpv,false);
      }
#endif
Mac Newbold's avatar
Mac Newbold committed
723 724
    }
  }
725
  
Mac Newbold's avatar
Mac Newbold committed
726
  // finish setting up pnode
727 728 729 730 731
  pnode->current_load++;

#ifdef PENALIZE_UNUSED_INTERFACES
  assert(pnode->used_interfaces <= pnode->total_interfaces);
  SADD((pnode->total_interfaces - pnode->used_interfaces) * SCORE_UNUSED_INTERFACE);
Mac Newbold's avatar
Mac Newbold committed
732
#endif
733 734 735 736 737 738

  vnode->assignment = pv;
  vnode->assigned = true;
  if (pnode->current_load > pnode->max_load) {
    SDEBUG(cerr << "  load to high - penalty (" << pnode->current_load <<
	   ")" << endl);
Mac Newbold's avatar
Mac Newbold committed
739 740 741 742
    SADD(SCORE_PNODE_PENALTY);
    vinfo.pnode_load++;
    violated++;
  } else {
743
    SDEBUG(cerr << "  load is fine" << endl);
Mac Newbold's avatar
Mac Newbold committed
744
  }
745 746
  if (pnode->current_load == 1) {
    SDEBUG(cerr << "  new pnode" << endl);
Mac Newbold's avatar
Mac Newbold committed
747
    SADD(SCORE_PNODE);
748 749
#ifdef LOAD_BALANCE
    //SADD(SCORE_PNODE); // Yep, twice
Mac Newbold's avatar
Mac Newbold committed
750 751
#endif
  }
752 753 754 755 756 757 758
#ifdef LOAD_BALANCE
  //SSUB(SCORE_PNODE * (1.0 / pnode->max_load));
  //SSUB(SCORE_PNODE * (1 + powf(((pnode->current_load-1) * 1.0)/pnode->max_load,2)));
  //SADD(SCORE_PNODE * (1 + powf(((pnode->current_load) * 1.0)/pnode->max_load,2)));
  SSUB(SCORE_PNODE * (powf(1 + ((pnode->current_load-1) * 1.0)/pnode->max_load,2)));
  SADD(SCORE_PNODE * (powf(1 + ((pnode->current_load) * 1.0)/pnode->max_load,2)));
#endif
Mac Newbold's avatar
Mac Newbold committed
759 760 761 762 763 764

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

765
  // features/desires
Christopher Alfeld's avatar
 
Christopher Alfeld committed
766
  int fd_violated;
767
  double fds = fd_score(vnode,pnode,fd_violated);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
768 769 770 771 772
  SADD(fds);
  violated += fd_violated;
  vinfo.desires += fd_violated;

  // pclass
773 774
  if (pnode->my_class && (pnode->my_class->used == 0)) {
    SDEBUG(cerr << "  new pclass" << endl);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
775
    SADD(SCORE_PCLASS);
776
  }
777 778

  // vclass
779 780 781
  if (vnode->vclass != NULL) {
    double score_delta = vnode->vclass->assign_node(vnode->type);
    SDEBUG(cerr << "  vclass assign " << score_delta << endl);
782 783 784 785 786 787 788
    SADD(score_delta*SCORE_VCLASS);
    if (score_delta >= 1) {
      violated++;
      vinfo.vclass++;
    }
  }

789 790
  SDEBUG(cerr << "  assignment=" << vnode->assignment << endl);
  SDEBUG(cerr << "  new score=" << score << " new violated=" << violated << endl);
Christopher Alfeld's avatar
 
Christopher Alfeld committed
791

792 793 794
  if (pnode->my_class) {
    pclass_set(vnode,pnode);
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
795
  
Mac Newbold's avatar
Mac Newbold committed
796 797 798 799 800 801
  return 0;
}

// returns "best" direct link between a and b.
// best = less users
//        break ties with minimum bw_used
802
bool direct_link(pvertex a,pvertex b,tb_vlink *vlink,pedge &edge)
Mac Newbold's avatar
Mac Newbold committed
803
{
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
  pvertex dest_pv;
  pedge best_pedge;
  tb_plink *plink;
  tb_plink *best_plink = NULL;
  poedge_iterator pedge_it,end_pedge_it;
  int best_users;
  double best_distance;
  tie(pedge_it,end_pedge_it) = out_edges(a,PG);
  for (;pedge_it!=end_pedge_it;++pedge_it) {
    dest_pv = target(*pedge_it,PG);
    if (dest_pv == a)
      dest_pv = source(*pedge_it,PG);
    if (dest_pv == b) {
      plink = get(pedge_pmap,*pedge_it);
      int users = plink->nonemulated;
      if (! vlink->emulated) {
	users += plink->emulated;
      }
      tb_delay_info physical_delay;
      physical_delay.bandwidth = plink->delay_info.bandwidth - plink->bw_used;
      physical_delay.delay = plink->delay_info.delay;
      physical_delay.loss = plink->delay_info.loss;
      double distance = vlink->delay_info.distance(physical_delay);
      if (distance == -1) {distance = DBL_MAX;}
      
      if ((! best_plink) ||
	  (users < best_users) ||
	  ((users == best_users) && (distance < best_distance))) {
	best_users = users;
	best_distance = distance;
	best_pedge = *pedge_it;
	best_plink = plink;
Mac Newbold's avatar
Mac Newbold committed
836 837 838
      }
    }
  }
839 840 841 842 843 844
  if (best_plink == NULL) {
    return false;
  } else {
    edge = best_pedge;
    return true;
  }
Mac Newbold's avatar
Mac Newbold committed
845 846
}

847 848
bool find_link_to_switch(pvertex pv,pvertex switch_pv,tb_vlink *vlink,
			 pedge &out_edge)
Mac Newbold's avatar
Mac Newbold committed
849
{
850 851
  pvertex dest_pv;
  double best_distance = 1000.0;
Mac Newbold's avatar
Mac Newbold committed
852
  int best_users = 1000;
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
  pedge best_pedge;
  bool found_best=false;
  poedge_iterator pedge_it,end_pedge_it;
  tie(pedge_it,end_pedge_it) = out_edges(pv,PG);
  for (;pedge_it!=end_pedge_it;++pedge_it) {
    dest_pv = target(*pedge_it,PG);
    if (dest_pv == pv)
      dest_pv = source(*pedge_it,PG);
    if (dest_pv == switch_pv) {
      tb_plink *plink = get(pedge_pmap,*pedge_it);
      tb_delay_info physical_delay;
      physical_delay.bandwidth = plink->delay_info.bandwidth - plink->bw_used;
      physical_delay.delay = plink->delay_info.delay;
      physical_delay.loss = plink->delay_info.loss;
      double distance = vlink->delay_info.distance(physical_delay);
      int users;

      // For sticking emulated links in emulated links we only care
      // about the distance.
      users = plink->nonemulated;
      if (! vlink->emulated) {
	users += plink->emulated;
      }
      if (distance == -1) {
	// -1 == infinity
	distance = DBL_MAX;
      }
      if ((users < best_users) ||
	  ((users  == best_users) && (distance < best_distance))) {
	best_pedge = *pedge_it;
	best_distance = distance;
	found_best = true;
	best_users = plink->emulated+plink->nonemulated;
Mac Newbold's avatar
Mac Newbold committed
886 887 888 889
      }
    }
  }

890 891 892 893 894 895 896 897 898 899 900
#ifdef FIX_SHARED_INTERFACES
  if ((!vlink->emulated) && found_best && (best_users > 0)) {
      return false;
  }
#endif
  if (found_best) {
    out_edge = best_pedge;
    return true;
  } else {
    return false;
  }
Mac Newbold's avatar
Mac Newbold committed
901 902
}

903 904 905 906
// 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.
907 908 909
int find_interswitch_path(pvertex src_pv,pvertex dest_pv,
			  int bandwidth,pedge_path &out_path,
			  pvertex_list &out_switches)
Mac Newbold's avatar
Mac Newbold committed
910
{
911 912 913 914 915 916
  // 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.
917 918 919 920 921 922 923

  svertex src_sv = get(pvertex_pmap,src_pv)->sgraph_switch;
  svertex dest_sv = get(pvertex_pmap,dest_pv)->sgraph_switch;

  sedge current_se;
  svertex current_sv = dest_sv;
  switch_pred_map &preds = *switch_preds[src_sv];
Mac Newbold's avatar
Mac Newbold committed
924
  
925
  if (preds[dest_sv] == dest_sv) {
926
    // unreachable
Mac Newbold's avatar
Mac Newbold committed
927 928
    return 0;
  }
929 930 931 932 933
  while (current_sv != src_sv) {
    out_switches.push_front(get(svertex_pmap,current_sv)->mate);
    current_se = edge(current_sv,preds[current_sv],SG).first;
    out_path.push_back(get(sedge_pmap,current_se)->mate);
    current_sv = preds[current_sv];
934
  }
935
  out_switches.push_front(get(svertex_pmap,current_sv)->mate);
936
  return 1;
Mac Newbold's avatar
Mac Newbold committed
937 938 939
}

// this does scoring for over users and over bandwidth on edges.
940 941 942 943 944
#ifdef FIX_PLINK_ENDPOINTS
void score_link(pedge pe,vedge ve,tb_pnode *src_pnode, tb_pnode *dst_pnode)
#else
void score_link(pedge pe,vedge ve)
#endif
Mac Newbold's avatar
Mac Newbold committed
945
{
946 947
  tb_plink *plink = get(pedge_pmap,pe);
  tb_vlink *vlink = get(vedge_pmap,ve);
Mac Newbold's avatar
Mac Newbold committed
948

949 950
  SDEBUG(cerr << "  score_link(" << pe << ") - " << plink->name << " / " <<
	 vlink->name << endl);
Mac Newbold's avatar
Mac Newbold committed
951

952 953 954 955 956 957
#ifdef SCORE_DEBUG_LOTS
  cerr << *plink;
  cerr << *vlink;
#endif
  
  if (plink->type == tb_plink::PLINK_NORMAL) {
958 959
    // need too account for three things here, the possiblity of a new plink
    // the user of a new emulated link, and a possible violation.
960 961
    if (vlink->emulated) {
      plink->emulated++;
962 963
      SADD(SCORE_EMULATED_LINK);
    }
964 965
    else plink->nonemulated++;
    if (plink->nonemulated+plink->emulated == 1) {
966
      // new link
967
      SDEBUG(cerr << "    first user" << endl);
968
      SADD(SCORE_DIRECT_LINK);
Mac Newbold's avatar
Mac Newbold committed
969
    } else {
970 971
      // check for violation, basically if this is the first of it's
      // type to be added.
972 973 974 975 976 977 978 979 980 981 982
      if (((! vlink->emulated) && (plink->nonemulated == 1)) ||
	  ((vlink->emulated) && (plink->emulated == 1))) {
	SDEBUG(cerr << "    link user - penalty" << endl);
	SADD(SCORE_DIRECT_LINK_PENALTY);
	vinfo.link_users++;
	violated++;
      }
#ifdef FIX_SHARED_INTERFACES
      if ((! vlink->emulated) && (plink->nonemulated > 1)) {
	  //cerr << "Penalizing overused link" << endl;
	  assert(false);
983 984 985
	  SADD(SCORE_DIRECT_LINK_PENALTY);
	  vinfo.link_users++;
	  violated++;
986
      }
987
#endif
Mac Newbold's avatar
Mac Newbold committed
988
    }
989
  }
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

#ifdef FIX_PLINK_ENDPOINTS
  if (plink->fixends) {
      // Add this to the list of endpoints used by this plink
      nodepair p;
      if (src_pnode->name < dst_pnode->name) {
	  p.first = src_pnode->name;
	  p.second = dst_pnode->name;
      } else {
	  p.first = dst_pnode->name;
	  p.second = src_pnode->name;
      }
      if (plink->vedge_counts.find(p) == plink->vedge_counts.end()) {
	  plink->vedge_counts[p] = 1;
      } else {
	  plink->vedge_counts[p]++;
      }
      // Figure out if we need to add a violation
      if (p == plink->current_endpoints) {
	  plink->current_count++;
      } else {
	  // Nope, we just passed the old leader
	  if (plink->vedge_counts[p] > plink->current_count) {
	      plink->current_endpoints = p;
	      plink->current_count++;
	  } else {
	      // Yup, it's a new violation
	      SADD(5 * SCORE_DIRECT_LINK_PENALTY);
	      vinfo.incorrect_endpoints++;
	      violated++;
	  }
      }
  }
#endif

  if (plink->type != tb_plink::PLINK_LAN) {
    tb_delay_info physical_delay;
    physical_delay.bandwidth = plink->delay_info.bandwidth - plink->bw_used;
    physical_delay.delay = plink->delay_info.delay;
    physical_delay.loss = plink->delay_info.loss;
1030
    
1031 1032 1033 1034 1035
    double distance = vlink->delay_info.distance(physical_delay);

    plink->bw_used += vlink->delay_info.bandwidth;
#ifdef PENALIZE_BANDWIDTH
    SADD(plink->penalty * (vlink->delay_info.bandwidth * 1.0) / (plink->delay_info.bandwidth));
Mac Newbold's avatar
Mac Newbold committed
1036
#endif
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

    if (distance == -1) {
      // violation
      SDEBUG(cerr << "    outside delay requirements." << endl);
      violated++;
      vinfo.delay++;
      SADD(SCORE_OUTSIDE_DELAY);
    } else {
      SADD(distance * SCORE_DELAY);
    }
Mac Newbold's avatar
Mac Newbold committed
1047 1048 1049
  }
}

1050 1051 1052 1053 1054
#ifdef FIX_PLINK_ENDPOINTS
void unscore_link(pedge pe,vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode)
#else
void unscore_link(pedge pe,vedge ve)
#endif
Mac Newbold's avatar
Mac Newbold committed
1055
{
1056 1057
  tb_plink *plink = get(pedge_pmap,pe);
  tb_vlink *vlink = get(vedge_pmap,ve);
Mac Newbold's avatar
Mac Newbold committed
1058

1059 1060 1061 1062 1063 1064
  SDEBUG(cerr << "  unscore_link(" << pe << ") - " << plink->name << " / " <<
	 vlink->name << endl);

#ifdef SCORE_DEBUG_LOTS
  cerr << *plink;
  cerr << *vlink;
Mac Newbold's avatar
Mac Newbold committed
1065 1066
#endif

1067 1068 1069
  if (plink->type == tb_plink::PLINK_NORMAL) {
    if (vlink->emulated) {
      plink->emulated--;
1070 1071
      SSUB(SCORE_EMULATED_LINK);
    } else {
1072 1073 1074 1075 1076 1077 1078 1079 1080
      plink->nonemulated--;
#ifdef FIX_SHARED_INTERFACES
      if (plink->nonemulated >= 1) {
	  //cerr << "Freeing overused link" << endl;
	  SSUB(SCORE_DIRECT_LINK_PENALTY);
	  vinfo.link_users--;
	  violated--;
      }
#endif
1081
    }
1082
    if (plink->nonemulated+plink->emulated == 0) {
1083
      // link no longer used
1084
      SDEBUG(cerr << "   freeing link" << endl);
1085 1086 1087 1088
      SSUB(SCORE_DIRECT_LINK);
    } else {
      // check to see if re freed up a violation, basically did
      // we remove the last of it's link type.
1089 1090
      if ((vlink->emulated && (plink->emulated == 0)) ||
	  ((! vlink->emulated) && plink->nonemulated == 0)) {
1091
	// all good
1092
	SDEBUG(cerr << "   users ok" << endl);
1093 1094 1095 1096
	SSUB(SCORE_DIRECT_LINK_PENALTY);
	vinfo.link_users--;
	violated--;
      }
Mac Newbold's avatar
Mac Newbold committed
1097 1098
    }
  }
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
#ifdef FIX_PLINK_ENDPOINTS
  if (plink->fixends) {
      // Subtract this from the list of endpoints for this plink
      nodepair p;
      if (src_pnode->name < dst_pnode->name) {
	  p.first = src_pnode->name;
	  p.second = dst_pnode->name;
      } else {
	  p.first = dst_pnode->name;
	  p.second = src_pnode->name;
      }
      int newcount;
      assert(plink->vedge_counts[p] > 0);
      newcount = --plink->vedge_counts[p];
      if (newcount == 0) {
	  plink->vedge_counts.erase(p);
      }

      // Ok, let's see if this removes any violations
      if (p == plink->current_endpoints) {
	  // Need to re-find the heaviest endpoint count
	  nodepair_count_map::iterator it = plink->vedge_counts.begin();
	  int highestcount = 0;
	  nodepair highestp;
	  while (it != plink->vedge_counts.end()) {
	      if (it->second > highestcount) {
		  highestcount = it->second;
		  highestp = it->first;
	      }
	      it++;
	  }
	  plink->current_endpoints = highestp;
	  plink->current_count = highestcount;
	  if (newcount < highestcount) {
	      // Yep, we just got rid of a violation
	      SSUB(5*SCORE_DIRECT_LINK_PENALTY);
	      vinfo.incorrect_endpoints--;
	      violated--;
	  }
      } else {
	  // Yep, we just got rid of a violation
	  SSUB(5*SCORE_DIRECT_LINK_PENALTY);
	  vinfo.incorrect_endpoints--;
	  violated--;
      }
  }
#endif
Mac Newbold's avatar
Mac Newbold committed
1146 1147
  
  // bandwidth check
1148 1149 1150 1151
  if (plink->type != tb_plink::PLINK_LAN) {
    plink->bw_used -= vlink->delay_info.bandwidth;
#ifdef PENALIZE_BANDWIDTH
    SSUB(plink->penalty * (vlink->delay_info.bandwidth * 1.0) / (plink->delay_info.bandwidth));
Mac Newbold's avatar
Mac Newbold committed
1152
#endif
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

    tb_delay_info physical_delay;
    physical_delay.bandwidth = plink->delay_info.bandwidth - plink->bw_used;
    physical_delay.delay = plink->delay_info.delay;
    physical_delay.loss = plink->delay_info.loss;
    double distance = vlink->delay_info.distance(physical_delay);

    if (distance == -1) {
      // violation
      SDEBUG(cerr << "    removing delay violation." << endl);
      violated--;
      vinfo.delay--;
      SSUB(SCORE_OUTSIDE_DELAY);
    } else {
      SSUB(distance * SCORE_DELAY);
    }
Mac Newbold's avatar
Mac Newbold committed
1169
  }
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1170

1171
  vlink->link_info.type = tb_link_info::LINK_UNKNOWN;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1172 1173
}

1174
double fd_score(tb_vnode *vnode,tb_pnode *pnode,int &fd_violated)
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1175 1176
{
  double fd_score=0;
1177 1178
  fd_violated=0;

Christopher Alfeld's avatar
 
Christopher Alfeld committed
1179
  double value;
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
  tb_vnode::desires_map::iterator desire_it;
  tb_pnode::features_map::iterator feature_it;
  for (desire_it = vnode->desires.begin();
       desire_it != vnode->desires.end();
       desire_it++) {
    feature_it = pnode->features.find((*desire_it).first);
    SDEBUG(cerr << "  desire = " << (*desire_it).first << " " <<
	   (*desire_it).second << endl);

    if (feature_it == pnode->features.end()) {
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1190
      // Unmatched desire.  Add cost.
1191 1192
      SDEBUG(cerr << "    unmatched" << endl);
      value = (*desire_it).second;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1193 1194
      fd_score += SCORE_DESIRE*value;
      if (value >= 1) {
1195
	fd_violated++;
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1196 1197 1198
      }
    }
  }
1199 1200 1201 1202 1203 1204 1205
  for (feature_it = pnode->features.begin();
       feature_it != pnode->features.end();++feature_it) {
    desire_it = vnode->desires.find((*feature_it).first);
    SDEBUG(cerr << "  feature = " << (*feature_it).first
	   << " " << (*feature_it).second << endl);

    if (desire_it == vnode->desires.end()) {
Christopher Alfeld's avatar
 
Christopher Alfeld committed
1206
      // Unused feature.  Add weight