resource-test.c 4.2 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
/*
 * Copyright (C) 2014 University of Chicago.
 * See COPYRIGHT notice in top-level directory.
 *
*/

#include "codes/resource.h"
#include "codes/resource-lp.h"
#include "codes/lp-msg.h"
#include "codes/configuration.h"
#include "codes/codes_mapping.h"
#include <stdint.h>

static int bsize = 1024;

static int s_magic = 12345;

enum s_type {
    S_KICKOFF,
    S_ALLOC_ACK,
    S_FREE, 
};

typedef struct {
    int id;
    uint64_t mem, mem_max;
} s_state;

typedef struct {
    msg_header h;
    resource_callback c;
    uint64_t mem_max_prev;
} s_msg;

static void s_init(s_state *ns, tw_lp *lp){
    ns->mem = 0;
    ns->mem_max = 0;
38
    ns->id = codes_mapping_get_lp_relative_id(lp->gid, 0, 0);
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    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);
54
            resource_lp_get(&h, bsize, 0, sizeof(s_msg), 
55
56
                    offsetof(s_msg, h), offsetof(s_msg, c), 
                    0, 0, NULL, lp);
57
58
59
60
61
62
63
64
            break;
        case S_ALLOC_ACK:
            if (m->c.ret == 0){
                ns->mem += bsize;
                m->mem_max_prev = ns->mem_max;
                ns->mem_max = max(ns->mem, ns->mem_max);
                msg_header h;
                msg_set_header(s_magic, S_ALLOC_ACK, lp->gid, &h);
65
                resource_lp_get(&h, bsize, 0, sizeof(s_msg), 
66
67
                        offsetof(s_msg, h), offsetof(s_msg, c), 
                        0, 0, NULL, lp);
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
                break;
            }
            /* else fall into the free stmt */ 
        case S_FREE:
            resource_lp_free(bsize, lp);
            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 = {
     (init_f) s_init,
     (event_f) s_event,
     (revent_f) s_event_rc,
     (final_f) s_finalize, 
     (map_f) codes_mapping,
     sizeof(s_state),
};

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