modelnet-p2p-bw.c 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2013 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

/* SUMMARY:
 *
 * This test program will execute a one way, point to point bandwidth test
 * between two hosts using the specified modelnet method.  The intention is
 * to roughly mimic the behavior of a standard bandwidth test such as
12 13 14
 * mpptest.  Output is presented in the same format as the default mpptest
 * parameters with two nodes, testing a range of sizes from 4 bytes to 64
 * MiB.
15 16 17 18 19 20 21 22 23 24 25 26 27
 */

#include <string.h>
#include <assert.h>
#include <ross.h>

#include "codes/model-net.h"
#include "codes/lp-io.h"
#include "codes/codes.h"
#include "codes/codes_mapping.h"
#include "codes/configuration.h"
#include "codes/lp-type-lookup.h"

Philip Carns's avatar
Philip Carns committed
28 29 30 31
/* TODO: these things should probably be configurable */
#define NUM_PINGPONGS 1000 /* number of pingpong exchanges per msg size */
#define MIN_SZ 4
#define NUM_SZS 25
32 33 34 35 36 37 38 39 40

static int net_id = 0;
static int num_routers = 0;
static int num_servers = 0;
static int offset = 2;

typedef struct svr_msg svr_msg;
typedef struct svr_state svr_state;

Philip Carns's avatar
Philip Carns committed
41 42 43 44 45 46 47 48
struct pingpong_stat
{
    int msg_sz;
    tw_stime start_time;
    tw_stime end_time;
};
struct pingpong_stat stat_array[25];

49 50 51
/* types of events that will constitute triton requests */
enum svr_event
{
Philip Carns's avatar
Philip Carns committed
52 53
    PING = 1,        /* request event */
    PONG,        /* ack event */
54 55 56 57
};

struct svr_state
{
Philip Carns's avatar
Philip Carns committed
58 59
    int pingpongs_completed; 
    int svr_idx;
60 61 62 63 64 65
};

struct svr_msg
{
    enum svr_event svr_event_type;
    tw_lpid src;          /* source of this request or ack */
Philip Carns's avatar
Philip Carns committed
66
    int size;
Philip Carns's avatar
Philip Carns committed
67
    int sent_size;        /* for rc */
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
};

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

tw_lptype svr_lp = {
     (init_f) svr_init,
     (event_f) svr_event,
     (revent_f) svr_rev_event,
     (final_f)  svr_finalize, 
     (map_f) codes_mapping,
     sizeof(svr_state),
};

extern const tw_lptype* svr_get_lp_type();
static void svr_add_lp_type();
static tw_stime ns_to_s(tw_stime ns);
static tw_stime s_to_ns(tw_stime ns);
Philip Carns's avatar
Philip Carns committed
100
static void handle_pong_event(
101 102 103 104
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
Philip Carns's avatar
Philip Carns committed
105
static void handle_ping_event(
106 107 108 109
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
Philip Carns's avatar
Philip Carns committed
110
static void handle_pong_rev_event(
111 112 113 114
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);
Philip Carns's avatar
Philip Carns committed
115
static void handle_ping_rev_event(
116 117 118 119 120 121 122
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp);

const tw_optdef app_opt [] =
{
Philip Carns's avatar
Philip Carns committed
123
	TWOPT_GROUP("Model net point to point ping pong benchmark" ),
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	TWOPT_END()
};

int main(
    int argc,
    char **argv)
{
    int nprocs;
    int rank;
    g_tw_ts_end = s_to_ns(60*60*24*365); /* one year, in nsecs */

    tw_opt_add(app_opt);
    tw_init(&argc, &argv);

    if(argc < 2)
    {
Philip Carns's avatar
Philip Carns committed
140
	    printf("\n Usage: mpirun <args> --sync=2/3 -- mapping_file_name.conf\n");
141 142 143 144 145 146 147 148 149
	    MPI_Finalize();
	    return 0;
    }
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
  
    configuration_load(argv[2], MPI_COMM_WORLD, &config);
    net_id=model_net_set_params();
    svr_add_lp_type();
Philip Carns's avatar
Philip Carns committed
150
   
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    codes_mapping_setup();
    
    num_servers = codes_mapping_get_group_reps("MODELNET_GRP") * codes_mapping_get_lp_count("MODELNET_GRP", "server");
    assert(num_servers == 2);
    if(net_id == DRAGONFLY)
    {
	  num_routers = codes_mapping_get_group_reps("MODELNET_GRP") * codes_mapping_get_lp_count("MODELNET_GRP", "dragonfly_router"); 
	  offset = 1;
    }

    tw_run();
    model_net_report_stats(net_id);

    tw_end();
    return 0;
}

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

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

static void svr_init(
    svr_state * ns,
    tw_lp * lp)
{
    tw_event *e;
    svr_msg *m;
    tw_stime kickoff_time;
Philip Carns's avatar
Philip Carns committed
185 186 187 188
    char grp_name[MAX_NAME_LENGTH];
    char lp_type_name[MAX_NAME_LENGTH];
    int grp_id, lp_type_id, grp_rep_id, offset;
    int i;
189 190 191
    
    memset(ns, 0, sizeof(*ns));

Philip Carns's avatar
Philip Carns committed
192 193 194 195 196 197
    /* find my own server index */
    codes_mapping_get_lp_info(lp->gid, grp_name, &grp_id,
            &lp_type_id, lp_type_name, &grp_rep_id, &offset);
    ns->svr_idx = grp_rep_id;

    /* first server sends a dummy event to itself that will kick off the real
198 199
     * simulation
     */
Philip Carns's avatar
Philip Carns committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    if(ns->svr_idx == 0)
    {
        /* initialize statistics; measured only at first server */
        ns->pingpongs_completed = -1;
        stat_array[0].msg_sz = MIN_SZ;
        for(i=1; i<NUM_SZS; i++)
            stat_array[i].msg_sz = stat_array[i-1].msg_sz * 2;

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

        e = codes_event_new(lp->gid, kickoff_time, lp);
        m = tw_event_data(e);
        m->svr_event_type = PONG;
        tw_event_send(e);
    }
216 217 218 219 220 221 222 223 224 225 226 227

    return;
}

static void svr_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
   switch (m->svr_event_type)
    {
Philip Carns's avatar
Philip Carns committed
228 229
        case PING:
            handle_ping_event(ns, b, m, lp);
230
            break;
Philip Carns's avatar
Philip Carns committed
231 232
        case PONG:
            handle_pong_event(ns, b, m, lp);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
            break;
        default:
	    printf("\n Invalid message type %d ", m->svr_event_type);
            assert(0);
        break;
    }
}

static void svr_rev_event(
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
    switch (m->svr_event_type)
    {
Philip Carns's avatar
Philip Carns committed
249 250
        case PING:
            handle_ping_rev_event(ns, b, m, lp);
251
            break;
Philip Carns's avatar
Philip Carns committed
252 253
        case PONG:
            handle_pong_rev_event(ns, b, m, lp);
254 255 256 257 258 259 260 261 262 263 264 265 266
            break;
        default:
            assert(0);
            break;
    }

    return;
}

static void svr_finalize(
    svr_state * ns,
    tw_lp * lp)
{
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    int i;
    double avg_time_us;
    double rate_b_s;
    double elapsed_s;

    if(ns->svr_idx != 0)
        return;

    printf("#p0\tp1\tdist\tlen\tave time (us)\trate\n");
    for(i=0; i<NUM_SZS; i++)
    {
        avg_time_us = stat_array[i].end_time - stat_array[i].start_time;
        avg_time_us /= 1000.0; /* ns to us */
        avg_time_us /= (double)NUM_PINGPONGS; /* avg */
        avg_time_us /= 2.0; /* divide by 2, to replicate scaling factor as in mpptest */

        /* rate is reported in bytes/s */
        elapsed_s = ns_to_s(stat_array[i].end_time - stat_array[i].start_time);
        rate_b_s = (double)stat_array[i].msg_sz * (double)NUM_PINGPONGS * 2.0;
        rate_b_s /= elapsed_s;
        printf("0\t1\t1\t%d\t%f\t%e\n", stat_array[i].msg_sz, avg_time_us, rate_b_s);
    }

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    return;
}

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

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

Philip Carns's avatar
Philip Carns committed
305 306
/* reverse handler for ping event */
static void handle_ping_rev_event(
307 308 309 310 311
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
312
    model_net_event_rc(net_id, lp, m->sent_size);
313 314 315
    return;
}

Philip Carns's avatar
Philip Carns committed
316 317
/* reverse handler for pong */
static void handle_pong_rev_event(
318 319 320 321 322
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
323
    ns->pingpongs_completed--;
Philip Carns's avatar
Philip Carns committed
324
    model_net_event_rc(net_id, lp, m->sent_size);
325

Philip Carns's avatar
Philip Carns committed
326 327 328 329 330
    /* NOTE: we do not attempt to reverse timing information stored in
     * stat_array[].  This is will get rewritten with the correct value when
     * right forward event is processed, and we don't count on this value
     * being accurate until the simulation is complete.
     */
331 332 333
    return;
}

Philip Carns's avatar
Philip Carns committed
334 335
/* handle recving pong */
static void handle_pong_event(
336 337 338 339 340
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
341 342 343
    svr_msg m_remote;
    int msg_sz_idx;
    tw_lpid peer_gid;
344

Philip Carns's avatar
Philip Carns committed
345
    /* printf("handle_pong_event(), lp %llu.\n", (unsigned long long)lp->gid); */
346

Philip Carns's avatar
Philip Carns committed
347 348
    assert(ns->svr_idx == 0);
    ns->pingpongs_completed++;
349

Philip Carns's avatar
Philip Carns committed
350 351
    /* which message size are we on now? */
    msg_sz_idx = ns->pingpongs_completed / NUM_PINGPONGS;
352

Philip Carns's avatar
Philip Carns committed
353
    if(ns->pingpongs_completed % NUM_PINGPONGS == 0)
354
    {
Philip Carns's avatar
Philip Carns committed
355 356 357 358
        /* finished one msg size range; record time */
        if(msg_sz_idx < NUM_SZS)
            stat_array[msg_sz_idx].start_time = tw_now(lp);
        if(msg_sz_idx > 0)
359
            stat_array[msg_sz_idx-1].end_time = tw_now(lp);
360
    }
Philip Carns's avatar
Philip Carns committed
361 362

    if(msg_sz_idx >= NUM_SZS)
363
    {
Philip Carns's avatar
Philip Carns committed
364 365
        /* done */
        return;
366 367
    }

Philip Carns's avatar
Philip Carns committed
368 369 370 371 372 373 374 375
    codes_mapping_get_lp_id("MODELNET_GRP", "server", 1,
        0, &peer_gid);

    m_remote.svr_event_type = PING;
    m_remote.src = lp->gid;
    m_remote.size = stat_array[msg_sz_idx].msg_sz;

    /* send next ping */
Philip Carns's avatar
Philip Carns committed
376
    m->sent_size = m_remote.size;
Philip Carns's avatar
Philip Carns committed
377 378
    model_net_event(net_id, "ping", peer_gid, stat_array[msg_sz_idx].msg_sz, sizeof(m_remote), &m_remote, 0, NULL, lp);

379 380 381
    return;
}

Philip Carns's avatar
Philip Carns committed
382 383
/* handle receiving ping */
static void handle_ping_event(
384 385 386 387 388
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
389
    svr_msg m_remote;
390

Philip Carns's avatar
Philip Carns committed
391
    assert(ns->svr_idx == 1);
392

Philip Carns's avatar
Philip Carns committed
393 394 395
    m_remote.svr_event_type = PONG;
    m_remote.src = lp->gid;
    m_remote.size = m->size;
396

Philip Carns's avatar
Philip Carns committed
397
    /* send pong msg back to sender */
Philip Carns's avatar
Philip Carns committed
398
    m->sent_size = m_remote.size;
Philip Carns's avatar
Philip Carns committed
399
    model_net_event(net_id, "pong", m->src, m->size, sizeof(m_remote), &m_remote, 0, NULL, lp);
400 401 402 403 404 405 406 407 408 409 410
    return;
}

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