read_ini.c 16 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
156
        user_config->iter_stage[i].counts = NULL;
        user_config->iter_stage[i].displs = NULL;
157
        user_config->iter_stage[i].real_bytes = 0;
158
159
160
161
      }
    }
}

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

205
/*
206
207
208
 * Imprime por salida estandar toda la informacion que contiene
 * la configuracion pasada como argumento
 */
209
void print_config(configuration *user_config, int grp) {
210
211
  if(user_config != NULL) {
    int i;
212
213
    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);
214
215
    for(i=0; i<user_config->iter_stages; i++) {
      printf("Stage %d: PT=%d, T_stage=%lf, bytes=%d\n",
216
        i, user_config->iter_stage[i].pt, user_config->iter_stage[i].t_stage, user_config->iter_stage[i].real_bytes);
217
    }
218
219
220
221
222
223
    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
224
225


226
227
228
229
/*
 * Imprime por salida estandar la informacion relacionada con un
 * solo grupo de procesos en su configuracion.
 */
230
void print_config_group(configuration *user_config, int grp) {
231
  int i;
232
233
234
235
236
237
238
239
240
241
  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];
    }

242
243
    printf("Config: matrix=%d, sdr=%d, adr=%d, aib=%d, css=%d, cst=%d, latency=%lf, bw=%lf\n",
        user_config->matrix_tam, user_config->sdr, user_config->adr, user_config->aib, user_config->css, user_config->cst, user_config->latency_m, user_config->bw_m);
244
245
    for(i=0; i<user_config->iter_stages; i++) {
      printf("Stage %d: PT=%d, T_stage=%lf, bytes=%d\n",
246
        i, user_config->iter_stage[i].pt, user_config->iter_stage[i].t_stage, user_config->iter_stage[i].real_bytes);
247
    }
248
249
250
251
252
    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);
  }
}

253
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
254
255
256
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//| FUNCIONES DE INTERCOMUNICACION DE ESTRUCTURA DE CONFIGURACION ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
257
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |/
iker_martin's avatar
iker_martin committed
258

259
260
261
262
263
264
265
266
/*
 * 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
267
268
void send_config_file(configuration *config_file, int root, MPI_Comm intercomm) {

269
  MPI_Datatype config_type, config_type_array, iter_stage_type;
iker_martin's avatar
iker_martin committed
270

271
272
  // Obtener un tipo derivado para enviar todos los
  // datos escalares con una sola comunicacion
iker_martin's avatar
iker_martin committed
273
274
  def_struct_config_file(config_file, &config_type);

275
276
277

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

280
281
  // Obtener un tipo derivado para enviar las estructuras de fases de iteracion
  // con una sola comunicacion
282
  def_struct_iter_stage(&(config_file->iter_stage[0]), config_file->iter_stages, &iter_stage_type);
283

284
  MPI_Bcast(config_file, 1, config_type, root, intercomm);
iker_martin's avatar
iker_martin committed
285
286
  MPI_Bcast(config_file, 1, config_type_array, root, intercomm);
  MPI_Bcast(config_file->factors, config_file->resizes, MPI_FLOAT, root, intercomm);
287
  MPI_Bcast(config_file->iter_stage, config_file->iter_stages, iter_stage_type, root, intercomm);
iker_martin's avatar
iker_martin committed
288

289
  //Liberar tipos derivados
iker_martin's avatar
iker_martin committed
290
291
  MPI_Type_free(&config_type);
  MPI_Type_free(&config_type_array);
292
  MPI_Type_free(&iter_stage_type);
iker_martin's avatar
iker_martin committed
293
294
}

295
296
297
298
299
300
301
302
303
304
305
/*
 * 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".
 */
306
void recv_config_file(int root, MPI_Comm intercomm, configuration **config_file_out) {
iker_martin's avatar
iker_martin committed
307

308
  MPI_Datatype config_type, config_type_array, iter_stage_type;
iker_martin's avatar
iker_martin committed
309

310

iker_martin's avatar
iker_martin committed
311
312
  configuration *config_file = malloc(sizeof(configuration) * 1);

313
314
315
  // 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
316
317
  MPI_Bcast(config_file, 1, config_type, root, intercomm);

318
319
320
321
  //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);

322
323
  // Obtener un tipo derivado para enviar los tres vectores
  // de enteros con una sola comunicacion
iker_martin's avatar
iker_martin committed
324
  def_struct_config_file_array(config_file, &config_type_array);
325
  def_struct_iter_stage(&(config_file->iter_stage[0]), config_file->iter_stages, &iter_stage_type);
iker_martin's avatar
iker_martin committed
326
327
328

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

331
  //Liberar tipos derivados
iker_martin's avatar
iker_martin committed
332
333
  MPI_Type_free(&config_type);
  MPI_Type_free(&config_type_array);
334
  MPI_Type_free(&iter_stage_type);
iker_martin's avatar
iker_martin committed
335

336
337
  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
338
339
}

340
/*
341
 * Tipo derivado para enviar 11 elementos especificos
342
343
 * de la estructura de configuracion con una sola comunicacion.
 */
iker_martin's avatar
iker_martin committed
344
void def_struct_config_file(configuration *config_file, MPI_Datatype *config_type) {
345
346
  int i, counts = 11;
  int blocklengths[11] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
iker_martin's avatar
iker_martin committed
347
348
349
350
  MPI_Aint displs[counts], dir;
  MPI_Datatype types[counts];

  // Rellenar vector types
351
352
  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
353

354
  // Rellenar vector displs
iker_martin's avatar
iker_martin committed
355
356
357
  MPI_Get_address(config_file, &dir);

  MPI_Get_address(&(config_file->resizes), &displs[0]);
358
359
360
  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]);
361
362
363
364
365
366
367
  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
368
369
370
371
372
373
374

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

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

375
376
377
378
/*
 * Tipo derivado para enviar tres vectores de enteros
 * de la estructura de configuracion con una sola comunicacion.
 */
iker_martin's avatar
iker_martin committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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;

400
  // Tipo derivado para enviar un solo elemento de tres vectores
iker_martin's avatar
iker_martin committed
401
  MPI_Type_create_struct(counts, blocklengths, displs, types, &aux);
402
403
  // 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
404
405
  MPI_Type_commit(config_type);
}
406
407
408
409
410
411


/*
 * Tipo derivado para enviar elementos especificos
 * de la estructuras de fases de iteracion en una sola comunicacion.
 */
412
void def_struct_iter_stage(iter_stage_t *iter_stage, int stages, MPI_Datatype *config_type) {
413
414
415
  int i, counts = 4;
  int blocklengths[4] = {1, 1, 1, 1};
  MPI_Aint displs[counts], dir;
416
  MPI_Datatype aux, types[counts];
417
418

  // Rellenar vector types
419
  types[0] = types[3] = MPI_INT;
420
  types[1] = MPI_FLOAT;
421
  types[2] = MPI_DOUBLE;
422
423
424
425
426
427
428
429
430
431
432

  // 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;

433
434
435
436
437
438
439
  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); 
  }
440
441
  MPI_Type_commit(config_type);
}