read_ini.c 15.5 KB
Newer Older
1
2
3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
iker_martin's avatar
iker_martin committed
4
#include <mpi.h>
5
#include "read_ini.h"
iker_martin's avatar
iker_martin committed
6
#include "../malleability/ProcessDist.h"
7
8
#include "ini.h"

iker_martin's avatar
iker_martin committed
9

10
11
void malloc_config_resizes(configuration *user_config, int resizes);
void init_config_stages(configuration *user_config, int stages);
iker_martin's avatar
iker_martin committed
12
13
void def_struct_config_file(configuration *config_file, MPI_Datatype *config_type);
void def_struct_config_file_array(configuration *config_file, MPI_Datatype *config_type);
14
void def_struct_iter_stage(iter_stage_t *iter_stage, int stages, MPI_Datatype *config_type);
iker_martin's avatar
iker_martin committed
15

16
17
18
19
20
21
22
/*
 * 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".
 */
23
24
25
26
27
28
29
30
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));
    int act_resize = pconfig->actual_resize;
    snprintf(resize_name, 10, "resize%d", act_resize);

31
32
33
34
    char *iter_name = malloc(10 * sizeof(char));
    int act_iter = pconfig->actual_iter;
    snprintf(iter_name, 10, "stage%d", act_iter);

35
36
37
    #define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
    if (MATCH("general", "resizes")) {
        pconfig->resizes = atoi(value) + 1;
38
39
40
41
42
        malloc_config_resizes(pconfig, pconfig->resizes);
    } else if (MATCH("general", "iter_stages")) {
        pconfig->iter_stages = atoi(value);
        pconfig->iter_stage = malloc(sizeof(iter_stage_t) * pconfig->iter_stages);
        init_config_stages(pconfig, pconfig->iter_stages);
43
44
45
46
47
48
    } else if (MATCH("general", "matrix_tam")) {
        pconfig->matrix_tam = atoi(value);
    } else if (MATCH("general", "SDR")) {
        pconfig->sdr = atoi(value);
    } else if (MATCH("general", "ADR")) {
        pconfig->adr = atoi(value);
49
    } else if (MATCH("general", "AIB")) { //TODO Refactor cambiar nombre
50
        pconfig->aib = atoi(value);
51
52
53
54
    } else if (MATCH("general", "CST")) {
        pconfig->cst = atoi(value);
    } else if (MATCH("general", "CSS")) {
        pconfig->css = atoi(value);
55

56
57
58
59
60
61
62
63
64
65
66
    // Iter stage
    } else if (MATCH(iter_name, "PT")) {
        pconfig->iter_stage[act_iter].pt = atoi(value);
    } else if (MATCH(iter_name, "bytes")) {
        pconfig->iter_stage[act_iter].bytes = atoi(value);
    } else if (MATCH(iter_name, "t_stage")) {
        pconfig->iter_stage[act_iter].t_stage = atof(value);

        pconfig->actual_iter = pconfig->actual_iter+1; // Ultimo elemento del grupo

    // Resize stage
67
68
69
70
71
72
73
74
75
76
    } else if (MATCH(resize_name, "iters")) {
        pconfig->iters[act_resize] = atoi(value);
    } else if (MATCH(resize_name, "procs")) {
        pconfig->procs[act_resize] = atoi(value);
    } else if (MATCH(resize_name, "factor")) {
        pconfig->factors[act_resize] = atof(value);
    } else if (MATCH(resize_name, "physical_dist")) {

	char *aux = strdup(value);
        if (strcmp(aux, "node") == 0) {
iker_martin's avatar
iker_martin committed
77
          pconfig->phy_dist[act_resize] = COMM_PHY_NODES;
78
	} else {
iker_martin's avatar
iker_martin committed
79
          pconfig->phy_dist[act_resize] = COMM_PHY_CPU;
80
	}
81

82
83
84
85
86
87
88
89
	free(aux);
        pconfig->actual_resize = pconfig->actual_resize+1; // Ultimo elemento del grupo

    } else {
        return 0;  /* unknown section or name, error */
    }
 
    free(resize_name);
90
    free(iter_name);
91
92
93
    return 1;
}

94
95
96
97
98
99
100
/*
 * 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()"
 */
101
102
103
104
105
106
107
108
109
configuration *read_ini_file(char *file_name) {
    configuration *config = NULL;

    config = malloc(sizeof(configuration) * 1);
    if(config == NULL) {
        printf("Error when reserving configuration structure\n");
	return NULL;
    }
    config->actual_resize=0;
110
    config->actual_iter=0;
111
112
113
114
115
116
117
118
119
120
121

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

/*
 * Reserva de memoria para los vectores de la estructura de configuracion
 *
122
123
124
125
126
127
128
 * Si se llama desde fuera de este fichero, la memoria de la estructura
 * tiene que reservarse con la siguiente linea:
 * "configuration *config = malloc(sizeof(configuration));"
 *
 * Sin embargo se puede obtener a traves de las funciones
 *  - read_ini_file
 *  - recv_config_file
129
 */
130
void malloc_config_resizes(configuration *user_config, int resizes) {
131
132
133
    if(user_config != NULL) {
      user_config->iters = malloc(sizeof(int) * resizes);
      user_config->procs = malloc(sizeof(int) * resizes);
iker_martin's avatar
iker_martin committed
134
      user_config->factors = malloc(sizeof(float) * resizes);
135
136
137
138
      user_config->phy_dist = malloc(sizeof(int) * resizes);
    }
}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Inicializa la memoria para las fases de iteraciones.
 * No se reserva memoria, pero si se pone a NULL
 * para poder liberar correctamente cada fase.
 *
 * Se puede obtener a traves de las funciones
 *  - read_ini_file
 *  - recv_config_file
 */
void init_config_stages(configuration *user_config, int stages) {
    int i;
    if(user_config != NULL) {
       for(i=0; i<user_config->iter_stages; i++) {
        user_config->iter_stage[i].array = NULL;
        user_config->iter_stage[i].full_array = NULL;
        user_config->iter_stage[i].double_array = NULL;
155
        user_config->iter_stage[i].real_bytes = 0;
156
157
158
159
      }
    }
}

160
161
162
/*
 * Libera toda la memoria de una estructura de configuracion
 */
163
void free_config(configuration *user_config) {
164
    int i;
165
    if(user_config != NULL) {
166
167
168
169
      free(user_config->iters);
      free(user_config->procs);
      free(user_config->factors);
      free(user_config->phy_dist);
170
      
171
      for(i=0; i < user_config->iter_stages; i++) {
172
        if(user_config->iter_stage[i].array != NULL) {
173
174
          free(user_config->iter_stage[i].array);
          user_config->iter_stage[i].array = NULL;
175
176
	}
        if(user_config->iter_stage[i].full_array != NULL) {
177
178
          free(user_config->iter_stage[i].full_array);
          user_config->iter_stage[i].full_array = NULL;
179
180
	}
        if(user_config->iter_stage[i].double_array != NULL) {
181
182
          free(user_config->iter_stage[i].double_array);
          user_config->iter_stage[i].double_array = NULL;
183
	}
184
      }
185
186
      
      //free(user_config->iter_stage); //FIXME ERROR de memoria relacionado con la carpeta malleability
187
      
188
      free(user_config);
189
190
191
    }
}

192
/*
193
194
195
 * Imprime por salida estandar toda la informacion que contiene
 * la configuracion pasada como argumento
 */
196
void print_config(configuration *user_config, int grp) {
197
198
  if(user_config != NULL) {
    int i;
199
200
    printf("Config loaded: resizes=%d, stages=%d, matrix=%d, sdr=%d, adr=%d, aib=%d, css=%d, cst=%d || grp=%d\n",
        user_config->resizes, user_config->iter_stages, user_config->matrix_tam, user_config->sdr, user_config->adr, user_config->aib, user_config->css, user_config->cst, grp);
201
202
    for(i=0; i<user_config->iter_stages; i++) {
      printf("Stage %d: PT=%d, T_stage=%lf, bytes=%d\n",
203
        i, user_config->iter_stage[i].pt, user_config->iter_stage[i].t_stage, user_config->iter_stage[i].real_bytes);
204
    }
205
206
207
208
209
210
    for(i=0; i<user_config->resizes; i++) {
      printf("Resize %d: Iters=%d, Procs=%d, Factors=%f, Phy=%d\n",
        i, user_config->iters[i], user_config->procs[i], user_config->factors[i], user_config->phy_dist[i]);
    }
  }
}
iker_martin's avatar
iker_martin committed
211
212


213
214
215
216
/*
 * Imprime por salida estandar la informacion relacionada con un
 * solo grupo de procesos en su configuracion.
 */
217
void print_config_group(configuration *user_config, int grp) {
218
  int i;
219
220
221
222
223
224
225
226
227
228
  if(user_config != NULL) {
    int parents, sons;
    parents = sons = 0;
    if(grp > 0) {
      parents = user_config->procs[grp-1];
    }
    if(grp < user_config->resizes - 1) {
      sons = user_config->procs[grp+1];
    }

229
230
    printf("Config: matrix=%d, sdr=%d, adr=%d, aib=%d, css=%d, cst=%d\n",
        user_config->matrix_tam, user_config->sdr, user_config->adr, user_config->aib, user_config->css, user_config->cst);
231
232
    for(i=0; i<user_config->iter_stages; i++) {
      printf("Stage %d: PT=%d, T_stage=%lf, bytes=%d\n",
233
        i, user_config->iter_stage[i].pt, user_config->iter_stage[i].t_stage, user_config->iter_stage[i].real_bytes);
234
    }
235
236
237
238
239
    printf("Config Group: iters=%d, factor=%f, phy=%d, procs=%d, parents=%d, sons=%d\n",
        user_config->iters[grp], user_config->factors[grp], user_config->phy_dist[grp], user_config->procs[grp], parents, sons);
  }
}

240
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
241
242
243
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//| FUNCIONES DE INTERCOMUNICACION DE ESTRUCTURA DE CONFIGURACION ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
244
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |/
iker_martin's avatar
iker_martin committed
245

246
247
248
249
250
251
252
253
/*
 * Envia una estructura de configuracion al grupo de procesos al que se 
 * enlaza este grupo a traves del intercomunicador pasado como argumento.
 *
 * Esta funcion tiene que ser llamada por todos los procesos del mismo grupo
 * e indicar cual es el proceso raiz que se encargara de enviar la
 * configuracion al otro grupo.
 */
iker_martin's avatar
iker_martin committed
254
255
void send_config_file(configuration *config_file, int root, MPI_Comm intercomm) {

256
  MPI_Datatype config_type, config_type_array, iter_stage_type;
iker_martin's avatar
iker_martin committed
257

258
259
  // Obtener un tipo derivado para enviar todos los
  // datos escalares con una sola comunicacion
iker_martin's avatar
iker_martin committed
260
261
  def_struct_config_file(config_file, &config_type);

262
263
264

  // Obtener un tipo derivado para enviar los tres vectores
  // de enteros con una sola comunicacion
iker_martin's avatar
iker_martin committed
265
  def_struct_config_file_array(config_file, &config_type_array);
266

267
268
  // Obtener un tipo derivado para enviar las estructuras de fases de iteracion
  // con una sola comunicacion
269
  def_struct_iter_stage(&(config_file->iter_stage[0]), config_file->iter_stages, &iter_stage_type);
270

271
  MPI_Bcast(config_file, 1, config_type, root, intercomm);
iker_martin's avatar
iker_martin committed
272
273
  MPI_Bcast(config_file, 1, config_type_array, root, intercomm);
  MPI_Bcast(config_file->factors, config_file->resizes, MPI_FLOAT, root, intercomm);
274
  MPI_Bcast(config_file->iter_stage, config_file->iter_stages, iter_stage_type, root, intercomm);
iker_martin's avatar
iker_martin committed
275

276
  //Liberar tipos derivados
iker_martin's avatar
iker_martin committed
277
278
  MPI_Type_free(&config_type);
  MPI_Type_free(&config_type_array);
279
  MPI_Type_free(&iter_stage_type);
iker_martin's avatar
iker_martin committed
280
281
}

282
283
284
285
286
287
288
289
290
291
292
/*
 * Recibe una estructura de configuracion desde otro grupo de procesos
 * y la devuelve. La memoria de la estructura se reserva en esta funcion.
 *
 * Esta funcion tiene que ser llamada por todos los procesos del mismo grupo
 * e indicar cual es el proceso raiz del otro grupo que se encarga de enviar
 * la configuracion a este grupo.
 *
 * La memoria de la configuracion devuelta tiene que ser liberada con
 * la funcion "free_config".
 */
293
void recv_config_file(int root, MPI_Comm intercomm, configuration **config_file_out) {
iker_martin's avatar
iker_martin committed
294

295
  MPI_Datatype config_type, config_type_array, iter_stage_type;
iker_martin's avatar
iker_martin committed
296

297

iker_martin's avatar
iker_martin committed
298
299
  configuration *config_file = malloc(sizeof(configuration) * 1);

300
301
302
  // Obtener un tipo derivado para recibir todos los
  // datos escalares con una sola comunicacion
  def_struct_config_file(config_file, &config_type);
iker_martin's avatar
iker_martin committed
303
304
  MPI_Bcast(config_file, 1, config_type, root, intercomm);

305
306
307
308
  //Inicializado de estructuras internas
  malloc_config_resizes(config_file, config_file->resizes); // Reserva de memoria de los vectores
  config_file->iter_stage = malloc(sizeof(iter_stage_t) * config_file->iter_stages);

309
310
  // Obtener un tipo derivado para enviar los tres vectores
  // de enteros con una sola comunicacion
iker_martin's avatar
iker_martin committed
311
  def_struct_config_file_array(config_file, &config_type_array);
312
  def_struct_iter_stage(&(config_file->iter_stage[0]), config_file->iter_stages, &iter_stage_type);
iker_martin's avatar
iker_martin committed
313
314
315

  MPI_Bcast(config_file, 1, config_type_array, root, intercomm);
  MPI_Bcast(config_file->factors, config_file->resizes, MPI_FLOAT, root, intercomm);
316
  MPI_Bcast(config_file->iter_stage, config_file->iter_stages, iter_stage_type, root, intercomm);
iker_martin's avatar
iker_martin committed
317

318
  //Liberar tipos derivados
iker_martin's avatar
iker_martin committed
319
320
  MPI_Type_free(&config_type);
  MPI_Type_free(&config_type_array);
321
  MPI_Type_free(&iter_stage_type);
iker_martin's avatar
iker_martin committed
322

323
324
  init_config_stages(config_file, config_file->iter_stages); // Inicializar a NULL vectores
  *config_file_out = config_file;
iker_martin's avatar
iker_martin committed
325
326
}

327
/*
328
 * Tipo derivado para enviar 11 elementos especificos
329
330
 * de la estructura de configuracion con una sola comunicacion.
 */
iker_martin's avatar
iker_martin committed
331
void def_struct_config_file(configuration *config_file, MPI_Datatype *config_type) {
332
333
  int i, counts = 11;
  int blocklengths[11] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
iker_martin's avatar
iker_martin committed
334
335
336
337
  MPI_Aint displs[counts], dir;
  MPI_Datatype types[counts];

  // Rellenar vector types
338
339
  types[0] = types[1] = types[2] = types[3] = types[4] = types[5] = types[6] = types[7] = types[8] = MPI_INT;
  types[9] = types[10] = MPI_DOUBLE;
iker_martin's avatar
iker_martin committed
340

341
  // Rellenar vector displs
iker_martin's avatar
iker_martin committed
342
343
344
  MPI_Get_address(config_file, &dir);

  MPI_Get_address(&(config_file->resizes), &displs[0]);
345
346
347
  MPI_Get_address(&(config_file->iter_stages), &displs[1]);
  MPI_Get_address(&(config_file->actual_resize), &displs[2]);
  MPI_Get_address(&(config_file->matrix_tam), &displs[3]);
348
349
350
351
352
353
354
  MPI_Get_address(&(config_file->sdr), &displs[4]);
  MPI_Get_address(&(config_file->adr), &displs[5]);
  MPI_Get_address(&(config_file->aib), &displs[6]);
  MPI_Get_address(&(config_file->css), &displs[7]);
  MPI_Get_address(&(config_file->cst), &displs[8]);
  MPI_Get_address(&(config_file->latency_m), &displs[9]);
  MPI_Get_address(&(config_file->bw_m), &displs[10]);
iker_martin's avatar
iker_martin committed
355
356
357
358
359
360
361

  for(i=0;i<counts;i++) displs[i] -= dir;

  MPI_Type_create_struct(counts, blocklengths, displs, types, config_type);
  MPI_Type_commit(config_type);
}

362
363
364
365
/*
 * Tipo derivado para enviar tres vectores de enteros
 * de la estructura de configuracion con una sola comunicacion.
 */
iker_martin's avatar
iker_martin committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
void def_struct_config_file_array(configuration *config_file, MPI_Datatype *config_type) {
  int i, counts = 3;
  int blocklengths[3] = {1, 1, 1};
  MPI_Aint displs[counts], dir;
  MPI_Datatype aux, types[counts];

  // Rellenar vector types
  types[0] = types[1] = types[2] = MPI_INT;

  // Modificar blocklengths al valor adecuado
  blocklengths[0] = blocklengths[1] = blocklengths[2] = config_file->resizes;

  //Rellenar vector displs
  MPI_Get_address(config_file, &dir);

  MPI_Get_address(config_file->iters, &displs[0]);
  MPI_Get_address(config_file->procs, &displs[1]);
  MPI_Get_address(config_file->phy_dist, &displs[2]);

  for(i=0;i<counts;i++) displs[i] -= dir;

387
  // Tipo derivado para enviar un solo elemento de tres vectores
iker_martin's avatar
iker_martin committed
388
  MPI_Type_create_struct(counts, blocklengths, displs, types, &aux);
389
390
  // Tipo derivado para enviar N elementos de tres vectores(3N en total)
  MPI_Type_create_resized(aux, 0, 1*sizeof(int), config_type); 
iker_martin's avatar
iker_martin committed
391
392
  MPI_Type_commit(config_type);
}
393
394
395
396
397
398


/*
 * Tipo derivado para enviar elementos especificos
 * de la estructuras de fases de iteracion en una sola comunicacion.
 */
399
void def_struct_iter_stage(iter_stage_t *iter_stage, int stages, MPI_Datatype *config_type) {
400
401
402
  int i, counts = 4;
  int blocklengths[4] = {1, 1, 1, 1};
  MPI_Aint displs[counts], dir;
403
  MPI_Datatype aux, types[counts];
404
405

  // Rellenar vector types
406
  types[0] = types[3] = MPI_INT;
407
  types[1] = MPI_FLOAT;
408
  types[2] = MPI_DOUBLE;
409
410
411
412
413
414
415
416
417
418
419

  // Rellenar vector displs
  MPI_Get_address(iter_stage, &dir);

  MPI_Get_address(&(iter_stage->pt), &displs[0]);
  MPI_Get_address(&(iter_stage->t_stage), &displs[1]);
  MPI_Get_address(&(iter_stage->t_op), &displs[2]);
  MPI_Get_address(&(iter_stage->bytes), &displs[3]);

  for(i=0;i<counts;i++) displs[i] -= dir;

420
421
422
423
424
425
426
  if (stages == 1) {
    MPI_Type_create_struct(counts, blocklengths, displs, types, config_type);
  } else { // Si hay mas de una fase(estructura), el "extent" se modifica.
    MPI_Type_create_struct(counts, blocklengths, displs, types, &aux);
    // Tipo derivado para enviar N elementos de la estructura
    MPI_Type_create_resized(aux, 0, 1*sizeof(iter_stage_t), config_type); 
  }
427
428
  MPI_Type_commit(config_type);
}