File: multi_tests.c

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (326 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download
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
220
221
222
223
224
225
226
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

/* This file is to be included in run_mpitests.c to provide utility routines
 * for running multi-tests.
 */

extern struct mpitest alltests[];
int num_tests;

FILE *test_in;
FILE *test_out;

/* routines used in run_mpitests.c */
static void init_multi_tests(void);
static bool get_test(const char **name, const char **args, run_fn * fn);
static void load_cvars(void);
static void cleanup_cvars(void);

/* internal routines */
static run_fn find_test(const char *name);
static void split_cmd(char *cmd, const char **name_out, const char **args_out);

static void load_cvars(void);
static void cleanup_cvars(void);
static int get_cvar_idx(const char *name);
static void set_cvar(char *cmd);
static void set_cvar_value(int cvar_idx, const char *val);
static int get_cvar_enum_value(int cvar_idx, const char *str);

/* parsing utilities */
#define skip_space(s)        while (isspace(*s)) s++
#define skip_nonspace(s)     while (*s && !isspace(*s)) s++
#define skip_word(s)         while (isalnum(*s) || *s == '_') s++

#define skip_line(s) \
    do { \
        while (*s && *s != '\n') s++; \
        while (*s && *s == '\n') s++; \
    } while (0)

#define check_null_return(s) \
    do { \
        if (*s == '\0') return; \
    } while (0)


/* for qsort */
static int cmp_testname(const void *a, const void *b)
{
    return strcmp(((struct mpitest *) a)->name, ((struct mpitest *) b)->name);
}

static void init_multi_tests(void)
{
    int n = 0;
    while (alltests[n].name) {
        n++;
    }

    num_tests = n;

    qsort(alltests, num_tests, sizeof(struct mpitest), cmp_testname);

    int thread_level = MPI_THREAD_SINGLE;
    int provided_thread_level;
    MPI_T_init_thread(thread_level, &provided_thread_level);

    load_cvars();

    test_in = stdin;
    test_out = stdout;
}

static void finalize_multi_tests(void)
{
    cleanup_cvars();
    MPI_T_finalize();
}

static run_fn find_test(const char *name)
{
    if (num_tests == 0) {
        return NULL;
    } else if (strcmp(name, alltests[0].name) == 0) {
        return alltests[0].run;
    } else if (num_tests > 1 && strcmp(name, alltests[num_tests - 1].name) == 0) {
        return alltests[num_tests - 1].run;
    }

    /* binary search */
    int i1 = 0;
    int i2 = num_tests - 1;
    while (i1 <= i2) {
        int i3 = (i1 + i2) / 2;
        int ret = strcmp(name, alltests[i3].name);
        if (ret == 0) {
            return alltests[i3].run;
        } else if (ret < 0) {
            i2 = i3 - 1;
        } else {
            i1 = i3 + 1;
        }
    }

    return NULL;
}

static void split_cmd(char *cmd, const char **name_out, const char **args_out)
{
    char *s = cmd;

    *name_out = NULL;
    *args_out = NULL;

    skip_space(s);
    check_null_return(s);
    *name_out = s;

    skip_nonspace(s);
    check_null_return(s);
    *s++ = '\0';

    skip_space(s);
    check_null_return(s);
    *args_out = s;
}

static bool get_test(const char **name, const char **args, run_fn * fn)
{
    static char cmd[ARGS_MAX];
    while (1) {
        /* rank 0 reads the command */
        if (wrank == 0) {
            if (!fgets(cmd, ARGS_MAX, test_in)) {
                cmd[0] = '\0';
            }
        }

        /* bcast */
        MPI_Bcast(cmd, ARGS_MAX, MPI_CHAR, 0, MPI_COMM_WORLD);

        /* all processes process the same command */
        if (cmd[0] == '\0') {
            /* all done */
            return false;
        } else if (strncmp(cmd, "MPIR_CVAR_", 10) == 0) {
            /* set or reset cvar */
            set_cvar(cmd);
            continue;
        } else if (strncmp(cmd, "TIMEOUT ", 8) == 0) {
            /* set timeout */
            int timeout = atoi(cmd + 8);
            if (timeout > 0) {
                alarm(timeout);
            }
            continue;
        } else {
            /* a testlist item */
            split_cmd(cmd, name, args);
            if (*name) {
                *fn = find_test(*name);
            } else {
                *fn = NULL;
            }
            return true;
        }
    }
}

/* CVARs */
#define CVAR_NAME_LEN 256
#define CVAR_VAL_LEN 400        /* ref: MPICH internally at 384 */
#define CVAR_MAX_ENUMS 20
struct cvar {
    char name[CVAR_NAME_LEN];
    MPI_Datatype datatype;
    int has_value;
    char value[CVAR_VAL_LEN];
    int num_enums;
    char *enum_list[CVAR_MAX_ENUMS];
};

static int num_cvars;
static struct cvar *cvar_list;

static void load_cvars(void)
{
    MPI_T_cvar_get_num(&num_cvars);
    cvar_list = malloc(num_cvars * sizeof(struct cvar));

    for (int i = 0; i < num_cvars; i++) {
        int namelen = CVAR_NAME_LEN;
        MPI_T_cvar_get_info(i, cvar_list[i].name, &namelen, NULL, &cvar_list[i].datatype,
                            NULL, NULL, NULL, NULL, NULL);
        cvar_list[i].has_value = 0;
        cvar_list[i].num_enums = 0;
    }
}

static void cleanup_cvars(void)
{
    for (int i = 0; i < num_cvars; i++) {
        if (cvar_list[i].num_enums > 0) {
            for (int j = 0; j < cvar_list[i].num_enums; j++) {
                free(cvar_list[i].enum_list[j]);
            }
        }
    }
    free(cvar_list);
}

static int get_cvar_idx(const char *name)
{
    int idx = -1;
    for (int i = 0; i < num_cvars; i++) {
        if (strcmp(cvar_list[i].name, name) == 0) {
            idx = i;
            break;
        }
    }
    return idx;
}

/* cmd to set cvar:
 *     MPIR_CVAR_XXX=xxx  - set cvar with value
 *     MPIR_CVAR_XXX=     - reset cvar
 */
static void set_cvar(char *cmd)
{
    char *s = cmd;
    skip_word(s);
    if (*s == '=') {
        *s = '\0';
        s++;
        int cvar_idx = get_cvar_idx(cmd);
        if (cvar_idx != -1) {
            if (isalnum(*s)) {
                char *val = s;
                skip_word(s);
                *s = '\0';
                set_cvar_value(cvar_idx, val);
            } else {
                /* reset CVAR */
                set_cvar_value(cvar_idx, NULL);
            }
        }
    }
}

static void set_cvar_value(int cvar_idx, const char *val)
{
    int count;
    MPI_T_cvar_handle handle;

    struct cvar *p = &cvar_list[cvar_idx];

    /* assume MPI_T_BIND_NO_OBJECT and scalar */
    MPI_T_cvar_handle_alloc(cvar_idx, NULL, &handle, &count);
    assert(count == 1 || (p->datatype == MPI_CHAR && count < CVAR_VAL_LEN));

    if (!p->has_value) {
        MPI_T_cvar_read(handle, p->value);
        p->has_value = 1;
    }

    int intval;
    if (val == NULL) {
        /* restore initial value */
        val = p->value;
        if (p->datatype == MPI_INT) {
            intval = *(int *) val;
        }
    } else if (p->datatype == MPI_CHAR) {
        /* val is a C string */
    } else if (p->datatype == MPI_INT) {
        if ((val[0] == '-' && isdigit(val[1])) || isdigit(val[0])) {
            intval = atoi(val);
        } else {
            /* must be an enum */
            intval = get_cvar_enum_value(cvar_idx, val);
        }

        val = (void *) &intval;
    }
    MPI_T_cvar_write(handle, val);
    MPI_T_cvar_handle_free(&handle);
}

static int get_cvar_enum_value(int cvar_idx, const char *str)
{
    struct cvar *p = &cvar_list[cvar_idx];

    /* grab enum_list if not already */
    if (p->num_enums == 0) {
        char desc[1024];
        int desclen = 1024;
        MPI_T_cvar_get_info(cvar_idx, NULL, NULL, NULL, NULL, NULL, desc, &desclen, NULL, NULL);
        const char *s = desc;
        int i = 0;
        while (*s) {
            /* enum values has pattern: /^\w+\s+-/ */
            const char *val = s;
            skip_word(s);
            int val_len = s - val;

            if (*s == ' ') {
                skip_space(s);
                if (s[0] == '-' && s[1] == ' ') {
                    p->enum_list[i++] = strndup(val, val_len);
                    p->num_enums = i;
                }
            }
            skip_line(s);
        }
    }

    for (int i = 0; i < p->num_enums; i++) {
        if (strcmp(p->enum_list[i], str) == 0) {
            return i;
        }
    }
    return -1;
}