model-net-synthetic-slimfly.c 39.6 KB
Newer Older
Noah Wolfe's avatar
Noah Wolfe committed
1 2 3 4 5 6 7
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

/*
8 9 10
 * The test program generates some synthetic traffic patterns for the slim fly network models.
 * currently it only supports uniform random and worst-case traffic patterns.
 */
Noah Wolfe's avatar
Noah Wolfe committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

#include "codes/model-net.h"
#include "codes/lp-io.h"
#include "codes/codes.h"
#include "codes/codes_mapping.h"
#include "codes/configuration.h"
#include "codes/lp-type-lookup.h"

#define LP_CONFIG_NM (model_net_lp_config_names[SLIMFLY])

#define PRINT_WORST_CASE_MATCH 0

FILE * slimfly_results_log_2=NULL;
FILE * slimfly_ross_csv_log=NULL;

static int net_id = 0;
static int offset = 2;
static int traffic = 1;
29
static double arrival_time = 0;
Noah Wolfe's avatar
Noah Wolfe committed
30 31
static double load = 0.0;				//percent utilization of each terminal's uplink when sending messages
static double MEAN_INTERVAL = 0.0;
32 33 34 35 36
static int payload_size = 0;
static double link_bandwidth = 0.0;
static int *worst_dest;						//Array mapping worst case destination for each router
static int num_terminals;
static int total_routers;
Noah Wolfe's avatar
Noah Wolfe committed
37

38 39 40 41
static char lp_io_dir[356] = {'\0'};
static lp_io_handle io_handle;
static unsigned int lp_io_use_suffix = 0;
static int do_lp_io = 0;
42
static int num_msgs = 20;
43

Noah Wolfe's avatar
Noah Wolfe committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static int num_servers_per_rep = 0;
static int num_routers_per_grp = 0;
static int num_nodes_per_grp = 0;

static int num_groups = 0;
static int num_nodes = 0;

typedef struct svr_msg svr_msg;
typedef struct svr_state svr_state;

/* global variables for codes mapping */
static char group_name[MAX_NAME_LENGTH];
static char lp_type_name[MAX_NAME_LENGTH];
static int group_index, lp_type_index, rep_id, offset;

59 60 61 62 63 64 65 66 67 68 69 70 71
/* 2D rank communication heat map */
static int **comm_map;

/* Function for computing local and global connections for a given router id */
static void get_router_connections(int src_router_id, int* local_channels, int* global_channels);

/* Global variables for worst-case workload pairing */
static int num_local_channels;
static int num_global_channels;
int *X;
int *X_prime;
int X_size;

Noah Wolfe's avatar
Noah Wolfe committed
72 73 74 75 76 77 78 79 80 81 82
/* type of events */
enum svr_event
{
    KICKOFF,	   /* kickoff event */
    REMOTE,        /* remote event */
    LOCAL      /* local event */
};

/* type of synthetic traffic */
enum TRAFFIC
{
83 84 85 86 87 88 89 90
    UNIFORM             = 1, /* sends message to a randomly selected node */
    WORST_CASE          = 2, /* sends messages in pairs configured to utilize same worst-case hop router */
    NEAREST_NEIGHBOR_1D = 3, /* sends messages to next and previous nodes */
    NEAREST_NEIGHBOR_2D = 4, /* sends messages to nearest neighbor nodes following 2D node mapping */
    NEAREST_NEIGHBOR_3D = 5, /* sends messages to nearest neighbor nodes following 3D node mapping */
    GATHER              = 6, /* sends messages from all ranks to rank 0 */
    SCATTER             = 7, /* sends messages from rank 0 to all other ranks */
    BISECTION           = 8  /* sends messages between paired nodes in opposing bisection partitions */
Noah Wolfe's avatar
Noah Wolfe committed
91 92 93 94 95 96 97 98 99
};

struct svr_state
{
    int msg_sent_count;   /* requests sent */
    int msg_recvd_count;  /* requests recvd */
    int local_recvd_count; /* number of local messages received */
    tw_stime start_ts;    /* time that we started sending requests */
    tw_stime end_ts;      /* time that we ended sending requests */
100 101
    char output_buf[512]; /* buffer space for lp-io output data */
    char output_buf2[65536]; /* buffer space for lp-io output data */
Noah Wolfe's avatar
Noah Wolfe committed
102 103 104 105 106 107 108
};

struct svr_msg
{
    enum svr_event svr_event_type;
    tw_lpid src;          /* source of this request or ack */
    int incremented_flag; /* helper for reverse computation */
109
    model_net_event_return event_rc;
Noah Wolfe's avatar
Noah Wolfe committed
110 111 112
};

static void svr_init(
113 114
        svr_state * ns,
        tw_lp * lp);
Noah Wolfe's avatar
Noah Wolfe committed
115
static void svr_event(
116 117 118 119
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp);
Noah Wolfe's avatar
Noah Wolfe committed
120
static void svr_rev_event(
121 122 123 124
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp);
Noah Wolfe's avatar
Noah Wolfe committed
125
static void svr_finalize(
126 127
        svr_state * ns,
        tw_lp * lp);
Noah Wolfe's avatar
Noah Wolfe committed
128 129 130 131 132 133

tw_lptype svr_lp = {
    (init_f) svr_init,
    (pre_run_f) NULL,
    (event_f) svr_event,
    (revent_f) svr_rev_event,
134
    (commit_f) NULL,
Noah Wolfe's avatar
Noah Wolfe committed
135 136 137 138 139
    (final_f)  svr_finalize,
    (map_f) codes_mapping,
    sizeof(svr_state),
};

140
/* setup for the ROSS event tracing
141 142
 * can have a different function for  rbev_trace_f and ev_trace_f
 * but right now it is set to the same function for both
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
 */
void svr_event_collect(svr_msg *m, tw_lp *lp, char *buffer, int *collect_flag)
{
    (void)lp;
    (void)collect_flag;
    int type = (int) m->svr_event_type;
    memcpy(buffer, &type, sizeof(type));
}

/* can add in any model level data to be collected along with simulation engine data
 * in the ROSS instrumentation.  Will need to update the last field in 
 * svr_model_types[0] for the size of the data to save in each function call
 */
void svr_model_stat_collect(svr_state *s, tw_lp *lp, char *buffer)
{
    (void)s;
    (void)lp;
    (void)buffer;
    return;
}

st_model_types svr_model_types[] = {
165 166 167 168 169 170 171
        {(ev_trace_f) svr_event_collect,
        sizeof(int),
        (model_stat_f) svr_model_stat_collect,
        0,
        NULL,
        NULL,
        0},
172
    {NULL, 0, NULL, 0, NULL, NULL, 0}
173 174 175 176 177 178 179 180 181
};

static const st_model_types  *svr_get_model_stat_types(void)
{
    return(&svr_model_types[0]);
}

void svr_register_model_types()
{
Neil McGlohon's avatar
Neil McGlohon committed
182
    st_model_type_register("nw-lp", svr_get_model_stat_types());
183 184
}

Noah Wolfe's avatar
Noah Wolfe committed
185 186
const tw_optdef app_opt [] =
{
187 188 189 190 191 192 193 194 195
    TWOPT_GROUP("Model net synthetic traffic " ),
    TWOPT_UINT("traffic", traffic, "1: Uniform Random, 2: Worst-Case, 3: 1D Nearest Neighbor, 4: 2D Nearest Neighbor, 5: 3D Nearest Neighbor, 6: Gather, 7: Scatter, 8: Bisection"),
    TWOPT_UINT("num_messages", num_msgs, "Number of messages to be generated per terminal "),
    TWOPT_UINT("payload_size", payload_size, "Size of data to be sent per terminal "),
    TWOPT_STIME("arrival_time", arrival_time, "INTER-ARRIVAL TIME"),
    TWOPT_STIME("load", load, "percentage of packet inter-arrival rate to simulate"), 
    TWOPT_CHAR("lp-io-dir", lp_io_dir, "Where to place io output (unspecified -> no output"),
    TWOPT_UINT("lp-io-use-suffix", lp_io_use_suffix, "Whether to append uniq suffix to lp-io directory (default 0)"),
    TWOPT_END(),
Noah Wolfe's avatar
Noah Wolfe committed
196 197 198 199
};

const tw_lptype* svr_get_lp_type()
{
200
    return(&svr_lp);
Noah Wolfe's avatar
Noah Wolfe committed
201 202 203 204
}

static void svr_add_lp_type()
{
Neil McGlohon's avatar
Neil McGlohon committed
205
    lp_type_register("nw-lp", svr_get_lp_type());
Noah Wolfe's avatar
Noah Wolfe committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
}

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

    /* bytes to GB */
    time = ((double)bytes)/(1024.0*1024.0*1024.0);
    /* MB to s */
    time = time / GB_p_s;
    /* s to ns */
    time = time * 1000.0 * 1000.0 * 1000.0;

    return(time);
}

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 250 251 252 253 254 255 256 257 258 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
/** Function checks to see if the provided src and dst routers are two hops away from one another and if they are, sets
 *  the intm input variable to the id of the intermediate router.
 *  @param[in] dest         Local/relative ID of the destination router
 *  @param[in] src          Local/relative ID of the source terminal
 *  @param[in/out] intm     Local/relative ID of the intm router if it exists
 */
bool is_two_hops_away(int src, int dst, int *intm, int *count)
{
    if(src == dst)
        return false;
    int *local_channel_src = (int*) malloc(num_local_channels*sizeof(int));
    int *global_channel_src = (int*) malloc(num_global_channels*sizeof(int));
    int *local_channel_dst = (int*) malloc(num_local_channels*sizeof(int));
    int *global_channel_dst = (int*) malloc(num_global_channels*sizeof(int));
    get_router_connections(src, local_channel_src, global_channel_src);
    get_router_connections(dst, local_channel_dst, global_channel_dst);
    int i,j;
    int intm_count = 0;
    bool return_val = false;
    int temp_vals[6];

    for(i=0;i<num_local_channels;i++)
    {
        for(j=0; j<num_global_channels;j++){
            if(local_channel_src[i] == global_channel_dst[j])
            {
                    temp_vals[intm_count] = local_channel_src[i];
                    intm_count++;
                    return_val = true;
            }
        }
    }
    for(i=0;i<num_global_channels;i++)
    {
        for(j=0; j<num_local_channels;j++){
            if(global_channel_src[i] == local_channel_dst[j])
            {
                    temp_vals[intm_count] = global_channel_src[i];
                    intm_count++;
                    return_val = true;
            }
        }
    }
    for(i=0;i<num_global_channels;i++)
    {
        for(j=0; j<num_global_channels;j++){
            if(global_channel_src[i] == global_channel_dst[j])
            {
                    temp_vals[intm_count] = global_channel_src[i];
                    intm_count++;
                    return_val = true;
            }
        }
    }
    for(i=0;i<num_local_channels;i++)
    {
        for(j=0; j<num_local_channels;j++){
            if(local_channel_src[i] == local_channel_dst[j])
            {
                    temp_vals[intm_count] = local_channel_src[i];
                    intm_count++;
                    return_val = true;
            }
        }
    }
    for(i=0;i<intm_count;i++){
        intm[i] = temp_vals[i];
        //printf("src:%d dst:%d intm router%d: %d\n", src, dst, i, intm[i]);
    }

    *count = intm_count;
    return return_val;
}

Noah Wolfe's avatar
Noah Wolfe committed
297 298 299
/** Latest implementation of function to return an array mapping each router with it's corresponding worst-case router pair
 *  @param[in/out] 	*worst_dest   		Router ID to send all messages to
 */
300
void init_worst_case_mapping()
Noah Wolfe's avatar
Noah Wolfe committed
301
{
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 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
    int pairings = 0;
    int srcID = -1;
    int* middled;
    //for(int start=0; start<total_routers; start++){
    //    printf("start:%d\n",start);
    int start = 0;
    // variable to track if a router is an intermediate router for another pairing
    middled = (int*)calloc(total_routers,sizeof(int));
    // Loop over all routers
    for(int src_idx=start; src_idx<total_routers+start; src_idx++){
        int src = src_idx % total_routers;
        if(src == srcID)
            printf("src:%d total_routers:%d\n",src, total_routers);
        // If no pairing yet
        if(worst_dest[src] < 0){
            // Check remaining routers
            for(int dst_idx=src+total_routers/2; dst_idx<total_routers+src+total_routers/2; dst_idx++){
                int dst = dst_idx % total_routers;
                if(src == srcID)
                    printf("potential dst:%d\n",dst);
                // Check if destination already has a pairing
                if(worst_dest[dst] < 0 && dst != src){
                    int intm[6];
                    int len_intm = 0;
                    // Check if the destination is two hops from the source
                    if(is_two_hops_away(src, dst, intm, &len_intm)){
                        for(int intm_indx=0;intm_indx<len_intm;intm_indx++){
                            assert(intm[intm_indx]>=0);
                            // Check if the intm router already has a pairing
                            if(worst_dest[intm[intm_indx]] < 0){
                                // Check remaining routers for worst case pairing for intm router
                                for(int intm_dst_idx=intm[intm_indx]+total_routers/2; intm_dst_idx<total_routers+intm[intm_indx]+total_routers/2; intm_dst_idx++){
                                    int intm_dst = intm_dst_idx % total_routers;
                                    // Make sure intm_dst is not equal to dst or src
                                    if(intm_dst != dst && intm_dst != src){
                                        // Check if destination already has a pairing
                                        if(worst_dest[intm_dst] < 0){
                                            int intm2[6];
                                            int len_intm2 = 0;
                                            // Check if the destination is two hops from the source
                                            if(is_two_hops_away(intm[intm_indx], intm_dst, intm2, &len_intm2)){
                                                for(int intm2_indx=0; intm2_indx<len_intm2; intm2_indx++){
                                                    assert(intm2[intm2_indx]>=0);
                                                    // Check if the intm2 router already has been set as an intermediate router
                                                    if(middled[intm2[intm2_indx]] < 1){
                                                        // Check if the destination is either the src or dst router
                                                        if(intm2[intm2_indx] == src){
                                                            worst_dest[src] = dst;
                                                            worst_dest[dst] = src;
                                                            worst_dest[intm[intm_indx]] = intm_dst;
                                                            worst_dest[intm_dst] = intm[intm_indx];
                                                            middled[src]++;
                                                            middled[intm[intm_indx]]++;
                                                            pairings+=4;
                                                            //printf("T0 found pairings: %d<->%d<->%d & %d<->%d<->%d. Total pairings:%d\n",intm_dst, src, intm[intm_indx], src, intm[intm_indx], dst, pairings);
                                                            break;
                                                        }
                                                        if(intm2[intm2_indx] == dst){
                                                            worst_dest[src] = dst;
                                                            worst_dest[dst] = src;
                                                            worst_dest[intm[intm_indx]] = intm_dst;
                                                            worst_dest[intm_dst] = intm[intm_indx];
                                                            middled[dst]++;
                                                            middled[intm[intm_indx]]++;
                                                            pairings+=4;
                                                            //printf("T1 found pairings: %d<->%d<->%d & %d<->%d<->%d. Total pairings:%d\n",src, intm[intm_indx], dst, intm[intm_indx], dst, intm_dst, pairings);
                                                            break;
                                                        }
                                                    }else if(src == srcID)
                                                        printf("intm2:%d is already paired with %d\n",intm2[intm2_indx], worst_dest[intm2[intm2_indx]]);
                                                }if(worst_dest[intm[intm_indx]] < 0){
                                                    if(src == srcID)
                                                        printf("No worst-case pairing found for the intermediate router:%d with src:%d and dst:%d\n",intm[intm_indx], src, dst);
                                                }else{
                                                    break;
                                                }
                                            }else if(src == srcID)
                                                printf("intm_dst:%d is not two hops away from intm:%d\n",intm_dst, intm[intm_indx]);
                                        }else if(src == srcID)
                                            printf("intm_dst:%d is already paired with %d\n",intm_dst, worst_dest[intm_dst]);
                                    }else if(src == srcID)
                                        printf("intm_dst:%d is equal to dst:%d\n", intm_dst, dst);
                                }if(worst_dest[intm[intm_indx]] < 0){
                                    if(src == srcID){
                                        printf("No worst-case pairing found for the intermediate router:%d with src:%d and dst:%d\n",intm[intm_indx], src, dst);
                                    }
                                }else{
                                    break;
                                }
                            }else{
                                if(src == srcID)
                                    printf("dst:%d intm:%d has a pairing\n",dst, intm[intm_indx]);
                            }
                        }if(worst_dest[src] < 0){
                            if(src == srcID){
                                printf("No worst-case pairing found for src:%d and dst:%d\n", src, dst);
                            }
                        }else{
                            if(src == srcID){
                                printf("Found worst-case pairing found for src:%d and dst:%d\n", src, dst);
                            }
                            break;
                        }
                    }else{
                        if(src == srcID)
                            printf("dst:%d isn't two hops away\n",dst);
                    }
                }else{
                    if(src == srcID)
                        printf("dst:%d has connection to %d\n",dst, worst_dest[dst]);
                }
            }
            // Verify a destination was found
            if(worst_dest[src] < 0){
                printf("No worst-case pairing found for source router:%d\n",src);
                //free(middled);
                //break;
                //exit(0);
            }
        }
    }
    //}
    int no_pairing_count = 0;
    int no_reverse_pairing_count = 0;
    int *number_of_pairings = (int*)calloc(total_routers,sizeof(int));
    int *number_of_reverse_pairings = (int*)calloc(total_routers,sizeof(int));
    int *reverse_pairings = (int*)calloc(total_routers,sizeof(int));
    for(int i=0; i<total_routers; i++){
        reverse_pairings[i] = -1;
    }
    for(int i=0; i<total_routers; i++){
        if (worst_dest[i] < 0){
            no_pairing_count++;
            //printf("no mapping for %d\n",i);
        }
        else{
            reverse_pairings[worst_dest[i]]= i;
            number_of_pairings[i]++;
            if(number_of_pairings[i] > 1)
                printf("router:%d has more than one pairing (%d pairings)\n",i,number_of_pairings[i]);
        }
    }
    for(int i=0; i<total_routers; i++){
        if(reverse_pairings[i] < 0){
            no_reverse_pairing_count++;
            //printf("no reverse mapping for %d\n",i);
        }
        else{
            number_of_reverse_pairings[i]++;
            if(number_of_reverse_pairings[i] > 1)
                printf("router:%d has more than one reverse pairing (%d pairings)\n",i,number_of_reverse_pairings[i]);
        }
    }
    printf("number of unpaired routers:%d\n",no_pairing_count);
    printf("number of unpaired reverse routers:%d\n",no_reverse_pairing_count);
}

/** Older implementation of function to return an array mapping each router with it's corresponding worst-case router pair
 *  @param[in/out] 	*worst_dest   		Router ID to send all messages to
 */
void old_init_worst_case_mapping()
{
    int i,j,k;
    int r1,r2;		//Routers to be paired
    for(k=0;k<2;k++)
    {
        for(j=0;j<num_routers_per_grp-1;j+=2)
        {
            for(i=0;i<num_routers_per_grp;i++)
            {
                r1 = i + j*num_routers_per_grp + k*num_routers_per_grp*num_routers_per_grp;
                r2 = i + (j+1)*num_routers_per_grp + k*num_routers_per_grp*num_routers_per_grp;
                worst_dest[r1] = r2;
                worst_dest[r2] = r1;
                //				printf("%d->%d, %d->%d\n",r1,worst_dest[r1],r2,worst_dest[r2]);
            }
        }
    }
    j = num_routers_per_grp-1;
    for(i=0;i<num_routers_per_grp;i++)
    {
        r1 = i + j*num_routers_per_grp + 0*num_routers_per_grp*num_routers_per_grp;
        r2 = i + j*num_routers_per_grp + 1*num_routers_per_grp*num_routers_per_grp;
        worst_dest[r1] = r2;
        worst_dest[r2] = r1;
        //		printf("%d->%d, %d->%d\n",r1,worst_dest[r1],r2,worst_dest[r2]);
    }
Noah Wolfe's avatar
Noah Wolfe committed
489 490 491
}

static void issue_event(
492 493
        svr_state * ns,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
494
{
Misbah Mubarak's avatar
Misbah Mubarak committed
495
    (void)ns;
Noah Wolfe's avatar
Noah Wolfe committed
496 497 498 499 500 501 502 503
    tw_event *e;
    svr_msg *m;
    tw_stime kickoff_time;

    /* each server sends a dummy event to itself that will kick off the real
     * simulation
     */

504 505 506 507
    configuration_get_value_double(&config, "PARAMS", "cn_bandwidth", NULL, &link_bandwidth);
    if(!link_bandwidth) {
        link_bandwidth = 4.7;
        fprintf(stderr, "Bandwidth of channels not specified, setting to %lf\n", link_bandwidth);
Noah Wolfe's avatar
Noah Wolfe committed
508 509 510 511 512 513 514 515
    }

    if(arrival_time!=0)
    {
        MEAN_INTERVAL = arrival_time;
    }
    if(load != 0)
    {
516
        MEAN_INTERVAL = bytes_to_ns(payload_size, load*link_bandwidth);
Noah Wolfe's avatar
Noah Wolfe committed
517 518 519
    }

    /* skew each kickoff event slightly to help avoid event ties later on */
520 521 522
    kickoff_time = 1.1 * g_tw_lookahead + tw_rand_integer(lp->rng, 0, arrival_time);
    unsigned int rng_calls = 0;
    kickoff_time = g_tw_lookahead + tw_rand_normal_sd(lp->rng, MEAN_INTERVAL, MEAN_INTERVAL*0.05, &rng_calls);
Noah Wolfe's avatar
Noah Wolfe committed
523 524 525 526 527 528 529 530

    e = tw_event_new(lp->gid, kickoff_time, lp);
    m = tw_event_data(e);
    m->svr_event_type = KICKOFF;
    tw_event_send(e);
}

static void svr_init(
531 532
        svr_state * ns,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
533
{
534 535 536 537 538 539 540 541
    // Initiailize comm_map 2D array
    if(!lp->gid){
        comm_map = (int**)malloc(num_nodes*sizeof(int*));
        for(int i=0; i<num_nodes; i++){
            comm_map[i] = (int*)calloc(num_nodes,sizeof(int));
        }
    }

Noah Wolfe's avatar
Noah Wolfe committed
542 543 544 545 546
    issue_event(ns, lp);
    return;
}

static void handle_kickoff_rev_event(
547 548 549 550
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
551
{
Misbah Mubarak's avatar
Misbah Mubarak committed
552 553 554
    (void)b;
    (void)m;
    (void)lp;
555
    if(m->incremented_flag)
Misbah Mubarak's avatar
Misbah Mubarak committed
556

557 558
        if(b->c1)
            tw_rand_reverse_unif(lp->rng);
559

560 561
    ns->msg_sent_count--;
    model_net_event_rc2(lp, &m->event_rc);
562 563

    tw_rand_reverse_unif(lp->rng);
Noah Wolfe's avatar
Noah Wolfe committed
564 565
}	
static void handle_kickoff_event(
566 567 568 569
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
570
{
Misbah Mubarak's avatar
Misbah Mubarak committed
571 572
    (void)m;

573 574 575 576 577 578 579
    if(ns->msg_sent_count >= num_msgs)
    {
        m->incremented_flag = 1;
        return;
    }
    m->incremented_flag = 0;

580
    char anno[MAX_NAME_LENGTH];
581

Noah Wolfe's avatar
Noah Wolfe committed
582 583 584 585 586 587 588 589 590
    svr_msg * m_local = malloc(sizeof(svr_msg));
    svr_msg * m_remote = malloc(sizeof(svr_msg));

    m_local->svr_event_type = LOCAL;
    m_local->src = lp->gid;

    memcpy(m_remote, m_local, sizeof(svr_msg));
    m_remote->svr_event_type = REMOTE;

591
    assert(net_id == SLIMFLY); /* only supported for slimfly model right now. */
Noah Wolfe's avatar
Noah Wolfe committed
592
    ns->start_ts = tw_now(lp);
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 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

    codes_mapping_get_lp_info(lp->gid, group_name, &group_index, lp_type_name, &lp_type_index, anno, &rep_id, &offset);

    int num_server_lps = codes_mapping_get_lp_count(group_name, 1, LP_CONFIG_NM, NULL, 0);

    int src_terminal_id, src_router_id, dst_router_id;

    int num_transfers = 0;
    int *local_dest;
    tw_lpid global_dest = -1;

    // Compute current server's local/relative ID
    int server_id = rep_id * num_server_lps + offset;

    /* in case of uniform random traffic, send to a random destination. */
    if(traffic == UNIFORM)
    {
        b->c1 = 1;
        num_transfers = 1;
        local_dest = (int*)malloc(num_transfers*sizeof(int));
        local_dest[0] = tw_rand_integer(lp->rng, 0, num_nodes - 1);
    }
    else if(traffic == WORST_CASE)
    {
        num_transfers = 1;
        local_dest = (int*)malloc(num_transfers*sizeof(int));

        // Compute local id of source terminal (same local id as current server, assuming one server per terminal)
        src_terminal_id = (rep_id * num_server_lps) + offset;  
        // Compute loacl id of source router
        src_router_id = src_terminal_id / (num_server_lps);
        // Only send messages if we have a worst-case pairing
        if(worst_dest[src_router_id] < 0)
            return;
        // Get local id of destination router from precomputed worst-case mapping
        dst_router_id = worst_dest[src_router_id];
        // Get local id of destination terminal (same offset connection from dest router as src terminal from src router)
        local_dest[0] = (dst_router_id * num_server_lps) + offset;
    }
    else if(traffic == NEAREST_NEIGHBOR_1D)
    {
        num_transfers = 2;
        local_dest = (int*)malloc(num_transfers*sizeof(int));

        // Neighbors in the first dimension
        local_dest[0] =  (server_id - 1);
        if(local_dest[0] < 0)
            local_dest[0] = num_nodes - 1;
        local_dest[1] =  (server_id + 1) % num_nodes;
    }
    else if(traffic == NEAREST_NEIGHBOR_2D)
    {
        num_transfers = 4;
        local_dest = (int*)malloc(num_transfers*sizeof(int));

        // Neighbors in the first dimension
        local_dest[0] =  (server_id - 1);
        if(local_dest[0] < 0)
            local_dest[0] = num_nodes - 1;
        local_dest[1] =  (server_id + 1) % num_nodes;
        // Neighbors in the second dimension
        local_dest[2] = (server_id + num_nodes/6) % num_nodes;
        local_dest[3] = (server_id - num_nodes/6) % num_nodes;
        if(local_dest[3] < 0)
            local_dest[3] = num_nodes + local_dest[3];
    }
    else if(traffic == NEAREST_NEIGHBOR_3D)
    {
        num_transfers = 6;
        local_dest = (int*)malloc(num_transfers*sizeof(int));

        // Neighbors in the first dimension
        local_dest[0] =  (server_id - 1);
        if(local_dest[0] < 0)
            local_dest[0] = num_nodes - 1;
        local_dest[1] =  (server_id + 1) % num_nodes;
        // Neighbors in the second dimension
        local_dest[2] = (server_id + num_nodes/3) % num_nodes;
        local_dest[3] = (server_id - num_nodes/3) % num_nodes;
        if(local_dest[3] < 0)
            local_dest[3] = num_nodes + local_dest[3];
        // Neighbors in the third dimension
        local_dest[4] = (server_id + num_nodes/3) % num_nodes;
        local_dest[5] = (server_id - num_nodes/3) % num_nodes;
        if(local_dest[5] < 0)
            local_dest[5] = num_nodes + local_dest[5];
    }
    else if(traffic == GATHER)
    {
        num_transfers = 0;
        if(server_id != 0){
            num_transfers = 1;
            local_dest = (int*)malloc(num_transfers*sizeof(int));
            local_dest[0] = 0;
        }
    }
    else if(traffic == SCATTER)
    {
        num_transfers = 0;
        if(server_id == 0){
            num_transfers = num_nodes - 1;
            local_dest = (int*)malloc(num_transfers*sizeof(int));
            for(int i=1; i<num_nodes; i++)
                local_dest[i-1] = i;
        }
    }
    else if(traffic == BISECTION)
    {
        num_transfers = 1;
        local_dest = (int*)malloc(num_transfers*sizeof(int));
        local_dest[0] = (server_id + (int)(num_nodes/3)) % num_nodes;
    }

    for(int i=0; i<num_transfers; i++){
        // Verify local/relative ID of the destination is a valid option
        assert(local_dest[i] < num_nodes);
        // Get global/lp ID of the destination
        codes_mapping_get_lp_id(group_name, lp_type_name, anno, 1, local_dest[i] / num_servers_per_rep, local_dest[i] % num_servers_per_rep, &global_dest);
        // Increment send count in communication heat map
        comm_map[server_id][local_dest[i]]++;
        // Increment send count
        ns->msg_sent_count++;
        // Issue event
        m->event_rc = model_net_event(net_id, "test", global_dest, payload_size, i*0.2, sizeof(svr_msg), (const void*)m_remote, sizeof(svr_msg), (const void*)m_local, lp);
    }
    issue_event(ns, lp);
    return;
Noah Wolfe's avatar
Noah Wolfe committed
720 721 722
}

static void handle_remote_rev_event(
723 724 725 726
        svr_state * ns,     
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
727
{
728 729 730 731
    (void)b;
    (void)m;
    (void)lp;
    ns->msg_recvd_count--;
Noah Wolfe's avatar
Noah Wolfe committed
732 733 734
}

static void handle_remote_event(
735 736 737 738
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
739
{
Misbah Mubarak's avatar
Misbah Mubarak committed
740 741 742
    (void)b;
    (void)m;
    (void)lp;
743
    ns->msg_recvd_count++;
Noah Wolfe's avatar
Noah Wolfe committed
744 745 746
}

static void handle_local_rev_event(
747 748 749 750
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
751
{
Misbah Mubarak's avatar
Misbah Mubarak committed
752 753 754
    (void)b;
    (void)m;
    (void)lp;
755
    ns->local_recvd_count--;
Noah Wolfe's avatar
Noah Wolfe committed
756 757 758
}

static void handle_local_event(
759 760 761 762
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
763
{
Misbah Mubarak's avatar
Misbah Mubarak committed
764 765 766
    (void)b;
    (void)m;
    (void)lp;
Noah Wolfe's avatar
Noah Wolfe committed
767 768 769 770 771 772
    ns->local_recvd_count++;
}

int index_mine = 0;

static void svr_finalize(
773 774
        svr_state * ns,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
775 776 777
{
    ns->end_ts = tw_now(lp);

778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    double observed_load = ((double)payload_size*(double)ns->msg_recvd_count)/(double)ns->end_ts;
    observed_load = observed_load * (double)(1000*1000*1000);
    observed_load = observed_load / (double)(1024*1024*1024);
    int written = 0;
    int written2 =0;

    if(lp->gid == 0){
        written = sprintf(ns->output_buf, "# Format <LP id> <Msgs Sent> <Msgs Recvd> <Bytes Sent> <Bytes Recvd> <Offered Load [GBps]> <Observed Load [GBps]> <End Time [ns]>\n");
        written2 = sprintf(ns->output_buf2, "# Format <server ID> <sends to svr 0> <sends to svr 1> ... <sends to svr N>\n");
    }

    written += sprintf(ns->output_buf + written, "%llu %d %d %d %d %f %f %f\n",LLU(lp->gid), ns->msg_sent_count, ns->msg_recvd_count,
            payload_size*ns->msg_sent_count, payload_size*ns->msg_recvd_count, load*link_bandwidth, observed_load, ns->end_ts);

    lp_io_write(lp->gid, "synthetic-stats", written, ns->output_buf);

    // Get server mapping info
    char anno[MAX_NAME_LENGTH];
    int num_server_lps = codes_mapping_get_lp_count(group_name, 1, model_net_lp_config_names[SLIMFLY], NULL, 0);
    codes_mapping_get_lp_info(lp->gid, group_name, &group_index, lp_type_name, &lp_type_index, anno, &rep_id, &offset);
    // Compute current server's local/relative ID
    int server_id = rep_id * num_server_lps + offset;
    written2 += sprintf(ns->output_buf2 + written2, "%d ", server_id);
    for(int j=0; j<num_nodes; j++){
        written2 += sprintf(ns->output_buf2 + written2, "%d ", comm_map[server_id][j]);
    }
    written2 += sprintf(ns->output_buf2 + written2, "\n");
    lp_io_write(lp->gid, "communication-map", written2, ns->output_buf2);

Noah Wolfe's avatar
Noah Wolfe committed
807 808 809 810
    return;
}

static void svr_rev_event(
811 812 813 814
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
815 816 817
{
    switch (m->svr_event_type)
    {
818 819 820 821 822 823 824 825 826 827 828 829
        case REMOTE:
            handle_remote_rev_event(ns, b, m, lp);
            break;
        case LOCAL:
            handle_local_rev_event(ns, b, m, lp);
            break;
        case KICKOFF:
            handle_kickoff_rev_event(ns, b, m, lp);
            break;
        default:
            assert(0);
            break;
Noah Wolfe's avatar
Noah Wolfe committed
830 831 832 833
    }
}

static void svr_event(
834 835 836 837
        svr_state * ns,
        tw_bf * b,
        svr_msg * m,
        tw_lp * lp)
Noah Wolfe's avatar
Noah Wolfe committed
838
{
839
    switch (m->svr_event_type)
Noah Wolfe's avatar
Noah Wolfe committed
840 841 842 843 844 845 846
    {
        case REMOTE:
            handle_remote_event(ns, b, m, lp);
            break;
        case LOCAL:
            handle_local_event(ns, b, m, lp);
            break;
847 848 849
        case KICKOFF:
            handle_kickoff_event(ns, b, m, lp);
            break;
Noah Wolfe's avatar
Noah Wolfe committed
850 851 852
        default:
            printf("\n Invalid message type %d ", m->svr_event_type);
            assert(0);
853
            break;
Noah Wolfe's avatar
Noah Wolfe committed
854 855 856 857
    }
}

int main(
858 859
        int argc,
        char **argv)
Noah Wolfe's avatar
Noah Wolfe committed
860 861 862 863 864
{
    int nprocs;
    int rank;
    int num_nets;
    int *net_ids;
865

Noah Wolfe's avatar
Noah Wolfe committed
866 867
    tw_opt_add(app_opt);
    tw_init(&argc, &argv);
868 869 870 871 872
#ifdef USE_RDAMARIS
    if(g_st_ross_rank)
    { // keep damaris ranks from running code between here up until tw_end()
#endif
    codes_comm_update();
Noah Wolfe's avatar
Noah Wolfe committed
873 874 875

    if(argc < 2)
    {
876 877 878
        printf("\n Usage: mpirun <args> --sync=2/3 mapping_file_name.conf (optional --nkp) ");
        MPI_Finalize();
        return 0;
Noah Wolfe's avatar
Noah Wolfe committed
879 880
    }

881 882
    MPI_Comm_rank(MPI_COMM_CODES, &rank);
    MPI_Comm_size(MPI_COMM_CODES, &nprocs);
Noah Wolfe's avatar
Noah Wolfe committed
883

884
    configuration_load(argv[2], MPI_COMM_CODES, &config);
Noah Wolfe's avatar
Noah Wolfe committed
885 886
    model_net_register();
    svr_add_lp_type();
887

888 889 890
    if (g_st_ev_trace || g_st_model_stats || g_st_use_analysis_lps)
        svr_register_model_types();

Noah Wolfe's avatar
Noah Wolfe committed
891 892
    codes_mapping_setup();
    net_ids = model_net_configure(&num_nets);
893
    //    assert(num_nets==1);
Noah Wolfe's avatar
Noah Wolfe committed
894 895 896
    net_id = *net_ids;
    free(net_ids);

Neil McGlohon's avatar
Neil McGlohon committed
897
    num_servers_per_rep = codes_mapping_get_lp_count("MODELNET_GRP", 1, "nw-lp", NULL, 1);
898 899
    configuration_get_value_int(&config, "PARAMS", "num_terminals", NULL, &num_terminals);
    configuration_get_value_int(&config, "PARAMS", "num_routers", NULL, &num_routers_per_grp);
Noah Wolfe's avatar
Noah Wolfe committed
900 901 902 903
    num_groups = (num_routers_per_grp * 2);
    num_nodes = num_groups * num_routers_per_grp * num_servers_per_rep;
    num_nodes_per_grp = num_routers_per_grp * num_servers_per_rep;
    total_routers = num_routers_per_grp * num_routers_per_grp * 2;
904 905 906 907 908

    if(lp_io_dir[0])
    {
        do_lp_io = 1;
        int flags = lp_io_use_suffix ? LP_IO_UNIQ_SUFFIX : 0;
909
        int ret = lp_io_prepare(lp_io_dir, flags, &io_handle, MPI_COMM_CODES);
910 911 912
        assert(ret == 0 || !"lp_io_prepare failure");
    }

Noah Wolfe's avatar
Noah Wolfe committed
913
    //WORST_CASE Initialization array
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
    if(traffic == WORST_CASE)
    {
        configuration_get_value_int(&config, "PARAMS", "local_channels", NULL, &num_local_channels);
        if(!num_local_channels) {
            num_local_channels = 2;
            fprintf(stderr, "Number of Local channels not specified, setting to %d\n", num_local_channels);
        }

        configuration_get_value_int(&config, "PARAMS", "global_channels", NULL, &num_global_channels);
        if(!num_global_channels) {
            num_global_channels = 2;
            fprintf(stderr, "Number of Global channels not specified, setting to %d\n", num_global_channels);
        }

        char       **values;
        size_t       length;
        int ret = configuration_get_multivalue(&config, "PARAMS", "generator_set_X", NULL, &values, &length);
        if (ret != 1)
            tw_error(TW_LOC, "unable to read PARAMS:generator_set_X\n");
        if (length < 2)
            fprintf(stderr, "generator set X less than 2 elements\n");

        X = (int*)malloc(sizeof(int)*length);
        for (size_t i = 0; i < length; i++)
        {
            X[i] = atoi(values[i]);
        }
        free(values);

        ret = configuration_get_multivalue(&config, "PARAMS", "generator_set_X_prime", NULL, &values, &length);
        if (ret != 1)
            tw_error(TW_LOC, "unable to read PARAMS:generator_set_X_prime\n");
        if (length < 2)
            fprintf(stderr, "generator set  X_prime less than 2 elements\n");

        X_size = length;
        X_prime = (int*)malloc(sizeof(int)*length);
        for (size_t i = 0; i < length; i++)
        {
            X_prime[i] = atoi(values[i]);
        }
        free(values);

        worst_dest = (int*)malloc(total_routers*sizeof(int));
        for(int i=0; i<total_routers; i++){
            worst_dest[i] = -1;
        }
961
        init_worst_case_mapping();
Noah Wolfe's avatar
Noah Wolfe committed
962 963 964 965
#if PRINT_WORST_CASE_MATCH
        int l;
        for(l=0; l<total_routers; l++)
        {
966
            printf("match %d->%d\n",l,worst_dest[l]);
Noah Wolfe's avatar
Noah Wolfe committed
967 968
        }
#endif
969
    }
970

971
    tw_run();
972

Noah Wolfe's avatar
Noah Wolfe committed
973

974 975
    if (do_lp_io){
        int ret = lp_io_flush(io_handle, MPI_COMM_CODES);
976
        assert(ret == 0 || !"lp_io_flush failure");
Noah Wolfe's avatar
Noah Wolfe committed
977
    }
978 979 980 981

    model_net_report_stats(net_id);

    #ifdef USE_RDAMARIS
982 983
    } // end if(g_st_ross_rank)
#endif
Noah Wolfe's avatar
Noah Wolfe committed
984 985 986 987 988
    tw_end();

    return 0;
}

989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
/** Get the local and global router connections for the given source router
 *  @param[in] src_router_id            The local/relative ID for the source router
 *  @param[in] num_global_channels      Number of global connections
 *  @param[in] num_local_channels       Number of local connections
 *  @param[in] total_routers            The total number of routers in one rail
 *  @param[in] *local_channels          Integer array pointer for storing the local connections
 *  @param[in] *global_channels         Integer array pointer for storing the global connections
 */
static void get_router_connections(int src_router_id, int* local_channels, int* global_channels){
    //Compute MMS router layout/connection graph
    int rid_s = src_router_id;	// ID for source router

    int rid_d;						// ID for dest. router
    int s_s,s_d;					// subgraph location for source and destination routers
    int i_s,i_d;					// x or m coordinates for source and destination routers
    int j_s,j_d; 					// y or c coordinates for source and destination routers
    int k;
    int local_idx = 0;
    int global_idx = 0;
    int generator_size = X_size;

    for(rid_d=0;rid_d<total_routers;rid_d++)
    {
        // Decompose source and destination Router IDs into 3D subgraph coordinates (subgraph,i,j)
        if(rid_d >= total_routers/2)
        {
            s_d = 1;
            i_d = (rid_d - total_routers/2) /  num_global_channels;
            j_d = (rid_d - total_routers/2) %  num_global_channels;
        }
        else
        {
            s_d = 0;
            i_d = rid_d /  num_global_channels;
            j_d = rid_d %  num_global_channels;
        }
        if(rid_s >= total_routers/2)
        {
            s_s = 1;
            i_s = (rid_s - total_routers/2) /  num_global_channels;
            j_s = (rid_s - total_routers/2) %  num_global_channels;
        }
        else
        {
            s_s = 0;
            i_s = rid_s /  num_global_channels;
            j_s = rid_s %  num_global_channels;
        }
        // Check for subgraph 0 local connections
        if(s_s==0 && s_d==0)
        {
            if(i_s==i_d)							// equation (2) y-y' is in X'
            {
                for(k=0;k<generator_size;k++)
                {
                    if(abs(j_s-j_d)==X[k])
                    {
                        if(src_router_id >= total_routers)
                            local_channels[local_idx++] = rid_d + total_routers;
                        else
                            local_channels[local_idx++] = rid_d;
                    }
                }
            }
        }
        // Check if global connections
        if(s_s==0 && s_d==1)
        {
            if(j_s == (i_d*i_s + j_d) % num_global_channels)							// equation (3) y=mx+c
            {
                if(src_router_id >= total_routers)
                    global_channels[global_idx++] = rid_d + total_routers;
                else
                    global_channels[global_idx++] = rid_d;
            }
        }
    }

    // Loop over second subgraph source routers
    for(rid_d=total_routers-1;rid_d>=0;rid_d--)
    {
        // Decompose source and destination Router IDs into 3D subgraph coordinates (subgraph,i,j)
        if(rid_d >= total_routers/2)
        {
            s_d = 1;
            i_d = (rid_d -  total_routers/2) /  num_global_channels;
            j_d = (rid_d -  total_routers/2) %  num_global_channels;
        }
        else
        {
            s_d = 0;
            i_d = rid_d /  num_global_channels;
            j_d = rid_d %  num_global_channels;
        }
        if(rid_s >= total_routers/2)
        {
            s_s = 1;
            i_s = (rid_s -  total_routers/2) /  num_global_channels;
            j_s = (rid_s -  total_routers/2) %  num_global_channels;
        }
        else
        {
            s_s = 0;
            i_s = rid_s /  num_global_channels;
            j_s = rid_s %  num_global_channels;
        }
        // Check for subgraph 1 local connections
        if(s_s==1 && s_d==1)
        {
            if(i_s==i_d)							// equation (2) c-c' is in X'
            {
                for(k=0;k<generator_size;k++)
                {
                    if(abs(j_s-j_d)==X_prime[k])
                    {
                        if(src_router_id >= total_routers)
                            local_channels[local_idx++] = rid_d + total_routers;
                        else
                            local_channels[local_idx++] = rid_d;
                    }
                }
            }
        }
        // Check if global connections
        if(s_s==1 && s_d==0)
        {
            if(j_d == (i_s*i_d + j_s) %  num_global_channels)							// equation (3) y=mx+c
            {
                if(src_router_id >= total_routers)
                    global_channels[global_idx++] = rid_d + total_routers;
                else
                    global_channels[global_idx++] = rid_d;
            }
        }
    }

    assert(local_idx == num_local_channels);
    assert(global_idx == num_global_channels);
}
Noah Wolfe's avatar
Noah Wolfe committed
1128 1129 1130 1131 1132 1133 1134 1135
/*
 * Local variables:
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 *
 * vim: ft=c ts=8 sts=4 sw=4 expandtab
 */