codes-dumpi-trace-nw-wrkld.c 25.7 KB
Newer Older
Jonathan Jenkins's avatar
Jonathan Jenkins committed
1 2 3 4 5 6
/*
 * Copyright (C) 2014 University of Chicago
 * See COPYRIGHT notice in top-level directory.
 *
 */

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
#include <ross.h>
#include <assert.h>

#include "dumpi/libundumpi/bindings.h"
#include "dumpi/libundumpi/libundumpi.h"
#include "codes/codes-nw-workload.h"
#include "codes-nw-workload-method.h"
#include "codes/quickhash.h"

#define RANK_HASH_TABLE_SIZE 400 
#define MAX_LENGTH 512
#define MAX_OPERATIONS 32768
#define DUMPI_IGNORE_DELAY 100

static struct qhash_table *rank_tbl = NULL;
static int rank_tbl_pop = 0;

/* context of the MPI workload */
typedef struct rank_mpi_context
{
	int64_t my_rank;
	double last_op_time;
	void* dumpi_mpi_array;	
	struct qhash_head hash_link;
} rank_mpi_context;

/* Holds all the data about MPI operations from the log */
typedef struct dumpi_op_data_array
{
	struct mpi_event_list* op_array;
        int64_t op_arr_ndx;
        int64_t op_arr_cnt;
} dumpi_op_data_array;

/* load the trace */
int dumpi_trace_nw_workload_load(const char* params, int rank);

/* dumpi implementation of get next operation in the workload */
void dumpi_trace_nw_workload_get_next(int rank, struct mpi_event_list *op);

/* get number of bytes from the workload data type and count */
int get_num_bytes(dumpi_datatype dt);

/* computes the delay between MPI operations */
void update_compute_time(const dumpi_time* time, rank_mpi_context* my_ctx);

/* initializes the data structures */
static void* dumpi_init_op_data();

/* removes next operations from the dynamic array */
static void dumpi_remove_next_op(void *mpi_op_array, struct mpi_event_list *mpi_op,
                                      double last_op_time);

/* resets the counters for the dynamic array once the workload is completely loaded*/
static void dumpi_finalize_mpi_op_data(void *mpi_op_array);

/* insert next operation */
static void dumpi_insert_next_op(void *mpi_op_array, struct mpi_event_list *mpi_op);

/* initialize the array data structure */
static void* dumpi_init_op_data()
{
	dumpi_op_data_array* tmp;
	
	tmp = malloc(sizeof(dumpi_op_data_array));
	assert(tmp);
	tmp->op_array = malloc(MAX_OPERATIONS * sizeof(struct mpi_event_list));
	assert(tmp->op_array);
        tmp->op_arr_ndx = 0;
	tmp->op_arr_cnt = MAX_OPERATIONS;

	return (void *)tmp;	
}

/* inserts next operation in the array */
static void dumpi_insert_next_op(void *mpi_op_array, struct mpi_event_list *mpi_op)
{
	dumpi_op_data_array *array = (dumpi_op_data_array*)mpi_op_array;
	struct mpi_event_list *tmp;

	/*check if array is full.*/
	if (array->op_arr_ndx == array->op_arr_cnt)
	{
		tmp = malloc((array->op_arr_cnt + MAX_OPERATIONS) * sizeof(struct mpi_event_list));
		assert(tmp);
		memcpy(tmp, array->op_array, array->op_arr_cnt * sizeof(struct mpi_event_list));
		free(array->op_array);
	        array->op_array = tmp;
	        array->op_arr_cnt += MAX_OPERATIONS;
	}

	/* add the MPI operation to the op array */
	array->op_array[array->op_arr_ndx] = *mpi_op;
	//printf("\n insert time %f end time %f ", array->op_array[array->op_arr_ndx].start_time, array->op_array[array->op_arr_ndx].end_time);
	array->op_arr_ndx++;
	return;
}

/* resets the counters after file is fully loaded */
static void dumpi_finalize_mpi_op_data(void *mpi_op_array)
{
	struct dumpi_op_data_array* array = (struct dumpi_op_data_array*)mpi_op_array;
113

114 115 116 117
	array->op_arr_cnt = array->op_arr_ndx;	
	array->op_arr_ndx = 0;
}

118
/* removes the next operation from the array */
119 120 121 122
static void dumpi_remove_next_op(void *mpi_op_array, struct mpi_event_list *mpi_op,
                                      double last_op_time)
{
	dumpi_op_data_array *array = (dumpi_op_data_array*)mpi_op_array;
123
	//printf("\n op array index %d array count %d ", array->op_arr_ndx, array->op_arr_cnt);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	if (array->op_arr_ndx == array->op_arr_cnt)
	 {
		mpi_op->op_type = CODES_NW_END;
	 }
	else
	{
		struct mpi_event_list *tmp = &(array->op_array[array->op_arr_ndx]);
		//printf("\n tmp end time %f ", tmp->end_time);
		*mpi_op = *tmp;
		array->op_arr_ndx++;
	}
	if(mpi_op->op_type == CODES_NW_END)
	{
		free(array->op_array);
		free(array);
	}
}

/* introduce delay between operations: delay is the compute time NOT spent in MPI operations*/
void update_compute_time(const dumpi_time* time, rank_mpi_context* my_ctx)
{
   if((time->start.nsec - my_ctx->last_op_time) > DUMPI_IGNORE_DELAY)
    {
147
	    struct mpi_event_list wrkld_per_rank;
148

149 150 151 152
            wrkld_per_rank.op_type = CODES_NW_DELAY;
            wrkld_per_rank.start_time = my_ctx->last_op_time;
	    wrkld_per_rank.end_time = time->start.nsec;
	    wrkld_per_rank.u.delay.nsecs = time->start.nsec - my_ctx->last_op_time;
153
            my_ctx->last_op_time = time->stop.nsec;
154
	    dumpi_insert_next_op(my_ctx->dumpi_mpi_array, &wrkld_per_rank); 
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    }
}

int handleDUMPIGeneric(const void* prm, uint16_t thread, const dumpi_time *cpu, const dumpi_time *wall, const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;

	update_compute_time(cpu, myctx);

	return 0;
}

int handleDUMPIISend(const dumpi_isend *prm, uint16_t thread, const dumpi_time *cpu, const dumpi_time *wall, const dumpi_perfinfo *perf, void *userarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)userarg;

171 172 173 174 175 176 177 178 179 180
	struct mpi_event_list wrkld_per_rank;

	wrkld_per_rank.op_type = CODES_NW_ISEND;
	wrkld_per_rank.u.send.tag = prm->tag;
	wrkld_per_rank.u.send.count = prm->count;
	wrkld_per_rank.u.send.data_type = prm->datatype;
        wrkld_per_rank.u.send.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.u.send.dest_rank = prm->dest;
        wrkld_per_rank.u.send.source_rank = myctx->my_rank;
        wrkld_per_rank.start_time = cpu->start.nsec;
181
	wrkld_per_rank.u.send.req_id = prm->request;
182 183 184 185
        wrkld_per_rank.end_time = cpu->stop.nsec;
     
	assert(wrkld_per_rank.u.send.num_bytes > 0); 
 	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
186 187 188 189 190 191 192 193 194
	
	update_compute_time(cpu, myctx);
	return 0;
}

int handleDUMPIIRecv(const dumpi_irecv *prm, uint16_t thread, const dumpi_time *cpu, const dumpi_time *wall, const dumpi_perfinfo *perf, void *userarg)
{
	//printf("\n irecv source %d count %d data type %d", prm->source, prm->count, prm->datatype);
	rank_mpi_context* myctx = (rank_mpi_context*)userarg;
195
        struct mpi_event_list* wrkld_per_rank = malloc(sizeof(mpi_event_list));
196 197

        wrkld_per_rank->op_type = CODES_NW_IRECV;
198 199 200
	wrkld_per_rank->u.recv.data_type = prm->datatype;
	wrkld_per_rank->u.recv.count = prm->count;
	wrkld_per_rank->u.recv.tag = prm->tag;
201 202 203
        wrkld_per_rank->u.recv.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank->u.recv.source_rank = prm->source;
        wrkld_per_rank->u.recv.dest_rank = -1;
204
	wrkld_per_rank->u.recv.req_id = prm->request;
205 206
        wrkld_per_rank->start_time = cpu->start.nsec;	
	wrkld_per_rank->end_time = cpu->stop.nsec;             
207

208 209
	if(wrkld_per_rank->u.recv.num_bytes == 0)
		wrkld_per_rank->u.recv.num_bytes = 8;
210
	assert(wrkld_per_rank->u.recv.num_bytes > 0);	
211 212 213 214 215 216 217 218 219 220 221
	dumpi_insert_next_op(myctx->dumpi_mpi_array, wrkld_per_rank); 

	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPISend(const dumpi_send *prm, uint16_t thread,
                      const dumpi_time *cpu, const dumpi_time *wall,
                      const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
222 223 224 225 226 227 228 229 230 231 232 233 234 235
        struct mpi_event_list wrkld_per_rank;

        wrkld_per_rank.op_type = CODES_NW_SEND;
	wrkld_per_rank.u.send.tag = prm->tag;
        wrkld_per_rank.u.send.count = prm->count;
        wrkld_per_rank.u.send.data_type = prm->datatype;
        wrkld_per_rank.u.send.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.u.send.dest_rank = prm->dest;
        wrkld_per_rank.u.send.source_rank = myctx->my_rank;
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;

	if(wrkld_per_rank.u.send.num_bytes < 0)
		printf("\n Number of bytes %d count %d data type %d num_bytes %d", prm->count * get_num_bytes(prm->datatype), prm->count, prm->datatype, get_num_bytes(prm->datatype));
236 237 238
	if(wrkld_per_rank.u.send.num_bytes == 0)
		wrkld_per_rank.u.send.num_bytes = 8;
	//	printf("\n Number of data bytes %d ", prm->count);
239 240
	assert(wrkld_per_rank.u.send.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank); 
241 242 243 244 245 246 247 248
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIRecv(const dumpi_recv *prm, uint16_t thread,
                      const dumpi_time *cpu, const dumpi_time *wall,
                      const dumpi_perfinfo *perf, void *uarg)
{
249
	//printf("\n irecv source %d count %d data type %d", prm->source, prm->count, prm->datatype);
250
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
251
	struct mpi_event_list wrkld_per_rank;
252

253 254 255 256 257 258
	wrkld_per_rank.op_type = CODES_NW_RECV;
        wrkld_per_rank.u.recv.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.u.recv.source_rank = prm->source;
        wrkld_per_rank.u.recv.dest_rank = -1;
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
259

260 261
	assert(wrkld_per_rank.u.recv.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank); 
262 263 264 265 266 267 268 269 270 271
	update_compute_time(cpu, myctx);
        return 0;

}

int handleDUMPIBcast(const dumpi_bcast *prm, uint16_t thread,
                       const dumpi_time *cpu, const dumpi_time *wall,
                       const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
272
	struct mpi_event_list wrkld_per_rank;
273

274 275 276 277
        wrkld_per_rank.op_type = CODES_NW_BCAST;
        wrkld_per_rank.u.collective.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.start_time = cpu->start.nsec; 
        wrkld_per_rank.end_time = cpu->stop.nsec;
278

279 280
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
281 282 283 284 285 286 287 288 289
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIAllgather(const dumpi_allgather *prm, uint16_t thread,
                           const dumpi_time *cpu, const dumpi_time *wall,
                           const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
290
	struct mpi_event_list wrkld_per_rank;
291

292 293 294 295
        wrkld_per_rank.op_type = CODES_NW_ALLGATHER;
        wrkld_per_rank.u.collective.num_bytes = prm->sendcount * get_num_bytes(prm->sendtype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
296

297 298
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
299 300 301 302 303 304 305 306 307
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIAllgatherv(const dumpi_allgatherv *prm, uint16_t thread,
                            const dumpi_time *cpu, const dumpi_time *wall,
                            const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
308
	struct mpi_event_list wrkld_per_rank;
309

310 311 312 313
        wrkld_per_rank.op_type = CODES_NW_ALLGATHERV;
        wrkld_per_rank.u.collective.num_bytes = prm->sendcount * get_num_bytes(prm->sendtype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
314

315 316
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
317 318 319 320 321 322 323 324 325
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIAlltoall(const dumpi_alltoall *prm, uint16_t thread,
                          const dumpi_time *cpu, const dumpi_time *wall,
                          const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
326
	struct mpi_event_list wrkld_per_rank;
327

328 329 330 331
        wrkld_per_rank.op_type = CODES_NW_ALLTOALL;
        wrkld_per_rank.u.collective.num_bytes = prm->sendcount * get_num_bytes(prm->sendtype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
332

333 334
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
335 336 337 338
	update_compute_time(cpu, myctx);
        return 0;
}

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
int handleDUMPIWait(const dumpi_wait *prm, uint16_t thread, 
		    const dumpi_time *cpu, const dumpi_time *wall, 
		    const dumpi_perfinfo *perf, void *userarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)userarg;
	struct mpi_event_list wrkld_per_rank;

        wrkld_per_rank.op_type = CODES_NW_WAIT;
        wrkld_per_rank.u.wait.req_id = prm->request;
	wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;

	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
	update_compute_time(cpu, myctx);
	return 0;
}
int handleDUMPIWaitsome(const dumpi_waitsome *prm, uint16_t thread,
		    const dumpi_time *cpu, const dumpi_time *wall,
		    const dumpi_perfinfo *perf, void *userarg)
{
	int i;	
	rank_mpi_context* myctx = (rank_mpi_context*)userarg;
        struct mpi_event_list wrkld_per_rank;
	
	wrkld_per_rank.op_type = CODES_NW_WAITSOME;
	wrkld_per_rank.u.waits.count = prm->count;
	wrkld_per_rank.u.waits.req_ids = (int16_t*)malloc(prm->count * sizeof(int16_t));	

	for( i = 0; i < prm->count; i++ )
		wrkld_per_rank.u.waits.req_ids[i] = (int16_t)prm->requests[i];

	wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;

        dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
        update_compute_time(cpu, myctx);
        return 0;

}

int handleDUMPIWaitany(const dumpi_waitany *prm, uint16_t thread, 
                    const dumpi_time *cpu, const dumpi_time *wall, 
                    const dumpi_perfinfo *perf, void *userarg)
{
	int i;	
        rank_mpi_context* myctx = (rank_mpi_context*)userarg;
        struct mpi_event_list wrkld_per_rank;

        wrkld_per_rank.op_type = CODES_NW_WAITANY;
	wrkld_per_rank.u.waits.count = prm->count;
	wrkld_per_rank.u.waits.req_ids = (int16_t*)malloc(prm->count * sizeof(int16_t));	

	for( i = 0; i < prm->count; i++ )
		wrkld_per_rank.u.waits.req_ids[i] = (int16_t)prm->requests[i];

        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;

	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
	update_compute_time(cpu, myctx);
        return 0;		
}

int handleDUMPIWaitall(const dumpi_waitall *prm, uint16_t thread,
                    const dumpi_time *cpu, const dumpi_time *wall,
                    const dumpi_perfinfo *perf, void *userarg)
{
        int i;
        rank_mpi_context* myctx = (rank_mpi_context*)userarg;
        struct mpi_event_list wrkld_per_rank;

        wrkld_per_rank.op_type = CODES_NW_WAITALL;

	wrkld_per_rank.u.waits.count = prm->count;
	wrkld_per_rank.u.waits.req_ids = (int16_t*)malloc(prm->count * sizeof(int16_t));	
        for( i = 0; i < prm->count; i++ )
                wrkld_per_rank.u.waits.req_ids[i] = prm->requests[i];
	
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;

        dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
        update_compute_time(cpu, myctx);
        return 0;
}

425 426 427 428 429
int handleDUMPIAlltoallv(const dumpi_alltoallv *prm, uint16_t thread,
                           const dumpi_time *cpu, const dumpi_time *wall,
                           const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
430
	struct mpi_event_list wrkld_per_rank;
431

432 433 434 435
        wrkld_per_rank.op_type = CODES_NW_ALLTOALLV;
        wrkld_per_rank.u.collective.num_bytes = prm->sendcounts[0] * get_num_bytes(prm->sendtype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
436

437 438
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
439 440 441 442 443 444 445 446 447
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIReduce(const dumpi_reduce *prm, uint16_t thread,
                        const dumpi_time *cpu, const dumpi_time *wall,
                        const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
448
	struct mpi_event_list wrkld_per_rank;
449

450 451 452 453
        wrkld_per_rank.op_type = CODES_NW_REDUCE;
        wrkld_per_rank.u.collective.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
454

455 456
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
457 458 459 460 461 462 463 464 465
	update_compute_time(cpu, myctx);
        return 0;
}

int handleDUMPIAllreduce(const dumpi_allreduce *prm, uint16_t thread,
                           const dumpi_time *cpu, const dumpi_time *wall,
                           const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
466
	struct mpi_event_list wrkld_per_rank;
467

468 469 470 471
        wrkld_per_rank.op_type = CODES_NW_ALLREDUCE;
        wrkld_per_rank.u.collective.num_bytes = prm->count * get_num_bytes(prm->datatype);
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
472

473 474
	assert(wrkld_per_rank.u.collective.num_bytes > 0);
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
475 476 477 478 479 480 481 482
	update_compute_time(cpu, myctx);
        return 0;

}

int handleDUMPIFinalize(const dumpi_finalize *prm, uint16_t thread, const dumpi_time *cpu, const dumpi_time *wall, const dumpi_perfinfo *perf, void *uarg)
{
	rank_mpi_context* myctx = (rank_mpi_context*)uarg;
483
	struct mpi_event_list wrkld_per_rank;
484

485 486 487
        wrkld_per_rank.op_type = CODES_NW_END;
        wrkld_per_rank.start_time = cpu->start.nsec;
        wrkld_per_rank.end_time = cpu->stop.nsec;
488
	
489
	dumpi_insert_next_op(myctx->dumpi_mpi_array, &wrkld_per_rank);
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	update_compute_time(cpu, myctx);
        return 0;
}

static int hash_rank_compare(void *key, struct qhash_head *link)
{
    int *in_rank = (int *)key;
    rank_mpi_context *tmp;

    tmp = qhash_entry(link, rank_mpi_context, hash_link);
    if (tmp->my_rank == *in_rank)
        return 1;

    return 0;
}

int dumpi_trace_nw_workload_load(const char* params, int rank)
{
	libundumpi_callbacks callbacks;
	libundumpi_cbpair callarr[DUMPI_END_OF_STREAM];
	dumpi_profile* profile;
	dumpi_trace_params* dumpi_params = (dumpi_trace_params*)params;
	char file_name[MAX_LENGTH];

514 515 516
	if(rank >= dumpi_params->num_net_traces)
		return -1;

517 518
	if(!rank_tbl)
    	{
519
            rank_tbl = qhash_init(hash_rank_compare, quickhash_32bit_hash, RANK_HASH_TABLE_SIZE);
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
            if(!rank_tbl)
                  return -1;
    	}
	
	rank_mpi_context *my_ctx;
	my_ctx = malloc(sizeof(rank_mpi_context));
	assert(my_ctx);
	my_ctx->my_rank = rank;
	my_ctx->last_op_time = 0.0;
	my_ctx->dumpi_mpi_array = dumpi_init_op_data();

	if(rank < 10)
            sprintf(file_name, "%s000%d.bin", dumpi_params->file_name, rank);
         else if(rank >=10 && rank < 100)
            sprintf(file_name, "%s00%d.bin", dumpi_params->file_name, rank);
           else if(rank >=100 && rank < 1000)
             sprintf(file_name, "%s0%d.bin", dumpi_params->file_name, rank);
             else
              sprintf(file_name, "%s%d.bin", dumpi_params->file_name, rank);
	profile =  undumpi_open(file_name);
        if(NULL == profile) {
                printf("Error: unable to open DUMPI trace: %s", file_name);
                exit(-1);
        }
	
	memset(&callbacks, 0, sizeof(libundumpi_callbacks));
        memset(&callarr, 0, sizeof(libundumpi_cbpair) * DUMPI_END_OF_STREAM);

	/* handle MPI function calls */	        
	callbacks.on_send = (dumpi_send_call)handleDUMPISend;
        callbacks.on_recv = (dumpi_recv_call)handleDUMPIRecv;
        callbacks.on_isend = (dumpi_isend_call)handleDUMPIISend;
        callbacks.on_irecv = (dumpi_irecv_call)handleDUMPIIRecv;
        callbacks.on_allreduce = (dumpi_allreduce_call)handleDUMPIAllreduce;
554
	callbacks.on_bcast = (dumpi_bcast_call)handleDUMPIBcast;
555 556 557 558 559 560 561 562 563
	callbacks.on_get_count = (dumpi_get_count_call)handleDUMPIGeneric;
	callbacks.on_bsend = (dumpi_bsend_call)handleDUMPIGeneric;
	callbacks.on_ssend = (dumpi_ssend_call)handleDUMPIGeneric;
	callbacks.on_rsend = (dumpi_rsend_call)handleDUMPIGeneric;
	callbacks.on_buffer_attach = (dumpi_buffer_attach_call)handleDUMPIGeneric;
	callbacks.on_buffer_detach = (dumpi_buffer_detach_call)handleDUMPIGeneric;
	callbacks.on_ibsend = (dumpi_ibsend_call)handleDUMPIGeneric;
	callbacks.on_issend = (dumpi_issend_call)handleDUMPIGeneric;
	callbacks.on_irsend = (dumpi_irsend_call)handleDUMPIGeneric;
564
	callbacks.on_wait = (dumpi_wait_call)handleDUMPIWait;
565 566
	callbacks.on_test = (dumpi_test_call)handleDUMPIGeneric;
	callbacks.on_request_free = (dumpi_request_free_call)handleDUMPIGeneric;
567
	callbacks.on_waitany = (dumpi_waitany_call)handleDUMPIWaitany;
568
	callbacks.on_testany = (dumpi_testany_call)handleDUMPIGeneric;
569
	callbacks.on_waitall = (dumpi_waitall_call)handleDUMPIWaitall;
570
	callbacks.on_testall = (dumpi_testall_call)handleDUMPIGeneric;
571
	callbacks.on_waitsome = (dumpi_waitsome_call)handleDUMPIWaitsome;
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	callbacks.on_testsome = (dumpi_testsome_call)handleDUMPIGeneric;
	callbacks.on_iprobe = (dumpi_iprobe_call)handleDUMPIGeneric;
	callbacks.on_probe = (dumpi_probe_call)handleDUMPIGeneric;
	callbacks.on_cancel = (dumpi_cancel_call)handleDUMPIGeneric;
	callbacks.on_test_cancelled = (dumpi_test_cancelled_call)handleDUMPIGeneric;
	callbacks.on_send_init = (dumpi_send_init_call)handleDUMPIGeneric;
	callbacks.on_bsend_init = (dumpi_bsend_init_call)handleDUMPIGeneric;
	callbacks.on_ssend_init = (dumpi_ssend_init_call)handleDUMPIGeneric;
	callbacks.on_rsend_init = (dumpi_rsend_init_call)handleDUMPIGeneric;
	callbacks.on_recv_init = (dumpi_recv_init_call)handleDUMPIGeneric;
	callbacks.on_start = (dumpi_start_call)handleDUMPIGeneric;
	callbacks.on_startall = (dumpi_startall_call)handleDUMPIGeneric;
	callbacks.on_sendrecv = (dumpi_sendrecv_call)handleDUMPIGeneric;
	callbacks.on_sendrecv_replace = (dumpi_sendrecv_replace_call)handleDUMPIGeneric;
	callbacks.on_type_contiguous = (dumpi_type_contiguous_call)handleDUMPIGeneric;
	callbacks.on_barrier = (dumpi_barrier_call)handleDUMPIGeneric;
        callbacks.on_gather = (dumpi_gather_call)handleDUMPIGeneric;
        callbacks.on_gatherv = (dumpi_gatherv_call)handleDUMPIGeneric;
        callbacks.on_scatter = (dumpi_scatter_call)handleDUMPIGeneric;
        callbacks.on_scatterv = (dumpi_scatterv_call)handleDUMPIGeneric;
        callbacks.on_allgather = (dumpi_allgather_call)handleDUMPIGeneric;
        callbacks.on_allgatherv = (dumpi_allgatherv_call)handleDUMPIGeneric;
        callbacks.on_alltoall = (dumpi_alltoall_call)handleDUMPIGeneric;
        callbacks.on_alltoallv = (dumpi_alltoallv_call)handleDUMPIGeneric;
        callbacks.on_alltoallw = (dumpi_alltoallw_call)handleDUMPIGeneric;
        callbacks.on_reduce = (dumpi_reduce_call)handleDUMPIGeneric;
        callbacks.on_reduce_scatter = (dumpi_reduce_scatter_call)handleDUMPIGeneric;
        callbacks.on_group_size = (dumpi_group_size_call)handleDUMPIGeneric;
        callbacks.on_group_rank = (dumpi_group_rank_call)handleDUMPIGeneric;
        callbacks.on_comm_size = (dumpi_comm_size_call)handleDUMPIGeneric;
        callbacks.on_comm_rank = (dumpi_comm_rank_call)handleDUMPIGeneric;
        callbacks.on_comm_get_attr = (dumpi_comm_get_attr_call)handleDUMPIGeneric;
        callbacks.on_comm_create = (dumpi_comm_create_call)handleDUMPIGeneric;
        callbacks.on_wtime = (dumpi_wtime_call)handleDUMPIGeneric;
        callbacks.on_finalize = (dumpi_finalize_call)handleDUMPIFinalize;

        libundumpi_populate_callbacks(&callbacks, callarr);

        dumpi_start_stream_read(profile);
        dumpi_header* trace_header = undumpi_read_header(profile);
        dumpi_free_header(trace_header);

        int finalize_reached = 0;
        int active = 1;
        int num_calls = 0;
        while(active && !finalize_reached)
        {
           num_calls++;
           active = undumpi_read_single_call(profile, callarr, (void*)my_ctx, &finalize_reached);
        }
	undumpi_close(profile);
	dumpi_finalize_mpi_op_data(my_ctx->dumpi_mpi_array);
	/* add this rank context to hash table */	
	qhash_add(rank_tbl, &(my_ctx->my_rank), &(my_ctx->hash_link));
	rank_tbl_pop++;

	return 0;
}

int get_num_bytes(dumpi_datatype dt)
{
   switch(dt)
   {
	case DUMPI_DATATYPE_ERROR:
	case DUMPI_DATATYPE_NULL:
		return -1; /* error state */
	break;

	case DUMPI_CHAR:
	case DUMPI_UNSIGNED_CHAR:
	case DUMPI_SIGNED_CHAR:
	case DUMPI_BYTE:
		return 1; /* 1 byte for char */
	break;

	case DUMPI_WCHAR:
		return 4; /* 4 bytes for a 64-bit version */
	break;

	case DUMPI_SHORT:
	case DUMPI_SHORT_INT:
	case DUMPI_UNSIGNED_SHORT:
		return 2;
	break;

	 case DUMPI_INT:
	 case DUMPI_UNSIGNED:
	 case DUMPI_FLOAT:
	 case DUMPI_FLOAT_INT:
		return 4;
	 break;

	case DUMPI_DOUBLE:
	case DUMPI_LONG:
	case DUMPI_LONG_INT:
	case DUMPI_UNSIGNED_LONG:
	case DUMPI_LONG_LONG_INT:
	case DUMPI_UNSIGNED_LONG_LONG:
	case DUMPI_LONG_LONG:
	case DUMPI_DOUBLE_INT:
		return 8;
	break;

	case DUMPI_LONG_DOUBLE:
	case DUMPI_LONG_DOUBLE_INT:
		return 16;
	break;
	
	default:
	  {
		printf("\n Undefined data type ");
		return 0;	
	  }	
   } 
}

void dumpi_trace_nw_workload_get_next(int rank, struct mpi_event_list *op)
{
   rank_mpi_context* temp_data;
   struct qhash_head *hash_link = NULL;
   struct mpi_event_list mpi_op;
   hash_link = qhash_search(rank_tbl, &rank);
   if(!hash_link)
   {
      op->op_type = CODES_NW_END;
      return;
   }
  temp_data = qhash_entry(hash_link, rank_mpi_context, hash_link);
  assert(temp_data);

  dumpi_remove_next_op(temp_data->dumpi_mpi_array, &mpi_op, temp_data->last_op_time); 
  if( mpi_op.op_type == CODES_NW_END)
  {
	qhash_del(hash_link);
        free(temp_data);

        rank_tbl_pop--;
        if (!rank_tbl_pop)
        {
            qhash_finalize(rank_tbl);
            rank_tbl = NULL;
        }
  }
  *op = mpi_op;
  return;
}

/* implements the codes workload method */
struct codes_nw_workload_method dumpi_trace_workload_method =
{
    .method_name = "dumpi-trace-workload",
    .codes_nw_workload_load = dumpi_trace_nw_workload_load,
    .codes_nw_workload_get_next = dumpi_trace_nw_workload_get_next,
};