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

7 8 9 10 11 12 13
#include <codes/resource.h>
#include <codes/resource-lp.h>
#include <codes/lp-msg.h>
#include <codes/configuration.h>
#include <codes/codes_mapping.h>
#include <codes/codes-callback.h>
#include <codes/codes-mapping-context.h>
14 15 16 17 18 19
#include <stdint.h>

static int bsize = 1024;

static int s_magic = 12345;

20 21
static uint64_t maxu64(uint64_t a, uint64_t b) { return a < b ? b : a; }

22 23 24 25 26 27 28 29 30
enum s_type {
    S_KICKOFF,
    S_ALLOC_ACK,
    S_FREE, 
};

typedef struct {
    int id;
    uint64_t mem, mem_max;
31
    struct codes_cb_info cb;
32 33 34 35
} s_state;

typedef struct {
    msg_header h;
36 37
    resource_return c;
    int tag;
38 39 40 41 42 43
    uint64_t mem_max_prev;
} s_msg;

static void s_init(s_state *ns, tw_lp *lp){
    ns->mem = 0;
    ns->mem_max = 0;
44
    INIT_CODES_CB_INFO(&ns->cb, s_msg, h, tag, c);
45
    ns->id = codes_mapping_get_lp_relative_id(lp->gid, 0, 0);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    tw_event *e = codes_event_new(lp->gid, codes_local_latency(lp), lp);
    s_msg *m = tw_event_data(e);
    msg_set_header(s_magic, S_KICKOFF, lp->gid, &m->h);
    tw_event_send(e);
}
static void s_finalize(s_state *ns, tw_lp *lp){
    printf("Server %d got %lu memory before failing\n", ns->id, ns->mem_max);
}

static void s_event(s_state *ns, tw_bf *bf, s_msg *m, tw_lp *lp){
    assert(m->h.magic == s_magic);
    switch(m->h.event_type){
        case S_KICKOFF: ;
            msg_header h;
            msg_set_header(s_magic, S_ALLOC_ACK, lp->gid, &h);
61
            resource_lp_get(bsize, 0, lp, CODES_MCTX_DEFAULT, 0, &h, &ns->cb);
62 63 64 65 66
            break;
        case S_ALLOC_ACK:
            if (m->c.ret == 0){
                ns->mem += bsize;
                m->mem_max_prev = ns->mem_max;
67
                ns->mem_max = maxu64(ns->mem, ns->mem_max);
68 69
                msg_header h;
                msg_set_header(s_magic, S_ALLOC_ACK, lp->gid, &h);
70 71
                resource_lp_get(bsize, 0, lp, CODES_MCTX_DEFAULT, 0, &h,
                        &ns->cb);
72 73 74 75
                break;
            }
            /* else fall into the free stmt */ 
        case S_FREE:
76
            resource_lp_free(bsize, lp, CODES_MCTX_DEFAULT);
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
            ns->mem -= bsize;
            if (ns->mem > 0){
                tw_event *e = 
                    codes_event_new(lp->gid, codes_local_latency(lp), lp);
                s_msg *m = tw_event_data(e);
                msg_set_header(s_magic, S_FREE, lp->gid, &m->h);
                tw_event_send(e);
            }
            break;
    }
}
static void s_event_rc(s_state *ns, tw_bf * b, s_msg *m, tw_lp *lp){
    assert(m->h.magic == s_magic);
    switch(m->h.event_type){
        case S_KICKOFF:
            resource_lp_get_rc(lp);
            break;
        case S_ALLOC_ACK:
            if (m->c.ret == 0){
                ns->mem -= bsize;
                ns->mem_max = m->mem_max_prev;
                resource_lp_get_rc(lp);
                break;
            }
            /* else fall into the free stmt */
        case S_FREE:
            /* undoing is unconditional given this lps logic */
            resource_lp_free_rc(lp);
            if (ns->mem > 0){
                codes_local_latency_reverse(lp);
            }
            ns->mem += bsize;
    }
}

static tw_lptype s_lp = {
113 114 115 116 117 118 119
    (init_f) s_init,
    (pre_run_f) NULL,
    (event_f) s_event,
    (revent_f) s_event_rc,
    (final_f) s_finalize, 
    (map_f) codes_mapping,
    sizeof(s_state),
120 121 122 123 124
};

static char conf_file_name[128] = {'\0'};
static const tw_optdef app_opt [] =
{
125
    TWOPT_GROUP("codes-mapping test case" ),
126
    TWOPT_CHAR("codes-config", conf_file_name, "name of codes configuration file"),
127
    TWOPT_END()
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
};
int main(int argc, char *argv[])
{
    g_tw_ts_end = 1e9*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;
    }
    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;
    }

    resource_lp_init();
    lp_type_register("server", &s_lp);

    codes_mapping_setup();

    resource_lp_configure();

    tw_run();
    tw_end();
}

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