model-net-synthetic-slimfly.c 16.7 KB
Newer Older
Noah Wolfe's avatar
Noah Wolfe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Copyright (C) 2015 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

/*
* The test program generates some synthetic traffic patterns for the model-net network models.
* currently it only support the dragonfly network model uniform random and nearest neighbor traffic patterns.
*/

#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 PAYLOAD_SZ 256
#define LP_CONFIG_NM (model_net_lp_config_names[SLIMFLY])

#define PRINT_WORST_CASE_MATCH 0

#define PARAMS_LOG 1
FILE * slimfly_results_log_2=NULL;
FILE * slimfly_ross_csv_log=NULL;

static int net_id = 0;
static int num_routers = 0;
static int num_servers = 0;
static int offset = 2;
static int traffic = 1;
static double arrival_time = 1000.0;
static double load = 0.0;				//percent utilization of each terminal's uplink when sending messages
static double MEAN_INTERVAL = 0.0;
int this_packet_size = 0;
double this_global_bandwidth = 0.0;
double this_link_delay = 0;
int *worst_dest;						//Array mapping worst case destination for each router
int num_terminals;
int total_routers;

43 44 45 46 47
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;

Noah Wolfe's avatar
Noah Wolfe committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* whether to pull instead of push */
static int do_pull = 0;

static int num_servers_per_rep = 0;
static int num_routers_per_grp = 0;
static int num_nodes_per_grp = 0;

static int num_reps = 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;

/* type of events */
enum svr_event
{
    KICKOFF,	   /* kickoff event */
    REMOTE,        /* remote event */
    LOCAL      /* local event */
};

/* type of synthetic traffic */
enum TRAFFIC
{
	UNIFORM = 1, /* sends message to a randomly selected node */
        WORST_CASE = 2,
	NEAREST_GROUP = 3, /* sends message to the node connected to the neighboring router */
	NEAREST_NEIGHBOR = 4 /* sends message to the next node (potentially connected to the same router) */
};

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

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

static void svr_init(
    svr_state * ns,
    tw_lp * lp);
static void svr_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
static void svr_rev_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
static void svr_finalize(
    svr_state * ns,
    tw_lp * lp);

tw_lptype svr_lp = {
    (init_f) svr_init,
    (pre_run_f) NULL,
    (event_f) svr_event,
    (revent_f) svr_rev_event,
122
    (commit_f) NULL,
Noah Wolfe's avatar
Noah Wolfe committed
123 124 125 126 127 128 129 130
    (final_f)  svr_finalize,
    (map_f) codes_mapping,
    sizeof(svr_state),
};

const tw_optdef app_opt [] =
{
        TWOPT_GROUP("Model net synthetic traffic " ),
131 132
        TWOPT_UINT("traffic", traffic, "UNIFORM RANDOM=1, NEAREST NEIGHBOR=2 "),
        TWOPT_STIME("arrival_time", arrival_time, "INTER-ARRIVAL TIME"),
133 134 135 136
        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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
};

const tw_lptype* svr_get_lp_type()
{
            return(&svr_lp);
}

static void svr_add_lp_type()
{
  lp_type_register("server", svr_get_lp_type());
}

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

/** 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
 */
167
void init_worst_case_mapping()
Noah Wolfe's avatar
Noah Wolfe committed
168 169 170 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 197 198 199 200 201 202 203 204 205 206 207
{
	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]);
	}
}

static void issue_event(
    svr_state * ns,
    tw_lp * lp)
{
    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
     */

208
    configuration_get_value_int(&config, "PARAMS", "packet_size", NULL, &this_packet_size);
Noah Wolfe's avatar
Noah Wolfe committed
209 210 211 212 213 214
    if(!this_packet_size) {
        this_packet_size = 0;
        fprintf(stderr, "Packet size not specified, setting to %d\n", this_packet_size);
        exit(0);
    }

215
    configuration_get_value_double(&config, "PARAMS", "global_bandwidth", NULL, &this_global_bandwidth);
Noah Wolfe's avatar
Noah Wolfe committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    if(!this_global_bandwidth) {
        this_global_bandwidth = 4.7;
        fprintf(stderr, "Bandwidth of global channels not specified, setting to %lf\n", this_global_bandwidth);
    }

/*    configuration_get_value_double(&config, "PARAMS", "link_delay", anno, &this_link_delay);
    if(!this_link_delay) {
        this_link_delay = 0;
        fprintf(stderr, "Link Delay not specified, setting to %lf\n", this_link_delay);
    }
*/
    if(arrival_time!=0)
    {
        MEAN_INTERVAL = arrival_time;
    }
    if(load != 0)
    {
        MEAN_INTERVAL = bytes_to_ns(this_packet_size, load*this_global_bandwidth) + this_link_delay;
    }

    /* skew each kickoff event slightly to help avoid event ties later on */
    kickoff_time = g_tw_lookahead + MEAN_INTERVAL + tw_rand_exponential(lp->rng, (double)MEAN_INTERVAL/100);

    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(
    svr_state * ns,
    tw_lp * lp)
{
    issue_event(ns, lp);
    return;
}

static void handle_kickoff_rev_event(
            svr_state * ns,
            tw_bf * b,
            svr_msg * m,
            tw_lp * lp)
{
	ns->msg_sent_count--;
	model_net_event_rc(net_id, lp, PAYLOAD_SZ);
}	
static void handle_kickoff_event(
	    svr_state * ns,
	    tw_bf * b,
	    svr_msg * m,
	    tw_lp * lp)
{
268
    char anno[MAX_NAME_LENGTH];
Noah Wolfe's avatar
Noah Wolfe committed
269 270 271 272 273 274 275 276 277 278 279
    tw_lpid local_dest = -1, global_dest = -1;
   
    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;

280
    assert(net_id == SLIMFLY); /* only supported for dragonfly model right now. */
Noah Wolfe's avatar
Noah Wolfe committed
281
    ns->start_ts = tw_now(lp);
282
//   codes_mapping_get_lp_info(lp->gid, group_name, &group_index, lp_type_name, &lp_type_index, anno, &rep_id, &offset);
Noah Wolfe's avatar
Noah Wolfe committed
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 308 309 310 311 312 313 314 315
   codes_mapping_get_lp_info(lp->gid, group_name, &group_index, lp_type_name, &lp_type_index, anno, &rep_id, &offset);
   /* in case of uniform random traffic, send to a random destination. */
   if(traffic == UNIFORM)
   {
   	local_dest = tw_rand_integer(lp->rng, 0, num_nodes - 1);
//	printf("\n LP %ld sending to %d ", lp->gid, local_dest);
   }
   else if(traffic == WORST_CASE)
   {
        // Assign the global router ID
        // TODO: be annotation-aware
        int num_lps = codes_mapping_get_lp_count(group_name, 1, LP_CONFIG_NM, anno, 0);

        int src_terminal_id = (rep_id * num_lps) + offset;  
    //   s->router_id=(int)s->terminal_id / (s->params->num_routers/2);
        int src_router_id = src_terminal_id / (num_lps);
        int dst_router_id = worst_dest[src_router_id];
        //			dst_lp = tw_rand_integer(lp->rng, total_routers + num_terminals*dst_router_id, total_routers + num_terminals*(dst_router_id+1)-1);
//        local_dest = total_routers + num_terminals*dst_router_id + (src_terminal_id % num_terminals);
        local_dest = num_lps*dst_router_id + (src_terminal_id % num_terminals);
//        printf("packet path initialized. src_term:%d src_router:%d dest_router:%d dest_term:%d\n",src_terminal_id, src_router_id, dst_router_id, (int)local_dest);
   }
   else if(traffic == NEAREST_GROUP)
   {
	local_dest = (rep_id * 2 + offset + num_nodes_per_grp) % num_nodes;
//	printf("\n LP %ld sending to %ld num nodes %d ", rep_id * 2 + offset, local_dest, num_nodes);
   }	
   else if(traffic == NEAREST_NEIGHBOR)
   {
	local_dest =  (rep_id * 2 + offset + 2) % num_nodes;
//	 printf("\n LP %ld sending to %ld num nodes %d ", rep_id * 2 + offset, local_dest, num_nodes);
   }
//printf("1local_dest:%d global_dest:%d num_nodes:%d\n", local_dest, global_dest, num_nodes);
316
   assert(local_dest < (tw_lpid)num_nodes);
Noah Wolfe's avatar
Noah Wolfe committed
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
   codes_mapping_get_lp_id(group_name, lp_type_name, anno, 1, local_dest / num_servers_per_rep, local_dest % num_servers_per_rep, &global_dest);
  
//printf("2local_dest:%d global_dest:%d num_nodes:%d\n", local_dest, global_dest, num_nodes);

   ns->msg_sent_count++;
   model_net_event(net_id, "test", global_dest, PAYLOAD_SZ, 0.0, sizeof(svr_msg), (const void*)m_remote, sizeof(svr_msg), (const void*)m_local, lp);
   issue_event(ns, lp);
   return;
}

static void handle_remote_rev_event(
            svr_state * ns,     
            tw_bf * b,
            svr_msg * m,
            tw_lp * lp)
{
        ns->msg_recvd_count--;
}

static void handle_remote_event(
	    svr_state * ns,
	    tw_bf * b,
	    svr_msg * m,
	    tw_lp * lp)
{
	ns->msg_recvd_count++;
}

static void handle_local_rev_event(
                svr_state * ns,
                tw_bf * b,
                svr_msg * m,
                tw_lp * lp)
{
	ns->local_recvd_count--;
}

static void handle_local_event(
                svr_state * ns,
                tw_bf * b,
                svr_msg * m,
                tw_lp * lp)
{
    ns->local_recvd_count++;
}
/* convert ns to seconds */
static tw_stime ns_to_s(tw_stime ns)
{
    return(ns / (1000.0 * 1000.0 * 1000.0));
}

/* convert seconds to ns */
static tw_stime s_to_ns(tw_stime ns)
{
    return(ns * (1000.0 * 1000.0 * 1000.0));
}

int index_mine = 0;

static void svr_finalize(
    svr_state * ns,
    tw_lp * lp)
{
    ns->end_ts = tw_now(lp);

//    printf("server %d server %llu recvd %d bytes in %f seconds, %f MiB/s sent_count %d recvd_count %d local_count %d \n", index_mine++,(unsigned long long)lp->gid, PAYLOAD_SZ*ns->msg_recvd_count, ns_to_s(ns->end_ts-ns->start_ts),
//        ((double)(PAYLOAD_SZ*ns->msg_sent_count)/(double)(1024*1024)/ns_to_s(ns->end_ts-ns->start_ts)), ns->msg_sent_count, ns->msg_recvd_count, ns->local_recvd_count);
    return;
}

static void svr_rev_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
    switch (m->svr_event_type)
    {
	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;
    }
}

static void svr_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
   switch (m->svr_event_type)
    {
        case REMOTE:
            handle_remote_event(ns, b, m, lp);
            break;
        case LOCAL:
            handle_local_event(ns, b, m, lp);
            break;
	case KICKOFF:
	    handle_kickoff_event(ns, b, m, lp);
	    break;
        default:
            printf("\n Invalid message type %d ", m->svr_event_type);
            assert(0);
        break;
    }
}

int main(
    int argc,
    char **argv)
{
    int nprocs;
    int rank;
    int num_nets;
    int *net_ids;
442
 
Noah Wolfe's avatar
Noah Wolfe committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    tw_opt_add(app_opt);
    tw_init(&argc, &argv);

    if(argc < 2)
    {
            printf("\n Usage: mpirun <args> --sync=2/3 mapping_file_name.conf (optional --nkp) ");
            MPI_Finalize();
            return 0;
    }

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

    configuration_load(argv[2], MPI_COMM_WORLD, &config);
    model_net_register();
    svr_add_lp_type();
    codes_mapping_setup();
    net_ids = model_net_configure(&num_nets);
461
//    assert(num_nets==1);
Noah Wolfe's avatar
Noah Wolfe committed
462 463 464 465
    net_id = *net_ids;
    free(net_ids);

    num_servers_per_rep = codes_mapping_get_lp_count("MODELNET_GRP", 1, "server", NULL, 1);
466 467
    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
468 469 470 471
    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;
472 473

/*    if(lp_io_prepare("modelnet-test", LP_IO_UNIQ_SUFFIX, &handle, MPI_COMM_WORLD) < 0)
Noah Wolfe's avatar
Noah Wolfe committed
474 475 476
    {
        return(-1);
    }
477 478 479 480 481 482 483 484 485 486
*/

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

Noah Wolfe's avatar
Noah Wolfe committed
487 488 489 490
    //WORST_CASE Initialization array
   if(traffic == WORST_CASE)
   {
        worst_dest = (int*)calloc(total_routers,sizeof(int));
491
        init_worst_case_mapping();
Noah Wolfe's avatar
Noah Wolfe committed
492 493 494 495 496 497 498 499 500
#if PRINT_WORST_CASE_MATCH
        int l;
        for(l=0; l<total_routers; l++)
        {
	        printf("match %d->%d\n",l,worst_dest[l]);
        }
#endif
   }

501 502 503 504 505 506 507 508 509
   tw_run();

 
   if (do_lp_io){
       int ret = lp_io_flush(io_handle, MPI_COMM_WORLD);
       assert(ret == 0 || !"lp_io_flush failure");
   }

   model_net_report_stats(net_id);
Noah Wolfe's avatar
Noah Wolfe committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

    if(rank == 0)
    {
#if PARAMS_LOG
	//Open file to append simulation results
	char log[200];
	sprintf( log, "slimfly-results-log.txt");
	slimfly_results_log_2=fopen(log, "a");
	if(slimfly_results_log_2 == NULL)
		tw_error(TW_LOC, "\n Failed to open slimfly results log file \n");
	printf("Printing Simulation Parameters/Results Log File\n");
	fprintf(slimfly_results_log_2,"%16.3d, %6.2f, %13.2f, ",traffic, load, MEAN_INTERVAL);
#endif
    }

525
/*    if(lp_io_flush(handle, MPI_COMM_WORLD) < 0)
Noah Wolfe's avatar
Noah Wolfe committed
526
    {
527
        assert(ret == 0 || !"lp_io_flush failure");
Noah Wolfe's avatar
Noah Wolfe committed
528 529
        return(-1);
    }
530
*/
Noah Wolfe's avatar
Noah Wolfe committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    tw_end();

    if(rank == 0)
    {
#if PARAMS_LOG
	slimfly_ross_csv_log=fopen("ross.csv", "r");
	if(slimfly_ross_csv_log == NULL)
		tw_error(TW_LOC, "\n Failed to open ross.csv log file \n");
	printf("Reading ROSS specific data from ross.csv and Printing to Slim Fly Log File\n");
	
	char * line = NULL;
	size_t len = 0;
	ssize_t read = getline(&line, &len, slimfly_ross_csv_log);
	while (read != -1) 
	{
		read = getline(&line, &len, slimfly_ross_csv_log);
	}
//		read = getline(&line, &len, slimfly_ross_csv_log);

	char * pch;
	pch = strtok (line,",");
	int idx = 0;
        int gvt_computations;
	long long total_events, rollbacks, net_events;
        float running_time, efficiency, event_rate;
	while (pch != NULL)
	{
		pch = strtok (NULL, ",");
//		printf("%d: %s\n",idx,pch);
		switch(idx)
		{
/*			case 0:
				printf("%s\n",pch);
				break;
			case 1:
				printf("%s\n",pch);
				break;
			case 2:
				printf("%s\n",pch);
				break;
			case 3:
				printf("%s\n",pch);
				break;
			case 4:
				printf("%s\n",pch);
				break;
*/			
			case 4:
				total_events = atoll(pch);
				break;
			case 13:
				rollbacks = atoll(pch);
				break;
			case 17:
				gvt_computations = atoi(pch);
				break;
			case 18:
				net_events = atoll(pch);
				break;
			case 3:
				running_time = atof(pch);
				break;
			case 8:
				efficiency = atof(pch);
				break;
			case 19:
				event_rate = atof(pch);
				break;
		}
		idx++;
	}
	fprintf(slimfly_results_log_2,"%12llu, %10llu, %16d, %10llu, %17.4f, %10.2f, %22.2f\n",total_events,rollbacks,gvt_computations,net_events,running_time,efficiency,event_rate);
	fclose(slimfly_results_log_2);
	fclose(slimfly_ross_csv_log);
#endif
    }
    return 0;
}

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