score.cc 68 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
/*
Robert Ricci's avatar
Robert Ricci committed
2
 * Copyright (c) 2000-2010 University of Utah and the Flux Group.
3
 *
4
 * {{{EMULAB-LICENSE
5
 *
6
 * This file is part of the Emulab network testbed software.
7
 *
8 9 10 11
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
12
 *
13 14 15 16
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
17
 *
18 19
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * }}}
Robert Ricci's avatar
Robert Ricci committed
22 23
 */

24
static const char rcsid[] = "$Id: score.cc,v 1.69 2009-12-09 22:53:44 ricci Exp $";
25

26
#include "port.h"
27

28
#include <iostream>
29 30 31 32
#include <float.h>

#include <boost/config.hpp>
#include <boost/utility.hpp>
33
#include BOOST_PMAP_HEADER
34 35 36 37
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;
38 39

#include "common.h"
40
#include "vclass.h"
41
#include "delay.h"
42
#include "physical.h"
43
#include "virtual.h"
44 45
#include "pclass.h"
#include "score.h"
46
#include "featuredesire.h"
47

48
#include "math.h"
49

50
extern switch_pred_map_map switch_preds;
51

52
extern bool disable_pclasses;
53 54
extern bool strategy_balance;
extern bool strategy_pack;
55

56
double score;			// The score of the current mapping
57 58 59 60 61
int violated;			// How many times the restrictions
				// have been violated.

violated_info vinfo;		// specific info on violations

62
extern tb_vgraph VG;		// virtual graph
63
extern tb_pgraph PG;		// physical grpaph
64
extern tb_sgraph SG;		// switch fabric
65

66 67
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);
68
bool find_best_link(pvertex pv,pvertex switch_pv,tb_vlink *vlink,
69
                    pedge &out_edge, bool check_src_iface,
70
                    bool check_dst_iface);
71 72 73
int find_interswitch_path(pvertex src_pv,pvertex dest_pv,
			  int bandwidth,pedge_path &out_path,
			  pvertex_list &out_switches);
74 75
score_and_violations score_fds(tb_vnode *vnode, tb_pnode *pnode, bool add);

76 77 78 79
void score_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode,
	tb_vnode *src_vnode, tb_vnode *dst_vnode);
void unscore_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode,
	tb_vnode *src_vnode, tb_vnode *dst_vnode);
80 81 82 83

#ifdef FIX_PLINK_ENDPOINTS
void score_link_endpoints(pedge pe);
#endif
84 85

#ifdef SCORE_DEBUG_MORE
86 87
#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
88 89 90 91 92
#else
#define SADD(amount) score += amount
#define SSUB(amount) score -= amount
#endif

93 94 95 96
// For convenience, so we can easily turn on or off one statement
#define SDEBADD(amount) cerr << "SADD: " << #amount << "=" << amount << " from " << score;score+=amount;cerr << " to " << score << endl
#define SDEBSUB(amount)  cerr << "SSUB: " << #amount << "=" << amount << " from " << score;score-=amount;cerr << " to " << score << endl

97 98 99 100 101 102
#ifdef SCORE_DEBUG_MAX
// Handy way to print only the first N debugging messages, since that's usually
// enough to get the idea
static unsigned long scoredebugcount = 0;
#endif

103
#ifdef SCORE_DEBUG
104 105 106
#ifdef SCORE_DEBUG_MAX
#define SDEBUG(a) if (scoredebugcount++ < SCORE_DEBUG_MAX) { a; }
#else
107
#define SDEBUG(a) a
108
#endif
109
#else
110
#define SDEBUG(a)
111 112
#endif

113 114 115
#define MIN(a,b) (((a) < (b))? (a) : (b))
#define MAX(a,b) (((a) > (b))? (a) : (b))

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/*
 * 'Constants' used in scoring. These can be changed, but it MUST be
 * done BEFORE any actual scoring is done.
 */
#ifdef PENALIZE_BANDWIDTH
float SCORE_DIRECT_LINK = 0.0;
float SCORE_INTRASWITCH_LINK = 0.0;
float SCORE_INTERSWITCH_LINK = 0.0;
#else
float SCORE_DIRECT_LINK = 0.01;
float SCORE_INTRASWITCH_LINK = 0.02;
float SCORE_INTERSWITCH_LINK = 0.2;
#endif
float SCORE_DIRECT_LINK_PENALTY = 0.5;
float SCORE_NO_CONNECTION = 0.5;
float SCORE_PNODE = 0.2;
float SCORE_PNODE_PENALTY = 0.5;
float SCORE_SWITCH = 0.5;
float SCORE_UNASSIGNED = 1.0;
float SCORE_MISSING_LOCAL_FEATURE = 1.0;
float SCORE_OVERUSED_LOCAL_FEATURE = 0.5;
#ifdef NO_PCLASS_PENALTY
float SCORE_PCLASS = 0.0;
#else
float SCORE_PCLASS = 0.5;
#endif
float SCORE_VCLASS = 1.0;
float SCORE_EMULATED_LINK = 0.01;
float SCORE_OUTSIDE_DELAY = 0.5;
#ifdef PENALIZE_UNUSED_INTERFACES
float SCORE_UNUSED_INTERFACE = 0.04;
#endif
float SCORE_TRIVIAL_PENALTY = 0.5;

float SCORE_TRIVIAL_MIX = 0.5;

float SCORE_SUBNODE = 0.5;
float SCORE_MAX_TYPES = 0.15;
float LINK_RESOLVE_TRIVIAL = 8.0;
float LINK_RESOLVE_DIRECT = 4.0;
float LINK_RESOLVE_INTRASWITCH = 2.0;
float LINK_RESOLVE_INTERSWITCH = 1.0;
float VIOLATION_SCORE = 1.0;

float opt_nodes_per_sw = 5.0;

162 163 164 165
/*
 * score()
 * Returns the score.
 */
166
double get_score() {return score;}
167 168 169 170 171 172 173 174

/*
 * init_score()
 * This initialized the scoring system.  It also clears all
 * assignments.
 */
void init_score()
{
175
  SDEBUG(cerr << "SCORE: Initializing" << endl);
176 177 178
  score=0;
  violated=0;

179 180 181 182 183
  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;
184 185 186 187
    SADD(SCORE_UNASSIGNED);
    vinfo.unassigned++;
    violated++;
  }
188 189 190 191
  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);
192 193 194 195 196
    SADD(SCORE_NO_CONNECTION);
    vlink->no_connection=true;
    vinfo.no_connection++;
    vlink->link_info.type_used = tb_link_info::LINK_UNMAPPED;
    violated++;
197
  }
198 199
  pvertex_iterator pvertex_it,end_pvertex_it;
  tie(pvertex_it,end_pvertex_it) = vertices(PG);
200
  /*
201 202 203 204 205 206
  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;
207
  }
208
  */
209 210 211 212 213 214 215
  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;
216 217
  }

218
  SDEBUG(cerr << "  score=" << score << " violated=" << violated << endl);
219
}
220

221 222 223 224 225 226 227 228
/*
 * Find all possbile resolutions for a link. The possible resolutions are placed
 * into the resolutions parameter, and the total weight of all resolutions are
 * returned.
 */
float find_link_resolutions(resolution_vector &resolutions, pvertex pv,
    pvertex dest_pv, tb_vlink *vlink, tb_pnode *pnode, tb_pnode *dest_pnode,
    bool flipped) {
229
  SDEBUG(cerr << "   finding link resolutions from " << pnode->name << " to " << dest_pnode->name << endl);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  /* 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<pvertex_list> switchlist_vector;

  float total_weight = 0;

  // Trivial link
  if (dest_pv == pv) {
    SDEBUG(cerr << "  trivial link" << endl);

    /*
     * Sometimes, we might not want to let a vlink be resolved as a
     * trivial link
     */
    if (allow_trivial_links && vlink->allow_trivial) {
      tb_link_info info(tb_link_info::LINK_TRIVIAL);
      resolutions.push_back(info);
      total_weight += LINK_RESOLVE_TRIVIAL;
    }

    /*
     * This is to preserve some old behaviour - if we see a trivial link, that's
     * the only one we consider using. Need to revisit this to see if it's always
     * desirable.
     */
    return total_weight;
  }

  pedge pe;
261
  // Direct link (have to check both interfaces if they are fixed)
262
  if (find_best_link(dest_pv,pv,vlink,pe,true,true)) {
263 264 265 266
    tb_link_info info(tb_link_info::LINK_DIRECT);
    info.plinks.push_back(pe);
    resolutions.push_back(info);
    total_weight += LINK_RESOLVE_DIRECT;
267
    SDEBUG(cerr << "    added a direct_link " << pe << endl);
268
  }
269 270 271 272 273 274 275 276 277 278 279 280

  /*
   * IMPORTANT NOTE ABOUT find_best_link -
   *
   * We have to tell it whether it is supposed to check the source of the
   * vlink, the dest of the vlink, or both, for fixed interfaces. Of course, we
   * only want to check the 'node' side, not the switch side, of interswitch
   * and intraswitch paths. Of course, which end we're looking at depends on
   * the order we're traversing the vlink - the flipped variable. This is why
   * you see 'flipped' and '!flipped' passed to find_best_link() below.
   */

281 282 283 284 285 286
  // 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()) {
287
      SDEBUG(cerr << "    intraswitch: both are connected to " << *switch_it << endl);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      bool first_link, second_link;
      /*
       * Check to see if either, or both, pnodes are actually
       * the switch we are looking for
       */
      if (pv == *switch_it) {
        first_link = false;
      } else {
        first_link = true;
      }
      if (dest_pv == *switch_it) {
        second_link = false;
      } else {
        second_link = true;
      }

      /*
       * Intraswitch links to switches are not allowed - they
       * will be caught as direct links above
       */
      if (pnode->is_switch || dest_pnode->is_switch) {
        SDEBUG(cerr << "    intraswitch failed - switch" << endl;)
          continue;
      }

      if (first_link) {
314
        SDEBUG(cerr << "    intraswitch: finding first link" << endl;)
315 316
        // See note above
        if (!find_best_link(pv,*switch_it,vlink,first,!flipped,flipped)) {
317 318 319 320 321 322 323 324
          SDEBUG(cerr << "    intraswitch failed - no link first" <<
              endl;)
            // No link to this switch
            continue;
        }
      }

      if (second_link) {
325
        SDEBUG(cerr << "    intraswitch: finding second link (" <<  ")" << endl;)
326 327
        // See note above
        if (!find_best_link(dest_pv,*switch_it,vlink,second,flipped,!flipped)) {
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
          // No link to this switch
          SDEBUG(cerr << "    intraswitch failed - no link second" <<
              endl;)
            continue;
        }
      }

      tb_link_info info(tb_link_info::LINK_INTRASWITCH);
      /*
       * Order these need to go in depends on flipped bit
       */
      if (flipped) {
        if (second_link) {
          info.plinks.push_back(second);
        }
        if (first_link) {
          info.plinks.push_back(first);
        }
      } else {
        if (first_link) {
          info.plinks.push_back(first);
        }
        if (second_link) {
          info.plinks.push_back(second);
        }
      }
      info.switches.push_front(*switch_it);
      resolutions.push_back(info);
356

357 358 359 360 361
      total_weight += LINK_RESOLVE_INTRASWITCH;
      SDEBUG(cerr << "    intraswitch " << first << " and " << second
          <<                     endl);
    }
  }
362 363 364 365 366 367 368

  // If we're not allowed to use interswitch links for this vlink, just
  // skip all of the attempts to find interswitch links
  if (!vlink->allow_interswitch){
      // Bad Rob, no GOTO
      goto skip_interswitch_links;
  }
369 370 371 372 373 374 375 376 377 378
  for (pvertex_set::iterator source_switch_it = pnode->switches.begin();
      source_switch_it != pnode->switches.end();
      ++source_switch_it) {
    for (pvertex_set::iterator dest_switch_it =
        dest_pnode->switches.begin();
        dest_switch_it != dest_pnode->switches.end();
        ++dest_switch_it) {
      if (*source_switch_it == *dest_switch_it) continue;
      tb_link_info info(tb_link_info::LINK_INTERSWITCH);

379 380 381 382 383 384 385 386
      /*
       * Check to see if either, or both, pnodes are actually the
       * switches we are looking for
       */
      bool first_link, second_link;
      if ((pv == *source_switch_it) || (pv ==
            *dest_switch_it)) {
        first_link = false;
387
        SDEBUG(cerr << "    interswitch: not first link in a path" << endl);
388
      } else {
389
        SDEBUG(cerr << "    interswitch: *is* first link in a path" << endl);
390 391 392 393 394
        first_link = true;
      }
      if ((dest_pv == *source_switch_it) ||
          (dest_pv == *dest_switch_it)) {
        second_link = false;
395
        SDEBUG(cerr << "    interswitch: not second link in a path" << endl);
396
      } else {
397
        SDEBUG(cerr << "    interswitch: *is* second link in a path" << endl);
398 399
        second_link = true;
      }
400

401 402
      // Get link objects
      if (first_link) {
403 404
        // See note above
        if (!find_best_link(pv,*source_switch_it,vlink,first,!flipped,flipped)) {
405 406 407
            // No link to this switch
            SDEBUG(cerr << "    interswitch failed - no first link"
                << endl;)
408 409
              continue;
          }
410 411 412
      }

      if (second_link) {
413 414 415
        // See note above
        if (!find_best_link(dest_pv,*dest_switch_it,vlink,second,flipped,!flipped)) {
          // No link to this switch
416 417
          SDEBUG(cerr << "    interswitch failed - no second link" <<                                          endl;)
            continue;
418
        }
419 420 421 422 423 424 425
      }

      // For regular links, we just grab the vlink's bandwidth; for links
      // where we're matching the link speed to the 'native' one for the
      // interface, we have to look up interface speeds on both ends.
      int bandwidth;
      if (vlink->delay_info.adjust_to_native_bandwidth) {
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 457 458 459
          // Grab the actual plink objects for both pedges - it's possible for
          // one or both to be missing if we're linking directly to a switch
          // (as with a LAN)
          tb_plink *first_plink = NULL;
          tb_plink *second_plink = NULL;
          if (first_link) {
              first_plink = get(pedge_pmap,first);
          }
          if (second_link) {
              second_plink = get(pedge_pmap,second);
          }

          if (first_plink != NULL && second_plink != NULL) {
              // If both endpoints are not switches, we use the minimum
              // bandwidth
              bandwidth = min(first_plink->delay_info.bandwidth,
                      second_plink->delay_info.bandwidth);
          } else if (first_plink == NULL) {
              // If one end is a switch, use the bandwidth from the other
              // end
              bandwidth = second_plink->delay_info.bandwidth;
          } else if (second_plink == NULL) {
              bandwidth = first_plink->delay_info.bandwidth;
          } else {
              // Both endpoints are switches! (eg. this might be a link between
              // two LANs): It is not at all clear what the right semantics
              // for this would be, and unfortunately, we can't catch this
              // earlier. So, exiting with an error is crappy, but it's
              // unlikely to happen in our regular use, and it's the best we
              // can do.
              cerr << "*** Using bandwidth adjustment on virutal links " <<
                      "between switches not allowed " << endl;
              exit(EXIT_FATAL);
          }
460
      } else {
461
          // If not auto-adjusting, just use the specified bandwidth
462 463
          bandwidth = vlink->delay_info.bandwidth;
      }
464

465 466 467 468
      // Find a path on the switch fabric between these two switches
      if (find_interswitch_path(*source_switch_it,*dest_switch_it,bandwidth,
            info.plinks, info.switches) != 0) {
        // Okay, we found a real resolution!
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
        if (flipped) { // Order these need to go in depends on flipped bit
          if (second_link) {
            info.plinks.push_front(second);
          }
          if (first_link) {
            info.plinks.push_back(first);
          }
        } else {
          if (first_link) {
           info.plinks.push_front(first);
          }
          if (second_link) {
            info.plinks.push_back(second);
          }
        }
        resolutions.push_back(info);
        total_weight += LINK_RESOLVE_INTERSWITCH;
        SDEBUG(cerr << "    interswitch " <<
            get(pvertex_pmap,*source_switch_it)->name
            << " and " <<
            get(pvertex_pmap,*dest_switch_it)->name <<
            endl);
      } else {
        SDEBUG(cerr << "    interswitch failed - no switch path";)
      }
    }
  }
496

497 498
skip_interswitch_links:

499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
  return total_weight;

}


/*
 * Return the cost of a resolution
 */
inline float resolution_cost(tb_link_info::linkType res_type) {
    switch (res_type) {
	case tb_link_info::LINK_DIRECT:
	    return LINK_RESOLVE_DIRECT; break;
	case tb_link_info::LINK_INTRASWITCH:
	    return LINK_RESOLVE_INTRASWITCH; break;
	case tb_link_info::LINK_INTERSWITCH:
	    return LINK_RESOLVE_INTERSWITCH; break;
	case tb_link_info::LINK_UNMAPPED:
	case tb_link_info::LINK_TRIVIAL:
517 518
	default:
	    // These shouldn't be passed in: fall through to below and die
519 520
	break;
    }
521

522 523
    cerr << "*** Internal error: Should not be here. (resolution_cost)" << endl;
    exit(EXIT_FATAL);
524 525 526 527 528 529
}

/*
 * Mark a vlink as unassigned
 */
void mark_vlink_unassigned(tb_vlink *vlink) {
530 531
    SDEBUG(cerr << "    marking " << vlink->name << " unassigned" << endl;)
    assert(!vlink->no_connection);
532 533 534 535 536 537 538
    SADD(SCORE_NO_CONNECTION);
    vlink->no_connection=true;
    vinfo.no_connection++;
    vlink->link_info.type_used = tb_link_info::LINK_UNMAPPED;
    violated++;
}

539 540 541 542 543 544 545 546 547 548 549 550
/*
 * Mark a vlink as assigned: fix up violations
 */
void mark_vlink_assigned(tb_vlink *vlink) {
    SDEBUG(cerr << "    marking " << vlink->name << " assigned" << endl;)
    assert(vlink->no_connection);
    SSUB(SCORE_NO_CONNECTION);
    vlink->no_connection=false;
    vinfo.no_connection--;
    violated--;
}

551 552 553 554 555 556 557 558 559 560
/*
 * Resolve an individual vlink
 */
void resolve_link(vvertex vv, pvertex pv, tb_vnode *vnode, tb_pnode *pnode,
    bool deterministic, vedge edge) {
  tb_vlink *vlink = get(vedge_pmap,edge);
  vvertex dest_vv = target(edge,VG);

  if (dest_vv == vv) {
    dest_vv = source(edge,VG);
561
    SDEBUG(cerr << "  dest_vv is backwards" << endl);
562 563 564 565 566 567 568 569 570
  }

  /*
   * Indicates that we've assigned the nodes in reverse order compared to
   * what's in the vlink, so we need to reverse the ordering in the
   * pedge_path
   */
  bool flipped = false;
  if (vlink->src != vv) {
571
    SDEBUG(cerr << "  vlink is flipped" << endl);
572
    flipped = true;
573
    assert(vlink->dst == vv);
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  }

  /*
   * Find the link's destination
   */
  tb_vnode *dest_vnode = get(vvertex_pmap,dest_vv);
  SDEBUG(cerr << "  edge to " << dest_vnode->name << endl);

  /*
   * If the other end of the link is assigned, we can go ahead and try to
   * find a mapping for the link
   */
  if (dest_vnode->assigned) {
    pvertex dest_pv = dest_vnode->assignment;
    tb_pnode *dest_pnode = get(pvertex_pmap,dest_pv);

    SDEBUG(cerr << "   goes to " << dest_pnode->name << endl);

    if (dest_pv == pv) {
      SDEBUG(cerr << "  trivial link" << endl);
594

595 596 597 598 599 600 601
      if (allow_trivial_links && vlink->allow_trivial) {
        vlink->link_info.type_used = tb_link_info::LINK_TRIVIAL;
        /*
         * XXX - okay, this is really bad, but score_link_info
         * doesn't usually get called for trivial links, and
         * letting them fall through into the 'normal' link code
         * below is disatrous!
602 603
         * Note: We can't get here when doing adjust_to_native_bandwidth,
         * since it's illegal to allow trivial links when it's in use.
604
         */
605 606 607 608
        SDEBUG(cerr << "    allowed" << endl;)
        if (vlink->no_connection) {
          mark_vlink_assigned(vlink);
        }
609 610
        score_link_info(edge,pnode,dest_pnode,vnode,dest_vnode);
      } else {
611 612 613 614
        SDEBUG(cerr << "    not allowed" << endl;)
        if (!vlink->no_connection) {
          mark_vlink_unassigned(vlink);
        }
615 616 617 618 619 620
      }
    } else {
      //assert(resolution_index <= 1)
      resolution_vector resolutions;
      float total_weight = find_link_resolutions(resolutions, pv, dest_pv,
          vlink,pnode,dest_pnode,flipped);
621

622
      int n_resolutions = resolutions.size();
623
      //int resolution_index = n_resolutions - 1;
624
      int resolution_index = n_resolutions;
625

626 627 628 629 630
      /*
       * check for no link
       */
      if (resolution_index == 0) {
        SDEBUG(cerr << "  Could not find any resolutions" << endl;)
631 632
        if (!vlink->no_connection) {
            mark_vlink_unassigned(vlink);
633
        }
634
      } else {
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        /*
         * Choose a link
         */
        int index;
        if (!deterministic && !greedy_link_assignment) {
          /*
           * Not deterministic - randomly pick between the possibilities
           * we've found.
           */
          float choice;
          choice = RANDOM()%(int)total_weight;
          for (index = 0;index < resolution_index;++index) {
            switch (resolutions[index].type_used) {
              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_UNMAPPED:
              case tb_link_info::LINK_TRIVIAL:
656
	      case tb_link_info::LINK_DELAYED:
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
                cerr << "*** Internal error: Should not be here." <<
                  endl;
                exit(EXIT_FATAL);
                break;
            }
            if (choice < 0) break;
          }
        } else {
          /*
           * Deterministic - try all possibilities, and go with the best one
           * (the one with the best score)
           */
          cerr << "Doing deterministic link resolution" << endl;
          cerr << "total_weight: " << total_weight << ", resolution_index: "
            << resolution_index << endl;
          int bestindex;
          int bestviolated = 10000;
          double bestscore=10000.0;
          int i;
          for (i=0;i<resolution_index;++i) {
            vlink->link_info = resolutions[i];
            score_link_info(edge,pnode,dest_pnode,vnode,dest_vnode);
            if ((score <= bestscore) &&
                (violated <= bestviolated)) {
              bestscore = score;
              bestviolated = violated;
              bestindex = i;
            }
            unscore_link_info(edge,pnode,dest_pnode,vnode,dest_vnode);
          }
          index = bestindex;
        }
#ifdef PENALIZE_UNUSED_INTERFACES
        pnode->used_interfaces++;
#endif
692 693 694
        /*
         * Make it so
         */
695 696 697
        vlink->link_info = resolutions[index];
        SDEBUG(cerr << "  choice:" << vlink->link_info);
        score_link_info(edge,pnode,dest_pnode,vnode,dest_vnode);
698 699 700 701 702 703 704 705

        /*
         * Check to see if we are fixing a violation
         */
        if (vlink->no_connection) {
          SDEBUG(cerr << "  Fixing previous violations." << endl);
          mark_vlink_assigned(vlink);
        }
706 707 708 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
      }
    }
  }
}

/*
 * Assign any links on the vnode that can now be assigned (due to the other
 * end of the link also being assigned)
 */
void resolve_links(vvertex vv, pvertex pv, tb_vnode *vnode, tb_pnode *pnode,
    bool deterministic) {
  /*
   * Loop through all vlinks connected to this vnode
   */
  voedge_iterator vedge_it,end_vedge_it;
  tie(vedge_it,end_vedge_it) = out_edges(vv,VG);
  hash_set<const tb_vlink*, hashptr<const tb_vlink*> > seen_links;
  for (;vedge_it!=end_vedge_it;++vedge_it) {
    /*
     * We have to be careful, because we can see loopback links twice, since
     * both endpoints are on the same vnode and the same pnode. If we've seen
     * this link before, just go onto the next
     */
    // XXX: Seems silly to do this here, then again at the top of resolve_link
    tb_vlink *vlink = get(vedge_pmap,*vedge_it);
    if (seen_links.find(vlink) != seen_links.end()) {
      SDEBUG(cerr << "    seen before - skipping" << endl);
      continue;
    } else {
735
      seen_links.insert(vlink);
736 737 738 739 740
      resolve_link(vv,pv,vnode,pnode,deterministic,*vedge_it);
    }
  }
}

741 742 743 744
/* 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.
 */
745 746
void unscore_link_info(vedge ve,tb_pnode *src_pnode,tb_pnode *dst_pnode, tb_vnode *src_vnode,
	tb_vnode *dst_vnode)
747 748
{
  tb_vlink *vlink = get(vedge_pmap,ve);
749 750 751

  // Handle vnodes that are not allowed to have a mix of trivial and
  // non-trivial links
752
  if (vlink->link_info.type_used == tb_link_info::LINK_TRIVIAL) {
753 754 755 756 757 758
      src_vnode->trivial_links--;
      dst_vnode->trivial_links--;
      if (src_vnode->disallow_trivial_mix &&
	      (src_vnode->trivial_links == 0) &&
	      (src_vnode->nontrivial_links != 0)) {
	  // We just removed the last trivial link
759
	  SSUB(SCORE_TRIVIAL_MIX);
760 761 762 763 764 765 766
	  violated--;
	  vinfo.trivial_mix--;
      }
      if (dst_vnode->disallow_trivial_mix &&
	      (dst_vnode->trivial_links == 0) &&
	      (dst_vnode->nontrivial_links != 0)) {
	  // We just removed the last trivial link
767
	  SSUB(SCORE_TRIVIAL_MIX);
768 769 770
	  violated--;
	  vinfo.trivial_mix--;
      }
771
  } else if (vlink->link_info.type_used != tb_link_info::LINK_UNMAPPED) {
772 773 774 775 776 777
      src_vnode->nontrivial_links--;
      dst_vnode->nontrivial_links--;
      if (src_vnode->disallow_trivial_mix &&
	      (src_vnode->nontrivial_links == 0) &&
	      (src_vnode->trivial_links != 0)) {
	  // We just removed the last nontrivial link
778
	  SSUB(SCORE_TRIVIAL_MIX);
779 780 781 782 783 784 785
	  violated--;
	  vinfo.trivial_mix--;
      }
      if (dst_vnode->disallow_trivial_mix &&
	      (dst_vnode->nontrivial_links == 0) &&
	      (dst_vnode->trivial_links != 0)) {
	  // We just removed the last nontrivial link
786
	  SSUB(SCORE_TRIVIAL_MIX);
787 788 789 790 791 792
	  violated--;
	  vinfo.trivial_mix--;
      }
  }

  // Unscore the link itself
793
  if (vlink->link_info.type_used == tb_link_info::LINK_DIRECT) {
794 795
    // DIRECT LINK
    SDEBUG(cerr << "   direct link" << endl);
796 797 798
    src_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;

799
    SSUB(SCORE_DIRECT_LINK);
800 801
    unscore_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    vlink->link_info.plinks.clear();
802
  } else if (vlink->link_info.type_used == tb_link_info::LINK_INTERSWITCH) {
803 804
    // INTERSWITCH LINK
    SDEBUG(cerr << "  interswitch link" << endl);
805 806
    src_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;
807

808 809 810
#ifndef INTERSWITCH_LENGTH
    SSUB(SCORE_INTERSWITCH_LINK);
#endif
811 812

    pedge_path &path = vlink->link_info.plinks;
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
    // XXX: Potentially bogus;
    int numinterlinks;
    numinterlinks = -2;
    for (pedge_path::iterator it=path.begin();
	 it != path.end();++it) {
      unscore_link(*it,ve,src_pnode,dst_pnode);
      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);
      }
835 836
      // We count the bandwidth into and out of the switch
      the_switch->nontrivial_bw_used -= vlink->delay_info.bandwidth * 2;
837 838
    }
    vlink->link_info.switches.clear();
839
  } else if (vlink->link_info.type_used == tb_link_info::LINK_INTRASWITCH) {
840 841
    // INTRASWITCH LINK
    SDEBUG(cerr << "   intraswitch link" << endl);
842 843 844
    src_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used -= vlink->delay_info.bandwidth;

845
    SSUB(SCORE_INTRASWITCH_LINK);
846

847 848 849 850 851
    unscore_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    unscore_link(vlink->link_info.plinks.back(),ve,src_pnode,dst_pnode);
    vlink->link_info.plinks.clear();
    tb_pnode *the_switch = get(pvertex_pmap,
			       vlink->link_info.switches.front());
852 853
    // We count the bandwidth into and out of the switch
    the_switch->nontrivial_bw_used -= vlink->delay_info.bandwidth * 2;
854 855 856 857 858 859
    if (--the_switch->switch_used_links == 0) {
      SDEBUG(cerr << "  releasing switch" << endl);
      SSUB(SCORE_SWITCH);
    }
    vlink->link_info.switches.clear();
  }
860 861

#ifdef TRIVIAL_LINK_BW
862
  else if (vlink->link_info.type_used == tb_link_info::LINK_TRIVIAL) {
863
      // Trivial link - we may get to remove violations
Robert Ricci's avatar
Robert Ricci committed
864 865 866 867 868 869
      SDEBUG(cerr << "  trivial bandwidth used " <<
	      src_pnode->trivial_bw_used << " max is " <<
	      src_pnode->trivial_bw << " subtracting " <<
	      vlink->delay_info.bandwidth << endl);
      if (src_pnode->trivial_bw) {
	int old_over_bw;
870
	int old_bw = src_pnode->trivial_bw_used;
Robert Ricci's avatar
Robert Ricci committed
871 872 873 874 875
	if (src_pnode->trivial_bw_used > src_pnode->trivial_bw) {
	  old_over_bw = src_pnode->trivial_bw_used - src_pnode->trivial_bw;
	} else {
	  old_over_bw = 0;
	}
876
        SDEBUG(cerr << "  old trivial bandwidth over by " << old_over_bw << endl);
Robert Ricci's avatar
Robert Ricci committed
877 878 879 880 881 882 883 884 885

	src_pnode->trivial_bw_used -= vlink->delay_info.bandwidth;

	int new_over_bw;
	if (src_pnode->trivial_bw_used > src_pnode->trivial_bw) {
	  new_over_bw = src_pnode->trivial_bw_used - src_pnode->trivial_bw;
	} else {
	  new_over_bw = 0;
	}
886
        SDEBUG(cerr << "  new trivial bandwidth over by " << new_over_bw << endl);
Robert Ricci's avatar
Robert Ricci committed
887 888

	if (old_over_bw) {
889
	  // Count how many multiples of the maximum bandwidth we're at
890 891 892 893 894
          int new_multiple = src_pnode->trivial_bw_used / src_pnode->trivial_bw;
          int old_multiple = old_bw / src_pnode->trivial_bw;
	  int num_violations = old_multiple - new_multiple;
          SDEBUG(cerr << "  removing " << num_violations <<
                 " violations for trivial bandwidth" << endl);
895 896
	  violated -= num_violations;
	  vinfo.bandwidth -= num_violations;
Robert Ricci's avatar
Robert Ricci committed
897 898 899 900
	  double removed_bandwidth_percent = (old_over_bw - new_over_bw) * 1.0 /
	    src_pnode->trivial_bw;
	  SSUB(SCORE_TRIVIAL_PENALTY * removed_bandwidth_percent);
	}
901
      }
Robert Ricci's avatar
Robert Ricci committed
902

903 904 905
      unscore_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
  }
#endif
906

907 908 909 910 911
  // If auto-adjusting the vlink bandwidth, we set it to a sentinel value
  // so that we can detect any problems next time we do a score_link_info()
  if (vlink->delay_info.adjust_to_native_bandwidth) {
      vlink->delay_info.bandwidth = -2;
  }
912

913
}
914

915 916
/*
 * This removes a virtual node from the assignments, adjusting
917 918
 * the score appropriately.
 */
919
void remove_node(vvertex vv)
920
{
921
  // Find the vnode associated with the vvertex, and the pnode it's assigned to
922 923 924 925 926 927 928 929 930 931
  tb_vnode *vnode = get(vvertex_pmap,vv);
  assert(vnode->assigned);
  pvertex pv = vnode->assignment;
  tb_pnode *pnode = get(pvertex_pmap,pv);

  SDEBUG(cerr <<  "SCORE: remove_node(" << vnode->name << ")" << endl);
  SDEBUG(cerr <<  "  assignment=" << pnode->name << endl);
#ifdef SCORE_DEBUG_LOTS
  cerr << *vnode;
  cerr << *pnode;
932 933 934 935
#endif

  assert(pnode != NULL);

936
  /*
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
   * Find the type on the pnode that this vnode is associated with
   */
  tb_pnode::types_map::iterator mit = pnode->types.find(vnode->type);
  tb_pnode::type_record *tr;
  if (mit == pnode->types.end()) {
      // This is kind of a hack - if we don't find the type, then the vnode
      // must have a vtype. So, we assume it must have been assigned to this
      // node's dynamic type at some point.
      // A consequence of this hack is that vtypes can't map to static types,
      // for now.
      RDEBUG(cerr << "Failed to find type " << vnode->type << " (for vnode " <<
	  vnode->name << ") on pnode " << pnode->name << endl;)
      tr = pnode->current_type_record;
  } else {
      tr = mit->second;
  }
953

954

955 956 957
  /*
   * Clean up the pnode's state
   */
958
  if (!tr->is_static()) {
959 960 961
    if (pnode->my_class) {
      pclass_unset(pnode);
    }
962
  }
963

964
  // pclass
965
  if ((!disable_pclasses) && !(tr->is_static()) && pnode->my_class
966
	  && (pnode->my_class->used_members == 0)) {
967
    SDEBUG(cerr << "  freeing pclass" << endl);
968 969
    SSUB(SCORE_PCLASS);
  }
970 971

  // vclass
972 973 974
  if (vnode->vclass != NULL) {
    double score_delta = vnode->vclass->unassign_node(vnode->type);
    SDEBUG(cerr << "  vclass unassign " << score_delta << endl);
975

976 977 978 979 980 981
    if (score_delta <= -1) {
      violated--;
      vinfo.vclass--;
    }
    SSUB(-score_delta*SCORE_VCLASS);
  }
982 983 984 985 986 987 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

  /*
   * Handle subnodes
   */
  if (vnode->subnode_of) {
      // First handle our parent
      if (vnode->subnode_of->assigned) {
	  tb_pnode *assignment = get(pvertex_pmap,
		  vnode->subnode_of->assignment);
	  if ((!pnode->subnode_of) ||
		 (pnode->subnode_of != assignment)) {
	      SSUB(SCORE_SUBNODE);
	      violated--;
	      vinfo.subnodes--;
	  }
      }
  }
  if (!vnode->subnodes.empty()) {
      // Then any children we might have
      tb_vnode::subnode_list::iterator sit;
      for (sit = vnode->subnodes.begin(); sit != vnode->subnodes.end(); sit++) {
	  if ((*sit)->assigned) {
	      tb_pnode *assignment = get(pvertex_pmap,(*sit)->assignment);
	  if ((!assignment->subnode_of) ||
		 (assignment->subnode_of != pnode)) {
		  SSUB(SCORE_SUBNODE);
		  violated--;
		  vinfo.subnodes--;
	      }
	  }
      }
  }

1015 1016 1017
  /*
   * Now, take care of the virtual links that are attached to the vnode
   */
1018 1019
  voedge_iterator vedge_it,end_vedge_it;
  tie(vedge_it,end_vedge_it) = out_edges(vv,VG);
1020
  hash_set<const tb_vlink*, hashptr<const tb_vlink*> > seen_loopback_links;
1021 1022
  for (;vedge_it!=end_vedge_it;++vedge_it) {
    tb_vlink *vlink = get(vedge_pmap,*vedge_it);
1023 1024 1025

    // Find the other end of the vlink - we might be either its source or
    // destination (target)
1026 1027 1028 1029 1030 1031
    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);

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    if (dest_vv == vv) {
      // We have to be careful with 'loopback' links, because we can see them
      // twice - bail out if this is the second time
      if (seen_loopback_links.find(vlink) != seen_loopback_links.end()) {
	SDEBUG(cerr << "    seen before - skipping" << endl);
	continue;
      } else {
	seen_loopback_links.insert(vlink);
      }
    }

1043

1044 1045
    // Only unscore the link if the vnode on the other end is assigned - this
    // way, only the first end to be unmapped causes unscoring
1046 1047 1048
    if (! dest_vnode->assigned) {
      continue;
    }
1049

1050
    // Find the pnode on the ther end of the link, and unscore it!
1051 1052
    pvertex dest_pv = dest_vnode->assignment;
    tb_pnode *dest_pnode = get(pvertex_pmap,dest_pv);
1053
    unscore_link_info(*vedge_it,pnode,dest_pnode,vnode,dest_vnode);
1054

1055 1056 1057 1058 1059
    // If the other end was connected before, it's not now
    if (!vlink->no_connection) {
      SDEBUG(cerr << "      link now in violation.\n";)
      mark_vlink_unassigned(vlink);
    }
1060

1061
  }
1062

1063
#ifdef PENALIZE_UNUSED_INTERFACES
1064
  // Keep track of the number of interfaces that the pnode is using
1065 1066 1067
  SSUB((pnode->total_interfaces - pnode->used_interfaces) * SCORE_UNUSED_INTERFACE);
  pnode->used_interfaces = 0;
#endif
1068

1069 1070 1071
  /*
   * Adjust scores for the pnode
   */
1072 1073
  int old_load = tr->get_current_load();
  tr->remove_load(vnode->typecount);
1074
  pnode->total_load -= vnode->typecount;
1075 1076 1077 1078 1079 1080 1081
  if (strategy_balance) {
    // Use this tricky formula to score based on how 'full' the pnode is, so
    // that we prefer to equally fill the minimum number of pnodes
    SSUB(SCORE_PNODE * (powf(1+ ((tr->get_current_load()+1) * 1.0)/tr->get_max_load(),2)));
    SADD(SCORE_PNODE * (powf(1+ tr->get_current_load() * 1.0/tr->get_max_load(),2)));
  }
  if (strategy_pack) {
1082 1083 1084 1085 1086 1087 1088 1089 1090
    // Inverse of strategy_balance - note that we have to make sure to not let
    // the number drop below zero (which might happen if allow_overload is set)
    int leftover_slots = tr->get_max_load() - tr->get_current_load();
    int prev_leftover_slots = tr->get_max_load() - (tr->get_current_load()+1);
    if (prev_leftover_slots < 0) { prev_leftover_slots = 0; }
    if (leftover_slots < 0) { leftover_slots = 0; }

    SSUB(SCORE_PNODE * (powf((prev_leftover_slots * 1.0)/tr->get_max_load(),0.5)));
    SADD(SCORE_PNODE * (powf((leftover_slots * 1.0)/tr->get_max_load(),0.5)));
1091
  }
1092
  if (pnode->total_load == 0) {
1093
    // If the pnode is now free, we need to do some cleanup
1094
    SDEBUG(cerr << "  releasing pnode" << endl);
1095
    SSUB(SCORE_PNODE);
1096
    pnode->remove_current_type();
1097
    pclass_reset_maps(pnode);
1098 1099 1100
    // ptypes
    tb_pnode::types_list::iterator lit = pnode->type_list.begin();
    while (lit != pnode->type_list.end()) {
1101 1102
	int removed_violations =
	    (*lit)->get_ptype()->remove_users((*lit)->get_max_load());
1103 1104 1105 1106 1107 1108 1109
	if (removed_violations) {
	    SSUB(SCORE_MAX_TYPES * removed_violations);
	    violated -= removed_violations;
	    vinfo.max_types -= removed_violations;
	}
	lit++;
    }
1110
  } else if (old_load > tr->get_max_load()) {
1111 1112 1113
    // If the pnode was over its load, remove the penalties for the nodes we
    // just removed, down to the max_load.
    SDEBUG(cerr << "  reducing penalty, old load was " << old_load <<
1114 1115 1116 1117 1118
	    ", new load = " << tr->get_current_load() << ", max load = " <<
	    tr->get_max_load() << endl);
    for (int i = old_load;
	 i > MAX(tr->get_current_load(),tr->get_max_load());
	 i--) {
1119 1120 1121 1122
      SSUB(SCORE_PNODE_PENALTY);
      vinfo.pnode_load--;
      violated--;
    }
1123
  }
1124

1125 1126 1127 1128 1129 1130 1131
  /*
   * Score the fact that we now have one more unassigned vnode
   */
  vnode->assigned = false;
  SADD(SCORE_UNASSIGNED);
  vinfo.unassigned++;
  violated++;
1132

1133

1134 1135 1136
  /*
   * Scoring for features and desires
   */
1137 1138 1139 1140
  score_and_violations sv = score_fds(vnode,pnode,false);
  SSUB(sv.first);
  violated -= sv.second;
  vinfo.desires -= sv.second;
1141 1142

  SDEBUG(cerr << "  new score = " << score << " new violated = " << violated << endl);
1143 1144
}

1145 1146 1147
/* 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.
1148
 */
1149 1150
void score_link_info(vedge ve, tb_pnode *src_pnode, tb_pnode *dst_pnode, tb_vnode *src_vnode,
	tb_vnode *dst_vnode)
1151
{
1152 1153
  tb_vlink *vlink = get(vedge_pmap,ve);
  tb_pnode *the_switch;
1154

1155 1156 1157 1158 1159 1160
  // If this link is to be adjusted to the native speed of the interface, go
  // ahead and do that now - we use the minimum of the two endpoint interfaces
  // Note! Not currently supported on trivial links! (it's illegal for
  // adjust_to_native_bandwidth and trivial_ok to both be true)
  if (vlink->delay_info.adjust_to_native_bandwidth &&
      vlink->link_info.type_used != tb_link_info::LINK_TRIVIAL) {
1161 1162 1163
    // Check for special sentinel value to make sure we remembered to re-set
    // the value before
    assert(vlink->delay_info.bandwidth == -2);
1164 1165 1166 1167 1168
    tb_plink *front_plink = get(pedge_pmap, vlink->link_info.plinks.front());
    tb_plink *back_plink = get(pedge_pmap, vlink->link_info.plinks.back());
    vlink->delay_info.bandwidth =
      min(front_plink->delay_info.bandwidth, back_plink->delay_info.bandwidth);
  }
1169

1170
  switch (vlink->link_info.type_used) {
1171 1172
  case tb_link_info::LINK_DIRECT:
    SADD(SCORE_DIRECT_LINK);
1173 1174
    src_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
1175 1176 1177 1178
    score_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    break;
  case tb_link_info::LINK_INTRASWITCH:
    SADD(SCORE_INTRASWITCH_LINK);
1179 1180
    src_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
1181 1182 1183 1184 1185 1186 1187 1188
    score_link(vlink->link_info.plinks.front(),ve,src_pnode,dst_pnode);
    score_link(vlink->link_info.plinks.back(),ve,src_pnode,dst_pnode);
    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);
    }
1189 1190
    // We count the bandwidth into and out of the switch
    the_switch->nontrivial_bw_used += vlink->delay_info.bandwidth * 2;
1191 1192
    break;
  case tb_link_info::LINK_INTERSWITCH:
1193 1194
    src_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
    dst_pnode->nontrivial_bw_used += vlink->delay_info.bandwidth;
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
#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) {
	score_link(*plink_It,ve,src_pnode,dst_pnode);
	numinterlinks++;
    }
#ifdef INTERSWITCH_LENGTH
    for (int i = 1; i <= numinterlinks; i++) {
	SADD(SCORE_INTERSWITCH_LINK);
    }
#endif
1212

1213 1214 1215 1216 1217 1218 1219
    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);
      }
1220 1221
      // We count the bandwidth into and out of the switch
      the_switch->nontrivial_bw_used += vlink->delay_info.bandwidth * 2;
1222 1223 1224
    }
    break;
  case tb_link_info::LINK_TRIVIAL:
1225
  #ifdef TRIVIAL_LINK_BW
Robert Ricci's avatar
Robert Ricci committed
1226 1227 1228 1229 1230 1231
    SDEBUG(cerr << "  trivial bandwidth used " <<
	    src_pnode->trivial_bw_used << " max is " <<
	    src_pnode->trivial_bw << " adding " << vlink->delay_info.bandwidth
	    << endl);
    if (src_pnode->trivial_bw) {
      int old_over_bw;
1232
      int old_bw = src_pnode->trivial_bw_used;
Robert Ricci's avatar
Robert Ricci committed
1233 1234 1235 1236 1237
      if (src_pnode->trivial_bw_used > src_pnode->trivial_bw) {
	old_over_bw = src_pnode->trivial_bw_used - src_pnode->trivial_bw;
      } else {
	old_over_bw = 0;
      }
1238
      SDEBUG(cerr << "  old trivial bandwidth over by " << old_over_bw << endl);
Robert Ricci's avatar
Robert Ricci committed
1239

1240
      dst_pnode->trivial_bw_used += vlink->delay_info.bandwidth;
Robert Ricci's avatar
Robert Ricci committed
1241 1242 1243 1244 1245 1246 1247

      int new_over_bw;
      if (src_pnode->trivial_bw_used > src_pnode->trivial_bw) {
	new_over_bw = src_pnode->trivial_bw_used - src_pnode->trivial_bw;
      } else {
	new_over_bw = 0;
      }
1248
      SDEBUG(cerr << "  new trivial bandwidth over by " << new_over_bw << endl);
1249

Robert Ricci's avatar
Robert Ricci committed
1250
      if (new_over_bw) {
1251
	// Count how many multiples of the maximum bandwidth we're at
1252 1253 1254 1255 1256
        int new_multiple = src_pnode->trivial_bw_used / src_pnode->trivial_bw;
        int old_multiple = old_bw / src_pnode->trivial_bw;
	int num_violations = new_multiple - old_multiple;
        SDEBUG(cerr << "  adding " << num_violations <<
               " violations for trivial bandwidth" << endl);
1257 1258 1259
	violated += num_violations;
	vinfo.bandwidth += num_violations;

Robert Ricci's avatar
Robert Ricci committed
1260 1261 1262
	double added_bandwidth_percent = (new_over_bw - old_over_bw) * 1.0 /
	  src_pnode->trivial_bw;
	SADD(SCORE_TRIVIAL_PENALTY * added_bandwidth_percent);
1263 1264 1265 1266
      }
    }
    break;
#endif
1267
  case tb_link_info::LINK_UNMAPPED:
1268
  case tb_link_info::LINK_DELAYED:
1269
    cout << "*** Internal error: Should not be here either." << endl;
1270
    exit(EXIT_FATAL);
1271
    break;
1272
  }
1273 1274 1275

  // Handle vnodes that are not allowed to have a mix of trivial and
  // non-trivial links
1276
  if (vlink->link_info.type_used == tb_link_info::LINK_TRIVIAL) {
1277 1278 1279 1280 1281 1282
      src_vnode->trivial_links++;
      dst_vnode->trivial_links++;
      if (src_vnode->disallow_trivial_mix &&
	      (src_vnode->trivial_links == 1) &&
	      (src_vnode->nontrivial_links != 0)) {
	  // We just added the first trivial link
1283
	  SADD(SCORE_TRIVIAL_MIX);
1284 1285 1286 1287 1288 1289 1290
	  violated++;
	  vinfo.trivial_mix++;
      }
      if (dst_vnode->disallow_trivial_mix &&
	      (dst_vnode->trivial_links == 1) &&
	      (dst_vnode->nontrivial_links != 0)) {
	  // We just added the first trivial link
1291
	  SADD(SCORE_TRIVIAL_MIX);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	  violated++;
	  vinfo.trivial_mix++;
      }
  } else {
      src_vnode->nontrivial_links++;
      dst_vnode->nontrivial_links++;
      if (src_vnode->disallow_trivial_mix &&
	      (src_vnode->nontrivial_links == 1) &&
	      (src_vnode->trivial_links != 0)) {
	  // We just added the first trivial link
1302
	  SADD(SCORE_TRIVIAL_MIX);
1303 1304 1305 1306 1307 1308 1309
	  violated++;
	  vinfo.trivial_mix++;
      }
      if (dst_vnode->disallow_trivial_mix &&
	      (dst_vnode->nontrivial_links == 1) &&
	      (dst_vnode->trivial_links != 0)) {
	  // We just added the first trivial link
1310
	  SADD(SCORE_TRIVIAL_MIX);
1311 1312 1313 1314 1315
	  violated++;
	  vinfo.trivial_mix++;
      }
  }

1316 1317 1318 1319 1320 1321 1322 1323 1324
}

/*
 * 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.
 */
1325
int add_node(vvertex vv,pvertex pv, bool deterministic, bool is_fixed, bool skip_links)
1326
{
1327
  // Get the vnode and pnode associated with the passed vertices
1328 1329
  tb_vnode *vnode = get(vvertex_pmap,vv);
  tb_pnode *pnode = get(pvertex_pmap,pv);
1330

1331 1332 1333 1334 1335 1336 1337
  assert(!vnode->assigned);

  SDEBUG(cerr << "SCORE: add_node(" << vnode->name << "," <<
	 pnode->name << ")" << endl);
#ifdef SCORE_DEBUG_LOTS
  cerr << *vnode;
  cerr << *pnode;
1338
#endif
1339
  SDEBUG(cerr << "  vnode type = " << vnode->type << endl);
1340

1341
  /*
1342 1343 1344 1345
   * Handle types - first, check to see if the node is capable of taking on the
   * vnode's type. If it can with a static type, just do the bookkeeping for
   * that static type. Otherwise convert the node to the correct dynamic type,
   * failing if we can't for some reason (ie. it already has another type.
1346
   */
1347 1348
  tb_pnode::type_record *tr;
  tb_pnode::types_map::iterator mit = pnode->types.find(vnode->type);
1349

1350 1351 1352 1353 1354 1355
  if (mit == pnode->types.end()) {
    // This pnode can't take on the vnode's type - we normally shouldn't get
    // here, due to the way we pick pnodes
    return 1;
  }

1356 1357 1358
  /*
   * Handle types
   */
1359
  tr = mit->second;
1360
  if (tr->is_static()) {
1361
    // XXX: Scoring???
1362
    if (tr->get_current_load() < tr->get_max_load()) {
1363
    } else {
1364 1365 1366
      return 1;
    }
  } else {
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    // Figure out the pnode's type
    if (!pnode->typed) {
      // If this pnode has no type yet, give it one
      SDEBUG(cerr << "  virgin pnode" << endl);
      SDEBUG(cerr << "    vtype = " << vnode->type << endl);

      // Remove check assuming at higher level?
      // Remove higher level checks?
      if (!pnode->set_current_type(vnode->type)) {
	// didn't find a type
	SDEBUG(cerr << "  no matching type" << endl);
	//cerr << "Failed due to bad type!" << endl;
1379 1380
	return 1;
      }
1381 1382

      SDEBUG(cerr << "  matching type found (" << pnode->current_type <<
1383 1384
	  ", max = " << pnode->current_type_record->get_max_load() << ")" <<
          endl);
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
      } else {
	// The pnode already has a type, let's just make sure it's compatible
	SDEBUG(cerr << "  pnode already has type" << endl);
	if (pnode->current_type != vnode->type) {
	  SDEBUG(cerr << "  incompatible types" << endl);
	  return 1;
	} else {
	  SDEBUG(cerr << "  compatible types" << endl);
	}
      }
1395 1396
    }

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
  /*
   * Handle subnodes
   */
  if (vnode->subnode_of) {
      // First handle our parent
      if (vnode->subnode_of->assigned) {
	  tb_pnode *assignment = get(pvertex_pmap,
		  vnode->subnode_of->assignment);
	  if ((!pnode->subnode_of) ||
		 (pnode->subnode_of != assignment)) {
	      SADD(SCORE_SUBNODE);
	      violated++;
	      vinfo.subnodes++;
	  }
      }
  }
  if (!vnode->subnodes.empty()) {
      // Then any children we might have
      tb_vnode::subnode_list::iterator sit;
      for (sit = vnode->subnodes.begin(); sit != vnode->subnodes.end(); sit++) {
	  if ((*sit)->assigned) {
	      tb_pnode *assignment = get(pvertex_pmap,(*sit)->assignment);
	  if ((!assignment->subnode_of) ||
		 (assignment->subnode_of != pnode)) {
		  SADD(SCORE_SUBNODE);
		  violated++;
		  vinfo.subnodes++;
	      }
	  }
      }
  }

1429 1430 1431
#ifdef PENALIZE_UNUSED_INTERFACES
  pnode->used_interfaces = 0;
#endif
1432

1433 1434 1435 1436 1437 1438
  /*
   * Record the node's assignment. Need to do this now so that 'loopback' links
   * work below.
   */
  vnode->assignment = pv;
  vnode->assigned = true;
1439

1440 1441 1442
  /* Links */
  if (!skip_links) {
      resolve_links(vv,pv,vnode,pnode,deterministic);
1443
  }
1444

1445
  int old_load = tr->get_current_load();
1446 1447
  int old_total_load = pnode->total_load;

1448
  // finish setting up pnode
1449
  tr->add_load(vnode->typecount);
1450
  pnode->total_load += vnode->typecount;
1451 1452

#ifdef PENALIZE_UNUSED_INTERFACES
1453
  // XXX
1454 1455
  assert(pnode->used_interfaces <= pnode->total_interfaces);
  SADD((pnode->total_interfaces - pnode->used_interfaces) * SCORE_UNUSED_INTERFACE);
1456
#endif
1457

1458
  if (tr->get_current_load() > tr->get_max_load()) {
1459
    SDEBUG(cerr << "  load too high - penalty (" <<
1460 1461 1462 1463
	pnode->current_type_record->get_current_load() << ")" << endl);
    for (int i = MAX(old_load,tr->get_max_load());
	 i < tr->get_current_load();
	 i++) {
1464 1465 1466 1467
      SADD(SCORE_PNODE_PENALTY);
      vinfo.pnode_load++;
      violated++;
    }
1468
  } else {
1469
    SDEBUG(cerr << "  load is fine" << endl);
1470
  }
1471
  if (old_total_load == 0) {
1472
    SDEBUG(cerr << "  new pnode" << endl);
1473
    SADD(SCORE_PNODE);
1474 1475 1476
    // ptypes
    tb_pnode::types_list::iterator lit = pnode->type_list.begin();
    while (lit != pnode->type_list.end()) {
1477
	int new_violations =
1478
	    (*lit)->get_ptype()->add_users((*lit)->get_max_load());
1479 1480 1481 1482 1483 1484 1485
	if (new_violations) {
	    SADD(SCORE_MAX_TYPES * new_violations);
	    violated += new_violations;
	    vinfo.max_types += new_violations;
	}
	lit++;
    }
1486
  }
1487 1488 1489 1490 1491 1492

  if (strategy_balance) {
    SSUB(SCORE_PNODE * (powf(1 + ((tr->get_current_load()-1) * 1.0)/tr->get_max_load(),2)));
    SADD(SCORE_PNODE * (powf(1 + ((tr->get_current_load()) * 1.0)/tr->get_max_load(),2)));
  }
  if (strategy_pack) {
1493 1494 1495 1496 1497 1498 1499 1500
    /* Note comment above about not letting drop below zero */
    int leftover_slots = tr->get_max_load() - tr->get_current_load();
    int prev_leftover_slots = tr->get_max_load() - (tr->get_current_load()-1);
    if (prev_leftover_slots < 0) { prev_leftover_slots = 0; }
    if (leftover_slots < 0) { leftover_slots = 0; }

    SSUB(SCORE_PNODE * (powf((prev_leftover_slots * 1.0)/tr->get_max_load(),0.5)));
    SADD(SCORE_PNODE * (powf((leftover_slots * 1.0)/tr->get_max_load(),0.5)));
1501
  }
1502 1503 1504 1505 1506 1507

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

Christopher Alfeld's avatar