read_ini.c 4.96 KB
Newer Older
1
2
3
4
5
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "read_ini.h"
#include "ini.h"
6
#include "../malleability/spawn_methods/ProcessDist.h"
7

iker_martin's avatar
iker_martin committed
8

9
ext_functions_t *user_functions;
iker_martin's avatar
iker_martin committed
10

11
12
13
14
15
16
17
/*
 * Funcion utilizada para leer el fichero de configuracion
 * y guardarlo en una estructura para utilizarlo en el futuro.
 *
 * Primero lee la seccion "general" y a continuacion cada una
 * de las secciones "resize%d".
 */
18
19
static int handler(void* user, const char* section, const char* name,
                   const char* value) {
20
    int ret_value=1;
21
22
    configuration* pconfig = (configuration*)user;

23
    if(pconfig->actual_group >= pconfig->n_groups && pconfig->actual_stage >= pconfig->n_stages) {
24
25
26
      return 1; // There is no more work to perform
    }

27
    char *resize_name = malloc(10 * sizeof(char));
28
    snprintf(resize_name, 10, "resize%zu", pconfig->actual_group);
29

30
    char *stage_name = malloc(10 * sizeof(char));
31
    snprintf(stage_name, 10, "stage%zu", pconfig->actual_stage);
32

33
    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
34
    #define LAST(iter, total) iter < total
35
    if (MATCH("general", "Total_Resizes")) {
36
37
        pconfig->n_resizes = strtoul(value, NULL, 10);
        pconfig->n_groups = pconfig->n_resizes+1;
38
39
        user_functions->resizes_f(pconfig);
    } else if (MATCH("general", "Total_Stages")) {
40
        pconfig->n_stages = strtoul(value, NULL, 10);
41
        user_functions->stages_f(pconfig); 
42
43
    } else if (MATCH("general", "Granularity")) {
        pconfig->granularity = atoi(value);
44
    } else if (MATCH("general", "SDR")) { // TODO Refactor a nombre manual
45
        pconfig->sdr = atoi(value);
46
    } else if (MATCH("general", "ADR")) { // TODO Refactor a nombre manual
47
        pconfig->adr = atoi(value);
48
49
    } else if (MATCH("general", "Rigid")) {
        pconfig->rigid_times = atoi(value);
50

51
    // Iter stage
52
    } else if (MATCH(stage_name, "Stage_Type") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
53
        pconfig->stages[pconfig->actual_stage].pt = atoi(value);
54
55
    } else if (MATCH(stage_name, "Stage_Time_Capped") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
        pconfig->stages[pconfig->actual_stage].t_capped = atoi(value);
56
    } else if (MATCH(stage_name, "Stage_Bytes") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
57
        pconfig->stages[pconfig->actual_stage].bytes = atoi(value);
58
    } else if (MATCH(stage_name, "Stage_Time") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
59
60
        pconfig->stages[pconfig->actual_stage].t_stage = (float) atof(value);
        pconfig->actual_stage = pconfig->actual_stage+1; // Ultimo elemento del grupo
61
62

    // Resize stage
63
64
65
66
67
68
69
    } else if (MATCH(resize_name, "Iters") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].iters = atoi(value);
    } else if (MATCH(resize_name, "Procs") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].procs = atoi(value);
    } else if (MATCH(resize_name, "FactorS") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].factor =(float) atof(value);
    } else if (MATCH(resize_name, "Dist") && LAST(pconfig->actual_group, pconfig->n_groups)) {
70
71
72
73
	int aux_value = MALL_DIST_COMPACT;
        if (strcmp(value, "spread") == 0) {
          aux_value = MALL_DIST_SPREAD;
  	}
74
75
76
77
78
79
80
81
        pconfig->groups[pconfig->actual_group].phy_dist = aux_value;
    } else if (MATCH(resize_name, "Asynch_Redistribution_Type") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].at = atoi(value);
    } else if (MATCH(resize_name, "Spawn_Method") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].sm = atoi(value);
    } else if (MATCH(resize_name, "Spawn_Strategy") && LAST(pconfig->actual_group, pconfig->n_groups)) {
        pconfig->groups[pconfig->actual_group].ss = atoi(value);
        pconfig->actual_group = pconfig->actual_group+1; // Ultimo elemento de la estructura
82

83
    // Unkown case
84
    } else {
85
        ret_value = 0;  /* unknown section or name, error */
86
87
88
    }
 
    free(resize_name);
89
    free(stage_name);
90
    return ret_value;
91
92
}

93
94
95
96
97
98
99
/*
 * Crea y devuelve una estructura de configuracion a traves
 * de un nombre de fichero dado.
 *
 * La memoria de la estructura se reserva en la funcion y es conveniente
 * liberarla con la funcion "free_config()"
 */
100
configuration *read_ini_file(char *file_name, ext_functions_t init_functions) {
101
102
    configuration *config = NULL;

103
    config = malloc(sizeof(configuration));
104
105
106
107
    if(config == NULL) {
        printf("Error when reserving configuration structure\n");
	return NULL;
    }
108
109
    config->n_resizes = 0;
    config->n_groups = 1;
110
    config->n_stages = 1;
111
    config->actual_group=0;
112
    config->actual_stage=0;
113

114
115
    user_functions = &init_functions;

116
117
118
119
120
121
    if(ini_parse(file_name, handler, config) < 0) { // Obtener configuracion
        printf("Can't load '%s'\n", file_name);
        return NULL;
    }
    return config;
}