fattree.c 97.2 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"
Nikhil's avatar
Nikhil committed
13 14
#include <ctype.h>
#include <search.h>
15

16 17 18 19 20
#ifdef ENABLE_CORTEX
#include <cortex/cortex.h>
#include <cortex/topology.h>
#endif

21 22 23
#define CREDIT_SIZE 8
#define MEAN_PROCESS 1.0

Nikhil's avatar
Nikhil committed
24
#define TERMINAL_GUID_PREFIX ((uint64_t)(64) << 32)
25
#define FTREE_HASH_TABLE_SIZE 4999
26

27
// debugging parameters
28 29
#define TRACK_PKT -1
//#define TRACK_PKT 2820
30
#define FATTREE_HELLO 0
31
#define FATTREE_DEBUG 0
32 33
#define FATTREE_CONNECTIONS 0
#define FATTREE_MSG 0
Nikhil's avatar
Nikhil committed
34
#define DEBUG_RC 0
35

36
//Data Collection Output Files
37
#define PARAMS_LOG 0
38

39 40 41
#define LP_CONFIG_NM (model_net_lp_config_names[FATTREE])
#define LP_METHOD_NM (model_net_method_names[FATTREE])

42 43 44 45 46
#ifdef ENABLE_CORTEX
/* This structure is defined at the end of the file */
extern cortex_topology fattree_cortex_topology;
#endif

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#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

67
long fattree_packet_gen = 0, fattree_packet_fin = 0;
Nikhil's avatar
Nikhil committed
68
int dump_topo = 0;
69

70 71 72
static double maxd(double a, double b) { return a < b ? b : a; }

// arrival rate
Noah Wolfe's avatar
Noah Wolfe committed
73
//static double MEAN_INTERVAL=200.0;
74 75

typedef struct fattree_param fattree_param;
76
/* annotation-specific parameters (unannotated entry occurs at the
77 78 79
 * last index) */
static uint64_t                  num_params = 0;
static fattree_param           * all_params = NULL;
Noah Wolfe's avatar
Noah Wolfe committed
80
static const config_anno_map_t * anno_map   = NULL;
81 82 83 84 85 86 87

/* 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;

Nikhil's avatar
Nikhil committed
88 89
enum ROUTING_ALGO
{
Nikhil's avatar
Nikhil committed
90
    STATIC=1,
Nikhil's avatar
Nikhil committed
91 92
    ADAPTIVE,
};
Nikhil's avatar
Nikhil committed
93 94 95 96 97 98

enum RAIL_SELECTION_ALGO
{
    RAIL_DEDICATED=1,
    RAIL_ADAPTIVE,
};
Nikhil's avatar
Nikhil committed
99 100 101
static char routing_folder[MAX_NAME_LENGTH];
static char dot_file_p[MAX_NAME_LENGTH];

102 103 104
/* switch magic number */
int switch_magic_num = 0;

105
/* terminal magic number */
106
int fattree_terminal_magic_num = 0;
107

Nikhil's avatar
Nikhil committed
108 109 110
/* global variables for DOT file generation */
static FILE *dot_file = NULL;

111 112 113 114 115 116 117 118
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;
};

119
void init_fattree_message_list(fattree_message_list *this,
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  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;
137 138 139
  int link_repetitions;
  int Ns; // number of switches per pod
  int Np; //Number of pods
140 141 142
  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 */
143
  double cn_bandwidth;/* bandwidth of the compute node channels
144 145 146
                        connected to switch */
  int vc_size; /* buffer size of the link channels */
  int cn_vc_size; /* buffer size of the compute node channels */
147
  int chunk_size; /* full-sized packets are broken into smaller chunks.*/
148
  int packet_size;
149
  int num_terminals;
Nikhil's avatar
Nikhil committed
150 151 152 153
  int l1_set_size, l0_set_size;
  int l1_term_size, l0_term_size;
  int cn_per_switch;
  double tapering_ratio;
154 155 156 157 158
  double cn_delay;
  double head_delay;
  double credit_delay;
  double router_delay;
  double soft_delay;
Nikhil's avatar
Nikhil committed
159
  int routing;
Nikhil's avatar
Nikhil committed
160 161 162 163
  int rail_select;
  int rail_size_limit;
  int num_rails;
  int ports_per_nic;
164 165
};

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
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;
};

181 182 183 184 185 186 187 188 189
/* 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;
190
  int packet_gen;
191
  int packet_fin;
192 193 194
  // Fattree specific parameters
  unsigned int terminal_id;
  unsigned int switch_id;
Nikhil's avatar
Nikhil committed
195 196
  unsigned int rail_id;
  tw_lpid *switch_lp;
197 198

  // Each terminal will have an input and output channel with the switch
Nikhil's avatar
Nikhil committed
199 200
  int *vc_occupancy; // NUM_VC
  tw_stime* terminal_available_time;
201

202 203
  struct mn_stats fattree_stats_array[CATEGORY_MAX];

204 205
  fattree_message_list **terminal_msgs;
  fattree_message_list **terminal_msgs_tail;
Nikhil's avatar
Nikhil committed
206 207 208
  int *terminal_length;
  int *in_send_loop;
  int *issueIdle;
209 210

   struct rc_stack * st;
211 212 213

  char * anno;
  fattree_param *params;
214 215 216 217 218 219 220 221 222 223 224

  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;

Nikhil's avatar
Nikhil committed
225 226
  tw_stime *last_buf_full;
  tw_stime *busy_time;
227
  char output_buf[4096];
228
  char output_buf3[4096];
229

230 231 232 233 234
  /* For sampling */
  long fin_chunks_sample;
  long data_size_sample;
  double fin_hops_sample;
  tw_stime fin_chunks_time;
Nikhil's avatar
Nikhil committed
235 236
  tw_stime busy_time_sample;

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
};

/* 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;
Nikhil's avatar
Nikhil committed
261
  unsigned int rail_id;
262 263 264 265 266 267 268 269 270 271 272
  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;
273 274 275 276 277
  tw_stime* last_buf_full;

  tw_stime* busy_time;
  tw_stime* busy_time_sample;

278 279 280 281 282 283 284 285 286 287
  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;

288 289 290
  char output_buf[4096];
  char output_buf2[4096];

Noah Wolfe's avatar
Noah Wolfe committed
291 292
  struct rc_stack * st;

293 294
  char * anno;
  fattree_param *params;
Nikhil's avatar
Nikhil committed
295 296
  /* array to store linear forwaring tables in case we use static routing */
  int *lft;
297 298
};

299 300
static tw_stime         fattree_total_time = 0;
static tw_stime         fattree_max_latency = 0;
Noah Wolfe's avatar
Noah Wolfe committed
301
//static tw_stime         max_collective = 0;
302 303 304 305 306 307 308 309


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;

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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));
}

334 335
static void free_tmp(void * ptr)
{
336
    struct ftree_qhash_entry * ftree = ptr;
337 338 339 340
    free(ftree->remote_event_data);
    free(ftree);
}

341
static void append_to_fattree_message_list(
342 343
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
344
        int index,
345 346 347 348 349 350
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thisq[index] = msg;
    } else {
        thistail[index]->next = msg;
        msg->prev = thistail[index];
351
    }
352 353 354
    thistail[index] = msg;
}

355
static void prepend_to_fattree_message_list(
356 357
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
358
        int index,
359 360 361 362 363 364
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thistail[index] = msg;
    } else {
        thisq[index]->prev = msg;
        msg->next = thisq[index];
365
    }
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    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
411
//static const tw_lptype* fattree_get_switch_lp_type(void);
412

413
/* returns the fattree message size */
414 415 416 417 418
static int fattree_get_msg_sz(void)
{
  return sizeof(fattree_message);
}

Nikhil's avatar
Nikhil committed
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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
static inline uint64_t get_term_guid(ft_terminal_state *t)
{
  return TERMINAL_GUID_PREFIX + t->terminal_id;
}

static inline uint64_t get_switch_guid(switch_state *s)
{
  return (((uint64_t)(s->switch_level + 1)) << 32) + s->switch_id;
}

typedef struct {
  uint64_t guid;
  uint64_t port;
} guid_port_combi_t;

static int cmp_guids(const void *g1, const void *g2)
{
  uint64_t guid1 = *((uint64_t *) g1);
  uint64_t guid2 = ((guid_port_combi_t *) g2)->guid;
  if (guid1 == guid2)
    return 0;
  else
    return 1;
}

/* parse external file with give forwarding tables
 * file name: 0x<switch guid>.lft
 * content: '0x<terminal guid> <egress port>' per line, e.g.:
 * `head 0x0000000100000000.lf`
 *    0x0000000100000000 0
 *    0x00000040000000ff 22
 *    0x0000000100000001 19
 */
static int read_static_lft(switch_state *s, tw_lp *lp)
{
  if (!s || !lp)
    return -1;

  char dir_name[512];
  char file_name[512];

  char *io_dir = routing_folder;
  sprintf(dir_name, "%s", io_dir);

  sprintf(file_name, "%s/0x%016"PRIx64".lft", dir_name, get_switch_guid(s));
  FILE *file = NULL;
  if (!(file = fopen(file_name, "r")))
    return -1;

  char line[UINT8_MAX];
  char *p = NULL, *e = NULL;
  uint64_t dest_guid = 0, port = 0;

  int num_added = 0;
  guid_port_combi_t *tmpLFT = calloc(s->params->num_terminals,
        sizeof(guid_port_combi_t));
  s->lft = malloc(s->params->num_terminals * sizeof(int));
  /* init all with -1 so that we find missing routing entries */
  for (int i = 0; i < s->params->num_terminals; i++) s->lft[i] = -1;

  while (fgets(line, sizeof(line), file)) {
    p = line;
    while (isspace(*p))
      p++;

    if (*p == '\0' || *p == '\n' || *p == '#')
      continue;

    dest_guid = strtoull(p, &e, 16);
    if (e == p || (!isspace(*e) && *e != '#' && *e != '\0')) {
      errno = EINVAL;
      return -1;
    }

    p = e;
    while (isspace(*p))
      p++;

    port = strtoull(p, &e, 0);
    if (e == p || (!isspace(*e) && *e != '#' && *e != '\0')) {
      errno = EINVAL;
      return -1;
    }

    if (dest_guid < TERMINAL_GUID_PREFIX){
      // we don't need switches
    } else {
      tmpLFT[num_added].guid = dest_guid;
      tmpLFT[num_added].port = port;
      num_added++;
    }
  }

  /* we want a real LFT with terminal_id as lookup index, so have to
   * convert the read lft, which might contain entries for switches
   * or is permutated, into the correct format
   */
  ft_terminal_state tmpTerm;
  for (int dest_num = 0; dest_num < s->params->num_terminals; dest_num++) {
    tmpTerm.terminal_id = dest_num;
    uint64_t key = get_term_guid(&tmpTerm);
    size_t size = s->params->num_terminals;
    guid_port_combi_t *elem = lfind(&key, tmpLFT, &size,
          sizeof(guid_port_combi_t), cmp_guids);
    assert(elem);
    // opensm uses ports=1...n, so convert back here
    //if(NULL != getenv("OSM_ROUTING"))
       s->lft[dest_num] = elem->port - 1;
    //else
    //   s->lft[dest_num] = elem->port;
  }

#if FATTREE_DEBUG
  printf("I am switch %d (guid=%016"PRIx64") and my LFT is:\n",
        s->switch_id, get_switch_guid(s));
  for (int dest_num = 0; dest_num < s->params->num_terminals; dest_num++)
     printf("\tdest %d -> egress port %d\n", dest_num, s->lft[dest_num]);
#endif

  free(tmpLFT);
  fclose(file);
  return 0;
}

static void dot_write_open_file(FILE **fout)
{
  if(!fout || *fout) return;
  if(!dump_topo) return;

  char dir_name[512];
  char file_name[512];

  char *io_dir = routing_folder;
  sprintf(dir_name, "%s", io_dir);

  char *dot_file_prefix = dot_file_p;
  int rank;
556
  MPI_Comm_rank(MPI_COMM_CODES, &rank);
Nikhil's avatar
Nikhil committed
557 558 559 560 561 562 563
  sprintf(file_name, "%s/%s.dot.%d", dir_name, dot_file_prefix, rank);
  *fout = fopen(file_name, "w+");
  if(*fout == NULL) {
    tw_error(TW_LOC, "Error opening dot file for writing");
  }
}

564
/*static void dot_write_close_file(FILE **fout)
Nikhil's avatar
Nikhil committed
565 566 567 568 569
{
  if(!fout || !(*fout)) return;
  if(!dump_topo) return;
  fclose(*fout);
  *fout = NULL;
570
}*/
Nikhil's avatar
Nikhil committed
571 572

/* sw IDs aren't unique, but level+ID info is */
573
static void dot_write_switch_info(switch_state *s, FILE *fout)
Nikhil's avatar
Nikhil committed
574 575 576
{
  if(!s || s->unused || !(s->params) || !fout) return;
  if(!dump_topo) return;
Nikhil's avatar
Nikhil committed
577
  if(s->rail_id) return;
Nikhil's avatar
Nikhil committed
578 579 580 581 582 583

  const char *root_attr = ",root_switch", *empty_attr = "";
  uint64_t switch_guid = get_switch_guid(s);
  fattree_param *p = s->params;

  fprintf(fout, "\t\"S_%d_%d\" [comment=\"0x%016"PRIx64",radix=%d%s\"];\n",
Nikhil's avatar
Nikhil committed
584
      s->switch_level, s->switch_id, switch_guid, s->radix,
Nikhil's avatar
Nikhil committed
585 586 587 588 589 590 591 592
      (p->num_levels == s->switch_level + 1) ? root_attr : empty_attr);
}

/* terminal IDs are unique in this model */
static void dot_write_term_info(ft_terminal_state *t, FILE *fout)
{
  uint64_t term_guid = t->terminal_id;
  if(!dump_topo) return;
Nikhil's avatar
Nikhil committed
593
  if(t->rail_id) return;
Nikhil's avatar
Nikhil committed
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

  if(!t || !fout) return;
  /* need to shift the term guid, because opensm doesn't like guid=0...0 */
  //if(NULL != getenv("OSM_ROUTING"))
     term_guid = get_term_guid(t);
  fprintf(fout, "\t\"H_%d\" [comment=\"0x%016"PRIx64"\"];\n",
        t->terminal_id, term_guid);
}

static void dot_write_sw2sw_link(int lsw_lvl, int lsw_id, int lsw_port,
                                 int rsw_lvl, int rsw_id, int rsw_port,
                                 FILE *fout)
{
  int tmp_lsw_port = 0, tmp_rsw_port = 0;

  if(!fout) return;
  if(!dump_topo) return;

  /* add +1 to ports, since ibsim/opensm start with physical port number 1
   * (port 0 is internal sw port)
   */
  //if(NULL != getenv("OSM_ROUTING")) {
     tmp_lsw_port = lsw_port + 1;
     tmp_rsw_port = rsw_port + 1;
  //} else {
  //   tmp_lsw_port = lsw_port;
  //   tmp_rsw_port = rsw_port;
  //}

  /* if original right hand side is unknown we write '?' instead
   * and must fix it later ... nice topology definition ... :-(
   */
  if(-1 == rsw_port)
     fprintf(fout, "\t\"S_%d_%d\" -> \"S_%d_%d\" [comment=\"P%d->P?\"];\n",
           lsw_lvl, lsw_id, rsw_lvl, rsw_id, tmp_lsw_port);
  else
     fprintf(fout, "\t\"S_%d_%d\" -> \"S_%d_%d\" [comment=\"P%d->P%d\"];\n",
           lsw_lvl, lsw_id, rsw_lvl, rsw_id, tmp_lsw_port, tmp_rsw_port);
}

static void dot_write_sw2term_link(int lsw_lvl, int lsw_id, int lsw_port,
                                   int term_id, int dump_reverse, FILE *fout)
{
  if(!fout) return;
  if(!dump_topo) return;

  /* add +1 to ports, since ibsim/opensm start with physical port number 1
   * (port 0 is internal sw port)
   */
  //if(NULL != getenv("OSM_ROUTING"))
     lsw_port++;

  fprintf(fout, "\t\"S_%d_%d\" -> \"H_%d\" [comment=\"P%d->P1\"];\n",
        lsw_lvl, lsw_id, term_id, lsw_port);
  if(dump_reverse)
     fprintf(fout, "\t\"H_%d\" -> \"S_%d_%d\" [comment=\"P1->P%d\"];\n",
           term_id, lsw_lvl, lsw_id, lsw_port);
}

653
/*static void dot_write_term2sw_link(int term_id, int rsw_lvl, int rsw_id,
Nikhil's avatar
Nikhil committed
654 655 656 657 658
                                   int rsw_port, FILE *fout)
{
  if(!fout) return;
  if(!dump_topo) return;

659
  *//* add +1 to ports, since ibsim/opensm start with physical port number 1
Nikhil's avatar
Nikhil committed
660 661 662
   * (port 0 is internal sw port)
   */
  //if(NULL != getenv("OSM_ROUTING"))
663
  //   rsw_port++;
Nikhil's avatar
Nikhil committed
664

665 666 667
  //fprintf(fout, "\t\"H_%d\" -> \"S_%d_%d\" [comment=\"P1->P%d\"];\n",
  //      term_id, rsw_lvl, rsw_id, rsw_port);
//}
Nikhil's avatar
Nikhil committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681

void post_switch_init(switch_state *s, tw_lp *lp)
{
  /* read any LFTs which might have been generated by an external routing
   * algorithm, e.g., through the use of opensm\
   */
  if(s->params->routing == STATIC && !dump_topo) {
   if(0 != read_static_lft(s, lp)) {
     tw_error(TW_LOC, "Error while reading the routing table");
   }
  }
}


Noah Wolfe's avatar
Noah Wolfe committed
682
static void fattree_read_config(const char * anno, fattree_param *p){
683 684 685 686 687 688 689
  uint32_t h1 = 0, h2 = 0;
  bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
  switch_magic_num = h1 + h2;
    
  bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
  fattree_terminal_magic_num = h1 + h2;
  
690 691
  int i;

Nikhil's avatar
Nikhil committed
692
  p->ft_type = 1;
693
  configuration_get_value_int(&config, "PARAMS", "ft_type", anno,
694
      &p->ft_type);
Nikhil's avatar
Nikhil committed
695
  if(!g_tw_mynode) printf("FT type is %d\n", p->ft_type);
696

697
  configuration_get_value_int(&config, "PARAMS", "num_levels", anno,
698 699 700 701 702 703 704
      &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");
  }
Nikhil's avatar
Nikhil committed
705 706 707 708 709 710 711 712
  if(!g_tw_mynode) printf("FT num levels is %d\n", p->num_levels);
  
  p->tapering_ratio = 1;
  configuration_get_value_double(&config, "PARAMS", "tapering", anno,
      &p->tapering_ratio);
  if(!g_tw_mynode) printf("FT tapering is %lf\n", p->tapering_ratio);

  p->tapering_ratio += 1;
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

  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,",");
  }

Nikhil's avatar
Nikhil committed
758 759 760 761 762
  if(fmod(p->switch_radix[0],  p->tapering_ratio)) {
    tw_error(TW_LOC, "Tapering ratio does not fit the given radix; tapering + 1 should"
    " evenly divide the L0 switch radix\n");
  }

763
  p->Ns = p->switch_radix[0]/2;
Nikhil's avatar
Nikhil committed
764 765 766 767 768 769 770 771 772 773 774
  if(p->num_switches[0] % p->Ns) {
    if(!g_tw_mynode) printf("Number of switches has to be a multiple of switch radix/2 (%d);" 
        "next largest value is %d\n", p->Ns, (p->num_switches[0]/p->Ns + 1)*p->Ns);
    tw_error(TW_LOC, "Incorrect number of switches");
  }

  int num_terminals = p->num_switches[0] * (p->tapering_ratio - 1)*(p->switch_radix[0]/p->tapering_ratio);
  p->num_terminals = num_terminals;
    
  p->l0_set_size = p->switch_radix[0]/2;
  p->l0_term_size = (p->tapering_ratio - 1)*(p->switch_radix[0]/p->tapering_ratio);
775

776
  if(p->num_levels == 2) {
Nikhil's avatar
Nikhil committed
777
    p->num_switches[1] = p->num_switches[0]/p->tapering_ratio/2;
778
    p->switch_radix[1] = p->switch_radix[0];
Nikhil's avatar
Nikhil committed
779 780
    p->l1_set_size =  p->num_switches[1];
    p->l1_term_size = num_terminals;
781
  } else {
Nikhil's avatar
Nikhil committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    p->switch_radix[1] = p->switch_radix[2] = p->switch_radix[0];
	  p->l1_set_size = p->switch_radix[0]/p->tapering_ratio;
    p->l1_term_size = p->l0_set_size * p->l0_term_size;
    p->Np = p->num_switches[0]/p->l0_set_size;
    p->num_switches[1] =  p->Np * p->l1_set_size;
    p->link_repetitions = p->switch_radix[0]/p->Np;
    if(p->l1_set_size % p->link_repetitions) {
      while(p->l1_set_size % p->link_repetitions) {
        p->link_repetitions--;
      }
    } 
    int halfPods = p->l1_set_size/p->link_repetitions;
    p->num_switches[2] = halfPods * p->Ns;
    if(!g_tw_mynode) printf("Pods %d; L2 pods %d; Link repetition:%d; l1_set_size %d\n", 
        p->Np, halfPods, p->link_repetitions, p->l1_set_size);
  }

  if(p->ft_type == 1 && p->num_levels == 3) {
    if(p->Np != p->switch_radix[0] && p->Np !=  p->switch_radix[0]/2) {
      if(!g_tw_mynode) {
        printf("ft_type 1 should be used for a system with %d or %d pods, which "
        "needs %d or %d L0 switches, respectively\n",  p->switch_radix[0], 
         p->switch_radix[0]/2,  p->switch_radix[0] *  p->switch_radix[0]/2,
          p->switch_radix[0]/2 *  p->switch_radix[0]/2);
      }
      tw_error(TW_LOC, "Incorrect number of switches for ft_type 1");
    }
809 810
  }

Nikhil's avatar
Nikhil committed
811
  for(int jj=0;jj<3 && !g_tw_mynode;jj++)
812 813
  {
    printf("num_switches[%d]=%d\n",jj,p->num_switches[jj]);
814 815
  }

Nikhil's avatar
Nikhil committed
816
  rc = configuration_get_value_int(&config, "PARAMS", "packet_size", anno,
817
      &p->packet_size);
Nikhil's avatar
Nikhil committed
818
  if(rc) {
819
    p->packet_size = 512;
Nikhil's avatar
Nikhil committed
820 821 822 823 824 825 826 827 828 829 830
  }
  if(!g_tw_mynode) printf("FT packet size is %d\n", p->packet_size);

  p->num_rails = 1;
  configuration_get_value_int(&config, "PARAMS", "num_rails", anno, &p->num_rails);
  if(!g_tw_mynode) printf("FT num rails is %d\n", p->num_rails);
  
  if(p->ft_type == 2) {
    p->ports_per_nic = p->num_rails;
  } else {
    p->ports_per_nic = 1;
831
  }
832

833 834 835
  p->router_delay = 50;
  configuration_get_value_double(&config, "PARAMS", "router_delay", anno,
      &p->router_delay);
Nikhil's avatar
Nikhil committed
836
  if(!g_tw_mynode) printf("FT router delay is %.1lf\n", p->router_delay);
837

838 839 840 841
  p->soft_delay = 1000;
  configuration_get_value_double(&config, "PARAMS", "soft_delay", anno,
      &p->soft_delay);

Nikhil's avatar
Nikhil committed
842 843 844
  rc = configuration_get_value_int(&config, "PARAMS", "vc_size", anno, &p->vc_size);
  if(rc) {
    p->vc_size = 8 * p->packet_size;
845
  }
Nikhil's avatar
Nikhil committed
846
  if(!g_tw_mynode) printf("FT buffer size links is %d\n", p->vc_size);
847

Nikhil's avatar
Nikhil committed
848
  rc = configuration_get_value_int(&config, "PARAMS", "cn_vc_size", anno,
849
      &p->cn_vc_size);
Nikhil's avatar
Nikhil committed
850 851
  if(rc) {
    p->cn_vc_size = 8 * p->packet_size;
852
  }
Nikhil's avatar
Nikhil committed
853
  if(!g_tw_mynode) printf("FT buffer size injection is %d\n", p->cn_vc_size);
854

855
  rc = configuration_get_value_int(&config, "PARAMS", "chunk_size", anno, &p->chunk_size);
Nikhil's avatar
Nikhil committed
856 857 858
  if(rc) {
    p->chunk_size = 512;
  }
859

Nikhil's avatar
Nikhil committed
860 861 862 863 864 865 866 867 868 869 870
  char routing_str[MAX_NAME_LENGTH];
  configuration_get_value(&config, "PARAMS", "routing", anno, routing_str,
      MAX_NAME_LENGTH);
  if(strcmp(routing_str, "static") == 0)
    p->routing = STATIC;
  else if(strcmp(routing_str, "adaptive")==0)
    p->routing = ADAPTIVE;
  else
  {
    p->routing = ADAPTIVE;
  }
Nikhil's avatar
Nikhil committed
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
  if(!g_tw_mynode) printf("FT routing is %d\n", p->routing);
  
  char rail_select_str[MAX_NAME_LENGTH];
  configuration_get_value(&config, "PARAMS", "rail_select", anno, rail_select_str,
      MAX_NAME_LENGTH);
  if(strcmp(rail_select_str, "dedicated") == 0)
    p->rail_select = RAIL_DEDICATED;
  else if(strcmp(rail_select_str, "adaptive")==0)
    p->rail_select = RAIL_ADAPTIVE;
  else {
    p->rail_select = RAIL_DEDICATED;
  }
  if(!g_tw_mynode) printf("FT rail selection is %d\n", p->rail_select);

  p->rail_size_limit = 32768;
  configuration_get_value_int(&config, "PARAMS", "rail_select_limit", anno,
      &p->rail_size_limit);
  if(!g_tw_mynode) printf("FT rail select limit  is %d\n", p->rail_size_limit);
Nikhil's avatar
Nikhil committed
889 890 891
  
  configuration_get_value_int(&config, "PARAMS", "dump_topo", anno,
      &dump_topo);
892

Nikhil's avatar
Nikhil committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  routing_folder[0] = '\0';
  rc = configuration_get_value(&config, "PARAMS", "routing_folder", anno, routing_folder,
      MAX_NAME_LENGTH);

  if(routing_folder[0] == '\0') {
    if(dump_topo || p->routing == STATIC) {
      tw_error(TW_LOC, "routing_folder has to be provided with dump_topo || static routing");
    }
  }
 
  dot_file_p[0] = '\0'; 
  rc = configuration_get_value(&config, "PARAMS", "dot_file", anno, dot_file_p,
      MAX_NAME_LENGTH);
  
  if(dot_file_p[0] == '\0') {
    if(dump_topo) {
      tw_error(TW_LOC, "dot_file has to be provided with dump_topo");
    }
  }
  
Nikhil's avatar
Nikhil committed
913
  rc = configuration_get_value_double(&config, "PARAMS", "link_bandwidth", anno,
914
      &p->link_bandwidth);
Nikhil's avatar
Nikhil committed
915
  if(rc) {
916 917 918
    p->link_bandwidth = 5;
  }

Nikhil's avatar
Nikhil committed
919
  rc = configuration_get_value_double(&config, "PARAMS", "cn_bandwidth", anno,
920 921 922 923
      &p->cn_bandwidth);
  if(!p->cn_bandwidth) {
    p->cn_bandwidth = 5;
  }
Nikhil's avatar
Nikhil committed
924 925 926
  
  if(!g_tw_mynode) printf("FT bandwidths link %.1lf node %.1lf\n", 
      p->link_bandwidth, p->cn_bandwidth);
927

928 929 930 931
#if FATTREE_DEBUG
  printf("l1_set_size:%d l1_term_size:%d\n",p->l1_set_size,p->l1_term_size);
#endif

932 933 934 935 936 937 938 939 940 941 942
  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
943 944
  for (int i = 0; i < anno_map->num_annos; i++){
    const char * anno = anno_map->annotations[i].ptr;
945 946 947 948 949
    fattree_read_config(anno, &all_params[i]);
  }
  if (anno_map->has_unanno_lp > 0){
    fattree_read_config(NULL, &all_params[anno_map->num_annos]);
  }
950 951 952
#ifdef ENABLE_CORTEX
  model_net_topology = fattree_cortex_topology;
#endif
953 954 955 956 957
}

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

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
    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);

Nikhil's avatar
Nikhil committed
984 985
   if(s->params->ft_type != 2) {
    num_lps /= s->params->num_rails;
986
   }
Nikhil's avatar
Nikhil committed
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 1015

   if(num_lps != s->params->l0_term_size) {
     tw_error(TW_LOC, "Number of NICs per repetition per rail has to be equal to "
         "%d, not the given value of %d\n", s->params->l0_term_size, num_lps);
   }
  
   if(s->params->ft_type != 2) {
    s->terminal_id = (mapping_rep_id * num_lps) + (mapping_offset/s->params->num_rails);
    s->rail_id = (mapping_offset % s->params->num_rails);
   } else {
    s->terminal_id = (mapping_rep_id * num_lps) + mapping_offset;
    s->rail_id = 0;
   }
   s->switch_id = s->terminal_id / s->params->l0_term_size;
   s->switch_lp = (tw_lpid*)malloc(s->params->ports_per_nic * sizeof(tw_lpid));
   if(s->params->ft_type != 2) {
     codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
         s->switch_id, 0 + s->params->num_levels * s->rail_id, &s->switch_lp[0]);
   } else {
     for(int i = 0; i < s->params->ports_per_nic; i++) {
       codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
           s->switch_id, 0 + s->params->num_levels * i, &s->switch_lp[i]);
     }
   }
   s->terminal_available_time = (tw_stime*) malloc(s->params->ports_per_nic * sizeof(tw_stime));
   s->vc_occupancy = (int*) malloc(s->params->ports_per_nic * sizeof(int));
   s->terminal_length = (int*) malloc(s->params->ports_per_nic * sizeof(int));
   s->in_send_loop = (int*) malloc(s->params->ports_per_nic * sizeof(int));
   s->issueIdle = (int*) malloc(s->params->ports_per_nic * sizeof(int));
1016
   s->terminal_msgs =
Nikhil's avatar
Nikhil committed
1017
     (fattree_message_list**)malloc(s->params->ports_per_nic*sizeof(fattree_message_list*));
1018
   s->terminal_msgs_tail =
Nikhil's avatar
Nikhil committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
     (fattree_message_list**)malloc(s->params->ports_per_nic*sizeof(fattree_message_list*));
   s->last_buf_full = (tw_stime*) malloc(s->params->ports_per_nic * sizeof(tw_stime));
   s->busy_time = (tw_stime*) malloc(s->params->ports_per_nic * sizeof(tw_stime));
   for(int i = 0; i < s->params->ports_per_nic; i++) {
     s->terminal_available_time[i] = 0.0;
     s->vc_occupancy[i] = 0;
     s->terminal_msgs[i] = NULL;
     s->terminal_msgs_tail[i] = NULL;
     s->terminal_length[i] = 0;
     s->in_send_loop[i] = 0;
     s->issueIdle[i] = 0;
     s->last_buf_full[i] = 0;
     s->busy_time[i] = 0;
   }
   s->packet_counter = 0;
1034 1035 1036 1037 1038 1039
   s->finished_msgs = 0;
   s->finished_chunks = 0;
   s->finished_packets = 0;
   s->total_time = 0.0;
   s->total_msg_size = 0;

Nikhil's avatar
Nikhil committed
1040 1041 1042
#if FATTREE_DEBUG
   printf("I am terminal %d (%llu), connected to switch %d in rail id %d\n", s->terminal_id,
       LLU(lp->gid), s->switch_id, s->rail_id);
1043
#endif
Noah Wolfe's avatar
Noah Wolfe committed
1044 1045 1046

   rc_stack_create(&s->st);

1047 1048 1049
   s->rank_tbl = NULL;
   //if(!s->rank_tbl)
   //  tw_error(TW_LOC, "\n Hash table not initialized! ");
1050

Nikhil's avatar
Nikhil committed
1051 1052 1053 1054
   /* dump partial topology into DOT format
    * skip term2sw link part, because we are missing the remote switch port
    * in this stage of the code
    */
Nikhil's avatar
Nikhil committed
1055
   if(!dot_file && !s->rail_id)
Nikhil's avatar
Nikhil committed
1056 1057 1058 1059
     dot_write_open_file(&dot_file);
   dot_write_term_info(s, dot_file);

   /* ensure the DOT file is out before reading it with external tools */
Nikhil's avatar
Nikhil committed
1060
   if(dot_file && !s->rail_id)
Nikhil's avatar
Nikhil committed
1061 1062
     fflush(dot_file);

1063 1064 1065 1066 1067 1068
   return;
}

/* sets up the switch */
void switch_init(switch_state * r, tw_lp * lp)
{
1069

1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
  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);
  }

  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;
Nikhil's avatar
Nikhil committed
1092 1093 1094
  if(mapping_rep_id >= p->num_switches[mapping_offset % p->num_levels]) {
    r->unused = 1;
    return;
1095 1096 1097 1098
  }

  r->unused = 0;

Nikhil's avatar
Nikhil committed
1099 1100 1101 1102 1103 1104
  if((mapping_offset % p->num_levels) == 0) {
    r->switch_id = mapping_rep_id;
  } else if((mapping_offset % p->num_levels) ==  1) {
    r->switch_id = mapping_rep_id + p->num_switches[0];
  } else {
    r->switch_id = mapping_rep_id + p->num_switches[0] + p->num_switches[1];
1105
  }
Nikhil's avatar
Nikhil committed
1106
  r->rail_id = mapping_offset / p->num_levels;
1107

Nikhil's avatar
Nikhil committed
1108
  r->switch_level = mapping_offset % p->num_levels;
1109 1110 1111

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

1112
  r->next_output_available_time = (tw_stime*) malloc (r->radix *
1113 1114 1115 1116 1117
      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));
1118
  r->pending_msgs =
1119
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1120
  r->pending_msgs_tail =
1121
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1122
  r->queued_msgs =
1123
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1124
  r->queued_msgs_tail =
1125 1126
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
  r->queued_length = (int*)malloc(r->radix * sizeof(int));
Nikhil's avatar
Nikhil committed
1127
  r->lft = NULL;
1128

1129 1130 1131 1132
  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
1133 1134
  rc_stack_create(&r->st);

1135 1136 1137
  for(int i = 0; i < r->radix; i++)
  {
    // Set credit & switch occupancy
1138 1139 1140
    r->last_buf_full[i] = 0.0;
    r->busy_time[i] = 0.0;
    r->busy_time_sample[i] = 0.0;
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    r->next_output_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;
  }

Nikhil's avatar
Nikhil committed
1152
  /* dump partial topology info into DOT format (switch radix, guid, ...) */
Nikhil's avatar
Nikhil committed
1153
  if(!dot_file && !r->rail_id)
Nikhil's avatar
Nikhil committed
1154
    dot_write_open_file(&dot_file);
1155
  dot_write_switch_info(r, dot_file);
Nikhil's avatar
Nikhil committed
1156

1157 1158
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
    tw_lpid next_switch_lid;
1159 1160
	int written = 0;
    int written_2 = 0;
1161
#endif
1162

1163 1164 1165
  //set lps connected to each port
  r->num_cons = 0;
  r->num_lcons = 0;
Nikhil's avatar
Nikhil committed
1166 1167 1168
#if FATTREE_DEBUG
  printf("I am switch %d (%llu), level %d, radix %d, rail id %d\n", r->switch_id,
    LLU(lp->gid), r->switch_level, r->radix, r->rail_id);
1169 1170 1171
#endif
  //if at level 0, first half ports go to terminals
  if(r->switch_level == 0) {
Nikhil's avatar
Nikhil committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
    int term_rails, term_railid;
    if(p->ft_type != 2) {
      term_rails = p->num_rails;
      term_railid = r->rail_id;
    } else {
      term_rails = 1;
      term_railid = 0;
    }
    int start_terminal = r->switch_id * p->l0_term_size;
    int end_terminal = start_terminal + p->l0_term_size;
1182 1183
    for(int term = start_terminal; term < end_terminal; term++) {
      tw_lpid nextTerm;
Nikhil's avatar
Nikhil committed
1184 1185
      int rep = term / p->l0_term_size;
      int off = (term % p->l0_term_size) * term_rails + term_railid;
1186 1187 1188
      codes_mapping_get_lp_id(def_group_name, LP_CONFIG_NM, NULL, 1,
          rep, off, &nextTerm);
      r->port_connections[r->num_cons++] = nextTerm;
1189
#if FATTREE_CONNECTIONS
Nikhil's avatar
Nikhil committed
1190
	    written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(codes_mapping_get_lp_relative_id(nextTerm,0,0)/term_rails));
1191
#endif
1192 1193
      r->num_lcons++;
#if FATTREE_DEBUG
Nikhil's avatar
Nikhil committed
1194 1195
      printf("L0->term I am switch %d, connect to terminal %d (%llu) at port %d, rail id %d yes collecting\n",
          r->switch_id, term, LLU(nextTerm), r->num_cons - 1, r->rail_id);
1196
#endif
Nikhil's avatar
Nikhil committed
1197
      /* write sw2term links and (reverse link, too) into DOT file */
Nikhil's avatar
Nikhil committed
1198 1199 1200
      if(!r->rail_id) {
        dot_write_sw2term_link(r->switch_level, r->switch_id, r->num_cons - 1, term, 1, dot_file);
      }
1201 1202 1203 1204
    }
    r->start_lneigh = start_terminal;
    r->end_lneigh = end_terminal;
    r->con_per_lneigh = 1;
Nikhil's avatar
Nikhil committed
1205
    assert(r->num_lcons == p->l0_term_size);
1206 1207 1208
    int l1_set;
    if(p->num_levels == 2) {
      l1_set = 0;
Nikhil's avatar
Nikhil committed
1209
      r->con_per_uneigh = 2;
1210
    } else {
Nikhil's avatar
Nikhil committed
1211
      l1_set = r->switch_id / p->l0_set_size;
Nikhil's avatar
Nikhil committed
1212
      r->con_per_uneigh = 1;
1213 1214 1215 1216 1217
    }
    int l1_base = l1_set * p->l1_set_size;
    r->start_uneigh = p->num_switches[0] + l1_base;
    for(int l1 = 0; l1 < p->l1_set_size; l1++) {
      tw_lpid nextTerm;
Nikhil's avatar
Nikhil committed
1218
      assert(l1_base < p->num_switches[1]);
1219
      codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
Nikhil's avatar
Nikhil committed
1220
          l1_base, 1 + r->rail_id * p->num_levels, &nextTerm);
1221 1222
      for(int con = 0; con < r->con_per_uneigh; con++) {
        r->port_connections[r->num_cons++] = nextTerm;
Nikhil's avatar
Nikhil committed
1223
#if FATTREE_CONNECTIONS
1224 1225
        codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
            &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
Nikhil's avatar
Nikhil committed
1226 1227
        next_switch_lid = mapping_rep_id +  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);
1228
#endif
1229
#if FATTREE_DEBUG
Nikhil's avatar
Nikhil committed
1230 1231
    printf("L0->L1 I am switch %d, connect to upper switch %d L1 (%llu) rel_id:%llu at port %d, rail id %d yes collecting\n",
          r->switch_id, l1_base, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1, r->rail_id);
1232
#endif
Nikhil's avatar
Nikhil committed
1233
        /* write all inter switch links to DOT file (initialized before) */
Nikhil's avatar
Nikhil committed
1234 1235 1236 1237
        if(!r->rail_id) {
          dot_write_sw2sw_link(r->switch_level, r->switch_id, r->num_cons - 1,
              r->switch_level + 1, p->num_switches[0] + l1_base, -1, dot_file);
        }
1238 1239 1240 1241
      }
      l1_base++;
    }
  } else if (r->switch_level == 1) {
Nikhil's avatar
Nikhil committed
1242
    int l0_base;
1243 1244 1245 1246
    if(p->num_levels == 2) {
      l0_base = 0;
      r->start_lneigh = 0;
      r->end_lneigh = p->num_switches[0];
Nikhil's avatar
Nikhil committed
1247
      r->con_per_lneigh = 2;
1248 1249
    } else {
      l0_base = ((r->switch_id - p->num_switches[0]) / p->l1_set_size) *
Nikhil's avatar
Nikhil committed
1250
        p->l0_set_size;
1251
      r->start_lneigh = l0_base;
Nikhil's avatar
Nikhil committed
1252
      r->end_lneigh = l0_base + p->l0_set_size;
Nikhil's avatar
Nikhil committed
1253
      r->con_per_lneigh = 1;
1254
    }
Nikhil's avatar
Nikhil committed
1255
    for(int l0 = 0; l0 < p->l0_set_size; l0++) {
1256 1257
      tw_lpid nextTerm;
      codes_mapping_get_lp_id(def_group_name, "fattree_switch", NULL, 1,
Nikhil's avatar
Nikhil committed
1258
          l0_base, 0 + r->rail_id * p->num_levels, &nextTerm);
1259 1260
      for(int con = 0; con < r->con_per_lneigh; con++) {
        r->port_connections[r->num_cons++] = nextTerm;
Nikhil's avatar
Nikhil committed
1261
#if FATTREE_CONNECTIONS
1262 1263
        codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
            &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
Nikhil's avatar
Nikhil committed
1264
        next_switch_lid = mapping_rep_id;
1265 1266
        written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1267 1268
        r->num_lcons++;
#if FATTREE_DEBUG
Nikhil's avatar
Nikhil committed
1269 1270
        printf("L1->L0 I am switch %d, connect to switch %d L0 (%llu) rel_id:%llu at port %d, rail id %d not collecting\n",
            r->switch_id, l0_base, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1, r->rail_id);
1271
#endif
Nikhil's avatar
Nikhil committed
1272
        /* write all inter switch links to DOT file (initialized before) */
Nikhil's avatar
Nikhil committed
1273 1274 1275 1276
        if(!r->rail_id) {
          dot_write_sw2sw_link(r->switch_level, r->switch_id, r->num_cons - 1,
              r->switch_level - 1, l0_base, -1, dot_file);
        }
1277 1278 1279 1280
      }
      l0_base++;
    }
    if(p->num_levels == 3) {
Nikhil's avatar
Nikhil committed
1281 1282 1283 1284
      int rep = p->link_repetitions;
      int l2 = ((r->switch_id - p->num_switches[0]) % p->l1_set_size)/rep * p->Ns;
      r->con_per_uneigh = 1;
      for(int off = 0; off < p->Ns; off++) {
1285
        tw_lpid nextTerm;
Nikhil's avatar
Nikhil committed
1286
        assert(l2 + off < p->num_switches[2]);
1287
        codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
Nikhil's avatar
Nikhil committed
1288 1289
            l2 + off, 2 + r->rail_id * p->num_levels, &nextTerm);
        for(int con = 0; con < r->con_per_uneigh; con++) {
1290
          r->port_connections[r->num_cons++] = nextTerm;
Nikhil's avatar
Nikhil committed
1291
#if FATTREE_CONNECTIONS
1292 1293
          codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
              &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
Nikhil's avatar
Nikhil committed
1294 1295
          next_switch_lid = mapping_rep_id +  p->num_switches[0] + p->num_switches[1];
          written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
1296
#endif
1297
#if FATTREE_DEBUG
Nikhil's avatar
Nikhil committed
1298 1299
          printf("L1->L2:t=0 I am switch %d, connect to upper switch %d L2 (%llu) rel_id:%llu at port %d, rail id %d yes collecting\n",
              r->switch_id, l2+off, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1, r->rail_id);
1300
#endif
1301
          /* write all inter switch links to DOT file (initialized before) */
Nikhil's avatar
Nikhil committed
1302 1303 1304 1305
          if(!r->rail_id) {
            dot_write_sw2sw_link(r->switch_level, r->switch_id, r->num_cons - 1,
                r->switch_level + 1, p->num_switches[0] + p->num_switches[1] + l2 + off, -1, dot_file);
          }
1306 1307
        }
      }
Nikhil's avatar
Nikhil committed
1308 1309 1310 1311 1312 1313 1314
    }
  } else {
    int rep = p->link_repetitions;
    r->con_per_lneigh = 1;
    int l1 = (r->switch_id - p->num_switches[0] - p->num_switches[1])/p->Ns * rep;
    for(; l1 < p->num_switches[1]; l1 += p->l1_set_size) {
      for(int off = 0; off < rep; off++) {
1315
        tw_lpid nextTerm;
Nikhil's avatar
Nikhil committed
1316
        assert(l1 + off < p->num_switches[1]);
1317
        codes_mapping_get_lp_id(lp_group_name, "fattree_switch", NULL, 1,
Nikhil's avatar
Nikhil committed
1318
            l1 + off, 1 + r->rail_id * p->num_levels, &nextTerm);
1319 1320
        for(int con = 0; con < r->con_per_lneigh; con++) {
          r->port_connections[r->num_cons++] = nextTerm;
Nikhil's avatar
Nikhil committed
1321
#if FATTREE_CONNECTIONS
1322 1323
          codes_mapping_get_lp_info(nextTerm, lp_group_name, &mapping_grp_id, NULL,
              &mapping_type_id, anno, &mapping_rep_id, &mapping_offset);
Nikhil's avatar
Nikhil committed
1324 1325
          next_switch_lid = mapping_rep_id + p->num_switches[0];
          written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
1326
#endif
1327 1328
          r->num_lcons++;
#if FATTREE_DEBUG
Nikhil's avatar
Nikhil committed
1329 1330
          printf("L2->L1:t=0 I am switch %d, connect to  switch %d L1 (%llu) rel_id:%llu at port %d, rail id %d not collecting\n",
              r->switch_id, l1 + rep, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1, r->rail_id);
1331
#endif
Nikhil's avatar
Nikhil committed
1332
          /* write all inter switch links to DOT file (initialized before) */
Nikhil's avatar
Nikhil committed
1333 1334 1335 1336
          if(!r->rail_id) {
            dot_write_sw2sw_link(r->switch_level, r->switch_id, r->num_cons - 1,
                r->switch_level - 1, p->num_switches[0] + l1 + off, -1, dot_file);
          }
1337 1338 1339 1340
        }
      }
    }
  }
Nikhil's avatar
Nikhil committed
1341 1342

  /* ensure the DOT file is out before reading it with external tools */
Nikhil's avatar
Nikhil committed
1343
  if(dot_file && !r->rail_id)
Nikhil's avatar
Nikhil committed
1344 1345
    fflush(dot_file);

1346
#if FATTREE_CONNECTIONS
1347 1348
  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);
1349
#endif
1350
  return;
1351
}
1352 1353

/* empty for now.. */
1354
static void fattree_report_stats()
1355
{
Nikhil's avatar
Nikhil committed
1356
  if(dump_topo) return;
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
#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;
1374

1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	MPI_Reduce( &packet_event_f, &t_packet_event_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &packet_event_r, &t_packet_event_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_generate_f, &tt_generate_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_generate_r, &tt_generate_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_send_f, &tt_send_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_send_r, &tt_send_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_arrive_f, &tt_arrive_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_arrive_r, &tt_arrive_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_buffer_f, &tt_buffer_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &t_buffer_r, &tt_buffer_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_send_f, &ts_send_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_send_r, &ts_send_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_arrive_f, &ts_arrive_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_arrive_r, &ts_arrive_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_buffer_f, &ts_buffer_f, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
	MPI_Reduce( &s_buffer_r, &ts_buffer_r, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
1391 1392 1393 1394 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

   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
1420 1421 1422 1423 1424 1425

   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;

1426 1427 1428 1429 1430 1431 1432
   MPI_Reduce( &total_hops, &avg_hops, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &N_finished_packets, &total_finished_packets, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &N_finished_msgs, &total_finished_msgs, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &N_finished_chunks, &total_finished_chunks, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &total_msg_sz, &final_msg_sz, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &fattree_total_time, &avg_time, 1,MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &fattree_max_latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_CODES);
1433

1434 1435
   MPI_Reduce( &fattree_packet_gen, &total_gen, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_CODES);
   MPI_Reduce( &fattree_packet_fin, &total_fin, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_CODES);
1436 1437 1438

   /* print statistics */
   if(!g_tw_mynode)
1439 1440
   {
      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",
1441 1442 1443
              (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);
   }
1444 1445

   if(!g_tw_mynode)
1446
   {
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
      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");
1465 1466
    fprintf(fattree_results_log_header,"<Avg Hops/Total Packets>, <Avg Time/Total Packets>, <Max Latency>, <Total Finished Packets>, <Total Finished Chunks>");
	fprintf(fattree_results_log,"%24.3lf, %24.3lf, %13.3lf, %24.3lld, %23.3lld, ", (float)avg_hops/total_finished_packets, avg_time/(total_finished_packets),max_time,total_finished_packets,total_finished_chunks);
1467 1468
	fclose(fattree_results_log_header);
	fclose(fattree_results_log);
1469
#endif
1470
  }
1471
}
1472 1473

/* fattree packet event */
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
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
1484
{
1485 1486 1487
#if DEBUG_RC
  packet_event_f++;
#endif
1488

Noah Wolfe's avatar
Noah Wolfe committed
1489 1490
  (void)message_offset;
  (void)sched_params;
1491 1492 1493 1494 1495 1496 1497 1498
  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);
1499 1500 1501 1502 1503
  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;
1504
  msg->packet_size = packet_size;
1505
  msg->travel_start_time = tw_now(sender);
1506 1507 1508
  msg->remote_event_size_bytes = 0;
  msg->local_event_size_bytes = 0;
  msg->type = T_GENERATE;
1509
  msg->dest_terminal_id = req->dest_mn_lp;
Nikhil's avatar
Nikhil committed
1510
  msg->rail_id = req->queue_offset;
1511 1512 1513
  msg->message_id = req->msg_id;
  msg->is_pull = req->is_pull;
  msg->pull_size = req->pull_size;
1514
  msg->magic = fattree_terminal_magic_num;
1515
  msg->msg_start_time = req->msg_start_time;
1516 1517

  /* Its the last packet so pass in remote and local event information*/
1518
  if(is_last_pckt)
Nikhil Jane's avatar