read_ini.c 16.2 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
#include "../malleability/distribution_methods/block_distribution.h"
8
9
#include "ini.h"

iker_martin's avatar
iker_martin committed
10

11
12
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
13
14
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);
15
void def_struct_iter_stage(iter_stage_t *iter_stage, int stages, MPI_Datatype *config_type);
iker_martin's avatar
iker_martin committed
16

17
18
19
20
21
22
23
/*
 * 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".
 */
24
25
26
27
28
29
30
31
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);

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

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

57
58
    // Iter stage
    } else if (MATCH(iter_name, "PT")) {
59
60
	if(pconfig->actual_iter < pconfig->iter_stages)
          pconfig->iter_stage[act_iter].pt = atoi(value);
61
    } else if (MATCH(iter_name, "bytes")) {
62
63
	if(pconfig->actual_iter < pconfig->iter_stages)
          pconfig->iter_stage[act_iter].bytes = atoi(value);
64
    } else if (MATCH(iter_name, "t_stage")) {
65
66
67
68
	if(pconfig->actual_iter < pconfig->iter_stages) {
          pconfig->iter_stage[act_iter].t_stage = atof(value);
          pconfig->actual_iter = pconfig->actual_iter+1; // Ultimo elemento del grupo
	}
69
70

    // Resize stage
71
    } else if (MATCH(resize_name, "iters")) {
72
73
	if(pconfig->actual_resize < pconfig->resizes)
          pconfig->iters[act_resize] = atoi(value);
74
    } else if (MATCH(resize_name, "procs")) {
75
76
	if(pconfig->actual_resize < pconfig->resizes)
          pconfig->procs[act_resize] = atoi(value);
77
    } else if (MATCH(resize_name, "factor")) {
78
79
	if(pconfig->actual_resize < pconfig->resizes)
          pconfig->factors[act_resize] = atof(value);
80
    } else if (MATCH(resize_name, "physical_dist")) {
81
82
83
84
85
86
87
88
89
	if(pconfig->actual_resize < pconfig->resizes) {
  	  char *aux = strdup(value);
          if (strcmp(aux, "node") == 0) {
            pconfig->phy_dist[act_resize] = COMM_PHY_NODES;
  	  } else {
            pconfig->phy_dist[act_resize] = COMM_PHY_CPU;
	  }
	  free(aux);
          pconfig->actual_resize = pconfig->actual_resize+1; // Ultimo elemento del grupo
90
	}
91

92
93
94
95
96
    } else {
        return 0;  /* unknown section or name, error */
    }
 
    free(resize_name);
97
    free(iter_name);
98
99
100
    return 1;
}

101
102
103
104
105
106
107
/*
 * 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()"
 */
108
109
110
111
112
113
114
115
116
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;
117
    config->actual_iter=0;
118
119
120
121
122
123
124
125
126
127
128

    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
 *
129
130
131
132
133
134
135
 * 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
136
 */
137
void malloc_config_resizes(configuration *user_config, int resizes) {
138
139
140
    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
141
      user_config->factors = malloc(sizeof(float) * resizes);
142
143
144
145
      user_config->phy_dist = malloc(sizeof(int) * resizes);
    }
}

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * 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;
162
        user_config->iter_stage[i].counts.counts = NULL;
163
        user_config->iter_stage[i].real_bytes = 0;
164
165
166
167
      }
    }
}

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

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


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

241
242
    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);
243
244
    for(i=0; i<user_config->iter_stages; i++) {
      printf("Stage %d: PT=%d, T_stage=%lf, bytes=%d\n",
245
        i, user_config->iter_stage[i].pt, user_config->iter_stage[i].t_stage, user_config->iter_stage[i].real_bytes);
246
    }
247
248
249
250
251
    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);
  }
}

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

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

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

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

274
275
276

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

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

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

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

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

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

309

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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