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

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

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

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

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

48
    // Iter stage
49
    } else if (MATCH(stage_name, "Stage_Type")) {
50
51
52
53
54
55
56
	//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
57
58

    // Resize stage
59
    } else if (MATCH(resize_name, "Iters")) {
60
61
	//if(pconfig->actual_resize < pconfig->n_resizes)
        pconfig->groups[pconfig->actual_resize].iters = atoi(value);
62
    } else if (MATCH(resize_name, "Procs")) {
63
        pconfig->groups[pconfig->actual_resize].procs = atoi(value);
64
    } else if (MATCH(resize_name, "FactorS")) {
65
        pconfig->groups[pconfig->actual_resize].factor =(float) atof(value);
66
    } else if (MATCH(resize_name, "Dist")) {
67
68
69
70
71
72
73
74
75
76
77
78
	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
79

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

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

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

110
111
    user_functions = &init_functions;

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