configuration.c 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mpi.h>
#include "../IOcodes/read_ini.h"
#include "configuration.h"
#include "../malleability/spawn_methods/ProcessDist.h"
#include "../malleability/distribution_methods/block_distribution.h"

void malloc_config_resizes(configuration *user_config);
11
void malloc_config_stages(configuration *user_config);
12

13
14
void free_config_stage(iter_stage_t *stage, int *freed_ids, size_t *found_ids);

15
16
17
void def_struct_config_file(configuration *config_file);
void def_struct_groups(configuration *config_file);
void def_struct_iter_stage(configuration *config_file);
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

/*
 * Inicializa una estructura de configuracion
 *
 * Si el parametro "file_name" no es nulo,
 * se obtiene la configuracion a partir de 
 * un fichero .ini
 *
 * En caso de que sea nulo, es el usuario
 * el que tiene que elegir los valores a
 * utilizar.
 */
void init_config(char *file_name, configuration **user_config) {
  if(file_name != NULL) {
    ext_functions_t mallocs;
    mallocs.resizes_f = malloc_config_resizes;
34
    mallocs.stages_f = malloc_config_stages;
35
36
37
38
39
    *user_config = read_ini_file(file_name, mallocs);
  } else {
    configuration *config = NULL;

    config = malloc(sizeof(configuration));
40
41
    config->n_resizes=0;
    config->n_groups=1;
42
43
44
    malloc_config_resizes(config);
    config->n_stages=1;
    malloc_config_stages(config);
45
46
47
48
49
50
51
    if(config == NULL) {
        perror("Error when reserving configuration structure\n");
	MPI_Abort(MPI_COMM_WORLD, -3);
	return;
    }
    *user_config=config;
  }
52
  def_struct_config_file(*user_config);
53
54
55
56
57
58
59
60
61
62
}

/*
 * Reserva de memoria para los vectores de la estructura de configuracion
 *
 * 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
63
 *  - init_config
64
65
66
 *  - recv_config_file
 */
void malloc_config_resizes(configuration *user_config) {
67
  size_t i;
68
  if(user_config != NULL) {
69
70
    user_config->groups = malloc(sizeof(group_config_t) * user_config->n_groups);
    for(i=0; i<user_config->n_groups; i++) {
71
72
73
74
75
      user_config->groups[i].iters = 0;
      user_config->groups[i].procs = 1;
      user_config->groups[i].sm = 0;
      user_config->groups[i].ss = 1;
      user_config->groups[i].phy_dist = 0;
76
77
      user_config->groups[i].rm = 0;
      user_config->groups[i].rs = 1;
78
79
      user_config->groups[i].factor = 1;
    }
80
    def_struct_groups(user_config);
81
82
83
84
85
86
87
88
89
  }
}

/*
 * 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
90
 *  - init_config
91
92
 *  - recv_config_file
 */
93
void malloc_config_stages(configuration *user_config) {
94
  size_t i;
95
  if(user_config != NULL) {
96
    user_config->stages = malloc(sizeof(iter_stage_t) * user_config->n_stages);
97
98
99
100
    for(i=0; i<user_config->n_stages; i++) {
      user_config->stages[i].array = NULL;
      user_config->stages[i].full_array = NULL;
      user_config->stages[i].double_array = NULL;
101
      user_config->stages[i].reqs = NULL;
102
      user_config->stages[i].counts.counts = NULL;
103
104
      user_config->stages[i].bytes = 0;
      user_config->stages[i].my_bytes = 0;
105
      user_config->stages[i].real_bytes = 0;
106
107
      user_config->stages[i].operations = 0;
      user_config->stages[i].pt = 0;
108
      user_config->stages[i].id = -1;
109
110
      user_config->stages[i].t_op = 0;
      user_config->stages[i].t_stage = 0;
111
      user_config->stages[i].t_capped = 0;
112
    }
113
    def_struct_iter_stage(user_config);
114
  }
115
116
117
118
119
120
121
}


/*
 * Libera toda la memoria de una estructura de configuracion
 */
void free_config(configuration *user_config) {
122
123
124
    size_t i, found_ids;
    int *freed_ids;
    found_ids = 0;
125
    if(user_config != NULL) {
126
      freed_ids = (int *) malloc(user_config->n_stages * sizeof(int));
127
      for(i=0; i < user_config->n_stages; i++) {
128
        free_config_stage(&(user_config->stages[i]), freed_ids, &found_ids);
129
      }
130
      //Liberar tipos derivados
131
132
133
134
135
136
137
138
      MPI_Type_free(&(user_config->config_type));
      user_config->config_type = MPI_DATATYPE_NULL;

      MPI_Type_free(&(user_config->group_type));
      user_config->group_type = MPI_DATATYPE_NULL;

      MPI_Type_free(&(user_config->iter_stage_type));
      user_config->iter_stage_type = MPI_DATATYPE_NULL;
139
      
140
      free(user_config->groups);
141
      free(user_config->stages);
142
      free(user_config);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
      free(freed_ids);
    }
}

/*
 * Libera toda la memoria de una stage
 */
void free_config_stage(iter_stage_t *stage, int *freed_ids, size_t *found_ids) {
  size_t i;
  int free_reqs;

  free_reqs = 1;
  if(stage->id > -1) {
    for(i=0; i<*found_ids; i++) {
      if(stage->id == freed_ids[i]) {
  	free_reqs = 0;
        break;
      }
    }
    if(free_reqs) {
      freed_ids[*found_ids] = stage->id;
      *found_ids=*found_ids + 1;
    }
  }
	
  if(stage->array != NULL) {
    free(stage->array);
    stage->array = NULL;
  }
  if(stage->full_array != NULL) {
    free(stage->full_array);
    stage->full_array = NULL;
  }
  if(stage->double_array != NULL) {
    free(stage->double_array);
    stage->double_array = NULL;
  }
  if(stage->reqs != NULL && free_reqs) {
    for(i=0; i<stage->req_count; i++) {
      if(stage->reqs[i] != MPI_REQUEST_NULL) {
        MPI_Request_free(&(stage->reqs[i]));
	stage->reqs[i] = MPI_REQUEST_NULL;
      }
186
    }
187
188
189
190
191
192
    free(stage->reqs);
    stage->reqs = NULL;
  }
  if(stage->counts.counts != NULL) {
    freeCounts(&(stage->counts));
  }
193
194
195
196
197
198
199
}


/*
 * Imprime por salida estandar toda la informacion que contiene
 * la configuracion pasada como argumento
 */
200
void print_config(configuration *user_config) {
201
  if(user_config != NULL) {
202
    size_t i;
203
204
    printf("Config loaded: R=%zu, S=%zu, granularity=%d, SDR=%zu, ADR=%zu, Rigid=%d, Capture_Method=%d\n",
        user_config->n_resizes, user_config->n_stages, user_config->granularity, user_config->sdr, user_config->adr, user_config->rigid_times, user_config->capture_method);
205
    for(i=0; i<user_config->n_stages; i++) {
206
207
      printf("Stage %zu: PT=%d, T_stage=%lf, bytes=%d, T_capped=%d\n",
        i, user_config->stages[i].pt, user_config->stages[i].t_stage, user_config->stages[i].real_bytes, user_config->stages[i].t_capped);
208
    }
209
    for(i=0; i<user_config->n_groups; i++) {
210
      printf("Group %zu: Iters=%d, Procs=%d, Factors=%f, Dist=%d, RM=%d, RS=%d, SM=%d, SS=%d\n",
211
        i, user_config->groups[i].iters, user_config->groups[i].procs, user_config->groups[i].factor, 
212
213
	user_config->groups[i].phy_dist, user_config->groups[i].rm, user_config->groups[i].rs,
	user_config->groups[i].sm, user_config->groups[i].ss);
214
215
216
217
218
219
220
221
222
    }
  }
}


/*
 * Imprime por salida estandar la informacion relacionada con un
 * solo grupo de procesos en su configuracion.
 */
223
224
void print_config_group(configuration *user_config, size_t grp) {
  size_t i;
225
226
227
228
  if(user_config != NULL) {
    int parents, sons;
    parents = sons = 0;
    if(grp > 0) {
229
      parents = user_config->groups[grp-1].procs;
230
    }
231
    if(grp < user_config->n_groups - 1) {
232
      sons = user_config->groups[grp+1].procs;
233
234
    }

235
236
    printf("Config: granularity=%d, SDR=%zu, ADR=%zu, Rigid=%d, Capture_Method=%d\n",
        user_config->granularity, user_config->sdr, user_config->adr, user_config->rigid_times, user_config->capture_method);
237
    for(i=0; i<user_config->n_stages; i++) {
238
239
      printf("Stage %zu: PT=%d, T_stage=%lf, bytes=%d, T_capped=%d\n",
        i, user_config->stages[i].pt, user_config->stages[i].t_stage, user_config->stages[i].real_bytes, user_config->stages[i].t_capped);
240
    }
241
    printf("Group %zu: Iters=%d, Procs=%d, Factors=%f, Dist=%d, RM=%d, RS=%d, SM=%d, SS=%d, parents=%d, children=%d\n",
242
      grp, user_config->groups[grp].iters, user_config->groups[grp].procs, user_config->groups[grp].factor,
243
244
      user_config->groups[grp].phy_dist, user_config->groups[grp].rm, user_config->groups[grp].rs,
      user_config->groups[grp].sm, user_config->groups[grp].ss, parents, sons);
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  }
}


//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//| FUNCIONES DE INTERCOMUNICACION DE ESTRUCTURA DE CONFIGURACION ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| |/

/*
 * 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.
 */
void send_config_file(configuration *config_file, int root, MPI_Comm intercomm) {
264
265
266
  MPI_Bcast(config_file, 1, config_file->config_type, root, intercomm);
  MPI_Bcast(config_file->groups, config_file->n_groups, config_file->group_type, root, intercomm);
  MPI_Bcast(config_file->stages, config_file->n_stages, config_file->iter_stage_type, root, intercomm);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
}



/*
 * 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".
 */
void recv_config_file(int root, MPI_Comm intercomm, configuration **config_file_out) {
283
284
  configuration *config_file = malloc(sizeof(configuration));
  def_struct_config_file(config_file);
285

286
  MPI_Bcast(config_file, 1, config_file->config_type, root, intercomm);
287
288

  //Inicializado de estructuras internas
289
  config_file->n_resizes = config_file->n_groups-1;
290
291
  malloc_config_resizes(config_file); // Inicializar valores de grupos
  malloc_config_stages(config_file); // Inicializar a NULL vectores stage
292

293
294
  MPI_Bcast(config_file->groups, config_file->n_groups, config_file->group_type, root, intercomm);
  MPI_Bcast(config_file->stages, config_file->n_stages, config_file->iter_stage_type, root, intercomm);
295
296
297
298
299
300

  *config_file_out = config_file;
}


/*
301
 * Tipo derivado para enviar 7 elementos especificos
302
303
 * de la estructura de configuracion con una sola comunicacion.
 */
304
void def_struct_config_file(configuration *config_file) {
305
306
  int i, counts = 7;
  int blocklengths[7] = {1, 1, 1, 1, 1, 1, 1};
307
308
309
310
  MPI_Aint displs[counts], dir;
  MPI_Datatype types[counts];

  // Rellenar vector types
311
  types[0] = types[1] = types[2] = types[3] = MPI_UNSIGNED_LONG;
312
  types[4] = types[5] = types[6] = MPI_INT;
313
314
315
316

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

317
  MPI_Get_address(&(config_file->n_groups), &displs[0]);
318
  MPI_Get_address(&(config_file->n_stages), &displs[1]);
319
320
321
  MPI_Get_address(&(config_file->sdr), &displs[2]);
  MPI_Get_address(&(config_file->adr), &displs[3]);
  MPI_Get_address(&(config_file->granularity), &displs[4]);
322
  MPI_Get_address(&(config_file->rigid_times), &displs[5]);
323
  MPI_Get_address(&(config_file->capture_method), &displs[6]);
324
325
326

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

327
328
  MPI_Type_create_struct(counts, blocklengths, displs, types, &(config_file->config_type));
  MPI_Type_commit(&(config_file->config_type));
329
330
331
}

/*
332
333
334
 * Tipo derivado para enviar elementos especificos
 * de la estructuras de la configuracion de cada grupo 
 * en una sola comunicacion.
335
 */
336
void def_struct_groups(configuration *config_file) {
337
338
  int i, counts = 8;
  int blocklengths[8] = {1, 1, 1, 1, 1, 1, 1, 1};
339
340
  MPI_Aint displs[counts], dir;
  MPI_Datatype aux, types[counts];
341
  group_config_t *groups = config_file->groups;
342
343

  // Rellenar vector types
344
345
  types[0] = types[1] = types[2] = types[3] = types[4] = types[5] = types[6] = MPI_INT;
  types[7] = MPI_FLOAT;
346

347
348
  // Rellenar vector displs
  MPI_Get_address(groups, &dir);
349

350
351
352
353
354
  MPI_Get_address(&(groups->iters), &displs[0]);
  MPI_Get_address(&(groups->procs), &displs[1]);
  MPI_Get_address(&(groups->sm), &displs[2]);
  MPI_Get_address(&(groups->ss), &displs[3]);
  MPI_Get_address(&(groups->phy_dist), &displs[4]);
355
356
357
  MPI_Get_address(&(groups->rm), &displs[5]);
  MPI_Get_address(&(groups->rs), &displs[6]);
  MPI_Get_address(&(groups->factor), &displs[7]);
358
359
360

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

361
362
363
  if (config_file->n_groups == 1) {
    MPI_Type_create_struct(counts, blocklengths, displs, types, &(config_file->group_type));
    MPI_Type_commit(&(config_file->group_type));
364
365
366
  } 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
367
368
    MPI_Type_create_resized(aux, 0, sizeof(group_config_t), &(config_file->group_type));
    MPI_Type_commit(&(config_file->group_type));
369
    MPI_Type_free(&aux);
370
  }
371
372
373
374
375
376
}

/*
 * Tipo derivado para enviar elementos especificos
 * de la estructuras de fases de iteracion en una sola comunicacion.
 */
377
void def_struct_iter_stage(configuration *config_file) {
378
379
  int i, counts = 6;
  int blocklengths[6] = {1, 1, 1, 1, 1, 1};
380
381
  MPI_Aint displs[counts], dir;
  MPI_Datatype aux, types[counts];
382
  iter_stage_t *stages = config_file->stages;
383
384

  // Rellenar vector types
385
386
  types[0] = types[1] = types[2] = types[3] = MPI_INT;
  types[4] = types[5] = MPI_DOUBLE;
387
388
389
390
391

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

  MPI_Get_address(&(stages->pt), &displs[0]);
392
393
394
395
396
  MPI_Get_address(&(stages->id), &displs[1]);
  MPI_Get_address(&(stages->bytes), &displs[2]);
  MPI_Get_address(&(stages->t_capped), &displs[3]);
  MPI_Get_address(&(stages->t_stage), &displs[4]);
  MPI_Get_address(&(stages->t_op), &displs[5]);
397
398
399

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

400
401
402
  if (config_file->n_stages == 1) {
    MPI_Type_create_struct(counts, blocklengths, displs, types, &(config_file->iter_stage_type));
    MPI_Type_commit(&(config_file->iter_stage_type));
403
404
405
  } 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
406
407
    MPI_Type_create_resized(aux, 0, sizeof(iter_stage_t), &(config_file->iter_stage_type)); 
    MPI_Type_commit(&(config_file->iter_stage_type));
408
    MPI_Type_free(&aux);
409
410
  }
}