fattree.c 92.6 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

#define CREDIT_SIZE 8
#define MEAN_PROCESS 1.0

Nikhil's avatar
Nikhil committed
19
#define TERMINAL_GUID_PREFIX ((uint64_t)(64) << 32)
20
21
#define FTREE_HASH_TABLE_SIZE 262144

22
// debugging parameters
23
24
#define TRACK_PKT -1
//#define TRACK_PKT 2820
25
#define FATTREE_HELLO 0
26
#define FATTREE_DEBUG 0
27
28
#define FATTREE_CONNECTIONS 0
#define FATTREE_MSG 0
Nikhil's avatar
Nikhil committed
29
#define DEBUG_RC 0
30

31
//Data Collection Output Files
Nikhil's avatar
Nikhil committed
32
#define PARAMS_LOG 0
33

34
35
36
#define LP_CONFIG_NM (model_net_lp_config_names[FATTREE])
#define LP_METHOD_NM (model_net_method_names[FATTREE])

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#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

57
long fattree_packet_gen = 0, fattree_packet_fin = 0;
Nikhil's avatar
Nikhil committed
58
int dump_topo = 0;
59

60
61
62
static double maxd(double a, double b) { return a < b ? b : a; }

// arrival rate
Noah Wolfe's avatar
Noah Wolfe committed
63
//static double MEAN_INTERVAL=200.0;
64
65

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

/* 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
78
79
80
81
82
83
84
85
enum ROUTING_ALGO
{
    STATIC=0,
    ADAPTIVE,
};
static char routing_folder[MAX_NAME_LENGTH];
static char dot_file_p[MAX_NAME_LENGTH];

86
87
88
/* switch magic number */
int switch_magic_num = 0;

89
/* terminal magic number */
90
int fattree_terminal_magic_num = 0;
91

Nikhil's avatar
Nikhil committed
92
93
94
/* global variables for DOT file generation */
static FILE *dot_file = NULL;

95
96
97
98
99
100
101
102
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;
};

103
void init_fattree_message_list(fattree_message_list *this,
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  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;
121
122
123
  int link_repetitions;
  int Ns; // number of switches per pod
  int Np; //Number of pods
124
125
126
  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 */
127
  double cn_bandwidth;/* bandwidth of the compute node channels
128
129
130
                        connected to switch */
  int vc_size; /* buffer size of the link channels */
  int cn_vc_size; /* buffer size of the compute node channels */
131
  int chunk_size; /* full-sized packets are broken into smaller chunks.*/
132
  int packet_size;
133
134
135
136
137
138
139
140
  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;
Nikhil's avatar
Nikhil committed
141
  int routing;
142
143
};

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
};

159
160
161
162
/* convert GiB/s and bytes to ns */
 static tw_stime bytes_to_ns(uint64_t bytes, double GB_p_s)
 {
     tw_stime time;
163

164
165
166
167
168
169
     /* 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;
170

171
172
173
     return(time);
 }

174
175
176
177
178
179
180
181
182
/* 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;
183
  int packet_gen;
184
  int packet_fin;
185
186
187
188
189
190
191
192
193
  // 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;
194

195
196
  struct mn_stats fattree_stats_array[CATEGORY_MAX];

197
198
  fattree_message_list **terminal_msgs;
  fattree_message_list **terminal_msgs_tail;
199
  int terminal_length;
200
  int in_send_loop;
201
202
203
  int issueIdle;

   struct rc_stack * st;
204
205
206

  char * anno;
  fattree_param *params;
207
208
209
210
211
212
213
214
215
216
217

  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;

218
  tw_stime last_buf_full;
219
  tw_stime busy_time;
220
  char output_buf[4096];
221
  char output_buf3[4096];
222

223
224
225
226
227
  /* For sampling */
  long fin_chunks_sample;
  long data_size_sample;
  double fin_hops_sample;
  tw_stime fin_chunks_time;
Nikhil's avatar
Nikhil committed
228
229
  tw_stime busy_time_sample;

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
};

/* 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;
266
267
268
269
270
  tw_stime* last_buf_full;

  tw_stime* busy_time;
  tw_stime* busy_time_sample;

271
272
273
274
275
276
277
278
279
280
  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;

281
282
283
  char output_buf[4096];
  char output_buf2[4096];

Noah Wolfe's avatar
Noah Wolfe committed
284
285
  struct rc_stack * st;

286
287
  char * anno;
  fattree_param *params;
Nikhil's avatar
Nikhil committed
288
289
  /* array to store linear forwaring tables in case we use static routing */
  int *lft;
290
291
};

292
293
static tw_stime         fattree_total_time = 0;
static tw_stime         fattree_max_latency = 0;
Noah Wolfe's avatar
Noah Wolfe committed
294
//static tw_stime         max_collective = 0;
295
296
297
298
299
300
301
302


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;

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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));
}

327
328
static void free_tmp(void * ptr)
{
329
    struct ftree_qhash_entry * ftree = ptr;
330
331
332
333
    free(ftree->remote_event_data);
    free(ftree);
}

334
static void append_to_fattree_message_list(
335
336
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
337
        int index,
338
339
340
341
342
343
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thisq[index] = msg;
    } else {
        thistail[index]->next = msg;
        msg->prev = thistail[index];
344
    }
345
346
347
    thistail[index] = msg;
}

348
static void prepend_to_fattree_message_list(
349
350
        fattree_message_list ** thisq,
        fattree_message_list ** thistail,
351
        int index,
352
353
354
355
356
357
        fattree_message_list *msg) {
    if(thisq[index] == NULL) {
        thistail[index] = msg;
    } else {
        thisq[index]->prev = msg;
        msg->next = thisq[index];
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    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
404
//static const tw_lptype* fattree_get_switch_lp_type(void);
405

406
/* returns the fattree message size */
407
408
409
410
411
static int fattree_get_msg_sz(void)
{
  return sizeof(fattree_message);
}

Nikhil's avatar
Nikhil committed
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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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;
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  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");
  }
}

static void dot_write_close_file(FILE **fout)
{
  if(!fout || !(*fout)) return;
  if(!dump_topo) return;
  fclose(*fout);
  *fout = NULL;
}

/* sw IDs aren't unique, but level+ID info is */
static void dot_write_switch_info(switch_state *s, int sw_gid, FILE *fout)
{
  if(!s || s->unused || !(s->params) || !fout) return;
  if(!dump_topo) return;

  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",
      s->switch_level, sw_gid, switch_guid, s->radix,
      (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;

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

static void dot_write_term2sw_link(int term_id, int rsw_lvl, int rsw_id,
                                   int rsw_port, 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"))
     rsw_port++;

  fprintf(fout, "\t\"H_%d\" -> \"S_%d_%d\" [comment=\"P1->P%d\"];\n",
        term_id, rsw_lvl, rsw_id, rsw_port);
}

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
673
static void fattree_read_config(const char * anno, fattree_param *p){
674
675
  int i;

Nikhil's avatar
Nikhil committed
676
  p->ft_type = 1;
677
  configuration_get_value_int(&config, "PARAMS", "ft_type", anno,
678
679
      &p->ft_type);

680
  configuration_get_value_int(&config, "PARAMS", "num_levels", anno,
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
      &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,",");
  }

733
  p->Ns = p->switch_radix[0]/2;
734
  int num_terminals = p->num_switches[0]*p->switch_radix[0]/2;
735

736
737
738
  if(p->num_levels == 2) {
    p->num_switches[1] = p->num_switches[0]/2;
    p->switch_radix[1] = p->switch_radix[0];
Nikhil's avatar
Nikhil committed
739
740
741
    p->l1_set_size = p->switch_radix[0]/4;
    p->l1_term_size = (p->l1_set_size * p->switch_radix[0]);

742
  } else {
743
	if(p->ft_type == 0){
744
      p->Np = ceil((num_terminals)/(double)(p->Ns*p->Ns));
745
746
747
748
      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);
749
      printf("Np:%d Ns:%d\n",p->Np,p->Ns);
750
751
	}else{
	  p->num_switches[1] = p->num_switches[0];
752
753
      p->num_switches[2] = p->num_switches[0]/2;
      p->switch_radix[1] = p->switch_radix[2] = p->switch_radix[0];
754
755
	}
	p->l1_set_size = p->switch_radix[0]/2;
Nikhil's avatar
Nikhil committed
756
    p->l1_term_size = (p->l1_set_size * (p->switch_radix[0] / 2));
757
758
  }

759
#if FATTREE_CONNECTIONS
760
761
762
763
764
765
766
  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

767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  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]) {
      tw_error(TW_LOC, "Not enough switches/radix at level %d for full "
          "bisection bandwidth\n", i);
    }
  }

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

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

790
791
792
  p->router_delay = 50;
  configuration_get_value_double(&config, "PARAMS", "router_delay", anno,
      &p->router_delay);
793

794
795
796
797
798
799
800
801
802
803
804
  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);
  }

805
  configuration_get_value_int(&config, "PARAMS", "cn_vc_size", anno,
806
807
808
      &p->cn_vc_size);
  if(!p->cn_vc_size) {
    p->cn_vc_size = 8*p->packet_size;
809
    fprintf(stderr, "Buffer size of compute node channels not specified, "
810
811
812
        "setting to %d\n", p->cn_vc_size);
  }

813
814
815
816
817
818
  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);
    }

Nikhil's avatar
Nikhil committed
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
  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
  {
    fprintf(stderr, 
        "No routing protocol specified, setting to adaptive routing\n");
    p->routing = ADAPTIVE;
  }
  
  configuration_get_value_int(&config, "PARAMS", "dump_topo", anno,
      &dump_topo);
835

Nikhil's avatar
Nikhil committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
  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");
    }
  }
  
856
  configuration_get_value_double(&config, "PARAMS", "link_bandwidth", anno,
857
858
859
      &p->link_bandwidth);
  if(!p->link_bandwidth) {
    p->link_bandwidth = 5;
860
    fprintf(stderr, "Bandwidth of links is specified, setting to %lf\n",
861
862
863
        p->link_bandwidth);
  }

864
  configuration_get_value_double(&config, "PARAMS", "cn_bandwidth", anno,
865
866
867
      &p->cn_bandwidth);
  if(!p->cn_bandwidth) {
    p->cn_bandwidth = 5;
868
    fprintf(stderr, "Bandwidth of compute node channels not specified, "
869
870
871
        "setting to %lf\n", p->cn_bandwidth);
  }

872
873
874
875
#if FATTREE_DEBUG
  printf("l1_set_size:%d l1_term_size:%d\n",p->l1_set_size,p->l1_term_size);
#endif

876
877
878
879
880
881
882
883
884
885
886
  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
887
888
  for (int i = 0; i < anno_map->num_annos; i++){
    const char * anno = anno_map->annotations[i].ptr;
889
890
891
892
893
894
895
896
897
898
    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 )
{
899
    s->packet_gen = 0;
900
901
    s->packet_fin = 0;

902
    uint32_t h1 = 0, h2 = 0;
903
904
    bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
    fattree_terminal_magic_num = h1 + h2;
905

906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
    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);
   }
934
   s->terminal_id = (mapping_rep_id * num_lps) + mapping_offset;
935
936
937
938
939
   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;
940
   s->terminal_msgs =
941
     (fattree_message_list**)malloc(1*sizeof(fattree_message_list*));
942
   s->terminal_msgs_tail =
943
944
     (fattree_message_list**)malloc(1*sizeof(fattree_message_list*));

945
946
947
948
949
950
   s->finished_msgs = 0;
   s->finished_chunks = 0;
   s->finished_packets = 0;
   s->total_time = 0.0;
   s->total_msg_size = 0;

951
952
953
   s->last_buf_full = 0;
   s->busy_time = 0;

954
#if FATTREE_HELLO
955
956
   printf("I am terminal %d (%llu), connected to switch %d\n", s->terminal_id,
       LLU(lp->gid), s->switch_id);
957
#endif
Noah Wolfe's avatar
Noah Wolfe committed
958
959
960

   rc_stack_create(&s->st);

961
962
963
964
965
966
967
   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;

968
969
970
971
   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! ");

972
   s->params->num_terminals = codes_mapping_get_lp_count(lp_group_name, 0,
973
      LP_CONFIG_NM, s->anno, 0);
Nikhil's avatar
Nikhil committed
974
975
976
977
978
979
980
981
982
983
984
985
   /* dump partial topology into DOT format
    * skip term2sw link part, because we are missing the remote switch port
    * in this stage of the code
    */
   if(!dot_file)
     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 */
   if(dot_file)
     fflush(dot_file);

986
987
988
989
990
991
   return;
}

/* sets up the switch */
void switch_init(switch_state * r, tw_lp * lp)
{
992
993
994
995
  uint32_t h1 = 0, h2 = 0;
  bj_hashlittle2(LP_METHOD_NM, strlen(LP_METHOD_NM), &h1, &h2);
  switch_magic_num = h1 + h2;

996
  char anno[MAX_NAME_LENGTH];
Noah Wolfe's avatar
Noah Wolfe committed
997
  int num_terminals = -1;
998
999
1000
1001
1002

  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);
1003
    num_terminals = codes_mapping_get_lp_count(def_group_name, 0,
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
1037
1038
1039
      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];

1040
  r->next_output_available_time = (tw_stime*) malloc (r->radix *
1041
      sizeof(tw_stime));
1042
  r->next_credit_available_time = (tw_stime*) malloc (r->radix *
1043
1044
1045
1046
1047
      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));
1048
  r->pending_msgs =
1049
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1050
  r->pending_msgs_tail =
1051
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1052
  r->queued_msgs =
1053
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
1054
  r->queued_msgs_tail =
1055
1056
    (fattree_message_list**)malloc(r->radix * sizeof(fattree_message_list*));
  r->queued_length = (int*)malloc(r->radix * sizeof(int));
Nikhil's avatar
Nikhil committed
1057
  r->lft = NULL;
1058

1059
1060
1061
1062
  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
1063
1064
  rc_stack_create(&r->st);

1065
1066
1067
  for(int i = 0; i < r->radix; i++)
  {
    // Set credit & switch occupancy
1068
1069
1070
    r->last_buf_full[i] = 0.0;
    r->busy_time[i] = 0.0;
    r->busy_time_sample[i] = 0.0;
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
    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;
  }

Nikhil's avatar
Nikhil committed
1083
1084
1085
1086
1087
  /* dump partial topology info into DOT format (switch radix, guid, ...) */
  if(!dot_file)
    dot_write_open_file(&dot_file);
  dot_write_switch_info(r, lp->gid, dot_file);

1088
1089
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
    tw_lpid next_switch_lid;
1090
1091
	int written = 0;
    int written_2 = 0;
1092
#endif
1093

1094
1095
1096
1097
  //set lps connected to each port
  r->num_cons = 0;
  r->num_lcons = 0;
#if FATTREE_HELLO
1098
1099
  printf("I am switch %d (%llu), level %d, radix %d\n", r->switch_id,
    LLU(lp->gid), r->switch_level, r->radix);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
#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;
1112
1113
1114
#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
1115
1116
      r->num_lcons++;
#if FATTREE_DEBUG
1117
      printf("L0->term I am switch %d, connect to terminal %d (%llu) at port %d yes collecting\n",
1118
          r->switch_id, term, LLU(nextTerm), r->num_cons - 1);
1119
#endif
Nikhil's avatar
Nikhil committed
1120
1121
      /* write sw2term links and (reverse link, too) into DOT file */
      dot_write_sw2term_link(r->switch_level, lp->gid, r->num_cons - 1, term, 1, dot_file);
1122
1123
1124
1125
1126
1127
1128
1129
    }
    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;
Nikhil's avatar
Nikhil committed
1130
      r->con_per_uneigh = 2;
1131
1132
    } else {
      l1_set = r->switch_id / p->l1_set_size;
Nikhil's avatar
Nikhil committed
1133
      r->con_per_uneigh = 1;
1134
1135
1136
1137
1138
1139
1140
1141
1142
    }
    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;
      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;
1143
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1144
1145
1146
        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];
1147
1148
	    written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1149
#if FATTREE_DEBUG
1150
    printf("L0->L1 I am switch %d, connect to upper switch %d L1 (%llu) rel_id:%llu at port %d yes collecting\n",
1151
          r->switch_id, l1_base, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1152
#endif
Nikhil's avatar
Nikhil committed
1153
1154
1155
        /* write all inter switch links to DOT file (initialized before) */
        dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
              r->switch_level + 1, nextTerm, -1, dot_file);
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
      }
      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];
Nikhil's avatar
Nikhil committed
1166
      r->con_per_lneigh = 2;
1167
1168
1169
1170
1171
1172
    } 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;
Nikhil's avatar
Nikhil committed
1173
      r->con_per_lneigh = 1;
1174
1175
1176
1177
1178
1179
1180
    }
    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;
1181
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1182
1183
1184
        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];
1185
1186
        written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1187
1188
        r->num_lcons++;
#if FATTREE_DEBUG
1189
1190
        printf("L1->L0 I am switch %d, connect to switch %d L0 (%llu) rel_id:%llu at port %d not collecting\n",
            r->switch_id, l0_base, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1191
#endif
Nikhil's avatar
Nikhil committed
1192
1193
1194
        /* write all inter switch links to DOT file (initialized before) */
        dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
              r->switch_level - 1, nextTerm, -1, dot_file);
1195
1196
1197
1198
      }
      l0_base++;
    }
    if(p->num_levels == 3) {
1199
      int l2_base = 0;
1200
      if(p->ft_type == 0) {
1201
1202
        /*for(int rep=0;rep<p->link_repetitions;rep++)*/{
            int rep = 0;
1203
1204
1205
1206
            int l2 = ((r->switch_id - p->num_switches[0]) % p->l1_set_size + rep*p->l1_set_size);
            /* not true anymore */
            r->start_uneigh = p->num_switches[0] + l2;
            r->con_per_uneigh = p->link_repetitions;
1207
            printf("link_repetitions:%d rep:%d l2:%d\n",p->link_repetitions,rep,l2);
1208
1209
1210
1211
1212
1213
            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;
1214
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1215
1216
1217
1218
1219
1220
                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
1221
1222
                printf("L1->L2:t=0 I am switch %d, connect to upper switch %d L2 (%llu) rel_id:%llu at port %d yes collecting\n",
                    r->switch_id, l2+rep*p->l1_set_size, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1223
#endif
1224
1225
1226
				/* write all inter switch links to DOT file (initialized before) */
            	dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
                  r->switch_level + 1, nextTerm, -1, dot_file);
1227
1228
1229
              }
            }
        }
1230
      } else {
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
        int l2 = ((r->switch_id - p->num_switches[0]) % p->l1_set_size);
        /* not true anymore */
        r->start_uneigh = p->num_switches[0] + l2;
        r->con_per_uneigh = 2;
        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;
1241
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1242
1243
1244
            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];
1245
1246
            written += sprintf(r->output_buf + written, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1247
#if FATTREE_DEBUG
1248
1249
            printf("L1->L2:t!=0 I am switch %d, connect to upper switch %d L2 (%llu) rel_id:%llu at port %d yes collecting\n",
                r->switch_id, l2, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1250
#endif
Nikhil's avatar
Nikhil committed
1251
1252
1253
            /* write all inter switch links to DOT file (initialized before) */
            dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
                  r->switch_level + 1, nextTerm, -1, dot_file);
1254
1255
1256
1257
1258
          }
        }
      }
    }
  } else {
1259
    if(p->ft_type == 0) {
1260
      r->con_per_lneigh = p->link_repetitions;
1261
1262
1263
      /* not true anymore */
      r->start_lneigh = p->num_switches[0];
      r->end_lneigh = r->start_lneigh + p->num_switches[1];
1264
1265
      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) {
1266
1267
1268
1269
1270
        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;
1271
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1272
1273
1274
          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];
1275
1276
	      written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1277
1278
          r->num_lcons++;
#if FATTREE_DEBUG
1279
1280
          printf("L2->L1:t=0 I am switch %d, connect to  switch %d L1 (%llu) rel_id:%llu at port %d not collecting\n",
                  r->switch_id, l1, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1281
#endif
1282
1283
1284
          /* write all inter switch links to DOT file (initialized before) */
          dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
                r->switch_level - 1, nextTerm, -1, dot_file);
1285
1286
1287
        }
      }
    } else {
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
      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;
1299
#if FATTREE_CONNECTIONS || FATTREE_DEBUG
1300
1301
1302
          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];
1303
1304
	      written_2 += sprintf(r->output_buf2 + written_2, "%u, %llu, ", r->switch_id+p->num_terminals,LLU(next_switch_lid)+p->num_terminals);
#endif
1305
1306
          r->num_lcons++;
#if FATTREE_DEBUG
1307
1308
          printf("L2->L1:t!=0 I am switch %d, connect to  switch %d L1 (%llu) rel_id:%llu at port %d not collecting\n",
              r->switch_id, l1, LLU(nextTerm), LLU(next_switch_lid), r->num_cons - 1);
1309
#endif
Nikhil's avatar
Nikhil committed
1310
1311
1312
          /* write all inter switch links to DOT file (initialized before) */
          dot_write_sw2sw_link(r->switch_level, lp->gid, r->num_cons - 1,
                r->switch_level - 1, nextTerm, -1, dot_file);
1313
1314
1315
1316
        }
      }
    }
  }
Nikhil's avatar
Nikhil committed
1317
1318
1319
1320
1321

  /* ensure the DOT file is out before reading it with external tools */
  if(dot_file)
    fflush(dot_file);

1322
#if FATTREE_CONNECTIONS
1323
1324
  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);
1325
#endif
1326
  return;
1327
}
1328
1329

/* empty for now.. */
1330
static void fattree_report_stats()
1331
{
Nikhil's avatar
Nikhil committed
1332
  if(dump_topo) return;
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
#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;
1350

1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
	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",