modelnet-p2p-bw.c 8.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * 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
 * mpptest.
 */

#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
26 27 28 29 30
/* TODO: these things should probably be configurable */
#define NUM_PINGPONGS 1000 /* number of pingpong exchanges per msg size */
#define MIN_SZ 4
#define MAX_SZ 67108864
#define NUM_SZS 25
31 32 33 34 35 36 37 38 39

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
40 41 42 43 44 45 46 47
struct pingpong_stat
{
    int msg_sz;
    tw_stime start_time;
    tw_stime end_time;
};
struct pingpong_stat stat_array[25];

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

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

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
65
    int size;
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
};

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();
Philip Carns's avatar
Philip Carns committed
96
#if 0
97
static tw_stime ns_to_s(tw_stime ns);
Philip Carns's avatar
Philip Carns committed
98
#endif
99
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	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)
    {
	    printf("\n Usage: mpirun <args> --sync=2/3 mapping_file_name.conf (optional --nkp) ");
	    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();
    
    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)
{
Philip Carns's avatar
Philip Carns committed
267
    /* TODO: print results on server 0 */
268 269 270
    return;
}

Philip Carns's avatar
Philip Carns committed
271
#if 0
272 273 274 275 276
/* convert ns to seconds */
static tw_stime ns_to_s(tw_stime ns)
{
    return(ns / (1000.0 * 1000.0 * 1000.0));
}
Philip Carns's avatar
Philip Carns committed
277
#endif
278 279 280 281 282 283 284

/* 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
285 286
/* reverse handler for ping event */
static void handle_ping_rev_event(
287 288 289 290 291 292 293 294
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
    return;
}

Philip Carns's avatar
Philip Carns committed
295 296
/* reverse handler for pong */
static void handle_pong_rev_event(
297 298 299 300 301
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
302
    ns->pingpongs_completed--;
303

Philip Carns's avatar
Philip Carns committed
304 305 306 307 308
    /* 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.
     */
309 310 311
    return;
}

Philip Carns's avatar
Philip Carns committed
312 313
/* handle recving pong */
static void handle_pong_event(
314 315 316 317 318
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
319 320 321
    svr_msg m_remote;
    int msg_sz_idx;
    tw_lpid peer_gid;
322

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

Philip Carns's avatar
Philip Carns committed
325 326
    assert(ns->svr_idx == 0);
    ns->pingpongs_completed++;
327

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

Philip Carns's avatar
Philip Carns committed
331
    if(ns->pingpongs_completed % NUM_PINGPONGS == 0)
332
    {
Philip Carns's avatar
Philip Carns committed
333 334 335 336 337 338 339
        printf("FOO: hit msg_sz_idx %d\n", msg_sz_idx);
        printf("FOO: completed %d\n", ns->pingpongs_completed);
        /* 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)
            stat_array[msg_sz_idx].end_time = tw_now(lp);
340
    }
Philip Carns's avatar
Philip Carns committed
341 342

    if(msg_sz_idx >= NUM_SZS)
343
    {
Philip Carns's avatar
Philip Carns committed
344 345
        /* done */
        return;
346 347
    }

Philip Carns's avatar
Philip Carns committed
348 349 350 351 352 353 354 355 356 357
    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 */
    model_net_event(net_id, "ping", peer_gid, stat_array[msg_sz_idx].msg_sz, sizeof(m_remote), &m_remote, 0, NULL, lp);

358 359 360
    return;
}

Philip Carns's avatar
Philip Carns committed
361 362
/* handle receiving ping */
static void handle_ping_event(
363 364 365 366 367
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
368
    svr_msg m_remote;
369

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

Philip Carns's avatar
Philip Carns committed
372 373 374
    m_remote.svr_event_type = PONG;
    m_remote.src = lp->gid;
    m_remote.size = m->size;
375

Philip Carns's avatar
Philip Carns committed
376 377
    /* send pong msg back to sender */
    model_net_event(net_id, "pong", m->src, m->size, sizeof(m_remote), &m_remote, 0, NULL, lp);
378 379 380 381 382 383 384 385 386 387 388
    return;
}

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