results.c 11.2 KB
Newer Older
1
2
3
4
5
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "results.h"

6
7
#define RESULTS_EXTRA_SIZE 100

8
void def_results_type(results_data *results, int resizes, MPI_Datatype *results_type);
9
10
11
12
13
14

//======================================================||
//======================================================||
//================MPI RESULTS FUNCTIONS=================||
//======================================================||
//======================================================||
15
16

/*
17
18
 * Comunica una estructura de resultados a todos los procesos del comunicador
 * a traves de un tipo derivado.
19
 *
20
21
22
23
 * Si se llama con un intercommunicador, el grupo de procesos que envia los datos
 * tiene que indicar en el proceso raiz el valor "MPI_ROOT" para "root" y el resto
 * de ese grupo el valor "MPI_PROC_NULL". Los procesos del otro grupo tienen que
 * indicar el Id del proceso raiz que ha puesto "MPI_ROOT".
24
 */
25
void comm_results(results_data *results, int root, size_t resizes, MPI_Comm intercomm) {
26
27
28
29
  MPI_Datatype results_type;

  // Obtener un tipo derivado para enviar todos los
  // datos escalares con una sola comunicacion
30
  def_results_type(results, resizes, &results_type);
31
32
33
34
35
36
  MPI_Bcast(results, 1, results_type, root, intercomm);

  //Liberar tipos derivados
  MPI_Type_free(&results_type);
}

37
38
39
40
/*
 * Define un tipo derivado de MPI para mandar los tiempos
 * con una sola comunicacion.
 *
41
 * En concreto son tres escalares y dos vectores de tamaño "resizes"
42
43
 */
void def_results_type(results_data *results, int resizes, MPI_Datatype *results_type) {
44
45
  int i, counts = 6;
  int blocklengths[] = {1, 1, 1, 1, 1, 1};
46
47
48
49
  MPI_Aint displs[counts], dir;
  MPI_Datatype types[counts];

  // Rellenar vector types
50
  types[0] = types[1] = types[2] = types[3] = types[4] = types[5] = MPI_DOUBLE;
51
  blocklengths[2] = blocklengths[3] = blocklengths[4] = blocklengths[5] = resizes;
52
53
54
55

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

56
57
58
59
60
61
  MPI_Get_address(&(results->exec_start), &displs[0]);
  MPI_Get_address(&(results->wasted_time), &displs[1]);
  MPI_Get_address(results->sync_time, &displs[2]);
  MPI_Get_address(results->async_time, &displs[3]);
  MPI_Get_address(results->spawn_real_time, &displs[4]);
  MPI_Get_address(results->spawn_time, &displs[5]);
62
63
64
65
66
67

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

  MPI_Type_create_struct(counts, blocklengths, displs, types, results_type);
  MPI_Type_commit(results_type);
}
68
69
70
71
72
73
74
75
76
77
78
79
80
//======================================================||
//======================================================||
//================SET RESULTS FUNCTIONS=================||
//======================================================||
//======================================================||

/*
 * Guarda los resultados respecto a la redistribución de datos
 * tras una reconfiguración. A llamar por los hijos tras
 * terminar la redistribución y obtener la configuración.
 */
void set_results_post_reconfig(results_data *results, int grp, int sdr, int adr) {
  if(sdr) { // Si no hay datos sincronos, el tiempo es 0
81
    results->sync_time[grp-1]  = results->sync_end - results->sync_time[grp-1];
82
  } else {
83
    results->sync_time[grp-1]  = 0;
84
85
  }
  if(adr) { // Si no hay datos asincronos, el tiempo es 0
86
    results->async_time[grp-1]  = results->async_end - results->async_time[grp-1];
87
  } else {
88
    results->async_time[grp-1]  = 0;
89
90
91
  }
}

92
93
94
95
96
97
98
99
100
101
102
/*
 * Pone el indice del siguiente elemento a escribir a 0 para los vectores
 * que tengan que ver con las iteraciones.
 * Por tanto, todos los anteriores valores de esos vectores pasan a ser invalidos
 * si se intentan acceder desde un código externo.
 *
 * Solo es necesario llamar a esta funcion cuando se ha realizado una
 * expansion con el metodo MERGE
 */
void reset_results_index(results_data *results) {
  results->iter_index = 0;
103
  results->iters_async = 0;
104
105
}

106
107
108
109
110
111
112
//=============================================================== FIXME BORRAR?
int compare(const void *_a, const void *_b) { 
        double *a, *b;
        a = (double *) _a;
        b = (double *) _b;
        return (*a - *b);
}
113
114
115
116
117
118
119
/*
 * Obtiene para cada iteracion, el tiempo maximo entre todos los procesos
 * que han participado.
 *
 * Es necesario obtener el maximo, pues es el que representa el tiempo real
 * que se ha utilizado.
 */
120
121
void compute_results_iter(results_data *results, int myId, int numP, int root, MPI_Comm comm) { //TODO Probar a quedarse la MEDIA en vez de MAX?
  if(myId == root) {
122
123
    MPI_Reduce(MPI_IN_PLACE, results->iters_time, results->iter_index, MPI_DOUBLE, MPI_MAX, root, comm);
    /*
124
125
    for(size_t i=0; i<results->iter_index; i++) {
      results->iters_time[i] = results->iters_time[i] / numP;
126
    }*/
127
  } else {
128
    MPI_Reduce(results->iters_time, NULL, results->iter_index, MPI_DOUBLE, MPI_MAX, root, comm);
129
  }
130
  /*
131
132
133
134
135
136
137
138
139
140
141
142
  double *aux_all_iters, *aux_id_iters, median;
  if(myId == root) {
    aux_all_iters = malloc(numP *results->iter_index * sizeof(double));
  }
  MPI_Gather(results->iters_time, results->iter_index, MPI_DOUBLE, aux_all_iters, results->iter_index, MPI_DOUBLE, root, comm);
  if(myId == root) {
    aux_id_iters = malloc(numP * sizeof(double));
    for(size_t i=0; i<results->iter_index; i++) {
      for(int j=0; j<numP; j++) {
        aux_id_iters[j] = aux_all_iters[i+(results->iter_index*j)];
      }
      // Get Median
143
      qsort(aux_id_iters, numP, sizeof(double), &compare);
144
145
146
147
148
149
      median = aux_id_iters[numP/2];
      if (numP % 2 == 0) median = (aux_id_iters[numP/2 - 1] + aux_id_iters[numP/2]) / 2;
      results->iters_time[i] = median;
    }
    free(aux_all_iters);
    free(aux_id_iters);
150
  }
151
  */
152
153
}

154
155
156
157
158
159
160
161

/*
 * Obtiene para cada stage de cada iteracion, el tiempo maximo entre todos los procesos
 * que han participado.
 *
 * Es necesario obtener el maximo, pues es el que representa el tiempo real
 * que se ha utilizado.
 */
162
void compute_results_stages(results_data *results, int myId, int numP, int root, int stages, MPI_Comm comm) { //TODO Probar a quedarse la MEDIA en vez de MAX?
163
164
165
  int i;
  if(myId == root) {
    for(i=0; i<stages; i++) {
166
167
168
169
      MPI_Reduce(MPI_IN_PLACE, results->stage_times[i], results->iter_index, MPI_DOUBLE, MPI_SUM, root, comm);
      for(size_t j=0; j<results->iter_index; j++) {
        results->stage_times[i][j] = results->stage_times[i][j] / numP;
      }
170
171
172
173
    }
  }
  else {
    for(i=0; i<stages; i++) {
174
      MPI_Reduce(results->stage_times[i], NULL, results->iter_index, MPI_DOUBLE, MPI_SUM, root, comm);
175
176
177
178
    }
  }
}

179
180
181
182
183
//======================================================||
//======================================================||
//===============PRINT RESULTS FUNCTIONS================||
//======================================================||
//======================================================||
184
185
186
187

/*
 * Imprime por pantalla los resultados locales.
 * Estos son los relacionados con las iteraciones, que son el tiempo
188
 * por iteracion, el tipo (Normal o durante communicacion asincrona).
189
 */
190
void print_iter_results(results_data results) {
191
  size_t i;
192

193
  printf("T_iter: ");
194
195
  for(i=0; i< results.iter_index; i++) {
    printf("%lf ", results.iters_time[i]);
196
197
  }

198
  printf("\nAsync_Iters: %ld\n", results.iters_async);
199
200
201
202
203
}

/*
 * Imprime por pantalla los resultados locales de un stage.
 */
204
205
void print_stage_results(results_data results, size_t n_stages) {
  size_t i, j;
206

207
208
209
  for(i=0; i < n_stages; i++) {
    printf("T_stage %ld: ", i);
    for(j=0; j < results.iter_index; j++) {
210
211
212
213
      printf("%lf ", results.stage_times[i][j]);
    }
    printf("\n");
  }
214
215
}

216
217
218
219
220
/*
 * Imprime por pantalla los resultados globales.
 * Estos son el tiempo de creacion de procesos, los de comunicacion
 * asincrona y sincrona y el tiempo total de ejecucion.
 */
221
222
void print_global_results(results_data results, size_t resizes) {
  size_t i;
223

224
  printf("T_spawn: ");
225
  for(i=0; i < resizes; i++) {
226
    printf("%lf ", results.spawn_time[i]);
227
228
  }

229
  printf("\nT_spawn_real: ");
230
  for(i=0; i< resizes; i++) {
231
    printf("%lf ", results.spawn_real_time[i]);
232
233
  }

234
  printf("\nT_SR: ");
235
  for(i=0; i < resizes; i++) {
236
    printf("%lf ", results.sync_time[i]);
237
238
  }

239
  printf("\nT_AR: ");
240
  for(i=0; i < resizes; i++) {
241
    printf("%lf ", results.async_time[i]);
242
243
  }

244
  printf("\nT_total: %lf\n", results.exec_time);
245
246
}

247
248
249
250
251
252
//======================================================||
//======================================================||
//=============INIT/FREE RESULTS FUNCTIONS==============||
//======================================================||
//======================================================||

253
254
255
256
257
258
/*
 * Inicializa los datos relacionados con una estructura de resultados.
 *
 * Los argumentos "resizes" y "iters_size" se necesitan para obtener el tamaño
 * de los vectores de resultados.
 */
259
260
void init_results_data(results_data *results, size_t resizes, size_t stages, size_t iters_size) {
  size_t i;
261
262

  results->spawn_time = calloc(resizes, sizeof(double));
263
  results->spawn_real_time = calloc(resizes, sizeof(double));
264
265
  results->sync_time = calloc(resizes, sizeof(double));
  results->async_time = calloc(resizes, sizeof(double));
266
  results->wasted_time = 0;
267

268
269
270
  results->iters_size = iters_size + RESULTS_EXTRA_SIZE;
  results->iters_time = calloc(results->iters_size, sizeof(double));
  results->stage_times = malloc(stages * sizeof(double*));
271
  for(i=0; i<stages; i++) {
272
    results->stage_times[i] = calloc(results->iters_size, sizeof(double));
273
274
  }

275
  results->iters_async = 0;
276
  results->iter_index = 0;
277

278
279
}

280
281
void realloc_results_iters(results_data *results, size_t stages, size_t needed) {
  int error = 0;
282
  double *time_aux;
283
  size_t i;
284
285
  time_aux = (double *) realloc(results->iters_time, needed * sizeof(double));

286
  for(i=0; i<stages; i++) { //TODO Comprobar que no da error el realloc
287
288
    results->stage_times[i] = (double *) realloc(results->stage_times[i], needed * sizeof(double));
    if(results->stage_times[i] == NULL) error = 1;
289
290
  }

291
292
  if(time_aux == NULL) error = 1;
  if(error) {
293
    fprintf(stderr, "Fatal error - No se ha podido realojar la memoria de resultados\n");
294
295
296
297
298
299
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  results->iters_time = time_aux;
}

300
301
302
/*
 * Libera toda la memoria asociada con una estructura de resultados.
 */
303
304
305
void free_results_data(results_data *results, size_t stages) {
  size_t i;
  if(results != NULL) {
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    if(results->spawn_time != NULL) {
      free(results->spawn_time);
      results->spawn_time = NULL;
    }
    if(results->spawn_real_time != NULL) {
      free(results->spawn_real_time);
      results->spawn_real_time = NULL;
    }
    if(results->sync_time != NULL) {
      free(results->sync_time);
      results->sync_time = NULL;
    }
    if(results->async_time != NULL) {
      free(results->async_time);
      results->async_time = NULL;
    }
322

323
324
325
326
    if(results->iters_time != NULL) {
      free(results->iters_time);
      results->iters_time = NULL;
    }
327
    for(i=0; i<stages; i++) {
328
329
330
331
332
333
334
335
      if(results->stage_times[i] != NULL) {
        free(results->stage_times[i]);
        results->stage_times[i] = NULL;
      }
    }
    if(results->stage_times != NULL) {
      free(results->stage_times);
      results->stage_times = NULL;
336
    }
337
  }
338
}