concurrent-msg-recv.c 12.4 KB
Newer Older
1 2 3 4 5 6
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

7
/* This is meant to be a template file to use when developing new LPs.
8 9 10 11 12 13 14 15 16 17 18 19 20
 * Roughly follows the format of the existing LPs in the CODES repos */

#include "codes/codes_mapping.h"
#include "codes/lp-type-lookup.h"
#include "codes/jenkins-hash.h"
#include "codes/codes.h"
#include "codes/model-net.h"
#include <string.h>
#include <assert.h>
#include <limits.h>

/**** BEGIN SIMULATION DATA STRUCTURES ****/

Jonathan Jenkins's avatar
Jonathan Jenkins committed
21
#define NUM_REQS 1
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

#define TEST_DEBUG 1

static int testsvr_magic; /* use this as sanity check on events */

int net_id;

typedef struct testsvr_state testsvr_state;
typedef struct testsvr_msg testsvr_msg;

/* event types */
enum testsvr_event
{
    KICKOFF,
    REQ,
    ACK,
    LOCAL, /* dummy to add some time between completion */
};

struct testsvr_state {
    int idx;
    int req_stat[NUM_REQS];

#if TEST_DEBUG
    FILE *fdebug;
    /* count the number of forward events processed so we know EXACTLY which
     * invocations of events cause events in other LPs */
49
    int event_ctr;
50 51 52 53 54 55 56 57 58
#endif
};

struct testsvr_msg {
    int magic;
    enum testsvr_event event_type;
    int idx_src;
    tw_lpid lp_src;
    int req_num;
59
    model_net_event_return ret[NUM_REQS];
60 61 62 63 64 65 66 67 68 69
#if TEST_DEBUG
    /* event count that produced this message in the first place */
    int src_event_ctr;
#endif
};

/**** END SIMULATION DATA STRUCTURES ****/

/**** BEGIN LP, EVENT PROCESSING FUNCTION DECLS ****/

70
/* ROSS LP processing functions */
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
static void testsvr_lp_init(
    testsvr_state * ns,
    tw_lp * lp);
static void testsvr_event_handler(
    testsvr_state * ns,
    tw_bf * b,
    testsvr_msg * m,
    tw_lp * lp);
static void testsvr_rev_handler(
    testsvr_state * ns,
    tw_bf * b,
    testsvr_msg * m,
    tw_lp * lp);
static void testsvr_finalize(
    testsvr_state * ns,
    tw_lp * lp);

/* event type handlers */
static void handle_testsvr_kickoff(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp);
static void handle_testsvr_req(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp);
static void handle_testsvr_ack(
    testsvr_state * ns,
99
    testsvr_msg * m);
100 101 102 103 104 105 106 107 108 109 110 111
static void handle_testsvr_local(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp);
static void handle_testsvr_kickoff_rev(
    testsvr_msg * m,
    tw_lp * lp);
static void handle_testsvr_req_rev(
    testsvr_state * ns,
    tw_lp * lp);
static void handle_testsvr_ack_rev(
    testsvr_state * ns,
112
    testsvr_msg * m);
113 114 115 116 117 118 119
static void handle_testsvr_local_rev(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp);

/* ROSS function pointer table for this LP */
tw_lptype testsvr_lp = {
120 121 122 123
    (init_f) testsvr_lp_init,
    (pre_run_f) NULL,
    (event_f) testsvr_event_handler,
    (revent_f) testsvr_rev_handler,
124 125
    (commit_f) NULL,
    (final_f)  testsvr_finalize,
126 127
    (map_f) codes_mapping,
    sizeof(testsvr_state),
128 129 130
};

/* for debugging: print messages */
131
static void dump_msg(testsvr_msg *m, tw_lp *lp, FILE *f);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
static void dump_state(tw_lp *lp, testsvr_state *ns, FILE *f);

/**** END LP, EVENT PROCESSING FUNCTION DECLS ****/

/**** BEGIN IMPLEMENTATIONS ****/

void testsvr_init(){
    uint32_t h1=0, h2=0;

    bj_hashlittle2("testsvr", strlen("testsvr"), &h1, &h2);
    testsvr_magic = h1+h2;

    lp_type_register("testsvr", &testsvr_lp);
}

void testsvr_lp_init(
        testsvr_state * ns,
        tw_lp * lp){
    /* for test, just use dummy way (assume 1 svr / 1 modelnet) */
    ns->idx = lp->gid / 2;
152

153 154 155 156 157 158
    /* expect exactly three servers */
    assert(ns->idx <= 2);

    memset(ns->req_stat, 0x0, NUM_REQS*sizeof(int));
    /* create kickoff event only if we're a request server */
    if (ns->idx == 0 || ns->idx == 2){
159
        tw_event *e = tw_event_new(lp->gid, codes_local_latency(lp), lp);
160 161 162 163 164 165 166 167 168 169 170
        testsvr_msg *m_local = tw_event_data(e);
        m_local->magic = testsvr_magic;
        m_local->event_type = KICKOFF;
        /* dummy values for kickoff */
        m_local->idx_src = INT_MAX;
        m_local->lp_src  = INT_MAX;
        m_local->req_num = INT_MAX;
        tw_event_send(e);
    }
#if TEST_DEBUG
    char name[32];
171
    sprintf(name, "testsvr.%d.%llu", ns->idx, LLU(lp->gid));
172 173 174 175 176 177 178 179
    ns->fdebug = fopen(name, "w");
    setvbuf(ns->fdebug, NULL, _IONBF, 0);
    assert(ns->fdebug != NULL);
    ns->event_ctr = 0;
#endif
}

/* test boilerplate helpers */
180
#if TEST_DEBUG
181 182 183 184
#define DUMP_PRE(lp, ns, m, type) \
    do{ \
        fprintf(ns->fdebug, type); \
        dump_state(lp,ns,ns->fdebug); \
185 186
        dump_msg(m,lp,ns->fdebug); \
        fflush(ns->fdebug);\
187 188 189 190 191 192
    }while(0)

#define DUMP_POST(lp, ns, type) \
    do{ \
        fprintf(ns->fdebug, type); \
        dump_state(lp,ns,ns->fdebug); \
193
        fflush(ns->fdebug);\
194 195 196 197 198 199 200 201 202 203 204 205
    }while(0)

#else
#define DUMP_PRE(lp, ns, m, type, is_rev) do{}while(0)
#define DUMP_POST(lp, ns, is_rev) do{}while(0)
#endif

void testsvr_event_handler(
        testsvr_state * ns,
        tw_bf * b,
        testsvr_msg * m,
        tw_lp * lp){
206
    (void)b;
207
    assert(m->magic == testsvr_magic);
208

209 210 211 212 213 214 215 216 217 218 219 220 221
    switch (m->event_type){
        case KICKOFF:
            DUMP_PRE(lp,ns,m,"== pre kickoff ==\n");
            handle_testsvr_kickoff(ns, m, lp);
            DUMP_POST(lp,ns,"== post kickoff ==\n");
            break;
        case REQ:
            DUMP_PRE(lp,ns,m,"== pre req ==\n");
            handle_testsvr_req(ns, m, lp);
            DUMP_POST(lp,ns,"== post req ==\n");
            break;
        case ACK:
            DUMP_PRE(lp,ns,m,"== pre ack ==\n");
222
            handle_testsvr_ack(ns, m);
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
            DUMP_POST(lp,ns,"== post ack ==\n");
            break;
        case LOCAL:
            DUMP_PRE(lp,ns,m,"== pre local ==\n");
            handle_testsvr_local(ns, m, lp);
            DUMP_POST(lp,ns,"== post local ==\n");
            break;
        /* ... */
        default:
            assert(!"testsvr event type not known");
            break;
    }
}

void testsvr_rev_handler(
        testsvr_state * ns,
        tw_bf * b,
        testsvr_msg * m,
        tw_lp * lp){
242
    (void)b;
243
    assert(m->magic == testsvr_magic);
244

245 246 247
    switch (m->event_type){
        case KICKOFF:
            DUMP_PRE(lp,ns,m,"== pre kickoff rev == ");
248
            handle_testsvr_kickoff_rev(m, lp);
249 250 251 252
            DUMP_POST(lp,ns,"== post kickoff rev ==\n");
            break;
        case REQ:
            DUMP_PRE(lp,ns,m,"== pre req rev ==\n");
253
            handle_testsvr_req_rev(ns, lp);
254 255 256 257
            DUMP_POST(lp,ns,"== post req rev ==\n");
            break;
        case ACK:
            DUMP_PRE(lp,ns,m,"== pre ack rev ==\n");
258
            handle_testsvr_ack_rev(ns, m);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            DUMP_POST(lp,ns,"== post ack rev ==\n");
            break;
        case LOCAL:
            DUMP_PRE(lp,ns,m,"== pre local rev ==\n");
            handle_testsvr_local_rev(ns, m, lp);
            DUMP_POST(lp,ns,"== post local rev ==\n");
            break;
        /* ... */
        default:
            assert(!"testsvr event type not known");
            break;
    }
}

void testsvr_finalize(
274 275 276
    testsvr_state * ns,
    tw_lp * lp) {
    (void)lp;
277 278 279 280 281 282 283 284 285 286 287 288 289 290
    /* ensure that all requests are accounted for */
    int req_expected = (ns->idx == 1) ? 2 : 1;
    int req;
    for (req = 0; req < NUM_REQS; req++){
        assert(ns->req_stat[req] == req_expected);
    }
}

void handle_testsvr_kickoff(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp){

    assert(ns->idx == 0 || ns->idx == 2);
291
    for (int req = 0; req < NUM_REQS; req++){
292 293 294 295 296 297 298 299 300 301
        tw_lpid dest_lp = (1) * 2; /* send to server 1 */
        testsvr_msg m_net;
        m_net.magic = testsvr_magic;
        m_net.event_type = REQ;
        m_net.idx_src = ns->idx;
        m_net.lp_src = lp->gid;
#if TEST_DEBUG
        m_net.src_event_ctr = ns->event_ctr++;
#endif
        m_net.req_num = req;
302
        m->ret[req] = model_net_event(net_id, "req", dest_lp, 1, 0.0, sizeof(m_net), &m_net, 0, NULL, lp);
303 304 305 306 307 308 309 310 311 312 313 314 315 316
    }
#if TEST_DEBUG
    ns->event_ctr++;
#endif
}

void handle_testsvr_req(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp){

    /* only server 1 processes requests */
    assert(ns->idx == 1);
    /* add a random amount of time to it */
317
    tw_event *e = tw_event_new(lp->gid, codes_local_latency(lp), lp);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    testsvr_msg *m_local = tw_event_data(e);
    *m_local = *m;
    m_local->event_type = LOCAL;
#if TEST_DEBUG
    m_local->src_event_ctr = ns->event_ctr;
#endif

    tw_event_send(e);

#if TEST_DEBUG
    ns->event_ctr++;
#endif
}

void handle_testsvr_ack(
    testsvr_state * ns,
334
    testsvr_msg * m){
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

    /* only servers 0 and 2 handle acks */
    assert(ns->idx == 0 || ns->idx == 2);

    ns->req_stat[m->req_num]++;
    assert(ns->req_stat[m->req_num] < 2);

#if TEST_DEBUG
    ns->event_ctr++;
#endif
}

void handle_testsvr_local(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp){

    assert(ns->idx == 1);
353

354 355 356 357 358 359 360 361 362 363
    testsvr_msg m_net;
    m_net.magic = testsvr_magic;
    m_net.event_type = ACK;
    m_net.idx_src = ns->idx;
    m_net.lp_src = lp->gid;
    m_net.req_num = m->req_num;
    tw_lpid dest_lp = m->idx_src * 2;
#if TEST_DEBUG
    m_net.src_event_ctr = ns->event_ctr;
#endif
364
    m->ret[0] = model_net_event(net_id, "ack", dest_lp,
365
            1, 0.0, sizeof(m_net), &m_net, 0, NULL, lp);
366 367 368 369 370 371 372 373 374 375 376 377
    ns->req_stat[m->req_num]++;
    /* we are handling exactly two reqs per slot */
    assert(ns->req_stat[m->req_num] <= 2);

#if TEST_DEBUG
    ns->event_ctr++;
#endif
}

void handle_testsvr_kickoff_rev(
    testsvr_msg * m,
    tw_lp * lp){
378 379
    int req;
    for (req = 0; req < NUM_REQS; req++){
380
        model_net_event_rc2(lp, &m->ret[req]);
381
    }
382 383 384 385 386 387 388 389 390 391 392 393 394
}

void handle_testsvr_req_rev(
    testsvr_state * ns,
    tw_lp * lp){

    assert(ns->idx == 1);

    codes_local_latency_reverse(lp);
}

void handle_testsvr_ack_rev(
    testsvr_state * ns,
395
    testsvr_msg * m){
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411

    assert(ns->idx == 0 || ns->idx == 2);

    ns->req_stat[m->req_num]--;
    assert(ns->req_stat[m->req_num] >= 0);
}

void handle_testsvr_local_rev(
    testsvr_state * ns,
    testsvr_msg * m,
    tw_lp * lp){

    assert(ns->idx == 1);

    ns->req_stat[m->req_num]--;
    assert(ns->req_stat[m->req_num] >= 0);
412

413
    model_net_event_rc2(lp, &m->ret[0]);
414 415 416
}

/* for debugging: print messages */
417
void dump_msg(testsvr_msg *m, tw_lp *lp, FILE *f){
418 419
    fprintf(f,"event: magic:%10d, src:%1d (LP:%llu), req:%1d, src_event_cnt:%2d, ts:%.5le\n",
            m->magic, m->idx_src, LLU(m->lp_src), m->req_num, m->src_event_ctr, tw_now(lp));
420 421 422 423
}

void dump_state(tw_lp *lp, testsvr_state *ns, FILE *f){
    char *buf = malloc(2048);
424
    int written = sprintf(buf, "idx:%d LP:%llu, event_cnt:%d, [%d",
425
            ns->idx, LLU(lp->gid), ns->event_ctr, ns->req_stat[0]);
426
    int req;
Jonathan Jenkins's avatar
Jonathan Jenkins committed
427
    for (req = 1; req < NUM_REQS; req++){
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
        written += sprintf(buf+written, ",%d",ns->req_stat[req]);
    }
    sprintf(buf+written, "]\n");
    fprintf(f, "%s",buf);
    free(buf);
}

/**** END IMPLEMENTATIONS ****/

/**** BEGIN MAIN ****/

char conf_file_name[256] = {0};

const tw_optdef app_opt[] = {
    TWOPT_GROUP("ROSD mock test model"),
    TWOPT_CHAR("codes-config", conf_file_name, "Name of codes configuration file"),
    TWOPT_END()
};

tw_stime s_to_ns(tw_stime ns)
{
    return(ns * (1000.0 * 1000.0 * 1000.0));
}

int main(int argc, char *argv[])
{
454
    int num_nets, *net_ids;
455 456 457 458 459 460 461 462 463 464 465 466
    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 (!conf_file_name[0]) {
        fprintf(stderr, "Expected \"codes-config\" option, please see --help.\n");
        MPI_Finalize();
        return 1;
    }

    /* loading the config file into the codes-mapping utility, giving us the
467
     * parsed config object in return.
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
     * "config" is a global var defined by codes-mapping */
    if (configuration_load(conf_file_name, MPI_COMM_WORLD, &config)){
        fprintf(stderr, "Error loading config file %s.\n", conf_file_name);
        MPI_Finalize();
        return 1;
    }

    /* currently restrict to simplenet, as other networks are trickier to
     * setup. TODO: handle other networks properly */
    if(net_id != SIMPLENET) {
        printf("\n The test works with simple-net configuration only! ");
        MPI_Finalize();
        return 1;
    }

    testsvr_init();
484
    model_net_register();
485 486 487

    codes_mapping_setup();

488 489 490 491 492 493 494
    /* Setup the model-net parameters specified in the global config object,
     * returned are the identifier for the network type */
    net_ids = model_net_configure(&num_nets);
    assert(num_nets==1);
    net_id = *net_ids;
    free(net_ids);

495 496 497 498 499 500 501 502 503 504 505 506 507 508
    tw_run();
    tw_end();

    return 0;
}

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