read_ini.c 4.84 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
53
	//if(pconfig->actual_stage < pconfig->n_stages)
        pconfig->stages[pconfig->actual_stage].pt = atoi(value);
54
    } else if (MATCH(stage_name, "Stage_Bytes") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
55
        pconfig->stages[pconfig->actual_stage].bytes = atoi(value);
56
    } else if (MATCH(stage_name, "Stage_Time") && LAST(pconfig->actual_stage, pconfig->n_stages)) {
57
58
        pconfig->stages[pconfig->actual_stage].t_stage = (float) atof(value);
        pconfig->actual_stage = pconfig->actual_stage+1; // Ultimo elemento del grupo
59
60

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

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

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

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

112
113
    user_functions = &init_functions;

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