example.c 13 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 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 113 114 115 116 117 118 119 120 121 122 123 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
 */

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

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

/* 'magic' numbers used as sanity check on events */
static int node_magic; 
static int forwarder_magic;

/* counts of the various types of nodes in the example system */
static int num_foo_nodes, num_bar_nodes;
static int num_foo_forwarders, num_bar_forwarders;

/* pings to perform (provided by config file) */
static int      num_pings;
static uint64_t payload_sz;

/* network type */
static int net_id;

/* event types */
enum node_event
{
    NODE_KICKOFF = 123,
    NODE_RECV_PING,
    NODE_RECV_PONG,
};

typedef struct node_state_s {
    int is_in_foo;    // whether we're in foo's or bar's cluster
    int id_clust;     // my index within the cluster
    int num_processed; // number of requests processed
} node_state;

typedef struct node_msg_s {
    msg_header h;
    int id_clust_src;
} node_msg;

enum forwarder_event
{
    FORWARDER_FWD = 234,
    FORWARDER_RECV,
};

typedef struct forwarder_state_s {
    int id; // index w.r.t. forwarders in my group 
    int is_in_foo;    
    int fwd_node_count;
    int fwd_forwarder_count;
} forwarder_state;

typedef struct forwarder_msg_s {
    msg_header h;
    int src_node_clust_id;
    int dest_node_clust_id;
    enum node_event node_event_type;
} forwarder_msg;

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

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

void node_lp_init(
        node_state * ns,
        tw_lp * lp){
    ns->num_processed = 0;
    // nodes are addressed in their logical id space (0...num_foo_nodes-1 and
    // 0...num_bar_nodes-1, respectively). LPs are computed upon use with
    // model-net, other events
    ns->id_clust = codes_mapping_get_lp_relative_id(lp->gid, 1, 0);
    int id_all = codes_mapping_get_lp_relative_id(lp->gid, 0, 0);
    // track which cluster we're in
    ns->is_in_foo = (id_all < num_foo_nodes);

    // send a self kickoff event
    tw_event *e = codes_event_new(lp->gid, codes_local_latency(lp), lp);
    node_msg *m = tw_event_data(e);
    msg_set_header(node_magic, NODE_KICKOFF, lp->gid, &m->h);
    tw_event_send(e);
}

void node_finalize(
        node_state * ns,
        tw_lp * lp){
    // do some error checking - here, we ensure we got the expected number of
    // messages
    int mult;
    if (ns->is_in_foo){
        mult = 1; 
    }
    else{
        mult = (num_foo_nodes / num_bar_nodes) +
            ((num_foo_nodes % num_bar_nodes) > ns->id_clust);
    }
    if (ns->num_processed != num_pings*mult){
        fprintf(stderr,
                "%s node %d, lp %lu: processed %d (expected %d)\n",
                ns->is_in_foo ? "foo" : "bar", ns->id_clust, lp->gid,
                ns->num_processed, num_pings*mult);
    }
}

/* event type handlers */
void handle_node_next(
        node_state * ns,
        node_msg * m,
        tw_lp * lp){
    // we must be in cluster foo for this function
    assert(ns->is_in_foo);

    // generate a message to send to the forwarder
    forwarder_msg m_fwd;
    msg_set_header(forwarder_magic, FORWARDER_FWD, lp->gid, &m_fwd.h);

    m_fwd.src_node_clust_id  = ns->id_clust;
    // compute the destination in cluster bar to ping based on a simple modulo
    // of the logical indexes
    m_fwd.dest_node_clust_id = ns->id_clust % num_bar_nodes;
    m_fwd.node_event_type = NODE_RECV_PING;

    // compute the dest forwarder index, again using a simple modulo
    int dest_fwd_id = ns->id_clust % num_foo_forwarders;

    // as the relative forwarder IDs are with respect to groups, the group
    // name must be used
    tw_lpid dest_fwd_lpid = codes_mapping_get_lpid_from_relative(dest_fwd_id,
            "FOO_FORWARDERS", "forwarder", NULL, 0);

    // as cluster nodes have only one network type (+ annotation), no need to
    // use annotation-specific messaging
    model_net_event(net_id, "ping", dest_fwd_lpid, payload_sz, 0.0,
            sizeof(m_fwd), &m_fwd, 0, NULL, lp);
}

void handle_node_recv_ping(
        node_state * ns,
        node_msg * m,
        tw_lp * lp){
    // we must be in cluster bar to receive pings
    assert(!ns->is_in_foo);

    // check that we received the msg from the expected source
    assert(m->id_clust_src % num_bar_nodes == ns->id_clust);

    // setup the response message through the forwarder
    forwarder_msg m_fwd;
    msg_set_header(forwarder_magic, FORWARDER_FWD, lp->gid, &m_fwd.h);
    m_fwd.src_node_clust_id = ns->id_clust;
    m_fwd.dest_node_clust_id = m->id_clust_src;
    m_fwd.node_event_type = NODE_RECV_PONG;

    // compute the dest forwarder index, again using a simple modulus
    int dest_fwd_id = ns->id_clust % num_bar_forwarders;

    // as the relative forwarder IDs are with respect to groups, the group
    // name must be used
    tw_lpid dest_fwd_lpid = codes_mapping_get_lpid_from_relative(dest_fwd_id,
            "BAR_FORWARDERS", "forwarder", NULL, 0);

    model_net_event(net_id, "pong", dest_fwd_lpid, payload_sz, 0.0,
            sizeof(m_fwd), &m_fwd, 0, NULL, lp);

    ns->num_processed++;
}

void handle_node_recv_pong(
        node_state * ns,
        node_msg * m,
        tw_lp * lp){
    // we must be in cluster foo
    assert(ns->id_clust < num_foo_nodes);

    // simply process the next message
    ns->num_processed++;
    if (ns->num_processed < num_pings){
        handle_node_next(ns, m, lp);
    }
}

void node_event_handler(
        node_state * ns,
        tw_bf * b,
        node_msg * m,
        tw_lp * lp){
    assert(m->h.magic == node_magic);
    
    switch (m->h.event_type){
        case NODE_KICKOFF:
            // nodes from foo ping to nodes in bar
            if (ns->is_in_foo){
                handle_node_next(ns, m, lp);
            }
            break;
        case NODE_RECV_PING:
            handle_node_recv_ping(ns, m, lp);
            break;
        case NODE_RECV_PONG:
            handle_node_recv_pong(ns, m, lp);
            break;
        /* ... */
        default:
            tw_error(TW_LOC, "node event type not known");
    }
}

/* ROSS function pointer table for this LP */
static tw_lptype node_lp = {
220 221 222 223 224 225 226
    (init_f) node_lp_init,
    (pre_run_f) NULL,
    (event_f) node_event_handler,
    (revent_f) NULL,
    (final_f)  node_finalize, 
    (map_f) codes_mapping,
    sizeof(node_state),
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
};

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

    bj_hashlittle2("node", strlen("node"), &h1, &h2);
    node_magic = h1+h2;

    lp_type_register("node", &node_lp);
}


/*** Forwarder LP ***/

void forwarder_lp_init(
        forwarder_state * ns,
        tw_lp * lp){
    // like nodes, forwarders in this example are addressed logically 
    ns->id = codes_mapping_get_lp_relative_id(lp->gid, 1, 0);
    int id_all = codes_mapping_get_lp_relative_id(lp->gid, 0, 0);
    ns->is_in_foo = (id_all < num_foo_forwarders);
}

void forwarder_finalize(
        forwarder_state * ns,
        tw_lp * lp){
    // nothing to see here
}

void handle_forwarder_fwd(
        forwarder_state * ns,
        forwarder_msg * m,
        tw_lp * lp){
    // compute the forwarder lpid to forward to 
    int mod;
    const char * dest_group;
    char * category;
    if (ns->is_in_foo){
        mod = num_bar_forwarders;
        dest_group = "BAR_FORWARDERS";
        category = "ping";
    }
    else{
        mod = num_foo_forwarders;
        dest_group = "FOO_FORWARDERS";
        category = "pong";
    }

    // compute the ROSS id corresponding to the dest forwarder
    tw_lpid dest_lpid = codes_mapping_get_lpid_from_relative(
            ns->id % mod, dest_group, "forwarder", NULL, 0);

    forwarder_msg m_fwd = *m;
    msg_set_header(forwarder_magic, FORWARDER_RECV, lp->gid, &m_fwd.h);

    // here, we need to use the unannotated forwarding network, so we
    // use the annotation version of model_net_event
    model_net_event_annotated(net_id, NULL, category, dest_lpid,
            payload_sz, 0.0, sizeof(m_fwd), &m_fwd, 0, NULL, lp);

    ns->fwd_node_count++;
}

void handle_forwarder_recv(
        forwarder_state * ns,
        forwarder_msg * m,
        tw_lp * lp) {
    // compute the node to relay the message to 
    const char * dest_group;
    const char * annotation;
    char * category;
    if (ns->is_in_foo){
        dest_group = "FOO_CLUSTER";    
        annotation = "foo";
        category = "pong";
    }
    else{
        dest_group = "BAR_CLUSTER";
        annotation = "bar";
        category = "ping";
    }

    tw_lpid dest_lpid = codes_mapping_get_lpid_from_relative(
                m->dest_node_clust_id, dest_group, "node",
                NULL, 0);

    node_msg m_node;
    msg_set_header(node_magic, m->node_event_type, lp->gid, &m_node.h);
    m_node.id_clust_src = m->src_node_clust_id;

    // here, we need to use the foo or bar cluster's internal network, so we use
    // the annotated version of model_net_event
    model_net_event_annotated(net_id, annotation, category, dest_lpid,
            payload_sz, 0.0, sizeof(m_node), &m_node, 0, NULL, lp);

    ns->fwd_forwarder_count++;
}

void forwarder_event_handler(
        forwarder_state * ns,
        tw_bf * b,
        forwarder_msg * m,
        tw_lp * lp){
    assert(m->h.magic == forwarder_magic);

    switch(m->h.event_type){
        case FORWARDER_FWD:
            handle_forwarder_fwd(ns, m, lp);
            break;
        case FORWARDER_RECV:
            handle_forwarder_recv(ns, m, lp);
            break;
        default:
            tw_error(TW_LOC, "unknown forwarder event type");
    }
}

static tw_lptype forwarder_lp = {
345 346 347 348 349 350 351
    (init_f) forwarder_lp_init,
    (pre_run_f) NULL,
    (event_f) forwarder_event_handler,
    (revent_f) NULL,
    (final_f)  forwarder_finalize, 
    (map_f) codes_mapping,
    sizeof(forwarder_state),
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 425 426 427 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 454 455 456 457 458 459 460 461 462 463
};

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

    bj_hashlittle2("forwarder", strlen("forwarder"), &h1, &h2);
    forwarder_magic = h1+h2;

    lp_type_register("forwarder", &forwarder_lp);
}

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

/* arguments to be handled by ROSS - strings passed in are expected to be
 * pre-allocated */
static char conf_file_name[256] = {0};
/* this struct contains default parameters used by ROSS, as well as
 * user-specific arguments to be handled by the ROSS config sys. Pass it in
 * prior to calling tw_init */
const tw_optdef app_opt [] =
{
	TWOPT_GROUP("Model net test case" ),
        TWOPT_CHAR("codes-config", conf_file_name, "name of codes configuration file"),
	TWOPT_END()
};

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

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

    /* ROSS initialization function calls */
    tw_opt_add(app_opt); /* add user-defined args */
    /* initialize ROSS and parse args. NOTE: tw_init calls MPI_Init */
    tw_init(&argc, &argv); 

    if (!conf_file_name[0]) {
        tw_error(TW_LOC, 
                "Expected \"codes-config\" option, please see --help.\n");
        return 1;
    }

    /* loading the config file into the codes-mapping utility, giving us the
     * parsed config object in return. 
     * "config" is a global var defined by codes-mapping */
    if (configuration_load(conf_file_name, MPI_COMM_WORLD, &config)){
        tw_error(TW_LOC, "Error loading config file %s.\n", conf_file_name);
        return 1;
    }

    /* register model-net LPs with ROSS */
    model_net_register();

    /* register the user LPs */
    node_register();
    forwarder_register();

    /* setup the LPs and codes_mapping structures */
    codes_mapping_setup();

    /* setup the globals */
    int rc = configuration_get_value_int(&config, "run_params", "num_reqs", NULL,
            &num_pings);
    if (rc != 0)
        tw_error(TW_LOC, "unable to read run_params:num_reqs");
    int payload_sz_d;
    rc = configuration_get_value_int(&config, "run_params", "payload_sz", NULL,
            &payload_sz_d);
    if (rc != 0)
        tw_error(TW_LOC, "unable to read run_params:payload_sz");
    payload_sz = (uint64_t) payload_sz_d;

    /* get the counts for the foo and bar clusters */
    num_foo_nodes = codes_mapping_get_lp_count("FOO_CLUSTER", 0, "node",
            NULL, 1);
    num_bar_nodes = codes_mapping_get_lp_count("BAR_CLUSTER", 0, "node",
            NULL, 1);
    num_foo_forwarders = codes_mapping_get_lp_count("FOO_FORWARDERS", 0,
            "forwarder", NULL, 1);
    num_bar_forwarders = codes_mapping_get_lp_count("BAR_FORWARDERS", 0,
            "forwarder", NULL, 1);


    /* Setup the model-net parameters specified in the global config object,
     * returned are the identifier(s) for the network type. In this example, we
     * only expect one*/
    int num_nets;
    int *net_ids = model_net_configure(&num_nets);
    assert(num_nets==1);
    net_id = *net_ids;
    free(net_ids);

    /* begin simulation */ 
    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
 */