assign.cc 28.3 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2006 University of Utah and the Flux Group.
Robert Ricci's avatar
Robert Ricci committed
4 5 6
 * All rights reserved.
 */

7 8 9 10 11 12 13 14
#include "port.h"

#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>
#include <boost/graph/dijkstra_shortest_paths.hpp>
15
#ifdef GRAPHVIZ_SUPPORT
16
#include <boost/graph/graphviz.hpp>
17
#endif
18

19 20
#include <fstream>
#include <iostream>
21
#include <time.h>
Mac Newbold's avatar
Mac Newbold committed
22
#include <stdlib.h>
23
#include <math.h>
Mac Newbold's avatar
Mac Newbold committed
24 25
#include <sys/types.h>
#include <sys/time.h>
26
#include <sys/resource.h>
27 28
#include <signal.h>
#include <sys/signal.h>
29
#include <queue>
30
#include <algorithm>
31 32

using namespace boost;
Mac Newbold's avatar
Mac Newbold committed
33

34 35
#include <stdio.h>

Mac Newbold's avatar
Mac Newbold committed
36
#include "common.h"
37
#include "delay.h"
Mac Newbold's avatar
Mac Newbold committed
38
#include "physical.h"
39
#include "virtual.h"
40
#include "vclass.h"
41
#include "pclass.h"
42
#include "score.h"
43 44 45
#include "solution.h"
#include "maps.h"
#include "anneal.h"
46 47 48 49
#ifdef WITH_XML
#include "parse_ptop_xml.h"
#include "parse_top_xml.h"
#endif
50 51 52

// Here we set up all our graphs.  Need to create the graphs
// themselves and then setup the property maps.
53
tb_pgraph PG;
54 55 56 57 58 59 60 61 62 63
tb_pgraph_vertex_pmap pvertex_pmap = get(vertex_data, PG);
tb_pgraph_edge_pmap pedge_pmap = get(edge_data, PG);
tb_sgraph SG;
tb_sgraph_vertex_pmap svertex_pmap = get(vertex_data, SG);
tb_sgraph_edge_pmap sedge_pmap = get(edge_data, SG);
tb_vgraph VG;
tb_vgraph_vertex_pmap vvertex_pmap = get(vertex_data, VG);
tb_vgraph_edge_pmap vedge_pmap = get(edge_data, VG);

// List of virtual types by name.
64
name_count_map vtypes;
65
name_list_map vclasses;
66 67

// A list of all pclasses.
68
pclass_list pclasses;
69 70 71
 	
// Map from a pnode* to the the corresponding pvertex.
pnode_pvertex_map pnode2vertex;
72 73 74

// Map of a type to a tt_entry, a vector of pclasses and the size of
// the vector.
75
pclass_types type_table;
76 77 78
#ifdef PER_VNODE_TT
pclass_types vnode_type_table;
#endif
79

80 81 82 83 84
// This datastructure contains all the information needed to calculate
// the shortest path between any two switches.  Indexed by svertex,
// the value will be a predicate map (indexed by svertex as well) of
// the shortest paths for the given vertex.
switch_pred_map_map switch_preds;
85

86 87
// Same, but for distances 
switch_dist_map_map switch_dist;
88

89 90
// Time started, finished, and the time limit
double timestart, timeend, timelimit, timetarget;
91

92 93 94
// An amount to scale the neighborhood size by
double scale_neighborhood = 1.0;

95 96 97 98 99
#ifdef GNUPLOT_OUTPUT
FILE *scoresout, *tempout, *deltaout;
#endif
// Whether or not assign is allowed to generate trivial links
bool allow_trivial_links = true;
100

101
// Whether or not assign should use pclasses
102
bool disable_pclasses = false;
103

Robert Ricci's avatar
Robert Ricci committed
104 105 106
// Whether or not assign should prune out pclasses that it knows can
// never be used
bool prune_pclasses = false;
107 108 109

// Whether or not we should use the experimental support for dynamic pclasses
bool dynamic_pclasses = false;
110 111 112

// Whether or not to allow assign to temporarily over-subscribe pnodes
bool allow_overload = false;
Robert Ricci's avatar
Robert Ricci committed
113 114 115 116

// Forces assign to do greedy link assignment, by chosing the first link in its
// list, which is usually the lowest-cost
bool greedy_link_assignment = false;
117 118 119 120 121

// Forces assign to skip melting, and, instead, use the temperature given as
// the initial temperature
bool no_melting = false;
double initial_temperature = 0.0f;
122 123 124 125 126 127

// Print out a summary of the solution in addition to the solution itself
bool print_summary = false;

// Use the 'connected' find algorithm
double use_connected_pnode_find = 0.0f;
128 129 130 131 132

#ifdef WITH_XML
// Use XML for file input
bool xml_input = false;
#endif
133 134 135 136
  
// XXX - shouldn't be in this file
double absbest;
int absbestviolated, iters, iters_to_best;
Robert Ricci's avatar
Robert Ricci committed
137

138 139 140
// Map of all physical types in use in the system
tb_ptype_map ptypes;

141 142 143
/*
 * Internal functions
 */
Mac Newbold's avatar
Mac Newbold committed
144

Robert Ricci's avatar
Robert Ricci committed
145
// Return the CPU time (in seconds) used by this process
146
float used_time() {
147 148 149 150 151 152
  struct rusage ru;
  getrusage(RUSAGE_SELF,&ru);
  return ru.ru_utime.tv_sec+ru.ru_utime.tv_usec/1000000.0+
    ru.ru_stime.tv_sec+ru.ru_stime.tv_usec/1000000.0;
}

Robert Ricci's avatar
Robert Ricci committed
153
// Read in the .ptop file
154
void read_physical_topology(char *filename) {
155 156
  ifstream ptopfile;
  ptopfile.open(filename);
Robert Ricci's avatar
Robert Ricci committed
157
  if (!ptopfile.is_open()) {
158
      cout << "*** Unable to open ptop file " << filename << endl;
159
      exit(EXIT_FATAL);
Robert Ricci's avatar
Robert Ricci committed
160
  }
161 162 163 164 165 166 167 168 169 170
  
#ifdef WITH_XML
  if (!xml_input) {
      cout << "Physical Graph: " << parse_ptop(PG,SG,ptopfile) << endl;
  } else {
      cout << "Physical Graph: " << parse_ptop_xml(PG,SG,filename) << endl;
  }
#else
cout << "Physical Graph: " << parse_ptop(PG,SG,ptopfile) << endl;
#endif
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

#ifdef DUMP_GRAPH
  {
    cout << "Physical Graph:" << endl;
    
    pvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(PG);
    
    for (;vit != vendit;vit++) {
      tb_pnode *p = get(pvertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    pedge_iterator eit,eendit;
    tie(eit,eendit) = edges(PG);

    for (;eit != eendit;eit++) {
      tb_plink *p = get(pedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,PG) << " <-> " <<
	target(*eit,PG) << ")\t" << *p;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
193
#endif // DUMP_GRAPH
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

#ifdef GRAPH_DEBUG
  {
    cout << "Switch Graph:" << endl;
    

    svertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(SG);
    
    for (;vit != vendit;vit++) {
      tb_switch *p = get(svertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    sedge_iterator eit,eendit;
    tie(eit,eendit) = edges(SG);

    for (;eit != eendit;eit++) {
      tb_slink *p = get(sedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,SG) << " <-> " <<
	target(*eit,SG) << ")\t" << *p;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
217
#endif // GRAPH_DEBUG
218

Robert Ricci's avatar
Robert Ricci committed
219 220
  // Set up pnode2vertex - a mapping between vertices in the physical graph and
  // the pnodes that we just read in from the ptop file
221 222 223 224 225 226 227 228
  pvertex_iterator pvit,pvendit;
  tie(pvit,pvendit) = vertices(PG);
  for (;pvit != pvendit;pvit++) {
    pnode2vertex[get(pvertex_pmap,*pvit)]=*pvit;
  }

}

Robert Ricci's avatar
Robert Ricci committed
229 230
// Calculate the minimum spanning tree for the switches - we only consider one
// potential path between each pair of switches.
231
void calculate_switch_MST() {
232
  cout << "Calculating shortest paths on switch fabric." << endl;
Robert Ricci's avatar
Robert Ricci committed
233 234

  // Set up the weight map for Dijkstra's
235 236 237 238 239 240 241 242 243 244
  tb_sgraph_weight_pmap sweight_pmap = get(edge_weight, SG);
  sedge_iterator seit,seendit;
  tie(seit,seendit) = edges(SG);
  for (;seit != seendit;seit++) {
    tb_slink *slink = get(sedge_pmap,*seit);
    // XXX should we make this more complicated depending on
    // latency/loss as well?
    put(sweight_pmap,*seit,
	100000000-get(pedge_pmap,slink->mate)->delay_info.bandwidth);
  }
245

Robert Ricci's avatar
Robert Ricci committed
246
  // Let boost do the Disjktra's for us, from each switch
247 248 249 250 251 252 253 254
  svertex_iterator svit,svendit;
  tie(svit,svendit) = vertices(SG);
  for (;svit != svendit;svit++) {
    switch_preds[*svit] = new switch_pred_map(num_vertices(SG));
    switch_dist[*svit] = new switch_dist_map(num_vertices(SG));
    dijkstra_shortest_paths(SG,*svit,
    			    predecessor_map(&((*switch_preds[*svit])[0])).
			    distance_map(&((*switch_dist[*svit])[0])));
255 256
  }

257 258 259 260 261 262 263 264 265
#ifdef GRAPH_DEBUG
  cout << "Shortest paths" << endl;
  tie(svit,svendit) = vertices(SG);
  for (;svit != svendit;svit++) {
    cout << *svit << ":" << endl;
    for (unsigned int i = 0;i<num_vertices(SG);++i) {
      cout << i << " " << (*switch_dist[*svit])[i] << endl;
    }
  }
Robert Ricci's avatar
Robert Ricci committed
266
#endif // GRAPH_DEBUG
267 268
}

Robert Ricci's avatar
Robert Ricci committed
269
// Read in the .top file
270
void read_virtual_topology(char *filename) {
271 272
  ifstream topfile;
  topfile.open(filename);
Robert Ricci's avatar
Robert Ricci committed
273
  if (!topfile.is_open()) {
274
      cout << "*** Unable to open top file " << filename << endl;
275
      exit(EXIT_FATAL);
Robert Ricci's avatar
Robert Ricci committed
276
  }
277 278 279 280 281 282 283 284 285 286
  
#ifdef WITH_XML
  if (!xml_input) {
      cout << "Virtual Graph: " << parse_top(VG,topfile) << endl;
  } else {
      cout << "Virtual Graph: " << parse_top_xml(VG,filename) << endl;
  }
#else
cout << "Virtual Graph: " << parse_top(VG,topfile) << endl;
#endif
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

#ifdef DUMP_GRAPH
  {
    cout << "Virtual Graph:" << endl;
    

    vvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(VG);
    
    for (;vit != vendit;vit++) {
      tb_vnode *p = get(vvertex_pmap,*vit);
      cout << *vit << "\t" << *p;
    }
    
    vedge_iterator eit,eendit;
    tie(eit,eendit) = edges(VG);

    for (;eit != eendit;eit++) {
      tb_vlink *p = get(vedge_pmap,*eit);
      cout << *eit << " (" << source(*eit,VG) << " <-> " <<
	target(*eit,VG) << ")\t" << *p;
    }
  }  
#endif
}
Robert Ricci's avatar
Robert Ricci committed
312 313 314 315 316 317 318 319 320 321
/*
 * Make a pass through the pclasses, looking for ones that no node can use, and
 * nuking them.
 */
void prune_unusable_pclasses() {
    cout << "Pruning pclasses." << endl;
    int pruned = 0;
    pclass_list::iterator pclass_iterator = pclasses.begin();
    while (pclass_iterator != pclasses.end()) {
	if ((*pclass_iterator)->refcount == 0) {
322 323 324 325 326 327 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 356 357 358 359 360 361 362 363 364
            /*
             * Remove the nodes in the pclass we're removing from the slot
             * counts for their ptypes
             */
            tb_pclass::pclass_members_map::iterator ptype_iterator;
            ptype_iterator = (*pclass_iterator)->members.begin();
            while (ptype_iterator != (*pclass_iterator)->members.end()) {
                /*
                 * Find the recort for this type in the ptypes structure
                 */
                fstring this_type = ptype_iterator->first;
                tb_ptype_map::iterator ptype = ptypes.find(this_type);
                assert(ptype != ptypes.end());
                tb_ptype *this_type_p = ptype->second;

                /*
                 * For every node with this type, we want to remove its slot
                 * count from the total slot count for the type, since we know
                 * we will never use this particular node
                 */
                tb_pnodelist::list_iter pnode_iterator =
                    ptype_iterator->second->L.begin();
                while (pnode_iterator != ptype_iterator->second->L.end()) {
                    /*
                     * Get the slotcount for this ptype
                     */
                    tb_pnode::types_map::iterator tm_iterator;
                    tm_iterator = (*pnode_iterator)->types.find(this_type);
                    assert(tm_iterator != (*pnode_iterator)->types.end());

                    /*
                     * Remove it from the current ptype
                     */
                    this_type_p->remove_slots(tm_iterator->second->max_load);

                    /*
                     * Move on to the next node
                     */
                    pnode_iterator++;
                }
                ptype_iterator++;
            }

Robert Ricci's avatar
Robert Ricci committed
365 366 367 368 369 370
	    pclass_list::iterator nukeme = pclass_iterator;
	    pclass_iterator++;
#ifdef PCLASS_DEBUG
	    cout << "Pruning " << (*nukeme)->name << endl;
#endif
	    pruned++;
371

Robert Ricci's avatar
Robert Ricci committed
372 373 374 375 376 377 378 379 380 381
	    delete *nukeme;
	    pclasses.erase(nukeme);
	} else {
	    pclass_iterator++;
	}
    }
    cout << "pclass pruning complete: removed " << pruned << " pclasses, " <<
	pclasses.size() << " remain." << endl;
}

382
void print_help() {
383 384
  cout << "assign [options] ptopfile topfile [config params]" << endl;
  cout << "Options: " << endl;
385
#ifdef TIME_TERMINATE
386
  cout << "  -l <time>   - Limit runtime." << endl;
Mac Newbold's avatar
Mac Newbold committed
387
#endif
388
  cout << "  -s <seed>   - Set the seed." << endl;
389
#ifdef GRAPHVIZ_SUPPORT
390
  cout << "  -v <viz>    - Produce graphviz files with given prefix." <<
391
    endl;
392
#endif
393 394 395
  cout << "  -r          - Don't allow trivial links." << endl;
  cout << "  -p          - Disable pclasses." << endl;
  cout << "  -d          - Enable dynamic pclasses." << endl;
396
#ifdef PER_VNODE_TT
397
  cout << "  -P          - Prune unusable pclasses." << endl;
398
#endif
399 400 401
  cout << "  -T          - Doing some scoring self-testing." << endl;
  cout << "  -H <float>  - Try <float> times harder." << endl;
  cout << "  -o          - Allow overloaded pnodes to be considered." << endl;
402 403
  cout << "  -t <float>  - Start the temperature at <float> instead of melting."
      << endl;
404 405
  cout << "  -u          - Print a summary of the solution." << endl;
  cout << "  -c <float>  - Use the 'connected' pnode finding algorithm " <<
406 407
      "<float>*100% of the time." << endl;
  cout << "  -n          - Don't anneal - just do the prechecks." << endl;
408 409 410
#ifdef WITH_XML
  cout << "  -x          - Use XML top and ptop files (still incomplete)" << endl;
#endif
411
  exit(EXIT_FATAL);
412 413 414 415 416 417 418
}
 
// Perfrom a pre-cehck to make sure that there are enough free nodes of the
// proper types. Returns 1 if the proper types exist, 0 if they do not.
// TODO - move away from using global variables
int type_precheck() {
    cout << "Type precheck:" << endl;
419

420
    bool ok = true;
Mac Newbold's avatar
Mac Newbold committed
421

422
    // First, check the regular types
423 424
    for (name_count_map::iterator vtype_it=vtypes.begin();
	    vtype_it != vtypes.end();++vtype_it) {
425

426
	// Check to see if there were any pnodes of the type at all
427
	tb_ptype_map::iterator ptype_it = ptypes.find(vtype_it->first);
428
	if (ptype_it == ptypes.end()) {
429
	    cout << "  *** No physical nodes of type " << vtype_it->first
430 431
		<< " found" << endl;
	    ok = false;
432
	} else {
433
	    // Okay, there are some - are there enough?
434
	    if (ptype_it->second->pnode_slots() < vtype_it->second) {
435
		cout << "  *** " << vtype_it->second << " nodes of type " <<
436
		    vtype_it->first << " requested, but only "
437 438
		    << ptype_it->second->pnode_slots() << " suitable nodes of "
                    << "type " << vtype_it->first<< " found" << endl;
439 440 441 442 443 444 445 446
		ok = false;
	    }
	    // Okay, there are enough - but are we allowed to use them?
	    if (ptype_it->second->maxusers() &&
		    (ptype_it->second->maxusers() < vtype_it->second)) {
		cout << "  *** " << vtype_it->second << " nodes of type " <<
		    vtype_it->first << " requested, but you are only " <<
		    "allowed to use " << ptype_it->second->maxusers() << endl;
447 448
		ok = false;
	    }
449
	}
450
    }
451 452 453 454

    // Check the vclasses, too
    for (name_list_map::iterator vclass_it = vclasses.begin();
	    vclass_it != vclasses.end(); ++vclass_it) {
455
	bool found_match = false;
456
        // Make sure we actually use this vclass
457
        name_vclass_map::iterator dit = vclass_map.find(vclass_it->first);
458 459 460 461 462 463 464 465 466 467 468
        if (dit == vclass_map.end()) {
            cout << "***: Internal error - unable to find vtype " <<
                vclass_it->first << endl;
            exit(EXIT_FATAL);
        } else {
            if (dit->second->empty()) {
                // Nobody uses it, don't check
                continue;
            }
        }

469
	for (vector<fstring>::iterator vtype_it = vclass_it->second.begin();
470
		vtype_it != vclass_it->second.end(); vtype_it++) {
471 472
	    tb_ptype_map::iterator mit = ptypes.find(*vtype_it);
	    if ((mit != ptypes.end()) && (mit->second->pnode_slots() != 0)) {
473 474 475 476 477 478 479 480 481 482 483 484
		found_match = true;
		break;
	    }
	}

	if (!found_match) {
	    cout << "  *** No physical nodes can satisfy vclass " <<
		vclass_it->first << endl;
	    ok = false;
	}
    }

485
    if (ok) {
486
      cout << "Type precheck passed." << endl;
487 488
      return 1;
    } else {
489
      cout << "*** Type precheck failed!" << endl;
490 491 492
      return 0;
    }
}
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
// Perfrom a pre-cehck to make sure that every vnode has at least one possible
// mapping.  Returns 1 if this is the case, 0 if not.
// TODO - move away from using global variables
int mapping_precheck() {
#ifdef PER_VNODE_TT
    cout << "Node mapping precheck:" << endl;
    /*
     * Build up an entry in the type table for each vnode, by first looking at
     * the type table entry for the vnode's type, then checking each entry to
     * make sure that it:
     * (1) Has enough interfaces
     * (2) Has enough total bandwidth (for emulated links)
     * (3) Meets any 1.0-weight features and/or desires
     */
    vvertex_iterator vit,vendit;
    tie(vit,vendit) = vertices(VG);
510

511 512 513 514
    /*
     * Indicates whether all nodes have potential matches or not
     */
    bool ok = true;
515

516 517
    for (;vit != vendit;vit++) {
	tb_vnode *v = get(vvertex_pmap,*vit);
518

519 520 521 522 523 524 525 526
	pclass_vector *vec = new pclass_vector();
	vnode_type_table[v->name] = tt_entry(0,vec);

	// This constitutes a list of the number of ptypes that matched the
	// criteria. We use to guess what's wrong with the vnode.
	int matched_bw = 0;
	// Keep track of desires had how many 'hits', so that we can tell
	// if any simply were not matched
527
	map<fstring,int> matched_desires;
528

529 530 531
	// Keep track of which link types had how many 'hits', so that we can
	// tell which type(s) caused this node to fail
	tb_vnode::link_counts_map matched_link_counts;
532
	map<fstring,bool> matched_links;
533

534 535 536 537
	tb_vclass *vclass = v->vclass;
	tb_vclass::members_map::iterator mit;
	if (vclass) {
	    mit = vclass->members.begin();
538
	}
539 540 541
	for (;;) {
	    // Loop over all types this node can take on, which might be only
	    // one, if it's not part of a vclass
542
	    fstring this_type;
543 544 545 546 547
	    if (vclass) {
		this_type = mit->first;
	    } else {
		this_type = v->type;
	    }
548

549 550 551 552 553 554 555 556
            // Check to make sure there are actually nodes of this type in the
            // physical topology
            if (type_table.find(this_type) == type_table.end()) {
                // Yes, I know, goto is evil. But I'm not gonna indent the next
                // 100 lines of code for this error case
                goto nosuchtype;
            }

557 558
	    for (pclass_vector::iterator it = type_table[this_type].second->begin();
		    it != type_table[this_type].second->end(); it++) {
559

560 561
		bool potential_match = true;
		// Grab the first node of the pclass as a representative sample
562
	 	tb_pnode *pnode = *((*it)->members[this_type]->L.begin());
563 564 565

		// Check to see if any of the link that this pnode has are of
		// the correct type for the virtual node
Mac Newbold's avatar
Mac Newbold committed
566

567 568 569 570 571 572
		// Check bandwidth on emulated links
		if (pnode->total_bandwidth >= v->total_bandwidth) {
		    matched_bw++;
		} else {
		    potential_match = false;
		}
Mac Newbold's avatar
Mac Newbold committed
573

574 575 576 577 578 579 580 581 582 583 584 585
		// Check to see if if the pnode has enough slots of the
		// appropriate type available
		tb_pnode::types_map::iterator mit = pnode->types.find(v->type);
		if (mit == pnode->types.end()) {
		    // Must have been a vtype to get here - ignore it
		} else {
		    if (v->typecount > mit->second->max_load) {
			// Nope, this vnode is too demanding
			potential_match = false;
		    }
		}

586 587 588 589 590 591 592
		/*
		 * Check features and desires
		*/
		tb_featuredesire_set_iterator
		    fdit(v->desires.begin(),v->desires.end(),
			 pnode->features.begin(),pnode->features.end());
		for (;!fdit.done();fdit++) {
593
		    // Skip 'local' and 'global' features
594
		    if (fdit->is_global() || fdit->is_local()) {
595 596
			continue;
		    }
597 598 599
		    // Only check for FDs that would result in a violation if
		    // unmatched.
		    if (fdit.either_violateable()) {
600 601 602 603 604
			if (fdit.membership() !=
				tb_featuredesire_set_iterator::BOTH) {
			    potential_match = false;
			}

605 606
			// We look for violateable desires on vnodes so that we
			// can report them to the user
607 608 609 610 611 612 613 614 615
			if ((fdit.membership() ==
				tb_featuredesire_set_iterator::FIRST_ONLY ||
			    fdit.membership() ==
				 tb_featuredesire_set_iterator::BOTH)
				&& fdit.first_iterator()->is_violateable()) {
			    if (matched_desires.find(fdit->name()) ==
				    matched_desires.end()) {
				matched_desires[fdit->name()] = 0;
			    }
616 617 618 619
			}
			if (fdit.membership() ==
				tb_featuredesire_set_iterator::BOTH) {
			    matched_desires[fdit->name()]++;
620 621 622
			}
		    }
		}
623
		
624 625 626 627
		// Check link types
		tb_vnode::link_counts_map::iterator vit;
		for (vit = v->link_counts.begin(); vit != v->link_counts.end();
		    vit++) {
628
		  fstring type = vit->first;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
		  int count = vit->second;
		  if (pnode->link_counts.find(type) !=
			pnode->link_counts.end()) {
		    // Found at least one link of this type
		    matched_links[type] = true;
		    if (pnode->link_counts[type] >= count) {
		      // Great, there are enough, too
		      matched_link_counts[type]++;
		    } else {
		      potential_match = false;
		    }
		  } else {
		    potential_match = false;
		  }
		}
Mac Newbold's avatar
Mac Newbold committed
644

645 646 647 648 649 650
		if (potential_match) {
		    vec->push_back(*it);
		    vnode_type_table[v->name].first++;
		    (*it)->refcount++;
#ifdef PCLASS_DEBUG
		    cerr << v->name << " can map to " << (*it)->name << endl;
651
#endif
652 653
		}
	    }
654

655
nosuchtype:
656 657 658 659 660 661 662 663 664
	    if (vclass) { 
		mit++;
		if (mit == vclass->members.end()) {
		    break;
		}
	    } else {
		// If not a vtype, we only had to do this once
		break;
	    }
665
	}
666

667
	if (vnode_type_table[v->name].first == 0) {
668
	    cout << "  *** No possible mapping for " << v->name << endl;
669
	    // Make an attempt to figure out why it didn't match
670 671 672 673 674
	    
	    // Check all of its link types
	    tb_vnode::link_counts_map::iterator lit;
	    for (lit = v->link_counts.begin(); lit != v->link_counts.end();
		lit++) {
675
	      fstring type = lit->first;
676 677 678 679 680 681 682 683
	      if (!matched_links[type]) {
		cout << "      No links of type " << type << " found!" << endl;
	      } else {
		if (!matched_link_counts[type]) {
		  cout << "      Too many links of type " << type << "!"
		    << endl;
		}
	      }
684
	    }
685

686
	    if (!matched_bw) {
687
		cout << "      Too much bandwidth on emulated links!" << endl;
688
	    }
689

690
	    for (map<fstring,int>::iterator dit = matched_desires.begin();
691 692 693
		    dit != matched_desires.end();
		    dit++) {
		if (dit->second == 0) {
694
		    cout << "      No physical nodes have feature " << dit->first
695 696
			<< "!" << endl;
		}
697
	    }
698
	    ok = false;
699
	}
700 701 702
#ifdef PCLASS_DEBUG
	cerr << v->name << " can map to " << vnode_type_table[v->name].first << " pclasses"
	    << endl;
703
#endif
704

705
    }
706

707 708 709
    if (ok) {
	cout << "Node mapping precheck succeeded" << endl;
	return 1;
710
    } else {
711
	cout << "*** Node mapping precheck failed!" << endl;
712
	return 0;
713 714
    }

715 716 717
#else // PER_VNODE_TT
    // PER_VNODE_TT is required for this check, just pretend it's OK.
    return 1;
Robert Ricci's avatar
Robert Ricci committed
718
#endif
719
}
720

721 722 723
// Signal handler - add a convneint way to kill assign and make it return an
// unretryable error
void exit_unretryable(int signal) {
724
  cout << "*** Killed with signal " << signal << " - exiting!" << endl;
725 726 727 728 729 730 731 732 733
  _exit(EXIT_UNRETRYABLE);
}

// Singal handler - add a way for a user to get some status information
extern double temp;
void status_report(int signal) {
  cout << "I: " << iters << " T: " << temp << " S: " << get_score() << " V: "
    << violated << " (Best S: " << absbest << " V:" << absbestviolated << ")"
    << endl;
734 735
}

736 737 738 739
// From anneal.cc - the best solution found
extern solution best_solution;

int main(int argc,char **argv) {
740
  int seed = 0;
741
#ifdef GRAPHVIZ_SUPPORT
742
  fstring viz_prefix;
743
#endif
744
  bool scoring_selftest = false;
745
  bool prechecks_only = false;
746 747 748 749 750
  
  // Handle command line
  char ch;
  timelimit = 0.0;
  timetarget = 0.0;
751
  while ((ch = getopt(argc,argv,"s:v:l:t:rpPTdH:oguc:nx")) != -1) {
752 753 754 755
    switch (ch) {
    case 's':
      if (sscanf(optarg,"%d",&seed) != 1) {
	print_help();
756
      }
757
      break;
758
#ifdef GRAPHVIZ_SUPPORT
759 760 761
    case 'v':
      viz_prefix = optarg;
      break;
762
#endif
763 764 765 766 767 768 769 770 771 772 773 774 775
#ifdef TIME_TERMINATE
    case 'l':
      if (sscanf(optarg,"%lf",&timelimit) != 1) {
	print_help();
      }
      break;
#endif
#ifdef TIME_TARGET
    case 't':
      if (sscanf(optarg,"%lf",&timetarget) != 1) {
	print_help();
      }
      break;
776
#endif
777 778 779
    case 'r':
      allow_trivial_links = false; break;
    case 'p':
780
      disable_pclasses = true; break;
Robert Ricci's avatar
Robert Ricci committed
781 782 783 784
#ifdef PER_VNODE_TT
    case 'P':
      prune_pclasses = true; break;
#endif
785 786
    case 'T':
      scoring_selftest = true; break;
787 788
    case 'd':
      dynamic_pclasses = true; break;
789 790 791 792 793
    case 'H':
      if (sscanf(optarg,"%lf",&scale_neighborhood) != 1) {
	print_help();
      }
      break;
794 795
    case 'o':
      allow_overload = true; break;
Robert Ricci's avatar
Robert Ricci committed
796 797
    case 'g':
      greedy_link_assignment = true; break;
798 799 800 801 802
    case 't':
      if (sscanf(optarg,"%lf",&initial_temperature) != 1) {
	print_help();
      }
      no_melting = true; break;
803 804 805 806 807 808 809
    case 'u':
      print_summary = true; break;
    case 'c':
      if (sscanf(optarg,"%lf",&use_connected_pnode_find) != 1) {
	print_help();
      }
      break;
810 811 812 813
    case 'n':
      prechecks_only = true;
      cout << "Doing only prechecks, exiting early" << endl;
      break;
814 815 816 817 818
#ifdef WITH_XML
    case 'x':
      xml_input = true;
      break;
#endif
819 820
    default:
      print_help();
821
    }
822
  }
823 824 825 826 827 828 829 830 831
  argc -= optind;
  argv += optind;
  
  if (seed == 0) {
    if (getenv("ASSIGN_SEED") != NULL) {
      sscanf(getenv("ASSIGN_SEED"),"%d",&seed);
    } else {
      seed = time(NULL)+getpid();
    }
832
  }
833

834
#ifdef GRAPHVIZ_SUPPORT
835 836 837
  if (viz_prefix.size() == 0) {
    if (getenv("ASSIGN_GRAPHVIZ") != NULL) {
      viz_prefix = getenv("ASSIGN_GRAPHVIZ");
838
    }
839
  }
840
#endif
Mac Newbold's avatar
Mac Newbold committed
841

842 843 844 845
  if (argc == 0) {
    print_help();
  }

846 847 848 849 850 851 852

  // Set up a signal handler for USR1 that exits with an unretryable error
  struct sigaction action;
  action.sa_handler = exit_unretryable;
  sigemptyset(&action.sa_mask);
  action.sa_flags = 0;
  sigaction(SIGUSR1,&action,NULL);
853
  sigaction(SIGINT,&action,NULL);
854

855 856 857 858 859
  // Set up a signal handler for control+T
  struct sigaction action2;
  action2.sa_handler = status_report;
  sigemptyset(&action2.sa_mask);
  action2.sa_flags = 0;
860
#ifdef __FreeBSD__
861
  sigaction(SIGINFO,&action2,NULL);
862 863
#endif 
  
864 865 866 867 868 869 870 871 872 873 874 875 876
  // Convert options to the common.h parameters.
  parse_options(argv, options, noptions);
#ifdef SCORE_DEBUG
  dump_options("Configuration options:", options, noptions);
#endif

#ifdef GNUPLOT_OUTPUT
  scoresout = fopen("scores.out","w");
  tempout = fopen("temp.out","w");
  deltaout = fopen("delta.out","w");
#endif

  cout << "seed = " << seed << endl;
877
  srandom(seed);
878 879 880

  read_physical_topology(argv[0]);
  calculate_switch_MST();
881 882 883 884 885

  read_virtual_topology(argv[1]);

  // Time while we make pclasses and do the type prechecks
  timestart = used_time();
886 887
  
  cout << "Generating physical equivalence classes:";
888
  generate_pclasses(PG,disable_pclasses,dynamic_pclasses);
889
  cout << pclasses.size() << endl;
890

891 892 893
#ifdef PCLASS_DEBUG
  pclass_debug();
#endif
894

895 896 897 898 899 900
  /*
   * There is a reason for the ordering of the prechecks. The mapping precheck
   * basically assumes that the type precheck has already run, so it doesn't
   * have to worry about nodes which cannot map due to type.
   */
  
901 902
  // Run the type precheck
  if (!type_precheck()) {
903
      exit(EXIT_UNRETRYABLE);
904
  }
905

906 907
  // Run the mapping precheck
  if (!mapping_precheck()) {
908
      exit(EXIT_UNRETRYABLE);
909
  }
Robert Ricci's avatar
Robert Ricci committed
910

911
#ifdef PER_VNODE_TT
Robert Ricci's avatar
Robert Ricci committed
912 913
  if (prune_pclasses) {
      prune_unusable_pclasses();
914 915 916 917 918 919 920 921 922
      /*
       * Run the type precheck again, this time without the pclasses we just
       * pruned. Yes, it's a bit redundant, but for the reasons stated above,
       * we mave to run the type precheck before the mapping precheck. And,
       * the type precheck is very fast.
       */
      if (!type_precheck()) {
          exit(EXIT_UNRETRYABLE);
      }
Robert Ricci's avatar
Robert Ricci committed
923
  }
924 925
#endif

926 927 928 929 930 931 932 933 934 935
  // Run the type precheck
  if (!type_precheck()) {
      exit(EXIT_UNRETRYABLE);
  }

  // Bomb out early if we're only doing the prechecks
  if (prechecks_only) {
      exit(EXIT_SUCCESS);
  }

936
  // Output graphviz if necessary
937
#ifdef GRAPHVIZ_SUPPORT
938
  if (viz_prefix.size() != 0) {
939 940 941
    fstring vviz = viz_prefix + "_virtual.viz";
    fstring pviz = viz_prefix + "_physical.viz";
    fstring sviz = viz_prefix + "_switch.viz";
942 943 944 945 946 947 948 949 950 951 952
    ofstream vfile,pfile,sfile;
    vfile.open(vviz.c_str());
    write_graphviz(vfile,VG,vvertex_writer(),vedge_writer(),graph_writer());
    vfile.close();
    pfile.open(pviz.c_str());
    write_graphviz(pfile,PG,pvertex_writer(),pedge_writer(),graph_writer());
    pfile.close();
    sfile.open(sviz.c_str());
    write_graphviz(sfile,SG,svertex_writer(),sedge_writer(),graph_writer());
    sfile.close();
  }
953
#endif
954 955 956 957 958 959 960 961 962

  // Handle the initial temperature, if one was given - a NULL initial temp.
  // means that we should start with the normal melting procedure
  double *initial_temperature_pointer;
  if (no_melting) {
    initial_temperature_pointer = &initial_temperature;
  } else {
    initial_temperature_pointer = NULL;
  }
963
 
964
  // Note, time is started earlier now, up by where we make pclasses
965 966
  anneal(scoring_selftest, scale_neighborhood, initial_temperature_pointer,
      use_connected_pnode_find);
967
  timeend = used_time();
968

969 970 971 972 973 974
#ifdef GNUPLOT_OUTPUT
  fclose(scoresout);
  fclose(tempout);
  fclose(deltaout);
#endif

975
  if ((!compare_scores(get_score(),absbest)) || (violated > absbestviolated)) {
976
    cout << "WARNING: Internal scoring inconsistency." << endl;
977
    cout << "score:" << get_score() << " absbest:" << absbest <<
978 979 980
      " violated:" << violated << " absbestviolated:" <<
      absbestviolated << endl;
  }
981
  
982
  cout << "   BEST SCORE:  " << get_score() << " in " << iters <<
983 984 985 986
    " iters and " << timeend-timestart << " seconds" << endl;
  cout << "With " << violated << " violations" << endl;
  cout << "Iters to find best score:  " << iters_to_best << endl;
  cout << "Violations: " << violated << endl;
987
  cout << vinfo;
988

989
  print_solution(best_solution);
990

991
  if (print_summary) {
992
    print_solution_summary(best_solution);
993 994
  }

995
#ifdef GRAPHVIZ_SUPPORT
996
  if (viz_prefix.size() != 0) {
997
    fstring aviz = viz_prefix + "_solution.viz";
998 999 1000 1001 1002 1003
    ofstream afile;
    afile.open(aviz.c_str());
    write_graphviz(afile,VG,solution_vertex_writer(),
		   solution_edge_writer(),graph_writer());
    afile.close();
  }
1004
#endif
1005
  
Robert Ricci's avatar
Robert Ricci committed
1006
  if (violated != 0) {
1007
      exit(EXIT_RETRYABLE);
1008
  } else {
1009
      exit(EXIT_SUCCESS);
1010
  }
1011
}
1012