modelnet-p2p-bw.c 8.56 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
/* 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
30 31 32 33 34 35 36 37 38

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

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

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

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
64
    int size;
Philip Carns's avatar
Philip Carns committed
65
    int sent_size;        /* for rc */
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
	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)
{
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
    svr_state * ns,
    tw_bf * b,
    svr_msg * m,
    tw_lp * lp)
{
Philip Carns's avatar
Philip Carns committed
292
    model_net_event_rc(net_id, lp, m->sent_size);
293 294 295
    return;
}

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

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

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

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

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

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

Philip Carns's avatar
Philip Carns committed
333
    if(ns->pingpongs_completed % NUM_PINGPONGS == 0)
334
    {
Philip Carns's avatar
Philip Carns committed
335 336 337 338 339 340 341
        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);
342
    }
Philip Carns's avatar
Philip Carns committed
343 344

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

Philip Carns's avatar
Philip Carns committed
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 */
Philip Carns's avatar
Philip Carns committed
358
    m->sent_size = m_remote.size;
Philip Carns's avatar
Philip Carns committed
359 360
    model_net_event(net_id, "ping", peer_gid, stat_array[msg_sz_idx].msg_sz, sizeof(m_remote), &m_remote, 0, NULL, lp);

361 362 363
    return;
}

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

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

Philip Carns's avatar
Philip Carns committed
375 376 377
    m_remote.svr_event_type = PONG;
    m_remote.src = lp->gid;
    m_remote.size = m->size;
378

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

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