model-net-dumpi-traces-dump.c 12.2 KB
Newer Older
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 43 44 45 46 47 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 167 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 208 209 210 211 212 213 214 215 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 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 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
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */
#include <ross.h>
#include <inttypes.h>

#include "codes/codes-nw-workload.h"
#include "codes/codes.h"
#include "codes/configuration.h"
#include "codes/codes_mapping.h"

#define TRACE -1
#define DEBUG 0

char workload_type[128];
char workload_file[8192];
char offset_file[8192];
static int wrkld_id;

typedef struct nw_state nw_state;
typedef struct nw_message nw_message;

static int net_id = 0;
static float noise = 5.0;
static int num_net_lps, num_nw_lps;
long long num_bytes_sent=0;
long long num_bytes_recvd=0;

double total_time = 0;
double avg_time = 0;
double avg_comm_time = 0;
double avg_wait_time = 0;
double avg_send_time = 0;
double avg_recv_time = 0;
double avg_col_time = 0;
double avg_compute_time = 0;

long total_waits = 0;
long total_collectives = 0;
long total_sends = 0;
long total_recvs = 0;
long total_delays = 0;

/* global variables for codes mapping */
static char lp_group_name[MAX_NAME_LENGTH], lp_type_name[MAX_NAME_LENGTH], annotation[MAX_NAME_LENGTH];
static int mapping_grp_id, mapping_type_id, mapping_rep_id, mapping_offset;

enum MPI_NW_EVENTS
{
	MPI_OP_GET_NEXT=1,
};

/* state of the network LP. It contains the pointers to send/receive lists */
struct nw_state
{
	long num_events_per_lp;
	tw_lpid nw_id;
	short wrkld_end;

	/* count of sends, receives, collectives and delays */
	unsigned long num_sends;
	unsigned long num_recvs;
	unsigned long num_cols;
	unsigned long num_delays;
	unsigned long num_wait;
	unsigned long num_waitall;
	unsigned long num_waitsome;
	unsigned long num_waitany;

	/* time spent by the LP in executing the app trace*/
	double elapsed_time;

	/* time spent in compute operations */
	double compute_time;

	/* time spent in message send/isend */
	double send_time;

	/* time spent in message receive */
	double recv_time;
	
	/* time spent in wait operation */
	double wait_time;

	/* time spent in collective operations*/
	double col_time;
	
	/* total time spent in operations */
	double total_time;
};

/* network event being sent. msg_type is the type of message being sent, found_match is the index of the list maintained for reverse computation, op is the MPI event to be executed/reversed */
struct nw_message
{
	int msg_type;
        struct mpi_event_list op;
};

/* initialize queues, get next operation */
static void get_next_mpi_operation(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp);

/* conversion from seconds to nanaoseconds */
static tw_stime s_to_ns(tw_stime ns);

/* issue next event */
static void codes_issue_next_event(tw_lp* lp);

/* Trigger getting next event at LP */
static void codes_issue_next_event(tw_lp* lp)
{
   tw_event *e;
   nw_message* msg;

   tw_stime ts;

   ts = g_tw_lookahead + 0.1 + tw_rand_exponential(lp->rng, noise);
   e = tw_event_new( lp->gid, ts, lp );
   msg = tw_event_data(e);

   msg->msg_type = MPI_OP_GET_NEXT;
   tw_event_send(e);
}

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

/* initializes the network node LP, loads the trace file in the structs, calls the first MPI operation to be executed */
void nw_test_init(nw_state* s, tw_lp* lp)
{
   /* initialize the LP's and load the data */
   char * params;
   scala_trace_params params_sc;
   dumpi_trace_params params_d;
  
   s->nw_id = lp->gid;
   s->wrkld_end = 0;

   s->num_sends = 0;
   s->num_recvs = 0;
   s->num_cols = 0;
   s->num_delays = 0;
   s->num_wait = 0;
   s->num_waitall = 0;
   s->num_waitsome = 0;
   s->num_waitany = 0;
   s->elapsed_time = 0;
   s->compute_time = 0;

   if (strcmp(workload_type, "dumpi") == 0){
       strcpy(params_d.file_name, workload_file);
       params_d.num_net_traces = num_net_lps;

       params = (char*)&params_d;
   }
  /* In this case, the LP will not generate any workload related events*/
   if(s->nw_id >= params_d.num_net_traces)
     {
	//printf("\n network LP not generating events %d ", (int)s->nw_id);
	return;
     }
   wrkld_id = codes_nw_workload_load("dumpi-trace-workload", params, (int)s->nw_id);

   /* clock starts ticking */
   s->elapsed_time = tw_now(lp);
   codes_issue_next_event(lp);

   return;
}

void nw_test_event_handler(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
	switch(m->msg_type)
	{
		case MPI_OP_GET_NEXT:
			get_next_mpi_operation(s, bf, m, lp);	
		break;

		default: 
			printf("\n Incorrect event handler ");
		break;
	}
}

void nw_test_event_handler_rc(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
        codes_nw_workload_get_next_rc(wrkld_id, (int)s->nw_id, &m->op);
        if(m->op.op_type == CODES_NW_END)
                return;

	s->total_time -= (m->op.end_time - m->op.start_time);
        switch(m->op.op_type)
        {
		case CODES_NW_SEND:
                case CODES_NW_ISEND:
		{
			s->num_sends--;
			s->send_time -= (m->op.end_time - m->op.start_time);
			num_bytes_sent -= m->op.u.send.num_bytes;
		};
		break;

		case CODES_NW_RECV:
		case CODES_NW_IRECV:
		{
			s->num_recvs--;
			s->recv_time -= (m->op.end_time - m->op.start_time);	
			num_bytes_recvd -= m->op.u.recv.num_bytes;
		}
		break;

		case CODES_NW_DELAY:
		{
			s->num_delays--;
			s->compute_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		case CODES_NW_BCAST:
		case CODES_NW_ALLGATHER:
		case CODES_NW_ALLGATHERV:
		case CODES_NW_ALLTOALL:
		case CODES_NW_ALLTOALLV:
		case CODES_NW_REDUCE:
		case CODES_NW_ALLREDUCE:
		case CODES_NW_COL:
		{
			s->num_cols--;
			s->col_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		case CODES_NW_WAIT:
		{
			s->num_wait--;
			s->wait_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		case CODES_NW_WAITALL:
		{
			s->num_waitall--;
			s->wait_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		case CODES_NW_WAITSOME:
		{
			s->num_waitsome--;
			s->wait_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		case CODES_NW_WAITANY:
		{
			s->num_waitany--;
			s->wait_time -= (m->op.end_time - m->op.start_time);
		}
		break;

		default:
		{
			printf("\n Invalid op type %d", m->op.op_type);
			return;
		}
	}
	tw_rand_reverse_unif(lp->rng);		
}

static void get_next_mpi_operation(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
		mpi_event_list mpi_op;
    		codes_nw_workload_get_next(wrkld_id, (int)s->nw_id, &mpi_op);
		memcpy(&m->op, &mpi_op, sizeof(struct mpi_event_list));

    		if(mpi_op.op_type == CODES_NW_END)
    	 	{
			return;
     		}
		
		s->total_time += (mpi_op.end_time - mpi_op.start_time);
		
		switch(mpi_op.op_type)
		{
			case CODES_NW_SEND:
			case CODES_NW_ISEND:
			 {
				s->num_sends++;
				s->send_time += (mpi_op.end_time - mpi_op.start_time);
				num_bytes_sent += mpi_op.u.send.num_bytes; 
			 }
			break;
	
			case CODES_NW_RECV:
			case CODES_NW_IRECV:
			  {
				s->num_recvs++;
				s->recv_time += (mpi_op.end_time - mpi_op.start_time);
				num_bytes_recvd += mpi_op.u.recv.num_bytes;
			  }
			break;

			case CODES_NW_DELAY:
			  {
				s->num_delays++;
				s->compute_time += (mpi_op.end_time - mpi_op.start_time);
			  }
			break;

			case CODES_NW_BCAST:
			case CODES_NW_ALLGATHER:
			case CODES_NW_ALLGATHERV:
			case CODES_NW_ALLTOALL:
			case CODES_NW_ALLTOALLV:
			case CODES_NW_REDUCE:
			case CODES_NW_ALLREDUCE:
			case CODES_NW_COL:
			  {
				s->num_cols++;
				s->col_time += (mpi_op.end_time - mpi_op.start_time);
			  }
			break;
			case CODES_NW_WAIT:
			{
				s->num_wait++;
				s->wait_time += (mpi_op.end_time - mpi_op.start_time);
			}
			break;
			case CODES_NW_WAITALL:
			{
				s->num_waitall++;
				s->wait_time += (mpi_op.end_time - mpi_op.start_time);	
			}
			break;
			case CODES_NW_WAITSOME:
			{
				s->num_waitsome++;
				s->wait_time += (mpi_op.end_time - mpi_op.start_time);
			}
			break;

			case CODES_NW_WAITANY:
			{
			   s->num_waitany++;
			   s->wait_time += (mpi_op.end_time - mpi_op.start_time);
			}
			break;
			default:
			{
				printf("\n Invalid op type %d ", m->op.op_type);
				return;
			}
		}
		codes_issue_next_event(lp);
}

void nw_test_finalize(nw_state* s, tw_lp* lp)
{
		total_waits += (s->num_wait + s->num_waitall + s->num_waitsome + s->num_waitany);
		total_recvs += (s->num_recvs);
		total_sends += (s->num_sends);
		total_delays += s->num_delays;
		total_collectives += s->num_cols;
		
		printf("\n LP %ld total sends %ld receives %ld wait_alls %ld waits %ld ", lp->gid, s->num_sends,s->num_recvs, s->num_waitall, s->num_wait);
	        avg_time += s->total_time;
		avg_compute_time += s->compute_time;
                avg_comm_time += (s->total_time - s->compute_time);
                avg_wait_time += s->wait_time;
                avg_send_time += s->send_time;
                avg_recv_time += s->recv_time;
		avg_col_time += s->col_time;
}

const tw_optdef app_opt [] =
{
	TWOPT_GROUP("Network workload test"),
    	TWOPT_CHAR("workload_type", workload_type, "workload type (either \"scalatrace\" or \"dumpi\")"),
	TWOPT_CHAR("workload_file", workload_file, "workload file name"),
	TWOPT_CHAR("offset_file", offset_file, "offset file name"),
	TWOPT_END()
};

tw_lptype nw_lp = {
    (init_f) nw_test_init,
    (pre_run_f) NULL,
    (event_f) nw_test_event_handler,
    (revent_f) nw_test_event_handler_rc,
    (final_f) nw_test_finalize,
    (map_f) codes_mapping,
    sizeof(nw_state)
};

const tw_lptype* nw_get_lp_type()
{
            return(&nw_lp);
}

static void nw_add_lp_type()
{
  lp_type_register("nw-lp", nw_get_lp_type());
}

int main( int argc, char** argv )
{
  int rank, nprocs;
  int num_nets;
  int* net_ids;

  g_tw_ts_end = s_to_ns(60*60*24*365); /* one year, in nsecs */

  workload_type[0]='\0';
  tw_opt_add(app_opt);
  tw_init(&argc, &argv);

  if(strlen(workload_file) == 0)
    {
	if(tw_ismaster())
		printf("\n Usage: mpirun -np n ./codes-nw-test --sync=1/2/3 --workload_type=type --workload_file=workload-file-name");
	tw_end();
	return -1;
    }

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

   configuration_load(argv[2], MPI_COMM_WORLD, &config);

   nw_add_lp_type();

   codes_mapping_setup();

   num_net_lps = codes_mapping_get_lp_count("MODELNET_GRP", 0, "nw-lp", NULL, 0);
   
    tw_run();

    long long total_bytes_sent, total_bytes_recvd;
    double avg_run_time;
    double avg_comm_run_time;
    double avg_col_run_time;
    double total_avg_send_time;
    double total_avg_wait_time;
    double total_avg_recv_time;
    double total_avg_col_time;
    double total_avg_comp_time;
    long overall_sends, overall_recvs, overall_waits, overall_cols;
	
    MPI_Reduce(&num_bytes_sent, &total_bytes_sent, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
    MPI_Reduce(&num_bytes_recvd, &total_bytes_recvd, 1, MPI_LONG_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&avg_time, &avg_run_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

   MPI_Reduce(&avg_recv_time, &total_avg_recv_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&avg_comm_time, &avg_comm_run_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&avg_col_time, &avg_col_run_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
  MPI_Reduce(&avg_wait_time, &total_avg_wait_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&avg_send_time, &total_avg_send_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&avg_compute_time, &total_avg_comp_time, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&total_sends, &overall_sends, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&total_recvs, &overall_recvs, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&total_waits, &overall_waits, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
   MPI_Reduce(&total_collectives, &overall_cols, 1, MPI_LONG, MPI_SUM, 0, MPI_COMM_WORLD);

   if(!g_tw_mynode)
	printf("\n Total bytes sent %lld recvd %lld \n avg runtime %lf \n avg comm time %lf avg compute time %lf \n avg collective time %lf avg send time %lf \n avg recv time %lf \n avg wait time %lf \n total sends %ld total recvs %ld total waits %ld total collectives %ld ", total_bytes_sent, total_bytes_recvd, 
			avg_run_time/num_net_lps,
			avg_comm_run_time/num_net_lps,
			total_avg_comp_time/num_net_lps,
			total_avg_col_time/num_net_lps,
			total_avg_send_time/num_net_lps,
			total_avg_recv_time/num_net_lps,
			total_avg_wait_time/num_net_lps,
			overall_sends, overall_recvs, overall_waits, overall_cols);
   tw_end();
  
  return 0;
}