resource-test.c 4.35 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);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
46
    tw_event *e = tw_event_new(lp->gid, codes_local_latency(lp), lp);
47
48
49
50
51
    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){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
52
53
    (void)lp;
    printf("Server %d got %llu memory before failing\n", ns->id, LLU(ns->mem_max));
54
55
56
}

static void s_event(s_state *ns, tw_bf *bf, s_msg *m, tw_lp *lp){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
57
    (void)bf;
58
    assert(m->h.magic == s_magic);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
59
    msg_header h;
60
61
62
    switch(m->h.event_type){
        case S_KICKOFF: ;
            msg_set_header(s_magic, S_ALLOC_ACK, lp->gid, &h);
63
            resource_lp_get(bsize, 0, lp, CODES_MCTX_DEFAULT, 0, &h, &ns->cb);
64
65
66
67
68
            break;
        case S_ALLOC_ACK:
            if (m->c.ret == 0){
                ns->mem += bsize;
                m->mem_max_prev = ns->mem_max;
69
                ns->mem_max = maxu64(ns->mem, ns->mem_max);
70
                msg_set_header(s_magic, S_ALLOC_ACK, lp->gid, &h);
71
72
                resource_lp_get(bsize, 0, lp, CODES_MCTX_DEFAULT, 0, &h,
                        &ns->cb);
73
74
75
76
                break;
            }
            /* else fall into the free stmt */ 
        case S_FREE:
77
            resource_lp_free(bsize, lp, CODES_MCTX_DEFAULT);
78
79
80
            ns->mem -= bsize;
            if (ns->mem > 0){
                tw_event *e = 
Jonathan Jenkins's avatar
Jonathan Jenkins committed
81
                    tw_event_new(lp->gid, codes_local_latency(lp), lp);
Jonathan Jenkins's avatar
Jonathan Jenkins committed
82
83
                s_msg *sm = tw_event_data(e);
                msg_set_header(s_magic, S_FREE, lp->gid, &sm->h);
84
85
86
87
88
89
                tw_event_send(e);
            }
            break;
    }
}
static void s_event_rc(s_state *ns, tw_bf * b, s_msg *m, tw_lp *lp){
Jonathan Jenkins's avatar
Jonathan Jenkins committed
90
    (void)b;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    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 = {
115
116
117
118
119
120
121
    (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),
122
123
124
125
126
};

static char conf_file_name[128] = {'\0'};
static const tw_optdef app_opt [] =
{
127
    TWOPT_GROUP("codes-mapping test case" ),
128
    TWOPT_CHAR("codes-config", conf_file_name, "name of codes configuration file"),
129
    TWOPT_END()
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
};
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
 */