read_ini.c 4.31 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
20
21
22
static int handler(void* user, const char* section, const char* name,
                   const char* value) {
    configuration* pconfig = (configuration*)user;

    char *resize_name = malloc(10 * sizeof(char));
23
    snprintf(resize_name, 10, "resize%d", pconfig->actual_resize);
24

25
26
    char *stage_name = malloc(10 * sizeof(char));
    snprintf(stage_name, 10, "stage%d", pconfig->actual_stage);
27

28
    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
29
    if (MATCH("general", "Total_Resizes")) {
30
        pconfig->n_resizes = atoi(value) + 1;
31
32
33
        //malloc_config_resizes(pconfig); //FIXME Unknown
        user_functions->resizes_f(pconfig);
    } else if (MATCH("general", "Total_Stages")) {
34
        pconfig->n_stages = atoi(value);
35
36
37
        pconfig->stages = malloc(sizeof(iter_stage_t) * (size_t) pconfig->n_stages);
        //init_config_stages(pconfig); //FIXME Unkown
        user_functions->stages_f(pconfig);
38
39
    } else if (MATCH("general", "Granularity")) {
        pconfig->granularity = atoi(value);
40
    } else if (MATCH("general", "SDR")) { // TODO Refactor a nombre manual
41
        pconfig->sdr = atoi(value);
42
    } else if (MATCH("general", "ADR")) { // TODO Refactor a nombre manual
43
        pconfig->adr = atoi(value);
44
    } else if (MATCH("general", "Asynch_Redistribution_Type")) {
45
        pconfig->at = atoi(value);
46
    } else if (MATCH("general", "Spawn_Method")) {
47
        pconfig->sm = atoi(value);
48
    } else if (MATCH("general", "Spawn_Strategy")) {
49
        pconfig->ss = atoi(value);
50

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

    // Resize stage
65
66
67
68
69
70
71
72
    } else if (MATCH(resize_name, "Iters")) {
	if(pconfig->actual_resize < pconfig->n_resizes)
          pconfig->iters[pconfig->actual_resize] = atoi(value);
    } else if (MATCH(resize_name, "Procs")) {
	if(pconfig->actual_resize < pconfig->n_resizes)
          pconfig->procs[pconfig->actual_resize] = atoi(value);
    } else if (MATCH(resize_name, "FactorS")) {
	if(pconfig->actual_resize < pconfig->n_resizes)
73
          pconfig->factors[pconfig->actual_resize] =(float) atof(value);
74
75
    } else if (MATCH(resize_name, "Dist")) {
	if(pconfig->actual_resize < pconfig->n_resizes) {
76
  	  char *aux = strdup(value);
77
          if (strcmp(aux, "spread") == 0) {
78
            pconfig->phy_dist[pconfig->actual_resize] = MALL_DIST_SPREAD;
79
  	  } else {
80
            pconfig->phy_dist[pconfig->actual_resize] = MALL_DIST_COMPACT;
81
82
83
	  }
	  free(aux);
          pconfig->actual_resize = pconfig->actual_resize+1; // Ultimo elemento del grupo
84
	}
85

86
87
88
89
90
    } else {
        return 0;  /* unknown section or name, error */
    }
 
    free(resize_name);
91
    free(stage_name);
92
93
94
    return 1;
}

95
96
97
98
99
100
101
/*
 * 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()"
 */
102
configuration *read_ini_file(char *file_name, ext_functions_t init_functions) {
103
104
    configuration *config = NULL;

105
    config = malloc(sizeof(configuration));
106
107
108
109
110
    if(config == NULL) {
        printf("Error when reserving configuration structure\n");
	return NULL;
    }
    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;
}