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

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

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

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

/*
 * 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;
33
    mallocs.stages_f = malloc_config_stages;
34
35
36
37
38
    *user_config = read_ini_file(file_name, mallocs);
  } else {
    configuration *config = NULL;

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

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

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


/*
 * Libera toda la memoria de una estructura de configuracion
 */
void free_config(configuration *user_config) {
121
122
123
    size_t i, found_ids;
    int *freed_ids;
    found_ids = 0;
124
    if(user_config != NULL) {
125
      freed_ids = (int *) malloc(user_config->n_stages * sizeof(int));
126
      for(i=0; i < user_config->n_stages; i++) {
127
        free_config_stage(&(user_config->stages[i]), freed_ids, &found_ids);
128
      }
129
      //Liberar tipos derivados
130
131
132
133
134
135
136
137
      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;
138
      
139
      free(user_config->groups);
140
      free(user_config->stages);
141
      free(user_config);
142
143
144
145
146
147
148
149
150
      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;
151
  int mpi_index, free_reqs;
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

  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) {
180
181
182
183
    for(mpi_index=0; mpi_index<stage->req_count; mpi_index++) {
      if(stage->reqs[mpi_index] != MPI_REQUEST_NULL) {
        MPI_Request_free(&(stage->reqs[mpi_index]));
	stage->reqs[mpi_index] = MPI_REQUEST_NULL;
184
      }
185
    }
186
187
188
189
190
191
    free(stage->reqs);
    stage->reqs = NULL;
  }
  if(stage->counts.counts != NULL) {
    freeCounts(&(stage->counts));
  }
192
193
194
195
196
197
198
}


/*
 * Imprime por salida estandar toda la informacion que contiene
 * la configuracion pasada como argumento
 */
199
void print_config(configuration *user_config) {
200
  if(user_config != NULL) {
201
    size_t i;
202
203
    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);
204
    for(i=0; i<user_config->n_stages; i++) {
205
206
      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);
207
    }
208
    for(i=0; i<user_config->n_groups; i++) {
209
      printf("Group %zu: Iters=%d, Procs=%d, Factors=%f, Dist=%d, RM=%d, RS=%d, SM=%d, SS=%d\n",
210
        i, user_config->groups[i].iters, user_config->groups[i].procs, user_config->groups[i].factor, 
211
212
	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);
213
214
215
216
217
218
219
220
221
    }
  }
}


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

234
235
    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);
236
    for(i=0; i<user_config->n_stages; i++) {
237
238
      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);
239
    }
240
    printf("Group %zu: Iters=%d, Procs=%d, Factors=%f, Dist=%d, RM=%d, RS=%d, SM=%d, SS=%d, parents=%d, children=%d\n",
241
      grp, user_config->groups[grp].iters, user_config->groups[grp].procs, user_config->groups[grp].factor,
242
243
      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);
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  }
}


//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ||
//| 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) {
263
264
265
  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);
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
}



/*
 * 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) {
282
283
  configuration *config_file = malloc(sizeof(configuration));
  def_struct_config_file(config_file);
284

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

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

292
293
  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);
294
295
296
297
298
299

  *config_file_out = config_file;
}


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

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

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

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

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

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

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

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

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

349
350
351
352
353
  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]);
354
355
356
  MPI_Get_address(&(groups->rm), &displs[5]);
  MPI_Get_address(&(groups->rs), &displs[6]);
  MPI_Get_address(&(groups->factor), &displs[7]);
357
358
359

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

360
361
362
  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));
363
364
365
  } 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
366
367
    MPI_Type_create_resized(aux, 0, sizeof(group_config_t), &(config_file->group_type));
    MPI_Type_commit(&(config_file->group_type));
368
    MPI_Type_free(&aux);
369
  }
370
371
372
373
374
375
}

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

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

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

  MPI_Get_address(&(stages->pt), &displs[0]);
391
392
393
394
395
  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]);
396
397
398

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

399
400
401
  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));
402
403
404
  } 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
405
406
    MPI_Type_create_resized(aux, 0, sizeof(iter_stage_t), &(config_file->iter_stage_type)); 
    MPI_Type_commit(&(config_file->iter_stage_type));
407
    MPI_Type_free(&aux);
408
409
  }
}