read_ini.c 3.94 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
        user_functions->resizes_f(pconfig);
    } else if (MATCH("general", "Total_Stages")) {
33
        pconfig->n_stages = atoi(value);
34
        user_functions->stages_f(pconfig);
35
36
    } else if (MATCH("general", "Granularity")) {
        pconfig->granularity = atoi(value);
37
    } else if (MATCH("general", "SDR")) { // TODO Refactor a nombre manual
38
        pconfig->sdr = atoi(value);
39
    } else if (MATCH("general", "ADR")) { // TODO Refactor a nombre manual
40
41
        pconfig->adr = atoi(value);

42
    // Iter stage
43
    } else if (MATCH(stage_name, "Stage_Type")) {
44
45
46
47
48
49
50
	//if(pconfig->actual_stage < pconfig->n_stages)
        pconfig->stages[pconfig->actual_stage].pt = atoi(value);
    } else if (MATCH(stage_name, "Stage_Bytes")) {
        pconfig->stages[pconfig->actual_stage].bytes = atoi(value);
    } else if (MATCH(stage_name, "Stage_Time")) {
        pconfig->stages[pconfig->actual_stage].t_stage = (float) atof(value);
        pconfig->actual_stage = pconfig->actual_stage+1; // Ultimo elemento del grupo
51
52

    // Resize stage
53
    } else if (MATCH(resize_name, "Iters")) {
54
55
	//if(pconfig->actual_resize < pconfig->n_resizes)
        pconfig->groups[pconfig->actual_resize].iters = atoi(value);
56
    } else if (MATCH(resize_name, "Procs")) {
57
        pconfig->groups[pconfig->actual_resize].procs = atoi(value);
58
    } else if (MATCH(resize_name, "FactorS")) {
59
        pconfig->groups[pconfig->actual_resize].factor =(float) atof(value);
60
    } else if (MATCH(resize_name, "Dist")) {
61
62
63
64
65
66
67
68
69
70
71
72
	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;
    } else if (MATCH(resize_name, "Asynch_Redistribution_Type")) {
        pconfig->groups[pconfig->actual_resize].at = atoi(value);
    } else if (MATCH(resize_name, "Spawn_Method")) {
        pconfig->groups[pconfig->actual_resize].sm = atoi(value);
    } else if (MATCH(resize_name, "Spawn_Strategy")) {
        pconfig->groups[pconfig->actual_resize].ss = atoi(value);
        pconfig->actual_resize = pconfig->actual_resize+1; // Ultimo elemento del grupo
73

74
    // Unkown case
75
76
77
78
79
    } else {
        return 0;  /* unknown section or name, error */
    }
 
    free(resize_name);
80
    free(stage_name);
81
82
83
    return 1;
}

84
85
86
87
88
89
90
/*
 * 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()"
 */
91
configuration *read_ini_file(char *file_name, ext_functions_t init_functions) {
92
93
    configuration *config = NULL;

94
    config = malloc(sizeof(configuration));
95
96
97
98
99
    if(config == NULL) {
        printf("Error when reserving configuration structure\n");
	return NULL;
    }
    config->actual_resize=0;
100
    config->actual_stage=0;
101

102
103
    user_functions = &init_functions;

104
105
106
107
108
109
    if(ini_parse(file_name, handler, config) < 0) { // Obtener configuracion
        printf("Can't load '%s'\n", file_name);
        return NULL;
    }
    return config;
}