fattree.c 81.4 KB
Newer Older
1 2
#include <ross.h>

Noah Wolfe's avatar
Noah Wolfe committed
3
#include "codes/jenkins-hash.h"
4 5 6 7 8 9 10
#include "codes/codes_mapping.h"
#include "codes/codes.h"
#include "codes/model-net.h"
#include "codes/model-net-method.h"
#include "codes/model-net-lp.h"
#include "codes/net/fattree.h"
#include "sys/file.h"
11
#include "codes/quickhash.h"
Noah Wolfe's avatar
Noah Wolfe committed
12
#include "codes/rc-stack.h"
13 14 15 16

#define CREDIT_SIZE 8
#define MEAN_PROCESS 1.0

17 18
#define FTREE_HASH_TABLE_SIZE 262144

19
// debugging parameters
Noah Wolfe's avatar
Noah Wolfe committed
20 21
//#define TRACK_PKT -1
#define TRACK_PKT 2820
22
#define FATTREE_HELLO 0
23
#define FATTREE_DEBUG 1
24
#define FATTREE_CONNECTIONS 1
25
#define FATTREE_MSG 0
Nikhil's avatar
Nikhil committed
26
#define DEBUG_RC 0
27

28
//Data Collection Output Files
29
#define PARAMS_LOG 1
30

31 32 33
#define LP_CONFIG_NM (model_net_lp_config_names[FATTREE])
#define LP_METHOD_NM (model_net_method_names[FATTREE])

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#if DEBUG_RC
  //Reverse Compute Debug Variables
  long long packet_event_f = 0;
  long long packet_event_r = 0;
  long long t_generate_f = 0;
  long long t_generate_r = 0;
  long long t_send_f = 0;
  long long t_send_r = 0;
  long long t_arrive_f = 0;
  long long t_arrive_r = 0;
  long long t_buffer_f = 0;
  long long t_buffer_r = 0;
  long long s_send_f = 0;
  long long s_send_r = 0;
  long long s_arrive_f = 0;
  long long s_arrive_r = 0;
  long long s_buffer_f = 0;
  long long s_buffer_r = 0;
#endif

54 55
long fattree_packet_gen = 0, fattree_packet_fin = 0;

56 57 58
static double maxd(double a, double b) { return a < b ? b : a; }

// arrival rate
Noah Wolfe's avatar
Noah Wolfe committed
59
//static double MEAN_INTERVAL=200.0;
60 61

typedef struct fattree_param fattree_param;
62
/* annotation-specific parameters (unannotated entry occurs at the
63 64 65
 * last index) */
static uint64_t                  num_params = 0;
static fattree_param           * all_params = NULL;
Noah Wolfe's avatar
Noah Wolfe committed
66
static const config_anno_map_t * anno_map   = NULL;
67 68 69 70 71 72 73

/* global variables for codes mapping */
static char lp_group_name[MAX_NAME_LENGTH];
static char def_group_name[MAX_NAME_LENGTH];
static int def_gname_set = 0;
static int mapping_grp_id, mapping_type_id, mapping_rep_id, mapping_offset;

74 75 76
/* switch magic number */
int switch_magic_num = 0;

77
/* terminal magic number */
78
int fattree_terminal_magic_num = 0;
79

80 81 82 83 84 85 86 87
typedef struct fattree_message_list fattree_message_list;
struct fattree_message_list {
    fattree_message msg;
    char* event_data;
    fattree_message_list *next;
    fattree_message_list *prev;
};

88
void init_fattree_message_list(fattree_message_list *this,
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  fattree_message *inmsg) {
    this->msg = *inmsg;
    this->event_data = NULL;
    this->next = NULL;
    this->prev = NULL;
}

void delete_fattree_message_list(fattree_message_list *this) {
    if(this->event_data != NULL) free(this->event_data);
    free(this);
}

struct fattree_param
{
  int ft_type;
  // configuration parameters
  int num_levels;
106 107 108
  int link_repetitions;
  int Ns; // number of switches per pod
  int Np; //Number of pods
109 110 111
  int *num_switches; //switches at various levels
  int *switch_radix; //radix of switches are various levels
  double link_bandwidth;/* bandwidth of a wire connecting switches */
112
  double cn_bandwidth;/* bandwidth of the compute node channels
113 114 115
                        connected to switch */
  int vc_size; /* buffer size of the link channels */
  int cn_vc_size; /* buffer size of the compute node channels */
116
  int chunk_size; /* full-sized packets are broken into smaller chunks.*/
117
  int packet_size;
118 119 120 121 122 123 124 125 126 127
  int num_terminals;
  int l1_set_size;
  int l1_term_size;
  double cn_delay;
  double head_delay;
  double credit_delay;
  double router_delay;
  double soft_delay;
};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
struct ftree_hash_key
{
    uint64_t message_id;
    tw_lpid sender_id;
};

struct ftree_qhash_entry
{
   struct ftree_hash_key key;
   char * remote_event_data;
   int num_chunks;
   int remote_event_size;
   struct qhash_head hash_link;
};

143 144 145 146
/* convert GiB/s and bytes to ns */
 static tw_stime bytes_to_ns(uint64_t bytes, double GB_p_s)
 {
     tw_stime time;
147

148 149 150 151 152 153
     /* bytes to GB */
     time = ((double)bytes)/(1024.0*1024.0*1024.0);
     /* GiB to s */
     time = time / GB_p_s;
     /* s to ns */
     time = time * 1000.0 * 1000.0 * 1000.0;
154

155 156 157
     return(time);
 }

158 159 160 161 162 163 164 165 166
/* handles terminal and switch events like packet generate/send/receive/buffer */
typedef enum event_t event_t;
typedef struct ft_terminal_state ft_terminal_state;
typedef struct switch_state switch_state;

/* fattree compute node data structure */
struct ft_terminal_state
{
  unsigned long long packet_counter;
167
  int packet_gen;
168
  int packet_fin;
169 170 171 172 173 174 175 176 177
  // Fattree specific parameters
  unsigned int terminal_id;
  unsigned int switch_id;
  tw_lpid switch_lp;

  // Each terminal will have an input and output channel with the switch
  int vc_occupancy; // NUM_VC
  tw_stime terminal_available_time;
  tw_stime next_credit_available_time;
178

179 180
  struct mn_stats fattree_stats_array[CATEGORY_MAX];

181 182
  fattree_message_list **terminal_msgs;
  fattree_message_list **terminal_msgs_tail;
183
  int terminal_length;
184
  int in_send_loop;
185 186 187
  int issueIdle;

   struct rc_stack * st;
188 189 190

  char * anno;
  fattree_param *params;
191 192 193 194 195 196 197 198 199 200 201

  struct qhash_table *rank_tbl;
  uint64_t rank_tbl_pop;

  tw_stime   total_time;
  uint64_t total_msg_size;
  double total_hops;
  long finished_msgs;
  long finished_chunks;
  long finished_packets;

202
  tw_stime last_buf_full;
203
  tw_stime busy_time;
204
  char output_buf[4096];
205
  char output_buf3[4096];
206

207 208 209 210 211
  /* For sampling */
  long fin_chunks_sample;
  long data_size_sample;
  double fin_hops_sample;
  tw_stime fin_chunks_time;
212
 tw_stime busy_time_sample;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
};

/* terminal event type (1-4) */
enum event_t
{
  T_GENERATE=1,
  T_ARRIVE,
  T_SEND,
  T_BUFFER,
  S_SEND,
  S_ARRIVE,
  S_BUFFER,
};

enum last_hop
{
   LINK,
   TERMINAL
};

struct switch_state
{
  unsigned int switch_id;
  int switch_level;
  int radix;
  int num_cons;
  int num_lcons;
  int con_per_lneigh;
  int con_per_uneigh;;
  int start_lneigh;
  int end_lneigh;
  int start_uneigh;
  int unused;

  tw_stime* next_output_available_time;
  tw_stime* next_credit_available_time;
249 250 251 252 253
  tw_stime* last_buf_full;

  tw_stime* busy_time;
  tw_stime* busy_time_sample;

254 255 256 257 258 259 260 261 262 263
  fattree_message_list **pending_msgs;
  fattree_message_list **pending_msgs_tail;
  fattree_message_list **queued_msgs;
  fattree_message_list **queued_msgs_tail;
  int *queued_length;
  int *in_send_loop;
  int* vc_occupancy;
  int64_t* link_traffic;
  tw_lpid *port_connections;

264 265 266
  char output_buf[4096];
  char output_buf2[4096];

Noah Wolfe's avatar
Noah Wolfe committed
267 268
  struct rc_stack * st;

269 270 271 272
  char * anno;
  fattree_param *params;
};

273 274
static tw_stime         fattree_total_time = 0;
static tw_stime         fattree_max_latency = 0;
Noah Wolfe's avatar
Noah Wolfe committed
275
//static tw_stime         max_collective = 0;
276 277 278 279 280 281 282 283


static long long       total_hops = 0;
static long long       N_finished_packets = 0;
static long long       total_msg_sz = 0;
static long long       N_finished_msgs = 0;
static long long       N_finished_chunks = 0;

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static int fattree_rank_hash_compare(
void *key, struct qhash_head *link)
{
    struct ftree_hash_key *message_key = (struct ftree_hash_key *)key;
    struct ftree_qhash_entry *tmp = NULL;

    tmp = qhash_entry(link, struct ftree_qhash_entry, hash_link);

    if (tmp->key.message_id == message_key->message_id
            && tmp->key.sender_id == message_key->sender_id)
        return 1;

    return 0;
}
static int fattree_hash_func(void *k, int table_size)
{
    struct ftree_hash_key *tmp = (struct ftree_hash_key *)k;
    uint64_t key = (~tmp->message_id) + (tmp->message_id << 18);
    key = key * 21;
    key = ~key ^ (tmp->sender_id >> 4);
    key = key * tmp->sender_id;
    return (int)(key & (table_size - 1));
}

308 309
static void free_tmp(void * ptr)
{
310
    struct ftree_qhash_entry * ftree = ptr;
311 312 313 314
    free(ftree->remote_event_data);
    free(ftree);
}

315
static void append_to_fattree_message_list(
316 317
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
318
        int index,
319 320 321 322 323 324
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thisq[index] = msg;
    } else {
        thistail[index]->next = msg;
        msg->prev = thistail[index];
325
    }
326 327 328
    thistail[index] = msg;
}

329
static void prepend_to_fattree_message_list(
330 331
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
332
        int index,
333 334 335 336 337 338
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thistail[index] = msg;
    } else {
        thisq[index]->prev = msg;
        msg->next = thisq[index];
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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    thisq[index] = msg;
}

static fattree_message_list* return_head(
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
        int index) {
    fattree_message_list *head = thisq[index];
    if(head != NULL) {
        thisq[index] = head->next;
        if(head->next != NULL) {
            head->next->prev = NULL;
            head->next = NULL;
        } else {
            thistail[index] = NULL;
        }
    }
    return head;
}

static fattree_message_list* return_tail(
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
        int index) {
    fattree_message_list *tail = thistail[index];
    if(tail->prev != NULL) {
        tail->prev->next = NULL;
        thistail[index] = tail->prev;
        tail->prev = NULL;
    } else {
        thistail[index] = NULL;
        thisq[index] = NULL;
    }
    return tail;
}

//decl
void switch_credit_send(switch_state * s, tw_bf * bf, fattree_message * msg,
    tw_lp * lp, int sq);
int ft_get_output_port( switch_state * s, tw_bf * bf, fattree_message * msg,
    tw_lp * lp, int *out_off );
int get_base_port(switch_state *s, int from_term, int index);


/* returns the fattree switch lp type for lp registration */
Noah Wolfe's avatar
Noah Wolfe committed
385
//static const tw_lptype* fattree_get_switch_lp_type(void);
386

387
/* returns the fattree message size */
388 389 390 391 392
static int fattree_get_msg_sz(void)
{
  return sizeof(fattree_message);
}

Noah Wolfe's avatar
Noah Wolfe committed
393
static void fattree_read_config(const char * anno, fattree_param *p){
394 395
  int i;

Nikhil's avatar
Nikhil committed
396
  p->ft_type = 1;
397
  configuration_get_value_int(&config, "PARAMS", "ft_type", anno,
398 399
      &p->ft_type);

400
  configuration_get_value_int(&config, "PARAMS", "num_levels", anno,
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
      &p->num_levels);
  if(p->num_levels <= 0) {
    tw_error(TW_LOC, "Too few num_levels, Aborting\n");
  }
  if(p->num_levels > 3) {
    tw_error(TW_LOC, "Too many num_levels, only upto 3 supported Aborting\n");
  }

  p->num_switches = (int *) malloc (p->num_levels * sizeof(int));
  p->switch_radix = (int*) malloc (p->num_levels * sizeof(int));

  char switch_counts_str[MAX_NAME_LENGTH];
  int rc = configuration_get_value(&config, "PARAMS", "switch_count", anno,
      switch_counts_str, MAX_NAME_LENGTH);
  if (rc == 0){
    tw_error(TW_LOC, "couldn't read PARAMS:switch_count");
  }
  char* token;
  token = strtok(switch_counts_str, ",");
  i = 0;
  while(token != NULL)
  {
    sscanf(token, "%d", &p->num_switches[i]);
    if(p->num_switches[i] <= 0)
    {
      tw_error(TW_LOC, "Invalid switch count  specified "
          "(%d at pos %d), exiting... ", p->num_switches[i], i);
    }
    i++;
    token = strtok(NULL,",");
  }

  char switch_radix_str[MAX_NAME_LENGTH];
  rc = configuration_get_value(&config, "PARAMS", "switch_radix", anno,
      switch_radix_str, MAX_NAME_LENGTH);
  if (rc == 0){
    tw_error(TW_LOC, "couldn't read PARAMS:switch_radix");
  }
  token = strtok(switch_radix_str, ",");
  i = 0;
  while(token != NULL)
  {
    sscanf(token, "%d", &p->switch_radix[i]);
    if(p->switch_radix[i] <= 0)
    {
      tw_error(TW_LOC, "Invalid switch radix  specified "
          "(%d at pos %d), exiting... ", p->switch_radix[i], i);
    }
    i++;
    token = strtok(NULL,",");
  }

453
  p->Ns = p->switch_radix[0]/2;
454
  int num_terminals = p->num_switches[0]*p->switch_radix[0]/2;
455

456 457 458 459
  if(p->num_levels == 2) {
    p->num_switches[1] = p->num_switches[0]/2;
    p->switch_radix[1] = p->switch_radix[0];
  } else {
460
    if(p->ft_type == 0){
461
      p->Np = ceil((num_terminals)/(double)(p->Ns*p->Ns));
462 463 464 465
      p->num_switches[1] = p->num_switches[0];
      p->num_switches[2] = ceil((p->Ns*p->Ns) / floor(p->switch_radix[0]/p->Np));
      p->switch_radix[1] = p->switch_radix[2] = p->switch_radix[0];
      p->link_repetitions = floor(p->switch_radix[2] / p->Np);
466
      printf("Np:%d Ns:%d\n",p->Np,p->Ns);
467 468 469 470 471 472 473
    }else{
      p->num_switches[1] = p->num_switches[0];
      p->num_switches[2] = p->num_switches[0]/2;
      p->switch_radix[1] = p->switch_radix[2] = p->switch_radix[0];
    }
  }

474
#if FATTREE_CONNECTIONS
475 476 477 478 479 480 481
  for(int jj=0;jj<3;jj++)
  {
    printf("num_switches[%d]=%d\n",jj,p->num_switches[jj]);
    printf("switch_radix[%d]=%d\n",jj,p->switch_radix[jj]);
  }
#endif

482 483 484 485
  i = 1;
  for(i = 1; i < p->num_levels - 1; i++) {
    if(p->num_switches[i - 1] * p->switch_radix[i - 1] >
       p->num_switches[i] * p->switch_radix[i]) {
486 487
//      tw_error(TW_LOC, "Not enough switches/radix at level %d for full "
//          "bisection bandwidth\n", i);
488 489 490 491 492
    }
  }

  if(p->num_switches[i - 1] * p->switch_radix[i - 1] > 2 * p->num_switches[i] *
      p->switch_radix[i]) {
493 494
//    tw_error(TW_LOC, "Not enough switches/radix at level %d (top) for full "
//        "bisection bandwidth\n", i);
495 496 497 498 499 500
  }

  configuration_get_value_int(&config, "PARAMS", "packet_size", anno,
      &p->packet_size);
  if(!p->packet_size) {
    p->packet_size = 512;
501
    fprintf(stderr, "Packet size is not specified, setting to %d\n",
502 503
        p->packet_size);
  }
504

505 506 507
  p->router_delay = 50;
  configuration_get_value_double(&config, "PARAMS", "router_delay", anno,
      &p->router_delay);
508

509 510 511 512 513 514 515 516 517 518 519
  p->soft_delay = 1000;
  configuration_get_value_double(&config, "PARAMS", "soft_delay", anno,
      &p->soft_delay);

  configuration_get_value_int(&config, "PARAMS", "vc_size", anno, &p->vc_size);
  if(!p->vc_size) {
    p->vc_size = 8*p->packet_size;
    fprintf(stderr, "Buffer size of global channels not specified, setting to "
        "%d\n", p->vc_size);
  }

520
  configuration_get_value_int(&config, "PARAMS", "cn_vc_size", anno,
521 522 523
      &p->cn_vc_size);
  if(!p->cn_vc_size) {
    p->cn_vc_size = 8*p->packet_size;
524
    fprintf(stderr, "Buffer size of compute node channels not specified, "
525 526 527
        "setting to %d\n", p->cn_vc_size);
  }

528 529 530 531 532 533
  rc = configuration_get_value_int(&config, "PARAMS", "chunk_size", anno, &p->chunk_size);
    if(rc) {
        p->chunk_size = 512;
        fprintf(stderr, "Chunk size for packets is specified, setting to %d\n", p->chunk_size);
    }

534
  configuration_get_value_double(&config, "PARAMS", "link_bandwidth", anno,
535 536 537
      &p->link_bandwidth);
  if(!p->link_bandwidth) {
    p->link_bandwidth = 5;
538
    fprintf(stderr, "Bandwidth of links is specified, setting to %lf\n",
539 540 541
        p->link_bandwidth);
  }

542
  configuration_get_value_double(&config, "PARAMS", "cn_bandwidth", anno,
543 544 545
      &p->cn_bandwidth);
  if(!p->cn_bandwidth) {
    p->cn_bandwidth = 5;
546
    fprintf(stderr, "Bandwidth of compute node channels not specified, "
547 548 549 550
        "setting to %lf\n", p->cn_bandwidth);
  }

  p->l1_set_size = p->switch_radix[0]/2;
551

552 553
  p->l1_term_size = (p->l1_set_size * (p->switch_radix[0] / 2));

554 555 556 557
#if FATTREE_DEBUG
  printf("l1_set_size:%d l1_term_size:%d\n",p->l1_set_size,p->l1_term_size);
#endif

558 559 560 561 562 563 564 565 566 567 568
  p->cn_delay = (1.0 / p->cn_bandwidth);
  p->head_delay = (1.0 / p->link_bandwidth);
  p->credit_delay = (1.0 / p->link_bandwidth) * 8; //assume 8 bytes packet
}

static void fattree_configure(){
  anno_map = codes_mapping_get_lp_anno_map(LP_CONFIG_NM);
  assert(anno_map);
  num_params = anno_map->num_annos + (anno_map->has_unanno_lp > 0);
  all_params = malloc(num_params * sizeof(*all_params));

Noah Wolfe's avatar
Noah Wolfe committed
569 570
  for (int i = 0; i < anno_map->num_annos; i++){
    const char * anno = anno_map->annotations[i].ptr;
571 572 573 574 575 576 577 578 579 580
    fattree_read_config(anno, &all_params[i]);
  }
  if (anno_map->has_unanno_lp > 0){
    fattree_read_config(NULL, &all_params[anno_map->num_annos]);
  }
}

/* initialize a fattree compute node terminal */
void ft_terminal_init( ft_terminal_state * s, tw_lp * lp )
{
581
    s->packet_gen = 0;
582 583
    s->packet_fin = 0;

584
    uint32_t h1 = 0, h2 = 0;
585 586
    bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
    fattree_terminal_magic_num = h1 + h2;
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    char anno[MAX_NAME_LENGTH];

    if(def_gname_set == 0) {
      def_gname_set = 1;
      codes_mapping_get_lp_info(0, def_group_name, &mapping_grp_id, NULL,
          &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
    }

    // Assign the global switch ID
    codes_mapping_get_lp_info(lp->gid, lp_group_name, &mapping_grp_id, NULL,
            &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
    if (anno[0] == '\0'){
        s->anno = NULL;
        s->params = &all_params[num_params-1];
    } else {
        s->anno = strdup(anno);
        int id = configuration_get_annotation_index(anno, anno_map);
        s->params = &all_params[id];
    }

   int num_lps = codes_mapping_get_lp_count(lp_group_name, 1, LP_CONFIG_NM,
           s->anno, 0);

   if(num_lps != (s->params->switch_radix[0]/2)) {
     tw_error(TW_LOC, "Number of NICs per repetition has to be equal to "
         "half the radix of leaf level switches %d vs %d\n", num_lps,
          s->params->switch_radix[0]/2);
   }
616
   s->terminal_id = (mapping_rep_id * num_lps) + mapping_offset;
617 618 619 620 621
   s->switch_id = s->terminal_id / (s->params->switch_radix[0] / 2);
   codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
           s->switch_id, 0, &s->switch_lp);
   s->terminal_available_time = 0.0;
   s->packet_counter = 0;
622
   s->terminal_msgs =
623
     (fattree_message_list**)malloc(1*sizeof(fattree_message_list*));
624
   s->terminal_msgs_tail =
625 626
     (fattree_message_list**)malloc(1*sizeof(fattree_message_list*));

627 628 629 630 631 632
   s->finished_msgs = 0;
   s->finished_chunks = 0;
   s->finished_packets = 0;
   s->total_time = 0.0;
   s->total_msg_size = 0;

633 634 635
   s->last_buf_full = 0;
   s->busy_time = 0;

636
#if FATTREE_HELLO
637 638
   printf("I am terminal %d (%llu), connected to switch %d\n", s->terminal_id,
       LLU(lp->gid), s->switch_id);
639
#endif
Noah Wolfe's avatar
Noah Wolfe committed
640 641 642

   rc_stack_create(&s->st);

643 644 645 646 647 648 649
   s->vc_occupancy = 0;
   s->terminal_msgs[0] = NULL;
   s->terminal_msgs_tail[0] = NULL;
   s->terminal_length = 0;
   s->in_send_loop = 0;
   s->issueIdle = 0;

650 651 652 653
   s->rank_tbl = qhash_init(fattree_rank_hash_compare, fattree_hash_func, FTREE_HASH_TABLE_SIZE);
   if(!s->rank_tbl)
     tw_error(TW_LOC, "\n Hash table not initialized! ");

654
   s->params->num_terminals = codes_mapping_get_lp_count(lp_group_name, 0,
655 656 657 658 659 660 661
      LP_CONFIG_NM, s->anno, 0);
   return;
}

/* sets up the switch */
void switch_init(switch_state * r, tw_lp * lp)
{
662 663 664 665
  uint32_t h1 = 0, h2 = 0;
  bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
  switch_magic_num = h1 + h2;

666
  char anno[MAX_NAME_LENGTH];
Noah Wolfe's avatar
Noah Wolfe committed
667
  int num_terminals = -1;
668 669 670 671 672

  if(def_gname_set == 0) {
    def_gname_set = 1;
    codes_mapping_get_lp_info(0, def_group_name, &mapping_grp_id, NULL,
        &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
673
    num_terminals = codes_mapping_get_lp_count(def_group_name, 0,
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
      LP_CONFIG_NM, anno, 0);
  }

  codes_mapping_get_lp_info(lp->gid, lp_group_name, &mapping_grp_id, NULL,
      &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);

  if (anno[0] == '\0'){
    r->anno = NULL;
    r->params = &all_params[num_params-1];
  } else {
    r->anno = strdup(anno);
    int id = configuration_get_annotation_index(anno, anno_map);
    r->params = &all_params[id];
  }

  // shorthand
  fattree_param *p = r->params;
  if(mapping_offset == p->num_levels - 1) {
    if(mapping_rep_id >= p->num_switches[mapping_offset]) {
      r->unused = 1;
      return;
    }
  }

  r->unused = 0;

  r->switch_id = mapping_rep_id + mapping_offset * p->num_switches[0];

  if(num_terminals != -1) {
    p->num_terminals = num_terminals;
  }

  r->switch_level = mapping_offset;

  r->radix = p->switch_radix[r->switch_level];

710
  r->next_output_available_time = (tw_stime*) malloc (r->radix *
711
      sizeof(tw_stime));
712
  r->next_credit_available_time = (tw_stime*) malloc (r->radix *
713 714 715 716 717
      sizeof(tw_stime));
  r->vc_occupancy = (int*) malloc (r->radix * sizeof(int));
  r->in_send_loop = (int*) malloc (r->radix * sizeof(int));
  r->link_traffic = (int64_t*) malloc (r->radix * sizeof(int64_t));
  r->port_connections = (tw_lpid*) malloc (r->radix * sizeof(tw_lpid));
718
  r->pending_msgs =
719
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
720
  r->pending_msgs_tail =
721
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
722
  r->queued_msgs =
723
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
724
  r->queued_msgs_tail =
725 726 727
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
  r->queued_length = (int*)malloc(r->radix * sizeof(int));

728

729 730 731 732
  r->last_buf_full = (tw_stime*)malloc(r->radix * sizeof(tw_stime));
  r->busy_time = (tw_stime*)malloc(r->radix * sizeof(tw_stime));
  r->busy_time_sample = (tw_stime*)malloc(r->radix * sizeof(tw_stime));

Noah Wolfe's avatar
Noah Wolfe committed
733 734
  rc_stack_create(&r->st);

735 736 737
  for(int i = 0; i < r->radix; i++)
  {
    // Set credit & switch occupancy
738 739 740
    r->last_buf_full[i] = 0.0;
    r->busy_time[i] = 0.0;
    r->busy_time_sample[i] = 0.0;
741 742 743 744 745 746 747 748 749 750 751 752
    r->next_output_available_time[i] = 0;
    r->next_credit_available_time[i] = 0;
    r->vc_occupancy[i] = 0;
    r->in_send_loop[i] = 0;
    r->link_traffic[i] = 0;
    r->pending_msgs[i] = NULL;
    r->pending_msgs_tail[i] = NULL;
    r->queued_msgs[i] = NULL;
    r->queued_msgs_tail[i] = NULL;
    r->queued_length[i] = 0;
  }

753
#if FATTREE_CONNECTIONS
754 755
	int written = 0;
    int written_2 = 0;
756 757
    tw_lpid next_switch_lid;
#endif
758

759 760 761 762
  //set lps connected to each port
  r->num_cons = 0;
  r->num_lcons = 0;
#if FATTREE_HELLO
763 764
  printf("I am switch %d (%llu), level %d, radix %d\n", r->switch_id,
    LLU(lp->gid), r->switch_level, r->radix);
765 766 767 768 769 770 771 772 773 774 775 776
#endif
  //if at level 0, first half ports go to terminals
  if(r->switch_level == 0) {
    int start_terminal = r->switch_id * (p->switch_radix[0] / 2);
    int end_terminal = start_terminal + (p->switch_radix[0] / 2);
    for(int term = start_terminal; term < end_terminal; term++) {
      tw_lpid nextTerm;
      int rep = term / (p->switch_radix[0] / 2);
      int off = term % (p->switch_radix[0] / 2);
      codes_mapping_get_lp_id(def_group_name, LP_CONFIG_NM, NULL, 1,
          rep, off, &nextTerm);
      r->port_connections[r->num_cons++] = nextTerm;
777 778 779
#if FATTREE_CONNECTIONS
	  written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(codes_mapping_get_lp_relative_id(nextTerm,0,0)));
#endif
780 781
      r->num_lcons++;
#if FATTREE_DEBUG
782 783
      printf("I am switch %d, connect to terminal %d (%llu) at port %d yes collecting\n",
          r->switch_id, term, LLU(nextTerm), r->num_cons - 1);
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
#endif
    }
    r->start_lneigh = start_terminal;
    r->end_lneigh = end_terminal;
    r->con_per_lneigh = 1;
    assert(r->num_lcons == (r->radix / 2));
    int l1_set;
    if(p->num_levels == 2) {
      l1_set = 0;
    } else {
      l1_set = r->switch_id / p->l1_set_size;
    }
    int l1_base = l1_set * p->l1_set_size;
    r->start_uneigh = p->num_switches[0] + l1_base;
    r->con_per_uneigh = 1;
    for(int l1 = 0; l1 < p->l1_set_size; l1++) {
      tw_lpid nextTerm;
      codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
          l1_base, 1, &nextTerm);
      for(int con = 0; con < r->con_per_uneigh; con++) {
        r->port_connections[r->num_cons++] = nextTerm;
805
#if FATTREE_CONNECTIONS
806 807 808
        codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
            &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
        next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
809 810
	    written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
811
#if FATTREE_DEBUG
812 813
      printf("I am switch %d, connect to upper switch %d L1 (%llu) rel_id:%llu at port %d yes collecting\n",
          r->switch_id, l1_base, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
#endif
      }
      l1_base++;
    }
  } else if (r->switch_level == 1) {
    int l0_set_size, l0_base;
    if(p->num_levels == 2) {
      l0_set_size = p->num_switches[0];
      l0_base = 0;
      r->start_lneigh = 0;
      r->end_lneigh = p->num_switches[0];
    } else {
      l0_set_size = p->l1_set_size;
      l0_base = ((r->switch_id - p->num_switches[0]) / p->l1_set_size) *
        l0_set_size;
      r->start_lneigh = l0_base;
      r->end_lneigh = l0_base + l0_set_size;
    }
    r->con_per_lneigh = 1;
    for(int l0 = 0; l0 < l0_set_size; l0++) {
      tw_lpid nextTerm;
      codes_mapping_get_lp_id(def_group_name, "fattree_switch", NULL, 1,
          l0_base, 0, &nextTerm);
      for(int con = 0; con < r->con_per_lneigh; con++) {
        r->port_connections[r->num_cons++] = nextTerm;
839
#if FATTREE_CONNECTIONS
840 841 842
        codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
            &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
        next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
843 844
        written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
845 846
        r->num_lcons++;
#if FATTREE_DEBUG
847 848
        printf("I am switch %d, connect to switch %d L0 (%llu) at port %d not collecting\n",
            r->switch_id, l0_base, LLU(nextTerm), r->num_cons - 1);
849 850 851 852 853
#endif
      }
      l0_base++;
    }
    if(p->num_levels == 3) {
854
      int l2_base = 0;
855
      if(p->ft_type == 0) {
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
        for(int rep=0;rep<p->link_repetitions;rep++){
            int l2 = ((r->switch_id - p->num_switches[0]) % p->l1_set_size + rep*p->l1_set_size);
            printf("link_repetitions:%d\n",p->link_repetitions);
            /* not true anymore */
            r->start_uneigh = p->num_switches[0] + l2;
            r->con_per_uneigh = p->link_repetitions;
            for(; l2 < p->num_switches[2]; l2 += p->l1_set_size) {
              tw_lpid nextTerm;
              codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
                      l2, 2, &nextTerm);
              for(int con = 0; con < r->con_per_uneigh; con++) {
                r->port_connections[r->num_cons++] = nextTerm;
#if FATTREE_CONNECTIONS
                codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
                    &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
                next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
                written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
#if FATTREE_DEBUG
                printf("I am switch %d, connect to upper switch %d L2 (%llu) at port %d yes collecting\n",
                    r->switch_id, l2+rep*p->l1_set_size, LLU(nextTerm), r->num_cons - 1);
#endif
              }
            }
        }
881
      } else {
882 883 884
        int l2 = ((r->switch_id - p->num_switches[0]) % p->l1_set_size);
        /* not true anymore */
        r->start_uneigh = p->num_switches[0] + l2;
885 886 887 888 889 890
        if(p->ft_type == 1){
          r->con_per_uneigh = 1;
        }
        else{
          r->con_per_uneigh = 2;
        }
891 892 893 894 895 896
        for(; l2 < p->num_switches[2]; l2 += p->l1_set_size) {
          tw_lpid nextTerm;
          codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
              l2, 2, &nextTerm);
          for(int con = 0; con < r->con_per_uneigh; con++) {
            r->port_connections[r->num_cons++] = nextTerm;
897
#if FATTREE_CONNECTIONS
898 899 900
            codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
                &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
            next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
901 902
            written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
903
#if FATTREE_DEBUG
904 905
            printf("I am switch %d, connect to upper switch %d L2 (%llu) at port %d yes collecting\n",
                r->switch_id, l2, LLU(nextTerm), r->num_cons - 1);
906 907 908 909 910 911
#endif
          }
        }
      }
    }
  } else {
912
    if(p->ft_type == 0) {
913
      r->con_per_lneigh = p->link_repetitions;
914 915 916
      /* not true anymore */
      r->start_lneigh = p->num_switches[0];
      r->end_lneigh = r->start_lneigh + p->num_switches[1];
917 918
      int l1 = (r->switch_id - p->num_switches[0] - p->num_switches[1]) % p->l1_set_size;
      for(; l1 < p->num_switches[1]; l1 += p->l1_set_size) {
919 920 921 922 923
        tw_lpid nextTerm;
        codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
            l1, 1, &nextTerm);
        for(int con = 0; con < r->con_per_lneigh; con++) {
          r->port_connections[r->num_cons++] = nextTerm;
924
#if FATTREE_CONNECTIONS
925 926 927
          codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
              &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
          next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
928 929
	      written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
930 931 932 933 934 935 936 937
          r->num_lcons++;
#if FATTREE_DEBUG
          printf("I am switch %d, connect to  switch %d L1 (%llu) at port %d not collecting\n",
              r->switch_id, l1, LLU(nextTerm), r->num_cons - 1);
#endif
        }
      }
    } else {
938 939 940 941 942 943 944 945 946 947 948
      r->con_per_lneigh = 2;
      /* not true anymore */
      r->start_lneigh = p->num_switches[0];
      r->end_lneigh = r->start_lneigh + p->num_switches[1];
      int l1 = (r->switch_id - p->num_switches[0] - p->num_switches[1]) % p->l1_set_size;
      for(; l1 < p->num_switches[1]; l1 += p->l1_set_size) {
        tw_lpid nextTerm;
        codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
            l1, 1, &nextTerm);
        for(int con = 0; con < r->con_per_lneigh; con++) {
          r->port_connections[r->num_cons++] = nextTerm;
949
#if FATTREE_CONNECTIONS
950 951 952
          codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
              &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
          next_switch_lid = mapping_rep_id + mapping_offset * p->num_switches[0];
953 954
	      written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
955 956
          r->num_lcons++;
#if FATTREE_DEBUG
957 958
          printf("I am switch %d, connect to  switch %d L1 (%llu) at port %d not collecting\n",
              r->switch_id, l1, LLU(nextTerm), r->num_cons - 1);
959 960 961 962 963
#endif
        }
      }
    }
  }
964
#if FATTREE_CONNECTIONS
965 966
  lp_io_write(lp->gid, "fattree-config-up-connections", written, r->output_buf);
  lp_io_write(lp->gid, "fattree-config-down-connections", written_2, r->output_buf2);
967
#endif
968
  return;
969
}
970 971

/* empty for now.. */
972
static void fattree_report_stats()
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
{
#if DEBUG_RC
	long long t_packet_event_f = 0;
	long long t_packet_event_r = 0;
	long long tt_generate_f = 0;
	long long tt_generate_r = 0;
	long long tt_send_f = 0;
	long long tt_send_r = 0;
	long long tt_arrive_f = 0;
	long long tt_arrive_r = 0;
	long long tt_buffer_f = 0;
	long long tt_buffer_r = 0;
	long long ts_send_f = 0;
	long long ts_send_r = 0;
	long long ts_arrive_f = 0;
	long long ts_arrive_r = 0;
	long long ts_buffer_f = 0;
	long long ts_buffer_r = 0;
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 1030 1031 1032 1033 1034 1035 1036
	MPI_Reduce( &packet_event_f, &t_packet_event_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &packet_event_r, &t_packet_event_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_generate_f, &tt_generate_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_generate_r, &tt_generate_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_send_f, &tt_send_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_send_r, &tt_send_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_arrive_f, &tt_arrive_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_arrive_r, &tt_arrive_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_buffer_f, &tt_buffer_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &t_buffer_r, &tt_buffer_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_send_f, &ts_send_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_send_r, &ts_send_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_arrive_f, &ts_arrive_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_arrive_r, &ts_arrive_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_buffer_f, &ts_buffer_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
	MPI_Reduce( &s_buffer_r, &ts_buffer_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);

   if(!g_tw_mynode)
   {
	printf("Reverse Computation Counters:\n");
	printf("packet_event_f:%llu\n",t_packet_event_f);
	printf("packet_event_r:%llu\n",t_packet_event_r);
	printf("t_gen_f:%llu\n",tt_generate_f);
	printf("t_gen_r:%llu\n",tt_generate_r);
	printf("t_gen_net:%llu\n",tt_generate_f-tt_generate_r);
	printf("t_send_f:%llu\n",tt_send_f);
	printf("t_send_r:%llu\n",tt_send_r);
	printf("t_send_net:%llu\n",tt_send_f-tt_send_r);
	printf("t_arrive_f:%llu\n",tt_arrive_f);
	printf("t_arrive_r:%llu\n",tt_arrive_r);
	printf("t_arrive_net:%llu\n",tt_arrive_f-tt_arrive_r);
	printf("t_buf_f:%llu\n",tt_buffer_f);
	printf("t_buf_r:%llu\n",tt_buffer_r);
	printf("t_buf_net:%llu\n",tt_buffer_f-tt_buffer_r);
	printf("s_send_f:%llu\n",ts_send_f);
	printf("s_send_r:%llu\n",ts_send_r);
	printf("s_send_net:%llu\n",ts_send_f-ts_send_r);
	printf("s_arrive_f:%llu\n",ts_arrive_f);
	printf("s_arrive_r:%llu\n",ts_arrive_r);
	printf("s_arrive_net:%llu\n",ts_arrive_f-ts_arrive_r);
	printf("s_buffer_f:%llu\n",ts_buffer_f);
	printf("s_buffer_r:%llu\n",ts_buffer_r);
	printf("s_buffer_net:%llu\n",ts_buffer_f-ts_buffer_r);
   }
#endif
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

   long long avg_hops, total_finished_packets, total_finished_chunks;
   long long total_finished_msgs, final_msg_sz;
   tw_stime avg_time, max_time;
   long total_gen, total_fin;

   MPI_Reduce( &total_hops, &avg_hops, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &N_finished_packets, &total_finished_packets, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &N_finished_msgs, &total_finished_msgs, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &N_finished_chunks, &total_finished_chunks, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &total_msg_sz, &final_msg_sz, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &fattree_total_time, &avg_time, 1,MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &fattree_max_latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
1050

1051 1052 1053 1054 1055
   MPI_Reduce( &fattree_packet_gen, &total_gen, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce( &fattree_packet_fin, &total_fin, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);

   /* print statistics */
   if(!g_tw_mynode)
1056 1057
   {
      printf(" Average number of hops traversed %f average chunk latency %lf us maximum chunk latency %lf us avg message size %lf bytes finished messages %lld finished chunks %lld \n",
1058 1059 1060
              (float)avg_hops/total_finished_chunks, avg_time/(total_finished_chunks*1000), max_time/1000, (float)final_msg_sz/total_finished_msgs, total_finished_msgs, total_finished_chunks);
      printf(" Total packets generated %ld finished %ld \n", total_gen, total_fin);
   }
1061 1062

   if(!g_tw_mynode)
1063
   {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
      printf(" Average number of hops traversed %f average chunk latency %lf us maximum chunk latency %lf us avg message size %lf bytes finished messages %lld \n", (float)avg_hops/total_finished_chunks, avg_time/(total_finished_chunks*1000), max_time/1000, (float)final_msg_sz/total_finished_msgs, total_finished_msgs);

#if PARAMS_LOG
//    throughput_avg = throughput_avg / (float)slim_total_terminals_noah;
//    throughput_avg2 = throughput_avg2 / (float)slim_total_terminals_noah;

	//Open file to append simulation results
	char temp_filename[1024];
	char temp_filename_header[1024];
	sprintf(temp_filename,"%s/sim_log.txt",modelnet_stats_dir);
	sprintf(temp_filename_header,"%s/sim_log_header.txt",modelnet_stats_dir);
	FILE *fattree_results_log=fopen(temp_filename, "a");
	FILE *fattree_results_log_header=fopen(temp_filename_header, "a");
	if(fattree_results_log == NULL)
		printf("\n Failed to open results log file %s \n",temp_filename);
	if(fattree_results_log_header == NULL)
		printf("\n Failed to open results log header file %s \n",temp_filename_header);
	printf("Printing Simulation Parameters/Results Log File\n");
	fprintf(fattree_results_log_header,"<Avg Hops/Total Packets>, <Avg Time/Total Packets>, <Max Latency>, <Total Finished Chunks>");
1083
	fprintf(fattree_results_log,"%24.3lf, %24.3lf, %13.3lf, %23.3lld, ", (float)avg_hops/total_finished_packets, avg_time/(total_finished_packets),max_time,total_finished_chunks);
1084 1085
	fclose(fattree_results_log_header);
	fclose(fattree_results_log);
1086
#endif
1087
  }
1088
}
1089 1090

/* fattree packet event */
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
static tw_stime fattree_packet_event(
        model_net_request const * req,
        uint64_t message_offset,
        uint64_t packet_size,
        tw_stime offset,
        mn_sched_params const * sched_params,
        void const * remote_event,
        void const * self_event,
        tw_lp *sender,
        int is_last_pckt)
Nikhil's avatar
Nikhil committed
1101
{
1102 1103 1104
#if DEBUG_RC
  packet_event_f++;
#endif
1105

Noah Wolfe's avatar
Noah Wolfe committed
1106 1107
  (void)message_offset;
  (void)sched_params;
1108 1109 1110 1111 1112 1113 1114 1115
  tw_event * e_new;
  tw_stime xfer_to_nic_time;
  fattree_message * msg;
  char* tmp_ptr;

  xfer_to_nic_time = codes_local_latency(sender);
  e_new = model_net_method_event_new(sender->gid, xfer_to_nic_time + offset,
      sender, FATTREE, (void**)&msg, (void**)&tmp_ptr);
1116 1117 1118 1119 1120
  strcpy(msg->category, req->category);
  msg->final_dest_gid = req->final_dest_lp;
  msg->total_size = req->msg_size;
  msg->sender_lp = req->src_lp;
  msg->sender_mn_lp = sender->gid;
1121
  msg->packet_size = packet_size;
1122
  msg->travel_start_time = tw_now(sender);
1123 1124 1125
  msg->remote_event_size_bytes = 0;
  msg->local_event_size_bytes = 0;
  msg->type = T_GENERATE;
1126 1127 1128 1129
  msg->dest_terminal_id = req->dest_mn_lp;
  msg->message_id = req->msg_id;
  msg->is_pull = req->is_pull;
  msg->pull_size = req->pull_size;
1130
  msg->magic = fattree_terminal_magic_num;
1131
  msg->msg_start_time = req->msg_start_time;
1132 1133

  /* Its the last packet so pass in remote and local event information*/
1134
  if(is_last_pckt)
1135
  {
1136
    if(req->remote_event_size > 0)
1137
    {
1138 1139 1140
      msg->remote_event_size_bytes = req->remote_event_size;
      memcpy(tmp_ptr, remote_event, req->remote_event_size);
      tmp_ptr += req->remote_event_size;
1141
    }
1142
    if(req->self_event_size > 0)
1143
    {
1144 1145 1146
      msg->local_event_size_bytes = req->self_event_size;
      memcpy(tmp_ptr, self_event, req->self_event_size);
      tmp_ptr += req->self_event_size;
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
    }
  }
  //printf("[%d] Send to %d\n", sender->gid, sender->gid);
  tw_event_send(e_new);
  return xfer_to_nic_time;
}

/* fattree packet event reverse handler */
static void fattree_packet_event_rc(tw_lp *sender)
{
1157 1158 1159
#if DEBUG_RC
  packet_event_r++;
#endif
1160 1161 1162 1163
  codes_local_latency_reverse(sender);
  return;
}

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
void ft_packet_generate_rc(ft_terminal_state * s, tw_bf * bf, fattree_message * msg, tw_lp * lp)
{
#if DEBUG_RC
    t_generate_r++;
#endif
    fattree_packet_gen--;
    s->packet_gen--;

    tw_rand_reverse_unif(lp->rng);

    int num_chunks = msg->packet_size/s->params->chunk_size;
    if(msg->packet_size % s->params->chunk_size)
	num_chunks++;

    if(!num_chunks)
	num_chunks = 1;

    int i;
    for(i = 0; i < num_chunks; i++) {
1183
	delete_fattree_message_list(return_tail(s->terminal_msgs,
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	    s->terminal_msgs_tail, 0));
	s->terminal_length -= s->params->chunk_size;
    }
    if(bf->c11) {
	s->issueIdle = 0;
	s->last_buf_full = msg->saved_busy_time;
    }
    if(bf->c5) {
	codes_local_latency_reverse(lp);
	s->in_send_loop = 0;
    }
1195

1196 1197 1198 1199 1200 1201 1202
    struct mn_stats* stat;
    stat = model_net_find_stats(msg->category, s->fattree_stats_array);
    stat->send_count--;
    stat->send_bytes -= msg->packet_size;
    stat->send_time -= (1/s->params->cn_bandwidth) * msg->packet_size;
}

1203 1204 1205
/* generates packet at the current fattree compute node */
void ft_packet_generate(ft_terminal_state * s, tw_bf * bf, fattree_message * msg,
    tw_lp * lp) {
1206 1207 1208
  bf->c11 = 0;
  bf->c5 = 0;

1209 1210
  fattree_packet_gen++;
  s->packet_gen++;
1211

1212 1213 1214
  fattree_param *p = s->params;
  tw_stime ts, nic_ts;

1215 1216 1217 1218 1219 1220
  int total_event_size;
  uint64_t num_chunks = msg->packet_size / p->chunk_size;
  if(msg->packet_size % s->params->chunk_size)
    num_chunks++;
  if(!num_chunks)
    num_chunks = 1;
1221

Nikhil's avatar
Nikhil committed
1222
  nic_ts = g_tw_lookahead + (msg->packet_size * s->params->cn_delay) + tw_rand_unif(lp->rng);
1223

1224 1225
  msg->my_N_hop = 0;

1226
  msg->packet_ID = lp->gid + g_tw_nlp * s->packet_counter;
1227 1228
  if(msg->packet_ID == LLU(TRACK_PKT))
    printf("\n Packet %llu generated at terminal %d terminal_gid %llu dest_terminal_id %llu final_dest_gid %llu size %llu num chunks %llu \n",
Noah Wolfe's avatar
Noah Wolfe committed
1229
       msg->packet_ID, s->terminal_id, LLU(lp->gid), LLU(msg->dest_terminal_id), LLU(msg->final_dest_gid),
1230 1231 1232 1233
       LLU(msg->packet_size), LLU(num_chunks));

  for(uint64_t i = 0; i < num_chunks; i++)
  {
1234
    fattree_message_list * cur_chunk = (fattree_message_list *)malloc(
1235
      sizeof(fattree_message_list));
1236 1237
    msg->origin_switch_id = s->switch_id;
    init_fattree_message_list(cur_chunk, msg);
1238

1239 1240
    if(msg->remote_event_size_bytes + msg->local_event_size_bytes > 0) {
      cur_chunk->event_data = (char*)malloc(
1241
        msg->remote_event_size_bytes + msg->local_event_size_bytes);
1242
    }
1243

1244 1245 1246 1247 1248 1249
    void *m_data_src = model_net_method_get_edata(FATTREE, msg);
    if(msg->remote_event_size_bytes){
      memcpy(cur_chunk->event_data, m_data_src, msg->remote_event_size_bytes);
    }
    if(msg->local_event_size_bytes){
      m_data_src = (char*)m_data_src + msg->remote_event_size_bytes;
1250
      memcpy((char*)cur_chunk->event_data + msg->remote_event_size_bytes,
1251
        m_data_src, msg->local_event_size_bytes);
1252
    }
1253

1254 1255
    cur_chunk->msg.chunk_id = i;
    cur_chunk->msg.origin_switch_id = s->switch_id;
1256
    append_to_fattree_message_list(s->terminal_msgs, s->terminal_msgs_tail,
1257 1258 1259
      0, cur_chunk);
    s->terminal_length += s->params->chunk_size;
  }
1260 1261 1262 1263 1264 1265

  if(s->terminal_length < 2 * s->params->cn_vc_size) {
    model_net_method_idle_event(nic_ts, 0, lp);
  } else {
    bf->c11 = 1;
    s->issueIdle = 1;
1266 1267
    msg->saved_busy_time = s->last_buf_full;
    s->last_buf_full = tw_now(lp);
1268 1269 1270 1271 1272
  }

  if(s->in_send_loop == 0) {
    fattree_message *m;
    bf->c5 = 1;
1273
    ts = codes_local_latency(lp);
1274
    tw_event* e = model_net_method_event_new(lp->gid, ts, lp, FATTREE,
1275 1276
      (void**)&m, NULL);
    m->type = T_SEND;
1277
    m->magic = fattree_terminal_magic_num;
1278 1279 1280 1281
    s->in_send_loop = 1;
    tw_event_send(e);
  }

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
  total_event_size = model_net_get_msg_sz(FATTREE) +
    msg->remote_event_size_bytes+ msg->local_event_size_bytes;
  mn_stats* stat;
  stat = model_net_find_stats(msg->category, s->fattree_stats_array);
  stat->send_count++;
  stat->send_bytes += msg->packet_size;
  stat->send_time += (1/p->cn_bandwidth) * msg->packet_size;
  if(stat->max_event_size < total_event_size)
    stat->max_event_size = total_event_size;

1292 1293 1294
  return;
}

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
void ft_packet_send_rc(ft_terminal_state * s, tw_bf *bf, fattree_message * msg, tw_lp * lp)
{
#if DEBUG_RC
    t_send_r++;
#endif
    if(bf->c1) {
      s->in_send_loop = 1;
      s->last_buf_full = msg->saved_busy_time;
      return;
    }

    tw_rand_reverse_unif(lp->rng);
    s->terminal_available_time = msg->saved_available_time;
    if(bf->c2) {
      codes_local_latency_reverse(lp);
    }
    s->packet_counter--;
1312
    s->vc_occupancy -= s->params->chunk_size;
1313 1314

    fattree_message_list* cur_entry = rc_stack_pop(s->st);
1315 1316

    prepend_to_fattree_message_list(s->terminal_msgs,
Nikhil's avatar
Nikhil committed
1317
        s->terminal_msgs_tail, 0, cur_entry);
1318
    s->terminal_length += s->params->chunk_size;
Nikhil's avatar
Nikhil committed
1319
#if DEBUG_RC
1320
    if(s->terminal_id == 0)
1321
       printf("time:%lf terminal_length:%d \n",tw_now(lp),s->terminal_length);
Nikhil's avatar
Nikhil committed
1322
#endif
1323 1324 1325 1326 1327 1328 1329

    if(bf->c3) {
      tw_rand_reverse_unif(lp->rng);
    }
    if(bf->c4) {
      s->in_send_loop = 1;
    }
Nikhil's avatar
Nikhil committed
1330
    if(bf->c5) {
1331 1332
      codes_local_latency_reverse(lp);
      s->issueIdle = 1;
Nikhil's avatar
Nikhil committed
1333
    }
1334
}
1335 1336 1337 1338
/* sends the packet from the compute node to the attached switch */
void ft_packet_send(ft_terminal_state * s, tw_bf * bf, fattree_message * msg,
    tw_lp * lp) {

1339 1340 1341 1342 1343 1344
  bf->c1 = 0;
  bf->c2 = 0;
  bf->c3 = 0;
  bf->c4 = 0;
  bf->c5 = 0;

1345 1346 1347
  tw_stime ts;
  tw_event *e;
  fattree_message *m;
1348

1349 1350
  fattree_message_list* cur_entry = s->terminal_msgs[0];

1351
  if(s->vc_occupancy + s->params->chunk_size > s->params->cn_vc_size ||
1352 1353 1354
    cur_entry == NULL) {
    bf->c1 = 1;
    s->in_send_loop = 0;
1355

1356 1357
    msg->saved_busy_time = s->last_buf_full;
    s->last_buf_full = tw_now(lp);
1358 1359
    return;
  }
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
  
  uint64_t num_chunks = cur_entry->msg.packet_size/s->params->chunk_size;
  if(cur_entry->msg.packet_size % s->params->chunk_size)
    num_chunks++;

  if(!num_chunks)
      num_chunks = 1;

  ts = g_tw_lookahead + + g_tw_lookahead * tw_rand_unif(lp->rng);
  if((cur_entry->msg.packet_size % s->params->chunk_size) && (cur_entry->msg.chunk_id == num_chunks - 1)) {
    ts += s->params->cn_delay * (cur_entry->msg.packet_size % s->params->chunk_size);
  } else {
    bf->c12 = 1;
    ts += s->params->cn_delay * s->params->chunk_size;
  }
1375 1376 1377 1378 1379 1380 1381

  msg->saved_available_time = s->terminal_available_time;
  s->terminal_available_time = maxd(s->terminal_available_time, tw_now(lp));
  s->terminal_available_time += ts;

  // we are sending an event to the switch, so no method_event here
  ts = s->terminal_available_time - tw_now(lp);
1382

1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
  e = tw_event_new(s->switch_lp, ts, lp);
  m = tw_event_data(e);
  memcpy(m, &cur_entry->msg, sizeof(fattree_message));
  if (m->remote_event_size_bytes){
    memcpy(model_net_method_get_edata(FATTREE, m), cur_entry->event_data,
        m->remote_event_size_bytes);
  }

  m->type = S_ARRIVE;
  m->src_terminal_id = lp->gid;
  m->intm_id = s->terminal_id;
  m->vc_index = 0;
  m->vc_off = 0; //we only have one connection to the terminal NIC
  m->local_event_size_bytes = 0;
  m->last_hop = TERMINAL;
1398
  m->magic = switch_magic_num;
1399 1400
  //printf("[%d] pack send Send to %d\n", lp->gid, s->switch_lp);
  tw_event_send(e);
1401

1402
  /* local completion message */
1403
  if(cur_entry->msg.chunk_id == num_chunks - 1 && (cur_entry->msg.local_event_size_bytes > 0))
1404 1405
  {
    bf->c2 = 1;
1406
    double tsT = codes_local_latency(lp);
1407 1408
    tw_event *e_new = tw_event_new(cur_entry->msg.sender_lp, tsT, lp);
    fattree_message *m_new = tw_event_data(e_new);
1409
    void *local_event = (char*)cur_entry->event_data +
1410 1411 1412 1413
                cur_entry->msg.remote_event_size_bytes;
    memcpy(m_new, local_event, cur_entry->msg.local_event_size_bytes);
    tw_event_send(e_new);
  }
1414

1415
  s->packet_counter++;
1416
  s->vc_occupancy += s->params->chunk_size;
1417
  cur_entry = return_head(s->terminal_msgs, s->terminal_msgs_tail, 0);
1418
  rc_stack_push(lp, cur_entry, free, s->st);
Noah Wolfe's avatar
Noah Wolfe committed
1419
  s->terminal_length -= s->params->chunk_size;
1420 1421

//  if(s->terminal_id == 1)
Noah Wolfe's avatar
Noah Wolfe committed
1422
//    printf("send time:%5.6lf lp_id:%3llu terminal_length:%5d \n",tw_now(lp),LLU(lp->gid),s->terminal_length);
1423 1424 1425 1426

  cur_entry = s->terminal_msgs[0];

  if(cur_entry != NULL &&
1427
    s->vc_occupancy + s->params->chunk_size <= s->params->cn_vc_size) {
1428
    bf->c3 = 1;
Noah Wolfe's avatar
Noah Wolfe committed
1429
    fattree_message *m_new;
1430
    ts = ts + g_tw_lookahead * tw_rand_unif(lp->rng);
1431
    e = model_net_method_event_new(lp->gid, ts, lp, FATTREE,
Noah Wolfe's avatar
Noah Wolfe committed
1432 1433 1434
      (void**)&m_new, NULL);
    m_new->type = T_SEND;
    m_new->magic = fattree_terminal_magic_num;
1435 1436 1437 1438 1439
    tw_event_send(e);
  } else {
    bf->c4 = 1;
    s->in_send_loop = 0;
  }
1440

Nikhil's avatar
Nikhil committed
1441
  if(s->issueIdle) {
1442 1443 1444 1445
    bf->c5 = 1;
    s->issueIdle = 0;
    model_net_method_idle_event(codes_local_latency(lp), 0, lp);
  }
Nikhil's avatar
Nikhil committed
1446

1447 1448 1449
  return;
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479
void switch_packet_receive_rc(switch_state * s,
        tw_bf * bf,
        fattree_message * msg,
        tw_lp * lp)
{
#if DEBUG_RC
	s_arrive_r++;
#endif
    int output_port = msg->saved_vc;
    if(bf->c1)
    {
        tw_rand_reverse_unif(lp->rng);
        delete_fattree_message_list(return_tail(s->pending_msgs,
        s->pending_msgs_tail, output_port));
        s->vc_occupancy[output_port] -= s->params->chunk_size;
        if(bf->c2)
        {
            codes_local_latency_reverse(lp);
            s->in_send_loop[output_port] = 0;
        }
    }
    if(bf->c3) 
    {
        delete_fattree_message_list(return_tail(s->queued_msgs,
        s->queued_msgs_tail, output_port));
        s->queued_length[output_port] -= s->params->chunk_size;
        s->last_buf_full[output_port] = msg->saved_busy_time;
    }
}

1480
/* Packet arrives at the switch and a credit is sent back to the sending
1481
 * terminal/switch */
1482
void switch_packet_receive( switch_state * s, tw_bf * bf,
1483 1484
    fattree_message * msg, tw_lp * lp ) {

1485 1486 1487 1488
  bf->c1 = 0;
  bf->c2 = 0;
  bf->c3 = 0;

1489 1490 1491 1492 1493