Source

mpi3-fortran / ompi / mca / coll / ml / coll_ml_config.c

Full commit
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include "ompi_config.h"

#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "opal/mca/base/mca_base_param.h"

#include "coll_ml.h"
#include "coll_ml_inlines.h"
#include "coll_ml_config.h"
#include "coll_ml_lex.h"

static char *key_buffer = NULL;
static size_t key_buffer_len = 0;

typedef struct section_config_t {
    char *section_name;
    int section_id;
    per_collective_configuration_t config;
} section_config_t;

typedef struct coll_config_t {
    char *coll_name;
    int coll_id;
    section_config_t section;
} coll_config_t;

static int algorithm_name_to_id(char *name)
{
    if (!strcasecmp(name,"ML_BCAST_SMALL_DATA_KNOWN"))
        return ML_BCAST_SMALL_DATA_KNOWN;
    if (!strcasecmp(name,"ML_BCAST_SMALL_DATA_UNKNOWN"))
        return ML_BCAST_SMALL_DATA_UNKNOWN;
    if (!strcasecmp(name,"ML_BCAST_SMALL_DATA_SEQUENTIAL"))
        return ML_BCAST_SMALL_DATA_SEQUENTIAL;
    if (!strcasecmp(name,"ML_BCAST_LARGE_DATA_KNOWN"))
        return ML_BCAST_LARGE_DATA_KNOWN;
    if (!strcasecmp(name,"ML_BCAST_LARGE_DATA_UNKNOWN"))
        return ML_BCAST_LARGE_DATA_UNKNOWN;
    if (!strcasecmp(name,"ML_BCAST_LARGE_DATA_SEQUENTIAL"))
        return ML_BCAST_LARGE_DATA_SEQUENTIAL;
    if (!strcasecmp(name,"ML_N_DATASIZE_BINS"))
        return ML_N_DATASIZE_BINS;
    if (!strcasecmp(name,"ML_NUM_BCAST_FUNCTIONS"))
        return ML_NUM_BCAST_FUNCTIONS;
    if (!strcasecmp(name,"ML_SCATTER_SMALL_DATA_KNOWN"))
        return ML_SCATTER_SMALL_DATA_KNOWN;
    if (!strcasecmp(name,"ML_SCATTER_N_DATASIZE_BINS"))
        return ML_SCATTER_N_DATASIZE_BINS;
    if (!strcasecmp(name,"ML_SCATTER_SMALL_DATA_UNKNOWN"))
        return ML_SCATTER_SMALL_DATA_UNKNOWN;
    if (!strcasecmp(name,"ML_SCATTER_SMALL_DATA_SEQUENTIAL"))
        return ML_SCATTER_SMALL_DATA_SEQUENTIAL;
    if (!strcasecmp(name,"ML_NUM_SCATTER_FUNCTIONS"))
        return ML_NUM_SCATTER_FUNCTIONS;
    if (!strcasecmp(name,"ML_SMALL_DATA_ALLREDUCE"))
        return ML_SMALL_DATA_ALLREDUCE;
    if (!strcasecmp(name,"ML_LARGE_DATA_ALLREDUCE"))
        return ML_LARGE_DATA_ALLREDUCE;
    if (!strcasecmp(name,"ML_NUM_ALLREDUCE_FUNCTIONS"))
        return ML_NUM_ALLREDUCE_FUNCTIONS;
    if (!strcasecmp(name,"ML_SMALL_DATA_ALLTOALL"))
        return ML_SMALL_DATA_ALLTOALL;
    if (!strcasecmp(name,"ML_LARGE_DATA_ALLTOALL"))
        return ML_LARGE_DATA_ALLTOALL;
    if (!strcasecmp(name,"ML_NUM_ALLTOALL_FUNCTIONS"))
        return ML_NUM_ALLTOALL_FUNCTIONS;
    if (!strcasecmp(name,"ML_SMALL_DATA_ALLGATHER"))
        return ML_SMALL_DATA_ALLGATHER;
    if (!strcasecmp(name,"ML_LARGE_DATA_ALLGATHER"))
        return ML_LARGE_DATA_ALLGATHER;
    if (!strcasecmp(name,"ML_NUM_ALLGATHER_FUNCTIONS"))
        return ML_NUM_ALLGATHER_FUNCTIONS;
    if (!strcasecmp(name,"ML_SMALL_DATA_GATHER"))
        return ML_SMALL_DATA_GATHER;
    if (!strcasecmp(name,"ML_LARGE_DATA_GATHER"))
        return ML_LARGE_DATA_GATHER;
    if (!strcasecmp(name,"ML_NUM_GATHER_FUNCTIONS"))
        return ML_NUM_GATHER_FUNCTIONS;
    if (!strcasecmp(name,"ML_BARRIER_DEFAULT"))
        return ML_BARRIER_DEFAULT;

    /* ERROR */
    return ML_UNDEFINED;
}

static int hierarchy_name_to_id(char *name)
{
    if (!strcasecmp(name, "FULL_HR")) {
        return COLL_ML_HR_FULL;
    }
    if (!strcasecmp(name, "FULL_HR_NO_BASESOCKET")) {
        return COLL_ML_HR_NBS;
    }
    if (!strcasecmp(name, "PTP_ONLY")) {
        return COLL_ML_HR_SINGLE_PTP;
    }
    if (!strcasecmp(name, "IBOFFLOAD_ONLY")) {
        return COLL_ML_HR_SINGLE_IBOFFLOAD;
    }
    /* Error */
    return ML_UNDEFINED;
}

static int section_name_to_id(char *name)
{
    if (!strcasecmp(name, "SMALL")) {
        return ML_SMALL_MSG;
    }

    if (!strcasecmp(name, "LARGE")) {
        return ML_LARGE_MSG;
    }
    /* Error */
    return ML_UNDEFINED;
}

static int coll_name_to_id(char *name)
{
    if (!strcasecmp(name, "ALLGATHER")) {
        return ML_ALLGATHER;
    }
    if (!strcasecmp(name, "ALLGATHERV")) {
        return ML_ALLGATHERV;
    }
    if (!strcasecmp(name, "ALLREDUCE")) {
        return ML_ALLREDUCE;
    }
    if (!strcasecmp(name, "ALLTOALL")) {
        return ML_ALLTOALL;
    }
    if (!strcasecmp(name, "ALLTOALLV")) {
        return ML_ALLTOALLV;
    }
    if (!strcasecmp(name, "ALLTOALLW")) {
        return ML_ALLTOALLW;
    }
    if (!strcasecmp(name, "ALLTOALLW")) {
        return ML_ALLTOALLW;
    }
    if (!strcasecmp(name, "BARRIER")) {
        return ML_BARRIER;
    }
    if (!strcasecmp(name, "BCAST")) {
        return ML_BCAST;
    }
    if (!strcasecmp(name, "EXSCAN")) {
        return ML_EXSCAN;
    }
    if (!strcasecmp(name, "GATHER")) {
        return ML_GATHER;
    }
    if (!strcasecmp(name, "GATHERV")) {
        return ML_GATHERV;
    }
    if (!strcasecmp(name, "REDUCE")) {
        return ML_REDUCE;
    }
    if (!strcasecmp(name, "REDUCE_SCATTER")) {
        return ML_REDUCE_SCATTER;
    }
    if (!strcasecmp(name, "SCAN")) {
        return ML_SCAN;
    }
    if (!strcasecmp(name, "SCATTER")) {
        return ML_SCATTER;
    }
    if (!strcasecmp(name, "SCATTERV")) {
        return ML_SCATTERV;
    }

    /* nonblocking functions */

    if (!strcasecmp(name, "IALLGATHER")) {
        return ML_IALLGATHER;
    }
    if (!strcasecmp(name, "IALLGATHERV")) {
        return ML_IALLGATHERV;
    }
    if (!strcasecmp(name, "IALLREDUCE")) {
        return ML_IALLREDUCE;
    }
    if (!strcasecmp(name, "IALLTOALL")) {
        return ML_IALLTOALL;
    }
    if (!strcasecmp(name, "IALLTOALLV")) {
        return ML_IALLTOALLV;
    }
    if (!strcasecmp(name, "IALLTOALLW")) {
        return ML_IALLTOALLW;
    }
    if (!strcasecmp(name, "IALLTOALLW")) {
        return ML_IALLTOALLW;
    }
    if (!strcasecmp(name, "IBARRIER")) {
        return ML_IBARRIER;
    }
    if (!strcasecmp(name, "IBCAST")) {
        return ML_IBCAST;
    }
    if (!strcasecmp(name, "IEXSCAN")) {
        return ML_IEXSCAN;
    }
    if (!strcasecmp(name, "IGATHER")) {
        return ML_IGATHER;
    }
    if (!strcasecmp(name, "IGATHERV")) {
        return ML_IGATHERV;
    }
    if (!strcasecmp(name, "IREDUCE")) {
        return ML_IREDUCE;
    }
    if (!strcasecmp(name, "IREDUCE_SCATTER")) {
        return ML_IREDUCE_SCATTER;
    }
    if (!strcasecmp(name, "ISCAN")) {
        return ML_ISCAN;
    }
    if (!strcasecmp(name, "ISCATTER")) {
        return ML_ISCATTER;
    }
    if (!strcasecmp(name, "ISCATTERV")) {
        return ML_ISCATTERV;
    }

    /* Error - collecives name was not matched */
    return ML_UNDEFINED;
}
static int set_collective_name(coll_config_t *coll_config) 
{
    int coll_id = 
        coll_name_to_id(coll_ml_config_yytext);

    if (ML_UNDEFINED == coll_id) {
        return OMPI_ERROR;
    }

    coll_config->coll_id = coll_id;
    coll_config->coll_name = strdup(coll_ml_config_yytext);

    return OMPI_SUCCESS;
}

static int set_section_name(section_config_t *section_config) 
{
    int section_id;

    section_id = section_name_to_id(coll_ml_config_yytext);

    if (ML_UNDEFINED == section_id) {
        return OMPI_ERROR;
    }

    section_config->section_id = section_id;
    section_config->section_name = strdup(coll_ml_config_yytext);

    return OMPI_SUCCESS;
}

void mca_coll_ml_reset_config(per_collective_configuration_t *config) 
{
    config->topology_id = ML_UNDEFINED;
    config->threshold = ML_UNDEFINED;;
    config->algorithm_id = ML_UNDEFINED;;
    config->fragmentation_enabled = ML_UNDEFINED;;
}

static void reset_section(section_config_t *section_cf)
{
    section_cf->section_name = NULL;
    section_cf->section_id = ML_UNDEFINED;
    mca_coll_ml_reset_config(&section_cf->config);
}

static void reset_collective(coll_config_t *coll_cf)
{
    coll_cf->coll_name = NULL;
    coll_cf->coll_id = ML_UNDEFINED;
    reset_section(&coll_cf->section);
}

/*
 * String to integer;
 */
static int string_to_int(char *str)
{
    while (isspace(*str)) {
        ++str;
    }

    /* Nope -- just decimal, so use atoi() */
    return atoi(str);
}

static int parse_algorithm_key(section_config_t *section, char *value)
{
    int ret;
    ret = algorithm_name_to_id(value);
    if (ML_UNDEFINED == ret) {
        return OMPI_ERROR;
    } else {
        section->config.algorithm_id = ret;
    }

    return OMPI_SUCCESS;
}

static int parse_threshold_key(section_config_t *section, char *value)
{
    if(!strcasecmp(value, "unlimited")) {
        section->config.threshold = -1;
    } else {
        section->config.threshold = string_to_int(value);
    }

    return OMPI_SUCCESS;
}

static int parse_hierarchy_key(section_config_t *section, char *value)
{
    int ret;

    ret = hierarchy_name_to_id(value);
    if (ML_UNDEFINED == ret) {
        return OMPI_ERROR;
    }

    section->config.topology_id = ret;

    return OMPI_SUCCESS;
}

static int parse_fragmentation_key(section_config_t *section, char *value)
{
    if(!strcasecmp(value, "enable")) {
        section->config.fragmentation_enabled = 1;
    } else if (!strcasecmp(value, "disable")) {
        section->config.fragmentation_enabled = 0;
    } else {
        ML_ERROR(("Line %d, unexpected fragmentation value %s. Ligal values are: enable/disable",
                    coll_ml_config_yynewlines, value));
        return OMPI_ERROR;
    }
    return OMPI_SUCCESS;
}

/* Save configuration that have been collected so far */
static void save_settings(coll_config_t *coll_config)
{
    per_collective_configuration_t *cf = 
        &mca_coll_ml_component.coll_config[coll_config->coll_id][coll_config->section.section_id];

    cf->topology_id = coll_config->section.config.topology_id;
    cf->threshold = coll_config->section.config.threshold; 
    cf->algorithm_id = coll_config->section.config.algorithm_id;
    cf->fragmentation_enabled = coll_config->section.config.fragmentation_enabled;
}

/*
 * Parse a single line
 */
static int parse_line(section_config_t *section)
{
    int val, ret = OMPI_SUCCESS;
    char *value = NULL;

    /* Save the name name */
    if (key_buffer_len < strlen(coll_ml_config_yytext) + 1) {
        char *tmp;
        key_buffer_len = strlen(coll_ml_config_yytext) + 1;
        tmp = (char *) realloc(key_buffer, key_buffer_len);
        if (NULL == tmp) {
            free(key_buffer);
            key_buffer_len = 0;
            key_buffer = NULL;
            return OMPI_ERR_TEMP_OUT_OF_RESOURCE;
        }
        key_buffer = tmp;
    }
    strncpy(key_buffer, coll_ml_config_yytext, key_buffer_len);

    /* The first thing we have to see is an "=" */
    val = coll_ml_config_yylex();
    if (coll_ml_config_parse_done || COLL_ML_CONFIG_PARSE_EQUAL != val) {
        ML_ERROR(("Line %d, expected = before key: %s", 
                    coll_ml_config_yynewlines, 
                    key_buffer));
        return OMPI_ERROR;
    }

    /* Next we get the value */
    val = coll_ml_config_yylex();
    if (COLL_ML_CONFIG_PARSE_SINGLE_WORD == val ||
        COLL_ML_CONFIG_PARSE_VALUE == val) {
        value = strdup(coll_ml_config_yytext);

        /* Now we need to see the newline */
        val = coll_ml_config_yylex();
        if (COLL_ML_CONFIG_PARSE_NEWLINE != val &&
            COLL_ML_CONFIG_PARSE_DONE != val) {
            ML_ERROR(("Line %d, expected new line after %s",
                    coll_ml_config_yynewlines,
                    key_buffer));
            free(value);
            return OMPI_ERROR;
        }
    }

    /* If we did not get EOL or EOF, something is wrong */
    else if (COLL_ML_CONFIG_PARSE_DONE != val &&
             COLL_ML_CONFIG_PARSE_NEWLINE != val) {
        ML_ERROR(("Line %d, expected new line or end of line",
                    coll_ml_config_yynewlines));
        ret = OMPI_ERROR;
        goto Error;
    }

    /* Line parsing is done, read the values */
    if (!strcasecmp(key_buffer, "algorithm")) {
        ret = parse_algorithm_key(section, value);
        if (OMPI_SUCCESS != ret) {
            goto Error;
        }
    }

    else if (!strcasecmp(key_buffer, "threshold")) {
        ret = parse_threshold_key(section, value);
        if (OMPI_SUCCESS != ret) {
            goto Error;
        }
    }

    else if (!strcasecmp(key_buffer, "hierarchy")) {
        ret = parse_hierarchy_key(section, value);
        if (OMPI_SUCCESS != ret) {
            goto Error;
        }
    }

    else if (!strcasecmp(key_buffer, "fragmentation")) {
        ret = parse_fragmentation_key(section, value);
        if (OMPI_SUCCESS != ret) {
            goto Error;
        }
    /* Failed to parse the key */
    } else {
        ML_ERROR(("Line %d, unknown key %s",
                    coll_ml_config_yynewlines, key_buffer));
    }
    
    /* All done */
Error:
    if (NULL != value) {
        free(value);
    }

    return ret;
}

/**************************************************************************/

/*
 * Parse a single file
 */
static int parse_file(char *filename)
{
    int val;
    int ret = OMPI_SUCCESS;
    bool first_section = true,
         first_coll = true;

    coll_config_t coll_config;

    reset_collective(&coll_config);

    /* Open the file */
    coll_ml_config_yyin = fopen(filename, "r");
    if (NULL == coll_ml_config_yyin) {
        ML_ERROR(("Failed to open config file %s", filename));
        ret = OMPI_ERR_NOT_FOUND;
        goto cleanup;
    }

    /* Do the parsing */
    coll_ml_config_parse_done = false;
    coll_ml_config_yynewlines = 1;
    coll_ml_config_init_buffer(coll_ml_config_yyin);
    while (!coll_ml_config_parse_done) {
        val = coll_ml_config_yylex();
        switch (val) {
        case COLL_ML_CONFIG_PARSE_DONE:
            break;
        case COLL_ML_CONFIG_PARSE_NEWLINE:
            break;
        case COLL_ML_CONFIG_PARSE_COLLECTIVE:
            /* dump all the information to last section that was defined*/
            if(!first_coll) {
                save_settings(&coll_config);
            }
            first_coll = false;
            /* reset collective config */
            reset_collective(&coll_config);

            ret = set_collective_name(&coll_config);
            if (OMPI_SUCCESS != ret) {
                goto cleanup;
            }
            break;
        case COLL_ML_CONFIG_PARSE_SECTION:
            if (NULL == coll_config.coll_name) {
                ML_ERROR(("Collective section wasn't defined !"));
                ret = OMPI_ERROR;
                goto cleanup;
            }
            /* dump all the information to last section that was defined */
            if(!first_section) {
                save_settings(&coll_config);
            }
            first_section = false;
            /* reset all section values */
            reset_section(&coll_config.section);
            /* set new section name */
            ret = set_section_name(&coll_config.section);
            if (OMPI_SUCCESS != ret) {
                goto cleanup;
            }
            break;
        case COLL_ML_CONFIG_PARSE_SINGLE_WORD:
            if (NULL == coll_config.coll_name ||
                NULL == coll_config.section.section_name) {
                ML_ERROR(("Collective section or sub-section was not defined !"));
                ret = OMPI_ERROR;
                goto cleanup;
            } else {
                parse_line(&coll_config.section);
            }
            break;

        default:
            /* anything else is an error */
            ML_ERROR(("Unexpected token!"));
            ret = OMPI_ERROR;
            goto cleanup;
            break;
        }
    }
    save_settings(&coll_config);
    fclose(coll_ml_config_yyin);
    coll_ml_config_yylex_destroy ();
    ret = OMPI_SUCCESS;

cleanup:
    reset_collective(&coll_config);
    if (NULL != key_buffer) {
        free(key_buffer);
        key_buffer = NULL;
        key_buffer_len = 0;
    }
    return ret;
}

int mca_coll_ml_config_file_init(void)
{

    int ret = OMPI_ERR_NOT_FOUND;

    ret = parse_file(mca_coll_ml_component.config_file_name);

    return (OMPI_SUCCESS == ret ) ?
        OMPI_SUCCESS : ret;
}