model-net-mpi-wrklds.c 45.9 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */
#include <ross.h>
7
#include <inttypes.h>
8

9
#include "codes/codes-workload.h"
10 11 12 13
#include "codes/codes.h"
#include "codes/configuration.h"
#include "codes/codes_mapping.h"
#include "codes/model-net.h"
14
#include "codes/rc-stack.h"
15

16
#define TRACE -1
17 18 19 20 21 22 23

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

24 25 26 27 28 29
/* Doing LP IO*/
static char lp_io_dir[256] = {'\0'};
static lp_io_handle io_handle;
static unsigned int lp_io_use_suffix = 0;
static int do_lp_io = 0;

30 31
typedef struct nw_state nw_state;
typedef struct nw_message nw_message;
32
typedef int16_t dumpi_req_id;
33 34 35 36 37 38

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;
39 40
double max_time = 0,  max_comm_time = 0, max_wait_time = 0, max_send_time = 0, max_recv_time = 0;
double avg_time = 0, avg_comm_time = 0, avg_wait_time = 0, avg_send_time = 0, avg_recv_time = 0;
41 42 43 44 45

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

46 47 48
/* runtime option for disabling computation time simulation */
static int disable_delay = 0;

49 50 51
/* MPI_OP_GET_NEXT is for getting next MPI operation when the previous operation completes.
* MPI_SEND_ARRIVED is issued when a MPI message arrives at its destination (the message is transported by model-net and an event is invoked when it arrives. 
* MPI_SEND_POSTED is issued when a MPI message has left the source LP (message is transported via model-net). */
52 53 54 55
enum MPI_NW_EVENTS
{
	MPI_OP_GET_NEXT=1,
	MPI_SEND_ARRIVED,
56
    MPI_SEND_ARRIVED_CB, // for tracking message times on sender
57 58 59
	MPI_SEND_POSTED,
};

60
/* stores pointers of pending MPI operations to be matched with their respective sends/receives. */
61 62
struct mpi_msgs_queue
{
63 64
	struct codes_workload_op * mpi_op;
	struct mpi_msgs_queue * next;
65 66
};

67
/* stores request IDs of completed MPI operations (Isends or Irecvs) */
68 69 70
struct completed_requests
{
	dumpi_req_id req_id;
71
	struct completed_requests * next;
72 73
};

74
/* for wait operations, store the pending operation and number of completed waits so far. */
75 76
struct pending_waits
{
77
	struct codes_workload_op * mpi_op;
78 79 80 81
	int num_completed;
	tw_stime start_time;
};

82
/* maintains the head and tail of the queue, as well as the number of elements currently in queue. Queues are pending_recvs queue (holds unmatched MPI recv operations) and arrival_queue (holds unmatched MPI send messages). */
83 84 85 86 87 88 89 90 91 92 93 94 95 96
struct mpi_queue_ptrs
{
	int num_elems;
	struct mpi_msgs_queue* queue_head;
	struct mpi_msgs_queue* queue_tail;
};

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

97
    struct rc_stack * st;
98 99 100 101 102
	/* count of sends, receives, collectives and delays */
	unsigned long num_sends;
	unsigned long num_recvs;
	unsigned long num_cols;
	unsigned long num_delays;
103 104 105
	unsigned long num_wait;
	unsigned long num_waitall;
	unsigned long num_waitsome;
106 107

	/* time spent by the LP in executing the app trace*/
108
	double start_time;
109
	double elapsed_time;
110
	/* time spent in compute operations */
111 112 113 114 115 116 117
	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;
118
	/* FIFO for isend messages arrived on destination */
119
	struct mpi_queue_ptrs * arrival_queue;
120
	/* FIFO for irecv messages posted but not yet matched with send operations */
121
	struct mpi_queue_ptrs * pending_recvs_queue;
122
	/* list of pending waits (and saved pending wait for reverse computation) */
123
	struct pending_waits * pending_waits;
124
	/* List of completed send/receive requests */
125
	struct completed_requests * completed_reqs;
126 127
};

128 129 130 131
/* data for handling reverse computation.
* saved_matched_req holds the request ID of matched receives/sends for wait operations.
* ptr_match_op holds the matched MPI operation which are removed from the queues when a send is matched with the receive in forward event handler. 
* network event being sent. op is the MPI operation issued by the network workloads API. rv_data holds the data for reverse computation (TODO: Fill this data structure only when the simulation runs in optimistic mode). */
132 133
struct nw_message
{
134
   int msg_type;
135

136 137 138 139 140 141 142 143 144 145 146
   struct
   {
     /* forward event handler */
     struct
     {
        int op_type;
        tw_lpid src_rank;
        tw_lpid dest_rank;
        int num_bytes;
        int data_type;
        double sim_start_time;
147 148
        // for callbacks - time message was received
        double msg_send_time;
149 150 151 152 153 154 155
        int16_t req_id;   
        int tag;
     } msg_info;

     /* required for reverse computation*/
     struct 
      {
156 157 158 159 160 161 162 163 164
        int found_match;
        short matched_op;
        dumpi_req_id saved_matched_req;
        struct codes_workload_op* ptr_match_op;
        struct pending_waits* saved_pending_wait;

        double saved_send_time;
        double saved_recv_time;
        double saved_wait_time;
165 166
      } rc;
  } u;
167 168
};

169
/* executes MPI wait operation */
170 171
static void codes_exec_mpi_wait(
        nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op);
172
/* reverse of mpi wait function. */
173
static void codes_exec_mpi_wait_rc(
174
        nw_state* s, nw_message* m, tw_lp* lp, struct codes_workload_op * mpi_op);
175
/* executes MPI isend and send operations */
176 177
static void codes_exec_mpi_send(
        nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op);
178
/* execute MPI irecv operation */
179 180
static void codes_exec_mpi_recv(
        nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op);
181
/* reverse of mpi recv function. */
182
static void codes_exec_mpi_recv_rc(
183
        nw_state* s, nw_message* m, tw_lp* lp, struct codes_workload_op * mpi_op);
184
/* execute the computational delay */
185 186
static void codes_exec_comp_delay(
        nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op);
187
/* execute collective operation, currently only skips these operations. */
188 189
static void codes_exec_mpi_col(
        nw_state* s, tw_lp* lp);
190
/* gets the next MPI operation from the network-workloads API. */
191 192
static void get_next_mpi_operation(
        nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp);
193
/* reverse handler of get next mpi operation. */
194 195
static void get_next_mpi_operation_rc(
        nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp);
196 197 198 199
/* Makes a call to get_next_mpi_operation. */
static void codes_issue_next_event(tw_lp* lp);

///////////////////// HELPER FUNCTIONS FOR MPI MESSAGE QUEUE HANDLING ///////////////
200
/* upon arrival of local completion message, inserts operation in completed send queue */
201 202
static void update_send_completion_queue(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
203
/* reverse of the above function */
204 205
static void update_send_completion_queue_rc(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
206
/* upon arrival of an isend operation, updates the arrival queue of the network */
207 208
static void update_arrival_queue(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
209
/* reverse of the above function */
210 211
static void update_arrival_queue_rc(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
212
/* callback to a message sender for computing message time */
213 214 215 216 217
static void update_message_time(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
/* reverse for computing message time */
static void update_message_time_rc(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
218

219
/* insert MPI operation in the waiting queue*/
220 221
static void mpi_pending_queue_insert_op(
        struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op* mpi_op);
222
/* remove completed request IDs from the queue for reuse. Reverse of above function. */
223 224
static void remove_req_id(
        struct completed_requests** requests, int16_t req_id);
225
/* remove MPI operation from the waiting queue.*/
226 227
static int mpi_queue_remove_matching_op(
        nw_state* s, tw_lp* lp, nw_message * m, struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op * mpi_op);
228
/* remove the tail of the MPI operation from waiting queue */
229 230
static int mpi_queue_remove_tail(
        tw_lpid lpid, struct mpi_queue_ptrs* mpi_queue);
231
/* insert completed MPI requests in the queue. */
232 233
static void mpi_completed_queue_insert_op(
        struct completed_requests** mpi_completed_queue, dumpi_req_id req_id);
234
/* notifies the wait operations (if any) about the completed receives and sends requests. */
235 236
static int notify_waits(
        nw_state* s, tw_bf* bf, tw_lp* lp, nw_message* m, dumpi_req_id req_id);
237
/* reverse of notify waits function. */
238 239
static void notify_waits_rc(
        nw_state* s, tw_bf* bf, tw_lp* lp, nw_message* m, dumpi_req_id completed_req);
240 241
/* conversion from seconds to eanaoseconds */
static tw_stime s_to_ns(tw_stime ns);
242

243 244 245 246 247
/* helper function - maps an MPI rank to an LP id */
static tw_lpid rank_to_lpid(int rank)
{
    return codes_mapping_get_lpid_from_relative(rank, NULL, "nw-lp", NULL, 0);
}
248

249 250 251 252 253 254 255 256 257 258 259 260
/* initializes the queue and allocates memory */
static struct mpi_queue_ptrs* queue_init()
{
	struct mpi_queue_ptrs* mpi_queue = malloc(sizeof(struct mpi_queue_ptrs));

	mpi_queue->num_elems = 0;
	mpi_queue->queue_head = NULL;
	mpi_queue->queue_tail = NULL;
	
	return mpi_queue;
}

261
/* helper function: counts number of elements in the queue */
262 263
static int numQueue(struct mpi_queue_ptrs* mpi_queue)
{
264
	struct mpi_msgs_queue* tmp = mpi_queue->queue_head;
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	int count = 0;

	while(tmp)
	{
		++count;
		tmp = tmp->next;
	}
	return count;
}

/* prints elements in a send/recv queue */
static void printQueue(tw_lpid lpid, struct mpi_queue_ptrs* mpi_queue, char* msg)
{
	printf("\n ************ Printing the queue %s *************** ", msg);
	struct mpi_msgs_queue* tmp = malloc(sizeof(struct mpi_msgs_queue));
	assert(tmp);

	tmp = mpi_queue->queue_head;
	
	while(tmp)
	{
286
		if(tmp->mpi_op->op_type == CODES_WK_SEND || tmp->mpi_op->op_type == CODES_WK_ISEND)
287 288 289
			printf("\n lpid %ld send operation data type %d count %d tag %d source %d", 
				    lpid, tmp->mpi_op->u.send.data_type, tmp->mpi_op->u.send.count, 
				     tmp->mpi_op->u.send.tag, tmp->mpi_op->u.send.source_rank);
290
		else if(tmp->mpi_op->op_type == CODES_WK_IRECV || tmp->mpi_op->op_type == CODES_WK_RECV)
291 292 293 294 295 296 297 298 299 300 301
			printf("\n lpid %ld recv operation data type %d count %d tag %d source %d", 
				   lpid, tmp->mpi_op->u.recv.data_type, tmp->mpi_op->u.recv.count, 
				    tmp->mpi_op->u.recv.tag, tmp->mpi_op->u.recv.source_rank );
		else
			printf("\n Invalid data type in the queue %d ", tmp->mpi_op->op_type);
		tmp = tmp->next;
	}
	free(tmp);
}

/* re-insert element in the queue at the index --- maintained for reverse computation */
302
static void mpi_queue_update(struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op* mpi_op, int pos)
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
{
	struct mpi_msgs_queue* elem = malloc(sizeof(struct mpi_msgs_queue));
	assert(elem);
	elem->mpi_op = mpi_op;
	
	/* inserting at the head */
	if(pos == 0)
	{
	   if(!mpi_queue->queue_tail)
		mpi_queue->queue_tail = elem;
	   elem->next = mpi_queue->queue_head;
	   mpi_queue->queue_head = elem;
	   mpi_queue->num_elems++;
	   return;
	}

	int index = 0;
	struct mpi_msgs_queue* tmp = mpi_queue->queue_head;
	while(index < pos - 1)
	{
		tmp = tmp->next;
		++index;
	}

	if(!tmp)
		printf("\n Invalid index! %d pos %d size %d ", index, pos, numQueue(mpi_queue));
	if(tmp == mpi_queue->queue_tail)
	    mpi_queue->queue_tail = elem;

	elem->next = tmp->next;
	tmp->next = elem;
	mpi_queue->num_elems++;

	return;
}

339
/* prints the elements of a queue (for debugging purposes). */
340 341 342 343
static void printCompletedQueue(nw_state* s, tw_lp* lp)
{
	   if(TRACE == lp->gid)
	   {
344
	   	printf("\n %lf contents of completed operations queue ", tw_now(lp));
345 346 347 348 349 350 351 352 353
	   	struct completed_requests* current = s->completed_reqs;
	   	while(current)
	    	{
			printf(" %d ",current->req_id);
			current = current->next;
	   	}
	   }
}

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
/* reverse handler of notify_waits function. */
static void notify_waits_rc(nw_state* s, tw_bf* bf, tw_lp* lp, nw_message* m, dumpi_req_id completed_req)
{
   int i;

   /*if(bf->c1)
    {*/
	/* if pending wait is still present and is of type MPI_WAIT then do nothing*/
/*	s->wait_time = s->saved_wait_time; 	
	mpi_completed_queue_insert_op(&s->completed_reqs, completed_req);	
	s->pending_waits = wait_elem;
	s->saved_pending_wait = NULL;
    }
*/
  if(lp->gid == TRACE)
	  printf("\n %lf reverse -- notify waits req id %d ", tw_now(lp), completed_req);
370
  
371
  printCompletedQueue(s, lp);
372
  
373
  if(m->u.rc.matched_op == 1)
374 375
	s->pending_waits->num_completed--;
   /* if a wait-elem exists, it means the request ID has been matched*/
376
   if(m->u.rc.matched_op == 2) 
377
    {
378 379 380 381 382
        if(lp->gid == TRACE)
        {
            printf("\n %lf matched req id %d ", tw_now(lp), completed_req);
            printCompletedQueue(s, lp);
        }
383
        struct pending_waits* wait_elem = m->u.rc.saved_pending_wait;
384 385
        s->wait_time = m->u.rc.saved_wait_time;
        int count = wait_elem->mpi_op->u.waits.count; 
386

387 388
        for( i = 0; i < count; i++ )
            mpi_completed_queue_insert_op(&s->completed_reqs, wait_elem->mpi_op->u.waits.req_ids[i]);
389

390 391 392
        wait_elem->num_completed--;	
        s->pending_waits = wait_elem;
        tw_rand_reverse_unif(lp->rng);
393 394 395 396 397
   }
}

/* notify the completed send/receive request to the wait operation. */
static int notify_waits(nw_state* s, tw_bf* bf, tw_lp* lp, nw_message* m, dumpi_req_id completed_req)
398 399 400 401 402 403 404
{
	int i;
	/* traverse the pending waits list and look what type of wait operations are 
	there. If its just a single wait and the request ID has just been completed, 
	then the network node LP can go on with fetching the next operation from the log.
	If its waitall then wait for all pending requests to complete and then proceed. */
	struct pending_waits* wait_elem = s->pending_waits;
405
	m->u.rc.matched_op = 0;
406 407 408
	
	if(lp->gid == TRACE)
		printf("\n %lf notify waits req id %d ", tw_now(lp), completed_req);
409 410 411

	if(!wait_elem)
		return 0;
412

413 414
	int op_type = wait_elem->mpi_op->op_type;

415
	if(op_type == CODES_WK_WAIT)
416 417 418
	{
		if(wait_elem->mpi_op->u.wait.req_id == completed_req)	
		  {
419
			m->u.rc.saved_wait_time = s->wait_time;
420
			s->wait_time += (tw_now(lp) - wait_elem->start_time);
421
                        remove_req_id(&s->completed_reqs, completed_req);
422
	
423
			m->u.rc.saved_pending_wait = wait_elem;			
424
            s->pending_waits = NULL;
425 426 427 428
			codes_issue_next_event(lp);	
			return 0;
		 }
	}
429
	else if(op_type == CODES_WK_WAITALL)
430
	{
431 432
	   int required_count = wait_elem->mpi_op->u.waits.count;
	  for(i = 0; i < required_count; i++)
433 434
	   {
	    if(wait_elem->mpi_op->u.waits.req_ids[i] == completed_req)
435 436 437
		{
			if(lp->gid == TRACE)
				printCompletedQueue(s, lp);
438
			m->u.rc.matched_op = 1;
439
			wait_elem->num_completed++;	
440
		}
441 442
	   }
	   
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	  if(wait_elem->num_completed == required_count)
	   {
            if(lp->gid == TRACE)
            {
                printf("\n %lf req %d completed %d", tw_now(lp), completed_req, wait_elem->num_completed);
                printCompletedQueue(s, lp);
            }
            m->u.rc.matched_op = 2;
            m->u.rc.saved_wait_time = s->wait_time;
            s->wait_time += (tw_now(lp) - wait_elem->start_time);
            m->u.rc.saved_pending_wait = wait_elem;
            s->pending_waits = NULL; 
            
            for(i = 0; i < required_count; i++)
                remove_req_id(&s->completed_reqs, wait_elem->mpi_op->u.waits.req_ids[i]);	
            
            codes_issue_next_event(lp); //wait completed
460
       }
461
    }
462 463 464
	return 0;
}

465
/* reverse handler of MPI wait operation */
466
static void codes_exec_mpi_wait_rc(nw_state* s, nw_message* m, tw_lp* lp, struct codes_workload_op * mpi_op)
467 468 469 470
{
    if(s->pending_waits)
     {
    	s->pending_waits = NULL;
471
	    return;
472 473 474
     }
   else
    {
475 476 477
 	    mpi_completed_queue_insert_op(&s->completed_reqs, mpi_op->u.wait.req_id);	
	    tw_rand_reverse_unif(lp->rng);	
        rc_stack_pop(s->st);
478 479
    }
}
480 481

/* execute MPI wait operation */
482
static void codes_exec_mpi_wait(nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
483
{
484 485
    /* check in the completed receives queue if the request ID has already been completed.*/
    assert(!s->pending_waits);
486
    dumpi_req_id req_id = mpi_op->u.wait.req_id;
487 488 489 490 491 492 493 494 495 496

    struct completed_requests* current = s->completed_reqs;
    while(current) {
        if(current->req_id == req_id) {
            remove_req_id(&s->completed_reqs, req_id);
            m->u.rc.saved_wait_time = s->wait_time;
            codes_issue_next_event(lp);
            return;
        }
        current = current->next;
497 498
    }

499
    /* If not, add the wait operation in the pending 'waits' list. */
500
    struct pending_waits* wait_op = malloc(sizeof(struct pending_waits));
501
    wait_op->mpi_op = mpi_op;
502 503
    wait_op->num_completed = 0;
    wait_op->start_time = tw_now(lp);
504
    s->pending_waits = wait_op;
505 506

    rc_stack_push(lp, wait_op, free, s->st);
507 508
}

509
static void codes_exec_mpi_wait_all_rc(nw_state* s, nw_message* m, tw_lp* lp, struct codes_workload_op * mpi_op)
510
{
511 512 513 514 515
  if(lp->gid == TRACE)
   {
       printf("\n %lf codes exec mpi waitall reverse %d ", tw_now(lp), m->u.rc.found_match);
       printCompletedQueue(s, lp); 
   } 
516
  if(m->u.rc.found_match)
517
    {
518
        int i;
519
        int count = mpi_op->u.waits.count;
520 521 522 523
        dumpi_req_id req_id[count];

        for( i = 0; i < count; i++)
        {
524
            req_id[i] = mpi_op->u.waits.req_ids[i];
525 526 527 528
            mpi_completed_queue_insert_op(&s->completed_reqs, req_id[i]);
        }
        tw_rand_reverse_unif(lp->rng);
   }
529 530
    else
    {
531
        struct pending_waits* wait_op = s->pending_waits;
532
        rc_stack_pop(s->st);
533 534 535 536 537
        s->pending_waits = NULL;
        assert(!s->pending_waits);
        if(lp->gid == TRACE)
            printf("\n %lf Nullifying codes waitall ", tw_now(lp));
   }
538
}
539 540
static void codes_exec_mpi_wait_all(
        nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
541 542
{
  //assert(!s->pending_waits);
543
  int count = mpi_op->u.waits.count;
544 545 546 547 548 549 550
  int i, num_completed = 0;
  dumpi_req_id req_id[count];
  struct completed_requests* current = s->completed_reqs;

  /* check number of completed irecvs in the completion queue */ 
  if(lp->gid == TRACE)
    {
551
  	printf(" \n (%lf) MPI waitall posted %d count", tw_now(lp), mpi_op->u.waits.count);
552
	for(i = 0; i < count; i++)
553
		printf(" %d ", (int)mpi_op->u.waits.req_ids[i]);
554 555 556 557 558 559
   	printCompletedQueue(s, lp);	 
   }
  while(current) 
   {
	  for(i = 0; i < count; i++)
	   {
560
	     req_id[i] = mpi_op->u.waits.req_ids[i];
561
	     if(req_id[i] == current->req_id)
562
 		    num_completed++;
563 564 565 566 567
   	  }
	 current = current->next;
   }

  if(TRACE== lp->gid)
568
	  printf("\n %lf Num completed %d count %d ", tw_now(lp), num_completed, count);
569

570
  m->u.rc.found_match = 0;
571
  if(count == num_completed)
572
  {
573
	m->u.rc.found_match = 1;
574
	for( i = 0; i < count; i++)	
575
		remove_req_id(&s->completed_reqs, req_id[i]);
576

577 578 579 580 581 582
	codes_issue_next_event(lp);
  }
  else
  {
 	/* If not, add the wait operation in the pending 'waits' list. */
	  struct pending_waits* wait_op = malloc(sizeof(struct pending_waits));
583
	  wait_op->mpi_op = mpi_op;  
584
	  wait_op->num_completed = num_completed;
585
	  wait_op->start_time = tw_now(lp);
586 587
      rc_stack_push(lp, wait_op, free, s->st);
      s->pending_waits = wait_op;
588 589 590 591
  }
}

/* request ID is being reused so delete it from the list once the matching is done */
592 593
static void remove_req_id(
        struct completed_requests** mpi_completed_queue, dumpi_req_id req_id)
594 595 596 597
{
	struct completed_requests* current = *mpi_completed_queue;

	if(!current)
598 599
		tw_error(TW_LOC, "\n REQ ID DOES NOT EXIST");
	
600
    if(current->req_id == req_id)
601 602 603 604 605 606 607 608 609 610 611 612
	{
		*mpi_completed_queue = current->next;
		free(current);
		return;
	}
	
	struct completed_requests* elem;
	while(current->next)
	{
	   elem = current->next;
	   if(elem->req_id == req_id)	
	     {
613 614 615 616
            current->next = elem->next;
            free(elem);
            return;
         }
617 618 619 620 621 622
	   current = current->next;	
	}
	return;
}

/* inserts mpi operation in the completed requests queue */
623 624
static void mpi_completed_queue_insert_op(
        struct completed_requests** mpi_completed_queue, dumpi_req_id req_id)
625 626 627 628 629 630 631 632
{
	struct completed_requests* reqs = malloc(sizeof(struct completed_requests));
	assert(reqs);

	reqs->req_id = req_id;

	if(!(*mpi_completed_queue))	
	{
633 634 635
        reqs->next = NULL;
        *mpi_completed_queue = reqs;
        return;
636 637 638
	}
	reqs->next = *mpi_completed_queue;
	*mpi_completed_queue = reqs;
639
	return;
640 641
}

642
/* insert MPI send or receive operation in the queues starting from tail. Unmatched sends go to arrival queue and unmatched receives go to pending receives queues. */
643 644
static void mpi_pending_queue_insert_op(
        struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op* mpi_op)
645 646 647 648 649 650
{
	/* insert mpi operation */
	struct mpi_msgs_queue* elem = malloc(sizeof(struct mpi_msgs_queue));
	assert(elem);

	elem->mpi_op = mpi_op;
651
    elem->next = NULL;
652 653 654 655 656 657 658

	if(!mpi_queue->queue_head)
	  mpi_queue->queue_head = elem;

	if(mpi_queue->queue_tail)
	    mpi_queue->queue_tail->next = elem;
	
659
    mpi_queue->queue_tail = elem;
660 661 662 663 664 665
	mpi_queue->num_elems++;

	return;
}

/* match the send/recv operations */
666 667
static int match_receive(
        nw_state* s, tw_lp* lp, tw_lpid lpid, struct codes_workload_op* op1, struct codes_workload_op* op2)
668
{
669 670 671 672 673 674 675 676 677
        assert(op1->op_type == CODES_WK_IRECV || op1->op_type == CODES_WK_RECV);
        assert(op2->op_type == CODES_WK_SEND || op2->op_type == CODES_WK_ISEND);

        if((op1->u.recv.num_bytes >= op2->u.send.num_bytes) &&
                   ((op1->u.recv.tag == op2->u.send.tag) || op1->u.recv.tag == -1) &&
                   ((op1->u.recv.source_rank == op2->u.send.source_rank) || op1->u.recv.source_rank == -1))
                   {
                        if(lp->gid == TRACE)
                           printf("\n op1 rank %d bytes %d ", op1->u.recv.source_rank, op1->u.recv.num_bytes);
678
                        s->recv_time += tw_now(lp) - op1->sim_start_time;
679 680 681 682
                        mpi_completed_queue_insert_op(&s->completed_reqs, op1->u.recv.req_id);
                        return 1;
                   }
        return -1;
683 684 685
}

/* used for reverse computation. removes the tail of the queue */
686
static int mpi_queue_remove_tail(tw_lpid lpid, struct mpi_queue_ptrs* mpi_queue)
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
{
	assert(mpi_queue->queue_tail);
	if(mpi_queue->queue_tail == NULL)
	{
		printf("\n Error! tail not updated ");	
		return 0;
	}
	struct mpi_msgs_queue* tmp = mpi_queue->queue_head;

	if(mpi_queue->queue_head == mpi_queue->queue_tail)
	{
		mpi_queue->queue_head = NULL;
		mpi_queue->queue_tail = NULL;
		free(tmp);
		mpi_queue->num_elems--;
		 return 1;
	}

	struct mpi_msgs_queue* elem = mpi_queue->queue_tail;

	while(tmp->next != mpi_queue->queue_tail)
		tmp = tmp->next;

	mpi_queue->queue_tail = tmp;
	mpi_queue->queue_tail->next = NULL;
	mpi_queue->num_elems--;

	free(elem);
	return 1;
}

/* search for a matching mpi operation and remove it from the list. 
 * Record the index in the list from where the element got deleted. 
 * Index is used for inserting the element once again in the queue for reverse computation. */
721
static int mpi_queue_remove_matching_op(nw_state* s, tw_lp* lp, nw_message * m, struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op * mpi_op)
722 723 724 725 726 727 728 729 730
{
	if(mpi_queue->queue_head == NULL)
		return -1;

	/* remove mpi operation */
	struct mpi_msgs_queue* tmp = mpi_queue->queue_head;
	int indx = 0;

	/* if head of the list has the required mpi op to be deleted */
731
	int rcv_val = 0;
732
	if(mpi_op->op_type == CODES_WK_SEND || mpi_op->op_type == CODES_WK_ISEND)
733
	  {
734
		rcv_val = match_receive(s, lp, lp->gid, tmp->mpi_op, mpi_op);
735
		m->u.rc.saved_matched_req = tmp->mpi_op->u.recv.req_id;  
736
	 }
737
	else if(mpi_op->op_type == CODES_WK_RECV || mpi_op->op_type == CODES_WK_IRECV)
738
	  {
739
		rcv_val = match_receive(s, lp, lp->gid, mpi_op, tmp->mpi_op);
740
	  	m->u.rc.saved_matched_req = mpi_op->u.recv.req_id;
741 742
	  }
	if(rcv_val >= 0)
743
	{
744
		/* TODO: fix RC */
745
		m->u.rc.ptr_match_op = tmp->mpi_op;
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
		if(mpi_queue->queue_head == mpi_queue->queue_tail)
		   {
			mpi_queue->queue_tail = NULL;
			mpi_queue->queue_head = NULL;
			 free(tmp);
		   }
		 else
		   {
			mpi_queue->queue_head = tmp->next;
			free(tmp);	
		   }
		mpi_queue->num_elems--;
		return indx;
	}

	/* record the index where matching operation has been found */
	struct mpi_msgs_queue* elem;

	while(tmp->next)	
	{
	   indx++;
	   elem = tmp->next;
768
	   
769
	    if(mpi_op->op_type == CODES_WK_SEND || mpi_op->op_type == CODES_WK_ISEND)
770
	     {
771
		    rcv_val = match_receive(s, lp, lp->gid, elem->mpi_op, mpi_op);
772
	     	m->u.rc.saved_matched_req = elem->mpi_op->u.recv.req_id; 
773
	     }
774
	    else if(mpi_op->op_type == CODES_WK_RECV || mpi_op->op_type == CODES_WK_IRECV)
775
	     {
776 777 778
            rcv_val = match_receive(s, lp, lp->gid, mpi_op, elem->mpi_op);
            m->u.rc.saved_matched_req = mpi_op->u.recv.req_id;
         }
779
   	     if(rcv_val >= 0)
780 781
		 {
		    m->u.rc.ptr_match_op = elem->mpi_op;
782
		    if(elem == mpi_queue->queue_tail)
783
			    mpi_queue->queue_tail = tmp;
784
		    
785 786 787 788
		    tmp->next = elem->next;

		    free(elem);
		    mpi_queue->num_elems--;
789
		
790
		    return indx;
791
		 }
792
	   tmp = tmp->next;
793
    }
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	return -1;
}
/* 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);
}

/* Simulate delays between MPI operations */
813 814
static void codes_exec_comp_delay(
        nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op)
815 816 817 818 819
{
	tw_event* e;
	tw_stime ts;
	nw_message* msg;

820 821 822 823 824 825 826 827 828
        if (disable_delay) {
            ts = 0.0; // no compute time sim
        }
        else {
            s->compute_time += s_to_ns(mpi_op->u.delay.seconds);
            ts = s_to_ns(mpi_op->u.delay.seconds);
        }

	ts += g_tw_lookahead + 0.1 + tw_rand_exponential(lp->rng, noise);
829 830 831 832 833 834
	
	e = tw_event_new( lp->gid, ts , lp );
	msg = tw_event_data(e);
	msg->msg_type = MPI_OP_GET_NEXT;

	tw_event_send(e); 
835
                
836 837 838
}

/* reverse computation operation for MPI irecv */
839
static void codes_exec_mpi_recv_rc(nw_state* s, nw_message* m, tw_lp* lp, struct codes_workload_op * mpi_op)
840
{
841
	num_bytes_recvd -= mpi_op->u.recv.num_bytes;
842 843
	s->recv_time = m->u.rc.saved_recv_time;
	if(m->u.rc.found_match >= 0)
844
	  {
845 846
		s->recv_time = m->u.rc.saved_recv_time;
		mpi_queue_update(s->arrival_queue, m->u.rc.ptr_match_op, m->u.rc.found_match);
847
		remove_req_id(&s->completed_reqs, mpi_op->u.recv.req_id);
848
		tw_rand_reverse_unif(lp->rng);
849
	  }
850
	else if(m->u.rc.found_match < 0)
851
	    {
852
		mpi_queue_remove_tail(lp->gid, s->pending_recvs_queue);
853
		if(mpi_op->op_type == CODES_WK_IRECV)
854
			tw_rand_reverse_unif(lp->rng);
855 856 857 858
	    }
}

/* Execute MPI Irecv operation (non-blocking receive) */ 
859
static void codes_exec_mpi_recv(nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
860 861 862 863 864
{
/* Once an irecv is posted, list of completed sends is checked to find a matching isend.
   If no matching isend is found, the receive operation is queued in the pending queue of
   receive operations. */

865
	m->u.rc.saved_recv_time = s->recv_time;
866
	mpi_op->sim_start_time = tw_now(lp);
867
	num_bytes_recvd += mpi_op->u.recv.num_bytes;
868 869

	if(lp->gid == TRACE)
870
		printf("\n %lf codes exec mpi recv req id %d", tw_now(lp), (int)mpi_op->u.recv.req_id);
871 872
	
	dumpi_req_id req_id;
873
	int found_matching_sends = mpi_queue_remove_matching_op(s, lp, m, s->arrival_queue, mpi_op);
874 875 876
	
	/* save the req id inserted in the completed queue for reverse computation. */
	//m->matched_recv = req_id;
877 878 879

	if(found_matching_sends < 0)
	  {
880
		m->u.rc.found_match = -1;
881 882 883
		mpi_pending_queue_insert_op(s->pending_recvs_queue, mpi_op);
	
	       /* for mpi irecvs, this is a non-blocking receive so just post it and move on with the trace read. */
884
		if(mpi_op->op_type == CODES_WK_IRECV)
885 886 887 888 889 890
		   {
			codes_issue_next_event(lp);	
			return;
		   }
		else
			printf("\n CODES MPI RECV OPERATION!!! ");
891
	  }
892
	else
893
	  {
894
	   	m->u.rc.found_match = found_matching_sends;
895
        codes_issue_next_event(lp); 
896 897 898 899
	 }
}

/* executes MPI send and isend operations */
900
static void codes_exec_mpi_send(nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op)
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
{
	/* model-net event */
	tw_lpid dest_rank;

	codes_mapping_get_lp_info(lp->gid, lp_group_name, &mapping_grp_id, 
	    lp_type_name, &mapping_type_id, annotation, &mapping_rep_id, &mapping_offset);

	if(net_id == DRAGONFLY) /* special handling for the dragonfly case */
	{
		int num_routers, lps_per_rep, factor;
		num_routers = codes_mapping_get_lp_count("MODELNET_GRP", 1,
                  "dragonfly_router", NULL, 1);
	 	lps_per_rep = (2 * num_nw_lps) + num_routers;	
		factor = mpi_op->u.send.dest_rank / num_nw_lps;
		dest_rank = (lps_per_rep * factor) + (mpi_op->u.send.dest_rank % num_nw_lps);	
	}
	else
	{
		/* other cases like torus/simplenet/loggp etc. */
		codes_mapping_get_lp_id(lp_group_name, lp_type_name, NULL, 1,  
	    	  mpi_op->u.send.dest_rank, mapping_offset, &dest_rank);
	}

	num_bytes_sent += mpi_op->u.send.num_bytes;

926 927
	nw_message local_m;
	nw_message remote_m;
928

929 930 931 932 933 934 935 936
    local_m.u.msg_info.sim_start_time = tw_now(lp);
    local_m.u.msg_info.dest_rank = mpi_op->u.send.dest_rank;
    local_m.u.msg_info.src_rank = mpi_op->u.send.source_rank;
    local_m.u.msg_info.op_type = mpi_op->op_type; 
    local_m.msg_type = MPI_SEND_POSTED;
    local_m.u.msg_info.tag = mpi_op->u.send.tag;
    local_m.u.msg_info.num_bytes = mpi_op->u.send.num_bytes;
    local_m.u.msg_info.req_id = mpi_op->u.send.req_id;
937

938 939
    remote_m = local_m;
	remote_m.msg_type = MPI_SEND_ARRIVED;
940

941
	model_net_event(net_id, "test", dest_rank, mpi_op->u.send.num_bytes, 0.0, 
942
	    sizeof(nw_message), (const void*)&remote_m, sizeof(nw_message), (const void*)&local_m, lp);
943

944
	/* isend executed, now get next MPI operation from the queue */ 
945
	if(mpi_op->op_type == CODES_WK_ISEND)
946
	   codes_issue_next_event(lp);
947

948 949 950
}

/* MPI collective operations */
951
static void codes_exec_mpi_col(nw_state* s, tw_lp* lp)
952 953 954 955 956 957 958 959 960 961 962 963 964 965
{
	codes_issue_next_event(lp);
}

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


static void update_send_completion_queue_rc(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
	//mpi_queue_remove_matching_op(&s->completed_isend_queue_head, &s->completed_isend_queue_tail, &m->op, SEND);
966
	if(m->u.msg_info.op_type == CODES_WK_SEND)
967
		tw_rand_reverse_unif(lp->rng);	
968

969
	if(m->u.msg_info.op_type == CODES_WK_ISEND)
970
	  {
971 972
		notify_waits_rc(s, bf, lp, m, m->u.msg_info.req_id);
		remove_req_id(&s->completed_reqs, m->u.msg_info.req_id);
973
	 }
974 975 976 977 978
}

/* completed isends are added in the list */
static void update_send_completion_queue(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
979
	if(TRACE == lp->gid)
980 981
		printf("\n %lf isend operation completed req id %d ", tw_now(lp), m->u.msg_info.req_id);
	if(m->u.msg_info.op_type == CODES_WK_ISEND)
982
	   {	
983 984
		mpi_completed_queue_insert_op(&s->completed_reqs, m->u.msg_info.req_id);
	   	notify_waits(s, bf, lp, m, m->u.msg_info.req_id);
985 986
	   }  
	
987
	/* blocking send operation */
988
	if(m->u.msg_info.op_type == CODES_WK_SEND)
989 990 991 992 993 994 995 996
		codes_issue_next_event(lp);	

	 return;
}

/* reverse handler for updating arrival queue function */
static void update_arrival_queue_rc(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
997
	s->recv_time = m->u.rc.saved_recv_time;
998 999 1000
    codes_local_latency_reverse(lp);
    
    rc_stack_pop(s->st);
1001

1002
	if(m->u.rc.found_match >= 0)
1003
	{
1004 1005
		// TODO: Modify for recvs
		if(lp->gid == TRACE)
1006 1007 1008
			printf("\n %lf reverse-- update arrival queue req ID %d", tw_now(lp), (int) m->u.rc.saved_matched_req);
		dumpi_req_id req_id = m->u.rc.saved_matched_req;
		notify_waits_rc(s, bf, lp, m, m->u.rc.saved_matched_req);
1009
		//int count = numQueue(s->pending_recvs_queue);
1010 1011
		mpi_queue_update(s->pending_recvs_queue, m->u.rc.ptr_match_op, m->u.rc.found_match);
		remove_req_id(&s->completed_reqs, m->u.rc.saved_matched_req);
1012
	
1013 1014 1015
		/*if(lp->gid == TRACE)
			printf("\n Reverse: after adding pending recvs queue %d ", s->pending_recvs_queue->num_elems);*/
	}
1016
	else if(m->u.rc.found_match < 0)
1017
	{
1018
		mpi_queue_remove_tail(lp->gid, s->arrival_queue);	
1019 1020 1021 1022 1023 1024 1025 1026
		/*if(lp->gid == TRACE)
			printf("\n Reverse: after removing arrivals queue %d ", s->arrival_queue->num_elems);*/
	}
}

/* once an isend operation arrives, the pending receives queue is checked to find out if there is a irecv that has already been posted. If no isend has been posted, */
static void update_arrival_queue(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
1027 1028
	//int count_before = numQueue(s->pending_recvs_queue);
	int is_blocking = 0; /* checks if the recv operation was blocking or not */
1029

1030
	m->u.rc.saved_recv_time = s->recv_time;
1031

1032 1033 1034 1035 1036 1037 1038 1039
    // send a callback to the sender to increment times
    tw_event *e_callback =
        tw_event_new(rank_to_lpid(m->u.msg_info.src_rank),
                codes_local_latency(lp), lp);
    nw_message *m_callback = tw_event_data(e_callback);
    m_callback->msg_type = MPI_SEND_ARRIVED_CB;
    m_callback->u.msg_info.msg_send_time = tw_now(lp) - m->u.msg_info.sim_start_time;
    tw_event_send(e_callback);
1040 1041 1042 1043

        /*NOTE: this computes send time with respect to the receiver, not the
         * sender
         * s->send_time += tw_now(lp) - m->u.msg_info.sim_start_time; */
1044 1045
	dumpi_req_id req_id = -1;

1046
        /* Now reconstruct the mpi op */
1047 1048 1049 1050 1051 1052 1053 1054
    struct codes_workload_op * arrived_op = (struct codes_workload_op *) malloc(sizeof(struct codes_workload_op));
    arrived_op->sim_start_time = m->u.msg_info.sim_start_time;
    arrived_op->op_type = m->u.msg_info.op_type;
    arrived_op->u.send.source_rank = m->u.msg_info.src_rank;
    arrived_op->u.send.dest_rank = m->u.msg_info.dest_rank;
    arrived_op->u.send.num_bytes = m->u.msg_info.num_bytes;
    arrived_op->u.send.tag = m->u.msg_info.tag;
    arrived_op->u.send.req_id = m->u.msg_info.req_id;
1055
    rc_stack_push(lp, arrived_op, free, s->st);
1056 1057 1058 1059

    int found_matching_recv = mpi_queue_remove_matching_op(s, lp, m, s->pending_recvs_queue, arrived_op);

    if(TRACE == lp->gid)
1060
        printf("\n %lf update arrival queue req id %d %d", tw_now(lp), arrived_op->u.send.req_id, arrived_op->u.send.source_rank);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    if(found_matching_recv < 0)
     {
        m->u.rc.found_match = -1;
        mpi_pending_queue_insert_op(s->arrival_queue, arrived_op);
    }
    else
      {
        m->u.rc.found_match = found_matching_recv;
        notify_waits(s, bf, lp, m, m->u.rc.saved_matched_req);
      }
1071 1072
}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
static void update_message_time(
        nw_state * s,
        tw_bf * bf,
        nw_message * m,
        tw_lp * lp)
{
    m->u.rc.saved_send_time = s->send_time;
    s->send_time += m->u.msg_info.msg_send_time;
}

static void update_message_time_rc(
        nw_state * s,
        tw_bf * bf,
        nw_message * m,
        tw_lp * lp)
{
    s->send_time = m->u.rc.saved_send_time;
}

1092 1093 1094 1095
/* 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 */
1096
   char * params = NULL;
1097 1098 1099 1100 1101
   dumpi_trace_params params_d;
  
   codes_mapping_get_lp_info(lp->gid, lp_group_name, &mapping_grp_id, lp_type_name, 
	&mapping_type_id, annotation, &mapping_rep_id, &mapping_offset);
  
1102
   memset(s, 0, sizeof(*s));
1103
   s->nw_id = (mapping_rep_id * num_nw_lps) + mapping_offset;
1104 1105 1106
   s->completed_reqs = NULL;

   s->pending_waits = NULL;
1107 1108 1109
   if(!num_net_traces) 
	num_net_traces = num_net_lps;

1110
   if (strcmp(workload_type, "dumpi") == 0){
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
       strcpy(params_d.file_name, workload_file);
       params_d.num_net_traces = num_net_traces;

       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;
     }
1122 1123 1124 1125 1126

   /* Initialize the RC stack */
   rc_stack_create(&s->st);
   assert(s->st != NULL);

1127
   wrkld_id = codes_workload_load("dumpi-trace-workload", params, 0, (int)s->nw_id);
1128 1129 1130 1131

   s->arrival_queue = queue_init(); 
   s->pending_recvs_queue = queue_init();

1132 1133
   /* clock starts when the first event is processed */
   s->start_time = tw_now(lp);
1134 1135 1136 1137 1138 1139 1140
   codes_issue_next_event(lp);

   return;
}

void nw_test_event_handler(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
1141
	*(int *)bf = (int)0;
1142 1143 1144
    rc_stack_gc(lp, s->st);

    switch(m->msg_type)
1145 1146 1147 1148 1149 1150 1151 1152 1153
	{
		case MPI_SEND_POSTED:
			update_send_completion_queue(s, bf, m, lp);
		break;

		case MPI_SEND_ARRIVED:
			update_arrival_queue(s, bf, m, lp);
		break;

1154 1155 1156 1157
		case MPI_SEND_ARRIVED_CB:
			update_message_time(s, bf, m, lp);
		break;

1158 1159 1160 1161 1162 1163 1164 1165
		case MPI_OP_GET_NEXT:
			get_next_mpi_operation(s, bf, m, lp);	
		break; 
	}
}

static void get_next_mpi_operation_rc(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
1166 1167 1168 1169 1170 1171
    struct codes_workload_op * mpi_op = 
        (struct codes_workload_op *)rc_stack_pop(s->st);
	
    codes_workload_get_next_rc(wrkld_id, 0, (int)s->nw_id, mpi_op);

	if(mpi_op->op_type == CODES_WK_END)
1172
		return;
1173

1174
	switch(mpi_op->op_type)
1175
	{
1176 1177
		case CODES_WK_SEND:
		case CODES_WK_ISEND:
1178
		{
1179
			if(lp->gid == TRACE)
1180 1181 1182
				printf("\n %lf reverse send req %d ", tw_now(lp), (int)mpi_op->u.send.req_id);
			model_net_event_rc(net_id, lp, mpi_op->u.send.num_bytes);
			if(mpi_op->op_type == CODES_WK_ISEND)
1183 1184
				tw_rand_reverse_unif(lp->rng);	
			s->num_sends--;
1185
			num_bytes_sent -= mpi_op->u.send.num_bytes;
1186 1187
		}
		break;
1188 1189

		case CODES_WK_IRECV:
1190
		case CODES_WK_RECV:
1191
		{
1192
			codes_exec_mpi_recv_rc(s, m, lp, mpi_op);
1193 1194 1195
			s->num_recvs--;
		}
		break;
1196
		case CODES_WK_DELAY:
1197 1198
		{
			s->num_delays--;
1199 1200
                        if (!disable_delay) {
                            tw_rand_reverse_unif(lp->rng);
1201
                            s->compute_time -= s_to_ns(mpi_op->u.delay.seconds);
1202
                        }
1203 1204
		}
		break;
1205 1206 1207 1208 1209 1210 1211 1212
		case CODES_WK_BCAST:
		case CODES_WK_ALLGATHER:
		case CODES_WK_ALLGATHERV:
		case CODES_WK_ALLTOALL:
		case CODES_WK_ALLTOALLV:
		case CODES_WK_REDUCE:
		case CODES_WK_ALLREDUCE:
		case CODES_WK_COL:
1213 1214 1215 1216 1217
		{
			s->num_cols--;
			tw_rand_reverse_unif(lp->rng);
		}
		break;
1218
	
1219
		case CODES_WK_WAIT:
1220
		{
1221
			s->num_wait--;
1222
			codes_exec_mpi_wait_rc(s, m, lp, mpi_op);
1223 1224
		}
		break;
1225
		case CODES_WK_WAITALL:
1226 1227
		{
			s->num_waitall--;
1228
			codes_exec_mpi_wait_all_rc(s, m, lp, mpi_op);
1229 1230
		}
		break;
1231 1232
		case CODES_WK_WAITSOME:
		case CODES_WK_WAITANY:
1233
		{
1234 1235
			s->num_waitsome--;
			tw_rand_reverse_unif(lp->rng);
1236 1237
		}
		break;
1238
		default:
1239
			printf("\n Invalid op type %d ", mpi_op->op_type);
1240 1241 1242 1243 1244
	}
}

static void get_next_mpi_operation(nw_state* s, tw_bf * bf, nw_message * m, tw_lp * lp)
{
1245 1246
		struct codes_workload_op * mpi_op = malloc(sizeof(struct codes_workload_op));
        codes_workload_get_next(wrkld_id, 0, (int)s->nw_id, mpi_op);
1247
        rc_stack_push(lp, mpi_op, free, s->st);
1248 1249 1250 1251 1252 1253 1254

        if(mpi_op->op_type == CODES_WK_END)
        {
            s->elapsed_time = tw_now(lp) - s->start_time;
            return;
        }
		switch(mpi_op->op_type)
1255
		{
1256 1257
			case CODES_WK_SEND:
			case CODES_WK_ISEND:
1258 1259
			 {
				s->num_sends++;
1260
				codes_exec_mpi_send(s, lp, mpi_op);
1261 1262
			 }
			break;
1263
	
1264 1265
			case CODES_WK_RECV:
			case CODES_WK_IRECV:
1266 1267
			  {
				s->num_recvs++;
1268
				codes_exec_mpi_recv(s, lp, m, mpi_op);
1269 1270 1271
			  }
			break;

1272
			case CODES_WK_DELAY:
1273 1274
			  {
				s->num_delays++;
1275
				codes_exec_comp_delay(s, lp, mpi_op);
1276 1277 1278
			  }
			break;

1279 1280 1281 1282 1283 1284 1285 1286
			case CODES_WK_BCAST:
			case CODES_WK_ALLGATHER:
			case CODES_WK_ALLGATHERV:
			case CODES_WK_ALLTOALL:
			case CODES_WK_ALLTOALLV:
			case CODES_WK_REDUCE:
			case CODES_WK_ALLREDUCE:
			case CODES_WK_COL:
1287 1288
            case CODES_WK_WAITSOME:
            case CODES_WK_WAITANY:
1289 1290
			  {
				s->num_cols++;
1291
				codes_exec_mpi_col(s, lp);
1292 1293
			  }
			break;
1294
			case CODES_WK_WAIT:
1295 1296
			{
				s->num_wait++;
1297
				codes_exec_mpi_wait(s, lp, m, mpi_op);	
1298 1299
			}
			break;
1300
			case CODES_WK_WAITALL:
1301 1302
			{
				s->num_waitall++;
1303
				codes_exec_mpi_wait_all(s, lp, m, mpi_op);
1304 1305