model-net-mpi-wrklds.c 44.4 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
14
#include "codes/codes.h"
#include "codes/configuration.h"
#include "codes/codes_mapping.h"
#include "codes/model-net.h"

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

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

typedef struct nw_state nw_state;
typedef struct nw_message nw_message;
25
typedef int16_t dumpi_req_id;
26
27
28
29
30
31

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;
32
33
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;
34
35
36
37
38

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

39
40
41
/* runtime option for disabling computation time simulation */
static int disable_delay = 0;

42
43
44
/* 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). */
45
46
47
48
enum MPI_NW_EVENTS
{
	MPI_OP_GET_NEXT=1,
	MPI_SEND_ARRIVED,
49
    MPI_SEND_ARRIVED_CB, // for tracking message times on sender
50
51
52
	MPI_SEND_POSTED,
};

53
/* stores pointers of pending MPI operations to be matched with their respective sends/receives. */
54
55
struct mpi_msgs_queue
{
56
57
	struct codes_workload_op * mpi_op;
	struct mpi_msgs_queue * next;
58
59
};

60
/* stores request IDs of completed MPI operations (Isends or Irecvs) */
61
62
63
struct completed_requests
{
	dumpi_req_id req_id;
64
	struct completed_requests * next;
65
66
};

67
/* for wait operations, store the pending operation and number of completed waits so far. */
68
69
struct pending_waits
{
70
	struct codes_workload_op * mpi_op;
71
72
73
74
	int num_completed;
	tw_stime start_time;
};

75
/* 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). */
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;

	/* count of sends, receives, collectives and delays */
	unsigned long num_sends;
	unsigned long num_recvs;
	unsigned long num_cols;
	unsigned long num_delays;
95
96
97
	unsigned long num_wait;
	unsigned long num_waitall;
	unsigned long num_waitsome;
98
99

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

120
121
122
123
/* 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). */
124
125
struct nw_message
{
126
127
128
   int msg_type;
   /* for reverse computation */
   struct codes_workload_op * op;
129

130
131
132
133
134
135
136
137
138
139
140
   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;
141
142
        // for callbacks - time message was received
        double msg_send_time;
143
144
145
146
147
148
149
        int16_t req_id;   
        int tag;
     } msg_info;

     /* required for reverse computation*/
     struct 
      {
150
151
152
153
154
155
156
157
158
        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;
159
160
      } rc;
  } u;
161
162
};

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

///////////////////// HELPER FUNCTIONS FOR MPI MESSAGE QUEUE HANDLING ///////////////
194
/* upon arrival of local completion message, inserts operation in completed send queue */
195
196
static void update_send_completion_queue(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
197
/* reverse of the above function */
198
199
static void update_send_completion_queue_rc(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
200
/* upon arrival of an isend operation, updates the arrival queue of the network */
201
202
static void update_arrival_queue(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
203
/* reverse of the above function */
204
205
static void update_arrival_queue_rc(
        nw_state*s, tw_bf* bf, nw_message* m, tw_lp * lp);
206
/* callback to a message sender for computing message time */
207
208
209
210
211
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);
212

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

237
238
239
240
241
/* 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);
}
242

243
244
245
246
247
248
249
250
251
252
253
254
/* 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;
}

255
/* helper function: counts number of elements in the queue */
256
257
static int numQueue(struct mpi_queue_ptrs* mpi_queue)
{
258
	struct mpi_msgs_queue* tmp = mpi_queue->queue_head;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
	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)
	{
280
		if(tmp->mpi_op->op_type == CODES_WK_SEND || tmp->mpi_op->op_type == CODES_WK_ISEND)
281
282
283
			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);
284
		else if(tmp->mpi_op->op_type == CODES_WK_IRECV || tmp->mpi_op->op_type == CODES_WK_RECV)
285
286
287
288
289
290
291
292
293
294
295
			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 */
296
static void mpi_queue_update(struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op* mpi_op, int pos)
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
{
	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;
}

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

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* 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);
364
  
365
  printCompletedQueue(s, lp);
366
  
367
  if(m->u.rc.matched_op == 1)
368
369
	s->pending_waits->num_completed--;
   /* if a wait-elem exists, it means the request ID has been matched*/
370
   if(m->u.rc.matched_op == 2) 
371
    {
372
373
374
375
376
        if(lp->gid == TRACE)
        {
            printf("\n %lf matched req id %d ", tw_now(lp), completed_req);
            printCompletedQueue(s, lp);
        }
377
        struct pending_waits* wait_elem = m->u.rc.saved_pending_wait;
378
379
        s->wait_time = m->u.rc.saved_wait_time;
        int count = wait_elem->mpi_op->u.waits.count; 
380

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

384
385
386
        wait_elem->num_completed--;	
        s->pending_waits = wait_elem;
        tw_rand_reverse_unif(lp->rng);
387
388
389
390
391
392

   }
}

/* 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)
393
394
395
396
397
398
399
{
	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;
400
	m->u.rc.matched_op = 0;
401
402
403
	
	if(lp->gid == TRACE)
		printf("\n %lf notify waits req id %d ", tw_now(lp), completed_req);
404
405
406

	if(!wait_elem)
		return 0;
407

408
409
	int op_type = wait_elem->mpi_op->op_type;

410
	if(op_type == CODES_WK_WAIT)
411
412
413
	{
		if(wait_elem->mpi_op->u.wait.req_id == completed_req)	
		  {
414
			m->u.rc.saved_wait_time = s->wait_time;
415
			s->wait_time += (tw_now(lp) - wait_elem->start_time);
416
                        remove_req_id(&s->completed_reqs, completed_req);
417
	
418
			m->u.rc.saved_pending_wait = wait_elem;			
419
420
421
422
423
			s->pending_waits = NULL;
			codes_issue_next_event(lp);	
			return 0;
		 }
	}
424
	else if(op_type == CODES_WK_WAITALL)
425
	{
426
427
	   int required_count = wait_elem->mpi_op->u.waits.count;
	  for(i = 0; i < required_count; i++)
428
429
	   {
	    if(wait_elem->mpi_op->u.waits.req_ids[i] == completed_req)
430
431
432
		{
			if(lp->gid == TRACE)
				printCompletedQueue(s, lp);
433
			m->u.rc.matched_op = 1;
434
			wait_elem->num_completed++;	
435
		}
436
437
	   }
	   
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	  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
          }
    }
457
458
459
	return 0;
}

460
461
462
463
464
465
/* reverse handler of MPI wait operation */
static void codes_exec_mpi_wait_rc(nw_state* s, tw_bf* bf, nw_message* m, tw_lp* lp)
{
    if(s->pending_waits)
     {
    	s->pending_waits = NULL;
466
	    return;
467
468
469
     }
   else
    {
470
471
 	    mpi_completed_queue_insert_op(&s->completed_reqs, m->op->u.wait.req_id);	
	    tw_rand_reverse_unif(lp->rng);		
472
473
    }
}
474
475

/* execute MPI wait operation */
476
static void codes_exec_mpi_wait(nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
477
{
Jonathan Jenkins's avatar
Jonathan Jenkins committed
478
479
    /* check in the completed receives queue if the request ID has already been completed.*/
    assert(!s->pending_waits);
480
    dumpi_req_id req_id = mpi_op->u.wait.req_id;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
481
482
483
484
485
486
487
488
489
490

    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;
491
492
    }

Jonathan Jenkins's avatar
Jonathan Jenkins committed
493
    /* If not, add the wait operation in the pending 'waits' list. */
494
    struct pending_waits* wait_op = malloc(sizeof(struct pending_waits));
495
    wait_op->mpi_op = mpi_op;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
496
497
    wait_op->num_completed = 0;
    wait_op->start_time = tw_now(lp);
498
    s->pending_waits = wait_op;
499
500
}

501
static void codes_exec_mpi_wait_all_rc(nw_state* s, tw_bf* bf, nw_message* m, tw_lp* lp)
502
{
503
504
505
506
507
  if(lp->gid == TRACE)
   {
       printf("\n %lf codes exec mpi waitall reverse %d ", tw_now(lp), m->u.rc.found_match);
       printCompletedQueue(s, lp); 
   } 
508
  if(m->u.rc.found_match)
509
    {
510
511
512
513
514
515
516
517
518
519
520
        int i;
        int count = m->op->u.waits.count;
        dumpi_req_id req_id[count];

        for( i = 0; i < count; i++)
        {
            req_id[i] = m->op->u.waits.req_ids[i];
            mpi_completed_queue_insert_op(&s->completed_reqs, req_id[i]);
        }
        tw_rand_reverse_unif(lp->rng);
   }
521
522
    else
    {
523
524
525
526
527
528
529
        struct pending_waits* wait_op = s->pending_waits;
        free(wait_op);
        s->pending_waits = NULL;
        assert(!s->pending_waits);
        if(lp->gid == TRACE)
            printf("\n %lf Nullifying codes waitall ", tw_now(lp));
   }
530
}
531
532
static void codes_exec_mpi_wait_all(
        nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
533
534
{
  //assert(!s->pending_waits);
535
  int count = mpi_op->u.waits.count;
536
537
538
539
540
541
542
  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)
    {
543
  	printf(" \n (%lf) MPI waitall posted %d count", tw_now(lp), mpi_op->u.waits.count);
544
	for(i = 0; i < count; i++)
545
		printf(" %d ", (int)mpi_op->u.waits.req_ids[i]);
546
547
548
549
550
551
   	printCompletedQueue(s, lp);	 
   }
  while(current) 
   {
	  for(i = 0; i < count; i++)
	   {
552
	     req_id[i] = mpi_op->u.waits.req_ids[i];
553
	     if(req_id[i] == current->req_id)
554
 		    num_completed++;
555
556
557
558
559
   	  }
	 current = current->next;
   }

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

562
  m->u.rc.found_match = 0;
563
  if(count == num_completed)
564
  {
565
	m->u.rc.found_match = 1;
566
	for( i = 0; i < count; i++)	
567
		remove_req_id(&s->completed_reqs, req_id[i]);
568

569
570
571
572
573
574
	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));
575
	  wait_op->mpi_op = mpi_op;  
576
	  wait_op->num_completed = num_completed;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
577
	  wait_op->start_time = tw_now(lp);
578
579
580
581
582
	  s->pending_waits = wait_op;
  }
}

/* request ID is being reused so delete it from the list once the matching is done */
583
584
static void remove_req_id(
        struct completed_requests** mpi_completed_queue, dumpi_req_id req_id)
585
586
587
588
{
	struct completed_requests* current = *mpi_completed_queue;

	if(!current)
589
590
		tw_error(TW_LOC, "\n REQ ID DOES NOT EXIST");
	
591
    if(current->req_id == req_id)
592
593
594
595
596
597
598
599
600
601
602
603
	{
		*mpi_completed_queue = current->next;
		free(current);
		return;
	}
	
	struct completed_requests* elem;
	while(current->next)
	{
	   elem = current->next;
	   if(elem->req_id == req_id)	
	     {
604
605
606
607
            current->next = elem->next;
            free(elem);
            return;
         }
608
609
610
611
612
613
	   current = current->next;	
	}
	return;
}

/* inserts mpi operation in the completed requests queue */
614
615
static void mpi_completed_queue_insert_op(
        struct completed_requests** mpi_completed_queue, dumpi_req_id req_id)
616
617
618
619
620
621
622
623
{
	struct completed_requests* reqs = malloc(sizeof(struct completed_requests));
	assert(reqs);

	reqs->req_id = req_id;

	if(!(*mpi_completed_queue))	
	{
624
625
626
        reqs->next = NULL;
        *mpi_completed_queue = reqs;
        return;
627
628
629
	}
	reqs->next = *mpi_completed_queue;
	*mpi_completed_queue = reqs;
630
	return;
631
632
}

633
/* 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. */
634
635
static void mpi_pending_queue_insert_op(
        struct mpi_queue_ptrs* mpi_queue, struct codes_workload_op* mpi_op)
636
637
638
639
640
641
{
	/* insert mpi operation */
	struct mpi_msgs_queue* elem = malloc(sizeof(struct mpi_msgs_queue));
	assert(elem);

	elem->mpi_op = mpi_op;
642
    elem->next = NULL;
643
644
645
646
647
648
649

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

	if(mpi_queue->queue_tail)
	    mpi_queue->queue_tail->next = elem;
	
650
    mpi_queue->queue_tail = elem;
651
652
653
654
655
656
	mpi_queue->num_elems++;

	return;
}

/* match the send/recv operations */
657
658
static int match_receive(
        nw_state* s, tw_lp* lp, tw_lpid lpid, struct codes_workload_op* op1, struct codes_workload_op* op2)
659
{
660
661
662
663
664
665
666
667
668
        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);
669
                        s->recv_time += tw_now(lp) - op1->sim_start_time;
670
671
672
673
                        mpi_completed_queue_insert_op(&s->completed_reqs, op1->u.recv.req_id);
                        return 1;
                   }
        return -1;
674
675
676
}

/* used for reverse computation. removes the tail of the queue */
677
static int mpi_queue_remove_tail(tw_lpid lpid, struct mpi_queue_ptrs* mpi_queue)
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
{
	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. */
712
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)
713
714
715
716
717
718
719
720
721
{
	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 */
722
	int rcv_val = 0;
723
	if(mpi_op->op_type == CODES_WK_SEND || mpi_op->op_type == CODES_WK_ISEND)
724
	  {
725
		rcv_val = match_receive(s, lp, lp->gid, tmp->mpi_op, mpi_op);
726
		m->u.rc.saved_matched_req = tmp->mpi_op->u.recv.req_id;  
727
	 }
728
	else if(mpi_op->op_type == CODES_WK_RECV || mpi_op->op_type == CODES_WK_IRECV)
729
	  {
730
		rcv_val = match_receive(s, lp, lp->gid, mpi_op, tmp->mpi_op);
731
	  	m->u.rc.saved_matched_req = mpi_op->u.recv.req_id;
732
733
	  }
	if(rcv_val >= 0)
734
	{
735
		/* TODO: fix RC */
736
		m->u.rc.ptr_match_op = tmp->mpi_op;
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
		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;
759
	   
760
	    if(mpi_op->op_type == CODES_WK_SEND || mpi_op->op_type == CODES_WK_ISEND)
761
	     {
762
		    rcv_val = match_receive(s, lp, lp->gid, elem->mpi_op, mpi_op);
763
	     	m->u.rc.saved_matched_req = elem->mpi_op->u.recv.req_id; 
764
	     }
765
	    else if(mpi_op->op_type == CODES_WK_RECV || mpi_op->op_type == CODES_WK_IRECV)
766
	     {
767
768
769
            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;
         }
770
   	     if(rcv_val >= 0)
771
772
		 {
		    m->u.rc.ptr_match_op = elem->mpi_op;
773
		    if(elem == mpi_queue->queue_tail)
774
			    mpi_queue->queue_tail = tmp;
775
		    
776
777
778
779
		    tmp->next = elem->next;

		    free(elem);
		    mpi_queue->num_elems--;
780
		
781
		    return indx;
782
		 }
783
	   tmp = tmp->next;
784
    }
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
	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 */
804
805
static void codes_exec_comp_delay(
        nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op)
806
807
808
809
810
{
	tw_event* e;
	tw_stime ts;
	nw_message* msg;

811
812
813
814
815
816
817
818
819
        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);
820
821
822
823
824
825
826
827
828
	
	e = tw_event_new( lp->gid, ts , lp );
	msg = tw_event_data(e);
	msg->msg_type = MPI_OP_GET_NEXT;

	tw_event_send(e); 
}

/* reverse computation operation for MPI irecv */
829
static void codes_exec_mpi_recv_rc(nw_state* s, nw_message* m, tw_lp* lp)
830
{
831
832
833
	num_bytes_recvd -= m->op->u.recv.num_bytes;
	s->recv_time = m->u.rc.saved_recv_time;
	if(m->u.rc.found_match >= 0)
834
	  {
835
836
837
		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);
		remove_req_id(&s->completed_reqs, m->op->u.recv.req_id);
838
		tw_rand_reverse_unif(lp->rng);
839
	  }
840
	else if(m->u.rc.found_match < 0)
841
	    {
842
843
		mpi_queue_remove_tail(lp->gid, s->pending_recvs_queue);
		if(m->op->op_type == CODES_WK_IRECV)
844
			tw_rand_reverse_unif(lp->rng);
845
846
847
848
	    }
}

/* Execute MPI Irecv operation (non-blocking receive) */ 
849
static void codes_exec_mpi_recv(nw_state* s, tw_lp* lp, nw_message * m, struct codes_workload_op * mpi_op)
850
851
852
853
854
{
/* 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. */

855
	m->u.rc.saved_recv_time = s->recv_time;
856
	mpi_op->sim_start_time = tw_now(lp);
857
	num_bytes_recvd += mpi_op->u.recv.num_bytes;
858
859

	if(lp->gid == TRACE)
860
		printf("\n %lf codes exec mpi recv req id %d", tw_now(lp), (int)mpi_op->u.recv.req_id);
861
862
	
	dumpi_req_id req_id;
863
	int found_matching_sends = mpi_queue_remove_matching_op(s, lp, m, s->arrival_queue, mpi_op);
864
865
866
	
	/* save the req id inserted in the completed queue for reverse computation. */
	//m->matched_recv = req_id;
867
868
869

	if(found_matching_sends < 0)
	  {
870
		m->u.rc.found_match = -1;
871
872
873
		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. */
874
		if(mpi_op->op_type == CODES_WK_IRECV)
875
876
877
878
879
880
		   {
			codes_issue_next_event(lp);	
			return;
		   }
		else
			printf("\n CODES MPI RECV OPERATION!!! ");
881
	  }
882
	else
883
	  {
884
	   	m->u.rc.found_match = found_matching_sends;
885
		codes_issue_next_event(lp); 
886
887
888
889
	 }
}

/* executes MPI send and isend operations */
890
static void codes_exec_mpi_send(nw_state* s, tw_lp* lp, struct codes_workload_op * mpi_op)
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
{
	/* 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;

916
917
	nw_message local_m;
	nw_message remote_m;
918

919
920
921
922
923
924
925
926
    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;
927

928
929
    remote_m = local_m;
	remote_m.msg_type = MPI_SEND_ARRIVED;
930

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

934
	/* isend executed, now get next MPI operation from the queue */ 
935
	if(mpi_op->op_type == CODES_WK_ISEND)
936
937
938
939
	   codes_issue_next_event(lp);
}

/* MPI collective operations */
940
static void codes_exec_mpi_col(nw_state* s, tw_lp* lp)
941
942
943
944
945
946
947
948
949
950
951
952
953
954
{
	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);
955
	if(m->u.msg_info.op_type == CODES_WK_SEND)
956
		tw_rand_reverse_unif(lp->rng);	
957

958
	if(m->u.msg_info.op_type == CODES_WK_ISEND)
959
	  {
960
961
		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);
962
	 }
963
964
965
966
967
}

/* 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)
{
968
	if(TRACE == lp->gid)
969
970
		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)
971
	   {	
972
973
		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);
974
975
	   }  
	
976
	/* blocking send operation */
977
	if(m->u.msg_info.op_type == CODES_WK_SEND)
978
979
980
981
982
983
984
985
		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)
{
986
987
988
	s->recv_time = m->u.rc.saved_recv_time;

        codes_local_latency_reverse(lp);
989

990
	if(m->u.rc.found_match >= 0)
991
	{
992
993
		// TODO: Modify for recvs
		if(lp->gid == TRACE)
994
995
996
			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);
997
		//int count = numQueue(s->pending_recvs_queue);
998
999
		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);
1000
	
1001
1002
1003
		/*if(lp->gid == TRACE)
			printf("\n Reverse: after adding pending recvs queue %d ", s->pending_recvs_queue->num_elems);*/
	}
1004
	else if(m->u.rc.found_match < 0)
1005
	{
1006
		mpi_queue_remove_tail(lp->gid, s->arrival_queue);	
1007
1008
1009
1010
1011
1012
1013
1014
		/*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)
{
1015
1016
	//int count_before = numQueue(s->pending_recvs_queue);
	int is_blocking = 0; /* checks if the recv operation was blocking or not */
1017

1018
	m->u.rc.saved_recv_time = s->recv_time;
1019

1020
1021
1022
1023
1024
1025
1026
1027
    // 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);
1028
1029
1030
1031

        /*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; */
1032
1033
	dumpi_req_id req_id = -1;

1034
        /* Now reconstruct the mpi op */
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
    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;

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

    if(TRACE == lp->gid)
        printf("\n %lf update arrival queue req id %d %d", tw_now(lp), arrived_op->u.send.req_id, m->op->u.send.source_rank);
    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);
      }
1058
1059
}

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
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;
}

1079
1080
1081
1082
/* 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 */
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1083
   char * params = NULL;
1084
1085
1086
1087
1088
   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);
  
1089
   memset(s, 0, sizeof(*s));
1090
   s->nw_id = (mapping_rep_id * num_nw_lps) + mapping_offset;
1091
1092
1093
   s->completed_reqs = NULL;

   s->pending_waits = NULL;
1094
1095
1096
   if(!num_net_traces) 
	num_net_traces = num_net_lps;

Jonathan Jenkins's avatar
Jonathan Jenkins committed
1097
   if (strcmp(workload_type, "dumpi") == 0){
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
       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;
     }
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1109
   wrkld_id = codes_workload_load("dumpi-trace-workload", params, 0, (int)s->nw_id);