torus.c 45.9 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7 8
#include <ross.h>
#include <assert.h>
9
#include <string.h>
10 11 12 13 14 15

#include "codes/lp-io.h"
#include "codes/codes_mapping.h"
#include "codes/codes.h"
#include "codes/model-net.h"
#include "codes/model-net-method.h"
16 17
#include "codes/model-net-lp.h"
#include "codes/net/torus.h"
18 19 20 21

#define CHUNK_SIZE 32
#define DEBUG 1
#define MEAN_INTERVAL 100
22
#define TRACE -1
23

24 25 26
/* collective specific parameters */
#define TREE_DEGREE 4
#define LEVEL_DELAY 1000
27
#define TORUS_COLLECTIVE_DEBUG 0
28 29 30 31
#define NUM_COLLECTIVES  1
#define COLLECTIVE_COMPUTATION_DELAY 5700
#define TORUS_FAN_OUT_DELAY 20.0

32 33 34
#define LP_CONFIG_NM (model_net_lp_config_names[TORUS])
#define LP_METHOD_NM (model_net_method_names[TORUS])

35 36
static double maxd(double a, double b) { return a < b ? b : a; }

37 38
/* Torus network model implementation of codes, implements the modelnet API */

39 40 41 42 43 44 45
typedef struct torus_param torus_param;
struct torus_param
{
    int n_dims; /*Dimension of the torus network, 5-D, 7-D or any other*/
    int* dim_length; /*Length of each torus dimension*/
    double link_bandwidth;/* bandwidth for each torus link */
    int buffer_size; /* number of buffer slots for each vc in flits*/
46
    //int num_net_traces; /* number of network traces to be mapped on torus */
47 48 49 50 51 52 53 54 55 56 57 58 59 60
    int num_vc; /* number of virtual channels for each torus link */
    float mean_process;/* mean process time for each flit  */
    int chunk_size; /* chunk is the smallest unit--default set to 32 */

    /* "derived" torus parameters */

    /* factor, used in torus coordinate calculation */
    int * factor;
    /* half length of each dimension, used in torus coordinates calculation */
    int * half_length;

    double head_delay;
    double credit_delay;
};
61 62

/* codes mapping group name, lp type name */
63
static char grp_name[MAX_NAME_LENGTH];
64
/* codes mapping group id, lp type id, repetition id and offset */
65
int mapping_grp_id, mapping_rep_id, mapping_type_id, mapping_offset;
66 67 68 69

/* for calculating torus model statistics, average and maximum travel time of a packet */
static tw_stime         total_time = 0;
static tw_stime         max_latency = 0;
70
static tw_stime         max_collective = 0;
71 72 73 74 75 76

/* number of finished packets on each PE */
static long long       N_finished_packets = 0;
/* total number of hops traversed by a message on each PE */
static long long       total_hops = 0;

77 78 79 80 81
/* annotation-specific parameters (unannotated entry occurs at the 
 * last index) */
static uint64_t                  num_params = 0;
static torus_param             * all_params = NULL;
static const config_anno_map_t * anno_map   = NULL;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

typedef struct nodes_state nodes_state;

/* state of a torus node */
struct nodes_state
{
  /* counts the number of packets sent from this compute node */
  unsigned long long packet_counter;            
  /* availability time of each torus link */
  tw_stime** next_link_available_time; 
  /* availability of each torus credit link */
  tw_stime** next_credit_available_time;
  /* next flit generate time */
  tw_stime** next_flit_generate_time;
  /* buffer size for each torus virtual channel */
  int** buffer;
  /* coordinates of the current torus node */
  int* dim_position;
  /* neighbor LP ids for this torus node */
  int* neighbour_minus_lpID;
  int* neighbour_plus_lpID;

  /* records torus statistics for this LP having different communication categories */
  struct mn_stats torus_stats_array[CATEGORY_MAX];
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
   /* for collective operations */

  /* collective init time */
  tw_stime collective_init_time;

  /* node ID in the tree */ 
   tw_lpid node_id;

   /* messages sent & received in collectives may get interchanged several times so we have to save the 
     origin server information in the node's state */
   tw_lpid origin_svr; 
  
  /* parent node ID of the current node */
   tw_lpid parent_node_id;
   /* array of children to be allocated in terminal_init*/
   tw_lpid* children;

   /* children of a node can be less than or equal to the tree degree */
   int num_children;

   short is_root;
   short is_leaf;

   /* to maintain a count of child nodes that have fanned in at the parent during the collective
      fan-in phase*/
   int num_fan_nodes;
132 133 134 135 136

   /* LPs annotation */
   const char * anno;
   /* LPs configuration */
   const torus_param * params;
137 138
};

139 140 141 142
static void torus_read_config(
        const char         * anno,
        torus_param        * params){
    char dim_length_str[MAX_NAME_LENGTH];
143
    int i;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    // shorthand
    torus_param *p = params;

    configuration_get_value_int(&config, "PARAMS", "n_dims", anno, &p->n_dims);
    if(!p->n_dims) {
        p->n_dims = 4; /* a 4-D torus */
        fprintf(stderr, 
                "Warning: Number of dimensions not specified, setting to %d\n",
                p->n_dims);
    }

    configuration_get_value_double(&config, "PARAMS", "link_bandwidth", anno,
            &p->link_bandwidth);
    if(!p->link_bandwidth) {
        p->link_bandwidth = 2.0; /*default bg/q configuration */
        fprintf(stderr, "Link bandwidth not specified, setting to %lf\n",
                p->link_bandwidth);
    }

    configuration_get_value_int(&config, "PARAMS", "buffer_size", anno, &p->buffer_size);
    if(!p->buffer_size) {
        p->buffer_size = 2048;
        fprintf(stderr, "Buffer size not specified, setting to %d",
                p->buffer_size);
    }

171

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    configuration_get_value_int(&config, "PARAMS", "chunk_size", anno, &p->chunk_size);
    if(!p->chunk_size) {
        p->chunk_size = 32;
        fprintf(stderr, "Warning: Chunk size not specified, setting to %d\n",
                p->chunk_size);
    }
    configuration_get_value_int(&config, "PARAMS", "num_vc", anno, &p->num_vc);
    if(!p->num_vc) {
        /* by default, we have one for taking packets,
         * another for taking credit*/
        p->num_vc = 1;
        fprintf(stderr, "Warning: num_vc not specified, setting to %d\n",
                p->num_vc);
    }

    int rc = configuration_get_value(&config, "PARAMS", "dim_length", anno,
            dim_length_str, MAX_NAME_LENGTH);
    if (rc == 0){
        tw_error(TW_LOC, "couldn't read PARAMS:dim_length");
    }
    char* token;
    p->dim_length=malloc(p->n_dims*sizeof(*p->dim_length));
    token = strtok(dim_length_str, ",");
    i = 0;
    while(token != NULL)
197
    {
198 199 200 201 202 203 204 205 206
        sscanf(token, "%d", &p->dim_length[i]);
        if(p->dim_length[i] <= 0)
        {
            tw_error(TW_LOC, "Invalid torus dimension specified "
                    "(%d at pos %d), exiting... ", p->dim_length[i], i);
        }
        i++;
        token = strtok(NULL,",");
    }
207 208 209 210 211 212 213
    /*int num_nodes = 1;

    for( i = 0; i < p->n_dims; i++)
	   num_nodes *= p->dim_length[i];
    
    configuration_get_value_int(&config, "PARAMS", "num_net_traces", anno, &p->num_net_traces);
    if(!p->num_net_traces) {
214

215 216 217 218 219 220
	p->num_net_traces = num_nodes;
        fprintf(stderr, "Number of network traces not specified, setting to %d",
                p->num_net_traces);
    }
   // Number of network traces should be <= number of torus network nodes `
   assert(p->num_net_traces <= num_nodes);*/
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    // create derived parameters
   
    // factor is an exclusive prefix product
    p->factor = malloc(p->n_dims * sizeof(int));
    p->factor[0] = 1;
    for(i = 1; i < p->n_dims; i++)
        p->factor[i] = p->factor[i-1] * p->dim_length[i-1];

    p->half_length = malloc(p->n_dims * sizeof(int));
    for (i = 0; i < p->n_dims; i++)
        p->half_length[i] = p->dim_length[i] / 2;

    // some latency numbers
    p->head_delay = (1.0 / p->link_bandwidth) * p->chunk_size;
    p->credit_delay = (1.0 / p->link_bandwidth) * p->chunk_size;
}

static void torus_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));

    for (uint64_t i = 0; i < anno_map->num_annos; i++){
        const char * anno = anno_map->annotations[i];
        torus_read_config(anno, &all_params[i]);
    }
    if (anno_map->has_unanno_lp > 0){
        torus_read_config(NULL, &all_params[anno_map->num_annos]);
250
    }
251 252
}

253 254 255
void torus_collective_init(nodes_state * s,
           		   tw_lp * lp)
{
256 257
    // TODO: be annotation-aware somehow 
    codes_mapping_get_lp_info(lp->gid, grp_name, &mapping_grp_id, NULL, &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
258
    int num_lps = codes_mapping_get_lp_count(grp_name, 1, LP_CONFIG_NM, s->anno, 0);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    int num_reps = codes_mapping_get_group_reps(grp_name);
    s->node_id = (mapping_rep_id * num_lps) + mapping_offset;

    int i;
   /* handle collective operations by forming a tree of all the LPs */
   /* special condition for root of the tree */
   if( s->node_id == 0)
    {
        s->parent_node_id = -1;
        s->is_root = 1;
   }
   else
   {
       s->parent_node_id = (s->node_id - ((s->node_id - 1) % TREE_DEGREE)) / TREE_DEGREE;
       s->is_root = 0;
   }
   s->children = (tw_lpid*)malloc(TREE_DEGREE * sizeof(tw_lpid));

   /* set the isleaf to zero by default */
   s->is_leaf = 1;
   s->num_children = 0;

   /* calculate the children of the current node. If its a leaf, no need to set children,
      only set isleaf and break the loop*/

   for( i = 0; i < TREE_DEGREE; i++ )
    {
        tw_lpid next_child = (TREE_DEGREE * s->node_id) + i + 1;
        if(next_child < (num_lps * num_reps))
        {
            s->num_children++;
            s->is_leaf = 0;
            s->children[i] = next_child;
        }
        else
           s->children[i] = -1;
    }

#if TORUS_COLLECTIVE_DEBUG == 1
   printf("\n LP %ld parent node id ", s->node_id);

   for( i = 0; i < TREE_DEGREE; i++ )
        printf(" child node ID %ld ", s->children[i]);
   printf("\n");

   if(s->is_leaf)
        printf("\n LP %ld is leaf ", s->node_id);
#endif
}
308
/* torus packet reverse event */
309 310 311 312 313 314
static void torus_packet_event_rc(tw_lp *sender)
{
  codes_local_latency_reverse(sender);
  return;
}

315 316 317 318 319 320
/* returns the torus message size */
static int torus_get_msg_sz(void)
{
   return sizeof(nodes_message);
}

321
/* torus packet event , generates a torus packet on the compute node */
322
static tw_stime torus_packet_event(char* category, tw_lpid final_dest_lp, uint64_t packet_size, int is_pull, uint64_t pull_size, tw_stime offset, const mn_sched_params *sched_params, int remote_event_size, const void* remote_event, int self_event_size, const void* self_event, tw_lpid src_lp, tw_lp *sender, int is_last_pckt)
323 324 325 326 327 328
{
    tw_event * e_new;
    tw_stime xfer_to_nic_time;
    nodes_message * msg;
    char* tmp_ptr;
   
329
    xfer_to_nic_time = g_tw_lookahead + codes_local_latency(sender); /* Throws an error of found last KP time > current event time otherwise */
330 331 332 333
    //e_new = tw_event_new(local_nic_id, xfer_to_nic_time+offset, sender);
    //msg = tw_event_data(e_new);
    e_new = model_net_method_event_new(sender->gid, xfer_to_nic_time+offset,
            sender, TORUS, (void**)&msg, (void**)&tmp_ptr);
334 335
    strcpy(msg->category, category);
    msg->final_dest_gid = final_dest_lp;
336
    msg->sender_svr= src_lp;
337 338 339 340
    msg->packet_size = packet_size;
    msg->remote_event_size_bytes = 0;
    msg->local_event_size_bytes = 0;
    msg->type = GENERATE;
341 342
    msg->is_pull = is_pull;
    msg->pull_size = pull_size;
343
    
344 345 346 347 348 349 350 351 352 353 354 355 356 357
    if(is_last_pckt) /* Its the last packet so pass in remote event information*/
     {
	if(remote_event_size > 0)
	 {
	    msg->remote_event_size_bytes = remote_event_size;
	    memcpy(tmp_ptr, remote_event, remote_event_size);
	    tmp_ptr += remote_event_size;
         }
	if(self_event_size > 0)
	{
	   msg->local_event_size_bytes = self_event_size;
	   memcpy(tmp_ptr, self_event, self_event_size);
	   tmp_ptr += self_event_size;
	}
358
      // printf("\n torus remote event %d local event %d last packet %d %lf ", msg->remote_event_size_bytes, msg->local_event_size_bytes, is_last_pckt, xfer_to_nic_time);
359 360
     }
    tw_event_send(e_new);
361
    return xfer_to_nic_time;
362
}
363

364 365 366 367 368
/*Initialize the torus model, this initialization part is borrowed from Ning's torus model */
static void torus_init( nodes_state * s, 
	   tw_lp * lp )
{
    int i, j;
369
    char anno[MAX_NAME_LENGTH];
370

371 372 373 374 375 376
    codes_mapping_get_lp_info(lp->gid, grp_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{
377
        s->anno = strdup(anno);
378 379 380 381
        int id = configuration_get_annotation_index(anno, anno_map);
        s->params = &all_params[id];
    }
    int dim_N[ s->params->n_dims + 1 ];
382

383 384
    // shorthand
    const torus_param *p = s->params;
385

386
    dim_N[ 0 ]=mapping_rep_id + mapping_offset;
387

388 389 390 391 392 393 394 395 396 397 398 399
    s->neighbour_minus_lpID = (int*)malloc(p->n_dims * sizeof(int));
    s->neighbour_plus_lpID = (int*)malloc(p->n_dims * sizeof(int));
    s->dim_position = (int*)malloc(p->n_dims * sizeof(int));
    s->buffer = (int**)malloc(2*p->n_dims * sizeof(int*));
    s->next_link_available_time = 
        (tw_stime**)malloc(2*p->n_dims * sizeof(tw_stime*));
    s->next_credit_available_time = 
        (tw_stime**)malloc(2*p->n_dims * sizeof(tw_stime*));
    s->next_flit_generate_time = 
        (tw_stime**)malloc(2*p->n_dims*sizeof(tw_stime*));

    for(i=0; i < 2*p->n_dims; i++)
400
    {
401 402 403 404 405 406 407
	s->buffer[i] = (int*)malloc(p->num_vc * sizeof(int));
	s->next_link_available_time[i] =
            (tw_stime*)malloc(p->num_vc * sizeof(tw_stime));
	s->next_credit_available_time[i] = 
            (tw_stime*)malloc(p->num_vc * sizeof(tw_stime));
	s->next_flit_generate_time[i] = 
            (tw_stime*)malloc(p->num_vc * sizeof(tw_stime));
408 409 410 411
    }

    //printf("\n LP ID %d ", (int)lp->gid);
  // calculate my torus co-ordinates
412
  for ( i=0; i < p->n_dims; i++ )
413
    {
414
      s->dim_position[i] = dim_N[i]%p->dim_length[i];
415
      //printf(" dim position %d ", s->dim_position[i]);
416
      dim_N[i + 1] = ( dim_N[i] - s->dim_position[i] )/p->dim_length[i];
417 418 419
    }
   //printf("\n");

420 421
  int temp_dim_pos[ p->n_dims ];
  for ( i = 0; i < p->n_dims; i++ )
422 423 424 425
    temp_dim_pos[ i ] = s->dim_position[ i ];

  tw_lpid neighbor_id;
  // calculate minus neighbour's lpID
426
  for ( j = 0; j < p->n_dims; j++ )
427
    {
428 429
      temp_dim_pos[ j ] = (s->dim_position[ j ] -1 + p->dim_length[ j ]) %
          p->dim_length[ j ];
430 431 432

      s->neighbour_minus_lpID[ j ] = 0;
      
433 434
      for ( i = 0; i < p->n_dims; i++ )
        s->neighbour_minus_lpID[ j ] += p->factor[ i ] * temp_dim_pos[ i ];
435
      
436
      codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, s->anno, 0,
437
              s->neighbour_minus_lpID[ j ], 0, &neighbor_id);
438
      //printf("\n minus neighbor %d lp id %d ", (int)s->neighbour_minus_lpID[ j ], (int)neighbor_id);
439 440 441 442
      
      temp_dim_pos[ j ] = s->dim_position[ j ];
    }
  // calculate plus neighbour's lpID
443
  for ( j = 0; j < p->n_dims; j++ )
444
    {
445 446
      temp_dim_pos[ j ] = ( s->dim_position[ j ] + 1 + p->dim_length[ j ]) %
          p->dim_length[ j ];
447 448 449

      s->neighbour_plus_lpID[ j ] = 0;
      
450 451
      for ( i = 0; i < s->params->n_dims; i++ )
        s->neighbour_plus_lpID[ j ] += p->factor[ i ] * temp_dim_pos[ i ];
452

453
      codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, s->anno, 0,
454
              s->neighbour_plus_lpID[ j ], 0, &neighbor_id);
455
      //printf("\n plus neighbor %d lp id %d ", (int)s->neighbour_plus_lpID[ j ], (int)neighbor_id);
456 457 458 459 460
      
      temp_dim_pos[ j ] = s->dim_position[ j ];
    }

  //printf("\n");
461
  for( j=0; j < 2 * p->n_dims; j++ )
462
   {
463
    for( i = 0; i < p->num_vc; i++ )
464 465 466 467 468 469 470 471
     {
       s->buffer[ j ][ i ] = 0; 
       s->next_link_available_time[ j ][ i ] = 0.0;
       s->next_credit_available_time[j][i] = 0.0; 
     }
   }
  // record LP time
    s->packet_counter = 0;
472 473 474 475 476 477 478 479 480 481 482 483 484
    torus_collective_init(s, lp);
}


/* collective operation for the torus network */
void torus_collective(char* category, int message_size, int remote_event_size, const void* remote_event, tw_lp* sender)
{
    tw_event * e_new;
    tw_stime xfer_to_nic_time;
    nodes_message * msg;
    tw_lpid local_nic_id;
    char* tmp_ptr;

485 486 487 488 489
    // TODO: be annotation-aware
    codes_mapping_get_lp_info(sender->gid, grp_name, &mapping_grp_id, NULL,
            &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
    codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
            mapping_rep_id, mapping_offset, &local_nic_id);
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

    xfer_to_nic_time = g_tw_lookahead + codes_local_latency(sender);
    e_new = model_net_method_event_new(local_nic_id, xfer_to_nic_time,
            sender, TORUS, (void**)&msg, (void**)&tmp_ptr);

    msg->remote_event_size_bytes = message_size;
    strcpy(msg->category, category);
    msg->sender_svr=sender->gid;
    msg->type = T_COLLECTIVE_INIT;

    tmp_ptr = (char*)msg;
    tmp_ptr += torus_get_msg_sz();
    if(remote_event_size > 0)
     {
            msg->remote_event_size_bytes = remote_event_size;
            memcpy(tmp_ptr, remote_event, remote_event_size);
            tmp_ptr += remote_event_size;
     }

    tw_event_send(e_new);
    return;
}

/* reverse for collective operation of the dragonfly network */
void torus_collective_rc(int message_size, tw_lp* sender)
{
     codes_local_latency_reverse(sender);
     return;
}

static void send_remote_event(nodes_state * s,
                        tw_bf * bf,
                        nodes_message * msg,
                        tw_lp * lp)
{
    // Trigger an event on receiving server
    if(msg->remote_event_size_bytes)
     {
            tw_event* e;
            tw_stime ts;
            nodes_message * m;
531
            ts = (1/s->params->link_bandwidth) * msg->remote_event_size_bytes;
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
            e = codes_event_new(s->origin_svr, ts, lp);
            m = tw_event_data(e);
            char* tmp_ptr = (char*)msg;
            tmp_ptr += torus_get_msg_sz();
            memcpy(m, tmp_ptr, msg->remote_event_size_bytes);
            tw_event_send(e);
     }
}

static void node_collective_init(nodes_state * s,
                        tw_bf * bf,
                        nodes_message * msg,
                        tw_lp * lp)
{
        tw_event * e_new;
        tw_lpid parent_nic_id;
        tw_stime xfer_to_nic_time;
        nodes_message * msg_new;
        int num_lps;

        msg->saved_collective_init_time = s->collective_init_time;
        s->collective_init_time = tw_now(lp);
	s->origin_svr = msg->sender_svr;
	
        if(s->is_leaf)
        {
            //printf("\n LP %ld sending message to parent %ld ", s->node_id, s->parent_node_id);
            /* get the global LP ID of the parent node */
560 561 562 563 564 565 566 567
            // TODO: be annotation-aware
            codes_mapping_get_lp_info(lp->gid, grp_name, &mapping_grp_id, NULL,
                    &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
            num_lps = codes_mapping_get_lp_count(grp_name, 1, LP_CONFIG_NM,
                    NULL, 1);
            codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
                    s->parent_node_id/num_lps, (s->parent_node_id % num_lps),
                    &parent_nic_id);
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

           /* send a message to the parent that the LP has entered the collective operation */
            xfer_to_nic_time = g_tw_lookahead + LEVEL_DELAY;
            //e_new = codes_event_new(parent_nic_id, xfer_to_nic_time, lp);
	    void* m_data;
	    e_new = model_net_method_event_new(parent_nic_id, xfer_to_nic_time,
            	lp, TORUS, (void**)&msg_new, (void**)&m_data);
	    	
            memcpy(msg_new, msg, sizeof(nodes_message));
	    if (msg->remote_event_size_bytes){
        	memcpy(m_data, model_net_method_get_edata(TORUS, msg),
                	msg->remote_event_size_bytes);
      	    }
	    
            msg_new->type = T_COLLECTIVE_FAN_IN;
            msg_new->sender_node = s->node_id;

            tw_event_send(e_new);
        }
        return;
}

static void node_collective_fan_in(nodes_state * s,
                        tw_bf * bf,
                        nodes_message * msg,
                        tw_lp * lp)
{
        int i;
        s->num_fan_nodes++;

598 599 600 601 602
        // TODO: be annotation-aware
        codes_mapping_get_lp_info(lp->gid, grp_name, &mapping_grp_id, NULL,
                &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
        int num_lps = codes_mapping_get_lp_count(grp_name, 1, LP_CONFIG_NM,
                NULL, 1);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620

        tw_event* e_new;
        nodes_message * msg_new;
        tw_stime xfer_to_nic_time;

        bf->c1 = 0;
        bf->c2 = 0;

        /* if the number of fanned in nodes have completed at the current node then signal the parent */
        if((s->num_fan_nodes == s->num_children) && !s->is_root)
        {
            bf->c1 = 1;
            msg->saved_fan_nodes = s->num_fan_nodes-1;
            s->num_fan_nodes = 0;
            tw_lpid parent_nic_id;
            xfer_to_nic_time = g_tw_lookahead + LEVEL_DELAY;

            /* get the global LP ID of the parent node */
621 622 623
            codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
                    s->parent_node_id/num_lps, (s->parent_node_id % num_lps),
                    &parent_nic_id);
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

           /* send a message to the parent that the LP has entered the collective operation */
            //e_new = codes_event_new(parent_nic_id, xfer_to_nic_time, lp);
            //msg_new = tw_event_data(e_new);
	    void * m_data;
      	    e_new = model_net_method_event_new(parent_nic_id,
              xfer_to_nic_time,
              lp, TORUS, (void**)&msg_new, &m_data);
	    
            memcpy(msg_new, msg, sizeof(nodes_message));
            msg_new->type = T_COLLECTIVE_FAN_IN;
            msg_new->sender_node = s->node_id;

            if (msg->remote_event_size_bytes){
	        memcpy(m_data, model_net_method_get_edata(TORUS, msg),
        	        msg->remote_event_size_bytes);
      	   }
	    
            tw_event_send(e_new);
      }

      /* root node starts off with the fan-out phase */
      if(s->is_root && (s->num_fan_nodes == s->num_children))
      {
           bf->c2 = 1;
           msg->saved_fan_nodes = s->num_fan_nodes-1;
           s->num_fan_nodes = 0;
           send_remote_event(s, bf, msg, lp);

           for( i = 0; i < s->num_children; i++ )
           {
                tw_lpid child_nic_id;
                /* Do some computation and fan out immediate child nodes from the collective */
                xfer_to_nic_time = g_tw_lookahead + COLLECTIVE_COMPUTATION_DELAY + LEVEL_DELAY + tw_rand_exponential(lp->rng, (double)LEVEL_DELAY/50);

                /* get global LP ID of the child node */
660 661 662
                codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
                        s->children[i]/num_lps, (s->children[i] % num_lps),
                        &child_nic_id);
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
                //e_new = codes_event_new(child_nic_id, xfer_to_nic_time, lp);

                //msg_new = tw_event_data(e_new);
                void * m_data;
	        e_new = model_net_method_event_new(child_nic_id,
                xfer_to_nic_time,
		lp, TORUS, (void**)&msg_new, &m_data);

		memcpy(msg_new, msg, sizeof(nodes_message));
	        if (msg->remote_event_size_bytes){
	                memcpy(m_data, model_net_method_get_edata(TORUS, msg),
        	               msg->remote_event_size_bytes);
      		}
		
                msg_new->type = T_COLLECTIVE_FAN_OUT;
                msg_new->sender_node = s->node_id;

                tw_event_send(e_new);
           }
      }
683
}
684 685 686 687 688 689 690
	     
static void node_collective_fan_out(nodes_state * s,
                        tw_bf * bf,
                        nodes_message * msg,
                        tw_lp * lp)
{
        int i;
691 692 693
        //TODO: be annotation-aware
        int num_lps = codes_mapping_get_lp_count(grp_name, 1, LP_CONFIG_NM,
                NULL, 1);
694 695 696 697
        bf->c1 = 0;
        bf->c2 = 0;

        send_remote_event(s, bf, msg, lp);
698

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
        if(!s->is_leaf)
        {
            bf->c1 = 1;
            tw_event* e_new;
            nodes_message * msg_new;
            tw_stime xfer_to_nic_time;

           for( i = 0; i < s->num_children; i++ )
           {
                xfer_to_nic_time = g_tw_lookahead + TORUS_FAN_OUT_DELAY + tw_rand_exponential(lp->rng, (double)TORUS_FAN_OUT_DELAY/10);

                if(s->children[i] > 0)
                {
                        tw_lpid child_nic_id;

                        /* get global LP ID of the child node */
715 716 717
                        codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1,
                                s->children[i]/num_lps,
                                (s->children[i] % num_lps), &child_nic_id);
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
                        //e_new = codes_event_new(child_nic_id, xfer_to_nic_time, lp);
                        //msg_new = tw_event_data(e_new);
                        //memcpy(msg_new, msg, sizeof(nodes_message) + msg->remote_event_size_bytes);
			void* m_data;
			e_new = model_net_method_event_new(child_nic_id,
							xfer_to_nic_time,
					                lp, TORUS, (void**)&msg_new, &m_data);
		        memcpy(msg_new, msg, sizeof(nodes_message));
		        if (msg->remote_event_size_bytes){
			        memcpy(m_data, model_net_method_get_edata(TORUS, msg),
			                msg->remote_event_size_bytes);
      			}


                        msg_new->type = T_COLLECTIVE_FAN_OUT;
                        msg_new->sender_node = s->node_id;
                        tw_event_send(e_new);
                }
           }
         }
	//printf("\n Fan out phase completed %ld ", lp->gid);
        if(max_collective < tw_now(lp) - s->collective_init_time )
          {
              bf->c2 = 1;
              max_collective = tw_now(lp) - s->collective_init_time;
          }
}
    
746 747 748 749 750 751
/*Returns the next neighbor to which the packet should be routed by using DOR (Taken from Ning's code of the torus model)*/
static void dimension_order_routing( nodes_state * s,
			     tw_lpid * dst_lp, 
			     int * dim, 
			     int * dir )
{
752 753
  int dim_N[s->params->n_dims], 
      dest[s->params->n_dims],
754 755 756
      i,
      dest_id=0;

757 758 759 760
  /* dummys - check later */
  *dim = -1;
  *dir = -1;

761 762
  //TODO: be annotation-aware
  codes_mapping_get_lp_info(*dst_lp, grp_name, &mapping_grp_id, NULL, &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
763
  dim_N[ 0 ]=mapping_rep_id + mapping_offset;
764 765

  // find destination dimensions using destination LP ID 
766
  for ( i = 0; i < s->params->n_dims; i++ )
767
    {
768 769
      dest[ i ] = dim_N[ i ] % s->params->dim_length[ i ];
      dim_N[ i + 1 ] = ( dim_N[ i ] - dest[ i ] ) / s->params->dim_length[ i ];
770 771
    }

772
  for( i = 0; i < s->params->n_dims; i++ )
773
    {
774
      if ( s->dim_position[ i ] - dest[ i ] > s->params->half_length[ i ] )
775 776 777 778 779 780
	{
	  dest_id = s->neighbour_plus_lpID[ i ];
	  *dim = i;
	  *dir = 1;
	  break;
	}
781
      if ( s->dim_position[ i ] - dest[ i ] < -s->params->half_length[ i ] )
782 783 784 785 786 787
	{
	  dest_id = s->neighbour_minus_lpID[ i ];
	  *dim = i;
	  *dir = 0;
	  break;
	}
788 789
      if ( ( s->dim_position[i] - dest[i] <= s->params->half_length[i] ) &&
              ( s->dim_position[ i ] - dest[ i ] > 0 ) )
790 791 792 793 794 795
	{
	  dest_id = s->neighbour_minus_lpID[ i ];
	  *dim = i;
	  *dir = 0;
	  break;
	}
796 797
      if (( s->dim_position[i] - dest[i] >= -s->params->half_length[i] ) &&
              ( s->dim_position[ i ] - dest[ i ] < 0) )
798 799 800 801 802 803 804
	{
	  dest_id = s->neighbour_plus_lpID[ i ];
	  *dim = i;
	  *dir = 1;
	  break;
	}
    }
805 806

  assert(*dim != -1 && *dir != -1);
807
  codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, NULL, 1, dest_id, 0, dst_lp);
808
}
809 810 811 812 813 814

/*Generates a packet. If there is a buffer slot available, then the packet is 
injected in the network. Else, a buffer overflow exception is thrown.
TODO: We might want to modify this so that if the buffer is full, the packet
injection is delayed in turn slowing down the injection rate. The average achieved
injection rate can be reported at the end of the simulation. */
815 816 817 818 819 820
static void packet_generate( nodes_state * s, 
		tw_bf * bf, 
		nodes_message * msg, 
		tw_lp * lp )
{
//    printf("\n msg local event size %d remote event size %d ", msg->local_event_size_bytes, msg->remote_event_size_bytes);
821
    int j, tmp_dir=-1, tmp_dim=-1, total_event_size;
822 823 824 825 826 827
    tw_stime ts;

//    event triggered when packet head is sent
    tw_event * e_h;
    nodes_message *m;

828 829 830
    int mapping_grp_id, mapping_rep_id, mapping_type_id, mapping_offset;
    tw_lpid dst_lp;
    // TODO: be annotation-aware
831 832 833
    dst_lp = model_net_find_local_device(TORUS, s->anno, 0,
            msg->final_dest_gid);
            //mapping_offset, &dst_lp);
834 835 836 837
    // dest_lp gets included to other required msgs through memcpys, so just
    // set here
    msg->dest_lp = dst_lp;

838 839 840 841 842 843 844 845
    dimension_order_routing( s, &dst_lp, &tmp_dim, &tmp_dir );

    msg->saved_src_dim = tmp_dim;
    msg->saved_src_dir = tmp_dir;

    //msg->saved_available_time = s->next_flit_generate_time[(2*tmp_dim) + tmp_dir][0];
    msg->travel_start_time = tw_now(lp);
    msg->packet_ID = lp->gid + g_tw_nlp * s->packet_counter;
846
    msg->my_N_hop = 0;
847

848 849 850 851
    uint64_t num_chunks = msg->packet_size/s->params->chunk_size;
    if(msg->packet_size % s->params->chunk_size)
        num_chunks++;

852

853 854 855 856 857 858
    s->packet_counter++;

    if(msg->packet_ID == TRACE)
	    printf("\n packet generated %lld at lp %d dest %d final dest %d", msg->packet_ID, (int)lp->gid, (int)dst_lp, (int)msg->dest_lp);
    for(j = 0; j < num_chunks; j++)
    { 
859
     if(s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ] < s->params->buffer_size)
860 861 862 863 864
      {
       ts = j + tw_rand_exponential(lp->rng, MEAN_INTERVAL/200);
       //s->next_flit_generate_time[(2*tmp_dim) + tmp_dir][0] = max(s->next_flit_generate_time[(2*tmp_dim) + tmp_dir][0], tw_now(lp));
       //s->next_flit_generate_time[(2*tmp_dim) + tmp_dir][0] += ts;
       //e_h = tw_event_new( lp->gid, s->next_flit_generate_time[(2*tmp_dim) + tmp_dir][0] - tw_now(lp), lp);
865
       //e_h = tw_event_new(lp->gid, ts, lp);
866 867 868
       msg->source_direction = tmp_dir;
       msg->source_dim = tmp_dim;

869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
       void *m_data;
       e_h = model_net_method_event_new(lp->gid, ts, lp, TORUS, (void**)&m,
               (void**)&m_data);

       //m = tw_event_data( e_h );
       //memcpy(m, msg, torus_get_msg_sz() + msg->local_event_size_bytes + msg->remote_event_size_bytes);
       void *m_data_src = model_net_method_get_edata(TORUS, msg);
       memcpy(m, msg, sizeof(nodes_message));
       if (msg->remote_event_size_bytes){
           memcpy(m_data, m_data_src,
                   msg->remote_event_size_bytes);
           m_data = (char*)m_data + msg->remote_event_size_bytes;
           m_data_src = (char*)m_data_src + msg->remote_event_size_bytes;
       }
       if (msg->local_event_size_bytes){
           memcpy(m_data, m_data_src, msg->local_event_size_bytes);
       }
886 887 888 889 890 891 892 893 894 895 896 897
       m->next_stop = dst_lp;
       m->chunk_id = j;

      // find destination dimensions using destination LP ID 
       m->type = SEND;
       m->source_direction = tmp_dir;
       m->source_dim = tmp_dim;
       tw_event_send(e_h);
      }
      else 
       {
   printf("\n %d Packet queued in line increase buffer space, dir %d dim %d buffer space %d dest LP %d ", (int)lp->gid, tmp_dir, tmp_dim, s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ], (int)msg->dest_lp);
898 899
       MPI_Finalize();
       exit(-1); 
900 901
       }
   }
902

903
   total_event_size = model_net_get_msg_sz(TORUS) + msg->remote_event_size_bytes + msg->local_event_size_bytes;   
904 905 906 907 908
   /* record the statistics of the generated packets */
   mn_stats* stat;
   stat = model_net_find_stats(msg->category, s->torus_stats_array);
   stat->send_count++;  
   stat->send_bytes += msg->packet_size;
909
   stat->send_time += (1/s->params->link_bandwidth) * msg->packet_size;
910 911 912
   /* record the maximum ROSS event size */
   if(stat->max_event_size < total_event_size)
	   stat->max_event_size = total_event_size;
913 914 915 916 917 918 919 920
}
/*Sends a 8-byte credit back to the torus node LP that sent the message */
static void credit_send( nodes_state * s, 
	    tw_bf * bf, 
	    tw_lp * lp, 
	    nodes_message * msg)
{
#if DEBUG
921
    //printf("\n (%lf) sending credit tmp_dir %d tmp_dim %d %lf ", tw_now(lp), msg->source_direction, msg->source_dim, s->params->credit_delay );
922 923 924 925 926 927 928 929 930
#endif
    bf->c1 = 0;
    tw_event * buf_e;
    nodes_message *m;
    tw_stime ts;
    int src_dir = msg->source_direction;
    int src_dim = msg->source_dim;

    msg->saved_available_time = s->next_credit_available_time[(2 * src_dim) + src_dir][0];
931
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] = maxd(s->next_credit_available_time[(2 * src_dim) + src_dir][0], tw_now(lp));
932 933
    ts =  s->params->credit_delay + 
        tw_rand_exponential(lp->rng, s->params->credit_delay/1000);
934 935
    s->next_credit_available_time[(2 * src_dim) + src_dir][0] += ts;

936 937
    //buf_e = tw_event_new( msg->sender_lp, s->next_credit_available_time[(2 * src_dim) + src_dir][0] - tw_now(lp), lp);
    //m = tw_event_data(buf_e);
938
    buf_e = model_net_method_event_new(msg->sender_node,
939 940
            s->next_credit_available_time[(2*src_dim) + src_dir][0] - tw_now(lp),
            lp, TORUS, (void**)&m, NULL);
941 942 943 944 945 946
    m->source_direction = msg->source_direction;
    m->source_dim = msg->source_dim;

    m->type = CREDIT;
    tw_event_send( buf_e );
}
947 948
/* send a packet from one torus node to another torus node
 A packet can be up to 256 bytes on BG/L and BG/P and up to 512 bytes on BG/Q */
949 950 951 952 953 954 955 956 957 958 959 960 961 962
static void packet_send( nodes_state * s, 
	         tw_bf * bf, 
		 nodes_message * msg, 
		 tw_lp * lp )
{ 
    bf->c2 = 0;
    bf->c1 = 0;
    int tmp_dir, tmp_dim;
    tw_stime ts;
    tw_event *e;
    nodes_message *m;
    tw_lpid dst_lp = msg->dest_lp;
    dimension_order_routing( s, &dst_lp, &tmp_dim, &tmp_dir );     

963
    if(s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ] < s->params->buffer_size)
964 965 966 967
    {
       bf->c2 = 1;
       msg->saved_src_dir = tmp_dir;
       msg->saved_src_dim = tmp_dim;
968 969
       ts = tw_rand_exponential( lp->rng, s->params->head_delay/200.0 ) + 
           s->params->head_delay;
970 971 972 973

//    For reverse computation 
      msg->saved_available_time = s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0];

974
      s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] = maxd( s->next_link_available_time[ tmp_dir + ( tmp_dim * 2 )][0], tw_now(lp) );
975 976
      s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] += ts;
    
977 978 979 980 981 982 983 984 985 986 987 988
      //e = tw_event_new( dst_lp, s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] - tw_now(lp), lp );
      //m = tw_event_data( e );
      //memcpy(m, msg, torus_get_msg_sz() + msg->remote_event_size_bytes);
      void * m_data;
      e = model_net_method_event_new(dst_lp, 
              s->next_link_available_time[tmp_dir+(tmp_dim*2)][0] - tw_now(lp),
              lp, TORUS, (void**)&m, &m_data);
      memcpy(m, msg, sizeof(nodes_message));
      if (msg->remote_event_size_bytes){
        memcpy(m_data, model_net_method_get_edata(TORUS, msg),
                msg->remote_event_size_bytes);
      }
989 990 991 992 993 994 995 996
      m->type = ARRIVAL;

      if(msg->packet_ID == TRACE)
        printf("\n lp %d packet %lld flit id %d being sent to %d after time %lf ", (int) lp->gid, msg->packet_ID, msg->chunk_id, (int)dst_lp, s->next_link_available_time[tmp_dir + ( tmp_dim * 2 )][0] - tw_now(lp)); 
      //Carry on the message info
      m->source_dim = tmp_dim;
      m->source_direction = tmp_dir;
      m->next_stop = dst_lp;
997
      m->sender_node = lp->gid;
998 999 1000 1001 1002 1003
      m->local_event_size_bytes = 0; /* We just deliver the local event here */

      tw_event_send( e );

      s->buffer[ tmp_dir + ( tmp_dim * 2 ) ][ 0 ]++;
    
1004 1005 1006 1007 1008
      uint64_t num_chunks = msg->packet_size/s->params->chunk_size;

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

1009 1010 1011
      if(msg->chunk_id == num_chunks - 1)
      {
        bf->c1 = 1;
1012
	/* Invoke an event on the sending server */
1013 1014 1015 1016
	if(msg->local_event_size_bytes > 0)
	{
          tw_event* e_new;
	  nodes_message* m_new;
1017
	  void* local_event;
1018
	  ts = (1/s->params->link_bandwidth) * msg->local_event_size_bytes;
1019
	  e_new = tw_event_new(msg->sender_svr, ts, lp);
1020
	  m_new = tw_event_data(e_new);
1021 1022 1023 1024
	  //local_event = (char*)msg;
	  //local_event += torus_get_msg_sz() + msg->remote_event_size_bytes;
          local_event = (char*)model_net_method_get_edata(TORUS, msg) +
              msg->remote_event_size_bytes;
1025 1026 1027 1028 1029 1030
	  memcpy(m_new, local_event, msg->local_event_size_bytes);
	  tw_event_send(e_new);
	}
     }
  } // end if
    else
1031
    {
1032
	    printf("\n buffer overflown ");
1033 1034 1035
	    MPI_Finalize();
	    exit(-1);
    }
1036 1037
}

1038 1039 1040
/*Processes the packet after it arrives from the neighboring torus node 
 * routes it to the next compute node if this is not the destination
 * OR if this is the destination then a remote event at the server is issued. */
1041 1042 1043 1044 1045 1046 1047 1048 1049
static void packet_arrive( nodes_state * s, 
		    tw_bf * bf, 
		    nodes_message * msg, 
		    tw_lp * lp )
{
  bf->c2 = 0;
  tw_event *e;
  tw_stime ts;
  nodes_message *m;
1050
  mn_stats* stat;
1051

1052
  credit_send( s, bf, lp, msg); 
1053 1054 1055 1056 1057 1058 1059
  
  msg->my_N_hop++;
  ts = 0.1 + tw_rand_exponential(lp->rng, MEAN_INTERVAL/200);
  if(msg->packet_ID == TRACE)
	  printf("\n packet arrived at lp %d final dest %d ", (int)lp->gid, (int)msg->dest_lp);
  if( lp->gid == msg->dest_lp )
    {   
1060 1061 1062 1063
        uint64_t num_chunks = msg->packet_size/s->params->chunk_size;
        if(msg->packet_size % s->params->chunk_size)
            num_chunks++;

1064 1065 1066
        if( msg->chunk_id == num_chunks - 1 )    
        {
	    bf->c2 = 1;
1067 1068 1069 1070 1071 1072
	    stat = model_net_find_stats(msg->category, s->torus_stats_array);
	    stat->recv_count++;
	    stat->recv_bytes += msg->packet_size;
	    stat->recv_time += tw_now( lp ) - msg->travel_start_time;

	    /*count the number of packets completed overall*/
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	    N_finished_packets++;
	    total_time += tw_now( lp ) - msg->travel_start_time;
	    total_hops += msg->my_N_hop;

	    if (max_latency < tw_now( lp ) - msg->travel_start_time) {
		  bf->c3 = 1;
		  msg->saved_available_time = max_latency;
	          max_latency=tw_now( lp ) - msg->travel_start_time;
     		}
	    // Trigger an event on receiving server
	    if(msg->remote_event_size_bytes)
	    {
1085
               void *tmp_ptr = model_net_method_get_edata(TORUS, msg);
1086
               if (msg->is_pull){
1087
                   int net_id = model_net_get_id(LP_METHOD_NM);
1088
                   model_net_event(net_id, msg->category, msg->sender_svr,
1089
                           msg->pull_size, 0.0, msg->remote_event_size_bytes,
1090 1091 1092 1093 1094 1095 1096 1097
                           tmp_ptr, 0, NULL, lp);
               }
               else{
                   e = tw_event_new(msg->final_dest_gid, ts, lp);
                   m = tw_event_data(e);
                   memcpy(m, tmp_ptr, msg->remote_event_size_bytes);
                   tw_event_send(e);
               }
1098 1099 1100 1101 1102
	    }
       }
    }
  else
    {
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
      //e = tw_event_new(lp->gid, ts , lp);
      //m = tw_event_data( e );
      //memcpy(m, msg, torus_get_msg_sz() + msg->remote_event_size_bytes);
      void *m_data;
      e = model_net_method_event_new(lp->gid, ts, lp, TORUS, (void**)&m,
              &m_data);
      memcpy(m, msg, sizeof(nodes_message));
      if (msg->remote_event_size_bytes){
        memcpy(m_data, model_net_method_get_edata(TORUS, msg),
                msg->remote_event_size_bytes);
      }
1114 1115 1116 1117 1118 1119
      m->type = SEND;
      m->next_stop = -1;
      tw_event_send(e);
   }
}

1120 1121
/* reports torus statistics like average packet latency, maximum packet latency and average
 * number of torus hops traversed by the packet */
1122 1123
static void torus_report_stats()
{
1124
    long long avg_hops, total_finished_packets;
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136
    tw_stime avg_time, max_time;

    MPI_Reduce( &total_hops, &avg_hops, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
    MPI_Reduce( &N_finished_packets, &total_finished_packets, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
    MPI_Reduce( &total_time, &avg_time, 1,MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
    MPI_Reduce( &max_latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

    if(!g_tw_mynode)
     {
       printf(" Average number of hops traversed %f average message latency %lf us maximum message latency %lf us \n", (float)avg_hops/total_finished_packets, avg_time/(total_finished_packets*1000), max_time/1000);
     }
}
1137
/* finalize the torus node and free all event buffers available */
1138 1139 1140
void
final( nodes_state * s, tw_lp * lp )
{
1141
  model_net_print_stats(lp->gid, &s->torus_stats_array[0]); 
1142 1143 1144
  free(s->next_link_available_time);
  free(s->next_credit_available_time);
  free(s->next_flit_generate_time);
1145 1146 1147 1148 1149 1150
  // since all LPs are sharing params, just let them leak for now
  // TODO: add a post-sim "cleanup" function?
  //free(s->buffer); 
  //free(s->params->dim_length);
  //free(s->params->factor);
  //free(s->params->half_length);
1151 1152
}

1153
/* increments the buffer count after a credit arrives from the remote compute node */
1154 1155 1156 1157
static void packet_buffer_process( nodes_state * s, tw_bf * bf, nodes_message * msg, tw_lp * lp )
{
   s->buffer[ msg->source_direction + ( msg->source_dim * 2 ) ][  0 ]--;
}
1158

1159
/* reverse handler for torus node */
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
static void node_rc_handler(nodes_state * s, tw_bf * bf, nodes_message * msg, tw_lp * lp)
{
  switch(msg->type)
    {
       case GENERATE:
		   {
		     s->packet_counter--;
		     int i;//, saved_dim, saved_dir;
	 	     //saved_dim = msg->saved_src_dim;
		     //saved_dir = msg->saved_src_dir;

1171 1172 1173 1174
                     uint64_t num_chunks = msg->packet_size/s->params->chunk_size;
                     if(msg->packet_size % s->params->chunk_size)
                         num_chunks++;

1175 1176
		     //s->next_flit_generate_time[(saved_dim * 2) + saved_dir][0] = msg->saved_available_time;
		     for(i=0; i < num_chunks; i++)
1177 1178 1179 1180 1181
  		        tw_rand_reverse_unif(lp->rng);
	     	     mn_stats* stat;
		     stat = model_net_find_stats(msg->category, s->torus_stats_array);
		     stat->send_count--; 
		     stat->send_bytes -= msg->packet_size;
1182
		     stat->send_time -= (1/s->params->link_bandwidth) * msg->packet_size;
1183 1184 1185 1186 1187 1188 1189 1190 1191
		   }
	break;
	
	case ARRIVAL:
		   {
  		    tw_rand_reverse_unif(lp->rng);
		    tw_rand_reverse_unif(lp->rng);
		    int next_dim = msg->source_dim;
		    int next_dir = msg->source_direction;
1192 1193 1194
                    uint64_t num_chunks = msg->packet_size/s->params->chunk_size;
                    if(msg->packet_size % s->params->chunk_size)
                        num_chunks++;
1195 1196 1197 1198

		    s->next_credit_available_time[next_dir + ( next_dim * 2 )][0] = msg->saved_available_time;
		    if(bf->c2)
		    {
1199 1200 1201 1202 1203
		       struct mn_stats* stat;
		       stat = model_net_find_stats(msg->category, s->torus_stats_array);
		       stat->recv_count--;
		       stat->recv_bytes -= msg->packet_size;
		        stat->recv_time -= tw_now(lp) - msg->travel_start_time;	    
1204 1205 1206 1207
		       N_finished_packets--;
		       total_time -= tw_now( lp ) - msg->travel_start_time;
		       total_hops -= msg->my_N_hop;
		    }
1208
 		    msg->my_N_hop--;
1209 1210 1211
                    if (lp->gid == msg->dest_lp && 
                            msg->chunk_id == num_chunks-1 &&
                            msg->remote_event_size_bytes && msg->is_pull){
1212
                        int net_id = model_net_get_id(LP_METHOD_NM);
1213 1214
                        model_net_event_rc(net_id, lp, msg->pull_size);
                    }
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
		   }
	break;	

	case SEND:
		 {
		    if(bf->c2)
		     {
                        int next_dim = msg->saved_src_dim;
			int next_dir = msg->saved_src_dir;
			s->next_link_available_time[next_dir + ( next_dim * 2 )][0] = msg->saved_available_time;
			s->buffer[ next_dir + ( next_dim * 2 ) ][ 0 ] --;
	                tw_rand_reverse_unif(lp->rng);
		    }
		 }
	break;

       case CREDIT:
		{
		  s->buffer[ msg->source_direction + ( msg->source_dim * 2 ) ][  0 ]++;
              }
       break;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	
       case T_COLLECTIVE_INIT:
                {
                    s->collective_init_time = msg->saved_collective_init_time;
                }
        break;

        case T_COLLECTIVE_FAN_IN:
                {
                   int i;
                   s->num_fan_nodes--;
                   if(bf->c1)
                    {
                        s->num_fan_nodes = msg->saved_fan_nodes;
                    }
                   if(bf->c2)
                     {
                        s->num_fan_nodes = msg->saved_fan_nodes;
                        for( i = 0; i < s->num_children; i++ )
                            tw_rand_reverse_unif(lp->rng);
                     }
                }
        break;

        case T_COLLECTIVE_FAN_OUT:
                {
                 int i;
                 if(bf->c1)
                    {
                        for( i = 0; i < s->num_children; i++ )
                            tw_rand_reverse_unif(lp->rng);
                    }
                }
        break;      
1270 1271 1272
     }
}

1273
/* forward event handler for torus node event */
1274 1275 1276 1277 1278 1279 1280 1281
static void event_handler(nodes_state * s, tw_bf * bf, nodes_message * msg, tw_lp * lp)
{
 *(int *) bf = (int) 0;
 switch(msg->type)
 {
  case GENERATE:
    packet_generate(s,bf,msg,lp);
  break;
1282

1283 1284 1285
  case ARRIVAL:
    packet_arrive(s,bf,msg,lp);
  break;
1286

1287 1288 1289
  case SEND:
   packet_send(s,bf,msg,lp);
  break;
1290

1291 1292 1293
  case CREDIT:
    packet_buffer_process(s,bf,msg,lp);
   break;
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

  case T_COLLECTIVE_INIT:
    node_collective_init(s, bf, msg, lp);
  break;

  case T_COLLECTIVE_FAN_IN:
    node_collective_fan_in(s, bf, msg, lp);
  break;

  case T_COLLECTIVE_FAN_OUT:
    node_collective_fan_out(s, bf, msg, lp);
  break;
 
1307
  default:
1308
	printf("\n Being sent to wrong LP %d", msg->type);
1309 1310 1311
  break;
 }
}
1312
/* event types */
1313 1314
tw_lptype torus_lp =
{
1315 1316 1317 1318 1319 1320 1321
    (init_f) torus_init,
    (pre_run_f) NULL,
    (event_f) event_handler,
    (revent_f) node_rc_handler,
    (final_f) final,
    (map_f) codes_mapping,
    sizeof(nodes_state),
1322 1323
};

1324
/* returns the torus lp type for lp registration */
1325 1326 1327 1328 1329
static const tw_lptype* torus_get_lp_type(void)
{
   return(&torus_lp); 
}

1330 1331 1332 1333
static tw_lpid torus_find_local_device(
        const char * annotation,
        int          ignore_annotations,
        tw_lp *sender)
1334 1335 1336
{
     tw_lpid dest_id;

1337 1338 1339
     //TODO: be annotation-aware
     codes_mapping_get_lp_info(sender->gid, grp_name, &mapping_grp_id, NULL,
             &mapping_type_id, NULL, &mapping_rep_id, &mapping_offset);
1340 1341
     codes_mapping_get_lp_id(grp_name, LP_CONFIG_NM, annotation,
             ignore_annotations, mapping_rep_id, mapping_offset, &dest_id);
1342 1343 1344 1345

    return(dest_id);
}

1346 1347 1348
/* data structure for torus statistics */
struct model_net_method torus_method =
{
1349
   .mn_configure = torus_configure,
1350
   .mn_register = NULL,
1351 1352
   .model_net_method_packet_event = torus_packet_event,
   .model_net_method_packet_event_rc = torus_packet_event_rc,
1353 1354
   .model_net_method_recv_msg_event = NULL,
   .model_net_method_recv_msg_event_rc = NULL,
1355 1356 1357
   .mn_get_lp_type = torus_get_lp_type,
   .mn_get_msg_sz = torus_get_msg_sz,
   .mn_report_stats = torus_report_stats,
1358
   .model_net_method_find_local_device = NULL,
1359 1360
   .mn_collective_call = torus_collective,
   .mn_collective_call_rc = torus_collective_rc
1361
};
Philip Carns's avatar
Philip Carns committed
1362 1363 1364 1365 1366 1367 1368 1369 1370

/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ts=8 sts=4 sw=4 expandtab
 */