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
24
25
26
    if(pconfig->actual_resize >= pconfig->n_resizes && pconfig->actual_stage >= pconfig->n_stages) {
      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_resize);
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
        pconfig->n_resizes = strtoul(value, NULL, 10) + 1;
37
38
        user_functions->resizes_f(pconfig);
    } else if (MATCH("general", "Total_Stages")) {
39
        pconfig->n_stages = strtoul(value, NULL, 10);
40
        user_functions->stages_f(pconfig); 
41
42
    } else if (MATCH("general", "Granularity")) {
        pconfig->granularity = atoi(value);
43
    } else if (MATCH("general", "SDR")) { // TODO Refactor a nombre manual
44
        pconfig->sdr = atoi(value);
45
    } else if (MATCH("general", "ADR")) { // TODO Refactor a nombre manual
46
        pconfig->adr = atoi(value);
47
48
    } else if (MATCH("general", "Rigid")) {
        pconfig->rigid_times = atoi(value);
49

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

    // Resize stage
62
    } else if (MATCH(resize_name, "Iters") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
63
64
	//if(pconfig->actual_resize < pconfig->n_resizes)
        pconfig->groups[pconfig->actual_resize].iters = atoi(value);
65
    } else if (MATCH(resize_name, "Procs") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
66
        pconfig->groups[pconfig->actual_resize].procs = atoi(value);
67
    } else if (MATCH(resize_name, "FactorS") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
68
        pconfig->groups[pconfig->actual_resize].factor =(float) atof(value);
69
    } else if (MATCH(resize_name, "Dist") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
70
71
72
73
74
	int aux_value = MALL_DIST_COMPACT;
        if (strcmp(value, "spread") == 0) {
          aux_value = MALL_DIST_SPREAD;
  	}
        pconfig->groups[pconfig->actual_resize].phy_dist = aux_value;
75
    } else if (MATCH(resize_name, "Asynch_Redistribution_Type") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
76
        pconfig->groups[pconfig->actual_resize].at = atoi(value);
77
    } else if (MATCH(resize_name, "Spawn_Method") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
78
        pconfig->groups[pconfig->actual_resize].sm = atoi(value);
79
    } else if (MATCH(resize_name, "Spawn_Strategy") && LAST(pconfig->actual_resize, pconfig->n_resizes)) {
80
81
        pconfig->groups[pconfig->actual_resize].ss = atoi(value);
        pconfig->actual_resize = pconfig->actual_resize+1; // Ultimo elemento del grupo
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 = 1;
    config->n_stages = 1;
110
    config->actual_resize=0;
111
    config->actual_stage=0;
112

113
114
    user_functions = &init_functions;

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