results.c 10.8 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
9
10
11
12
void compute_max(results_data *results, double *computed_array, int myId, int root, MPI_Comm comm);
void compute_mean(results_data *results, double *computed_array, int myId, int numP, int root, MPI_Comm comm);
void compute_median(results_data *results, double *computed_array, size_t *used_ids, int myId, int numP, int root, MPI_Comm comm);
void match_median(results_data *results, double *computed_array, size_t *used_ids, int myId, int numP, int root, MPI_Comm comm);

13
14
15
16
17
18
//======================================================||
//======================================================||
//================SET RESULTS FUNCTIONS=================||
//======================================================||
//======================================================||

19
20
21
22
23
24
25
26
27
28
29
/*
 * 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;
30
  results->iters_async = 0;
31
32
}

33
34
35
36
37
38
39
/*
 * 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.
 */
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
void compute_results_iter(results_data *results, int myId, int numP, int root, size_t stages, int capture_method, MPI_Comm comm) {
  size_t i, *used_ids;
  switch(capture_method) {
    case RESULTS_MAX:
      compute_max(results, results->iters_time, myId, root, comm);
      for(i=0; i<stages; i++) {
        compute_max(results, results->stage_times[i], myId, root, comm);
      }
      break;
    case RESULTS_MEAN:
      compute_mean(results, results->iters_time, myId, numP, root, comm);
      for(i=0; i<stages; i++) {
        compute_mean(results, results->stage_times[i], myId, numP, root, comm);
      }
      break;
    case RESULTS_MEDIAN:
      used_ids = malloc(results->iter_index * sizeof(size_t));
      compute_median(results, results->iters_time, used_ids, myId, numP, root, comm);
      for(i=0; i<stages; i++) {
        //compute_median(results, results->stage_times[i], myId, numP, root, comm);
        match_median(results, results->stage_times[i], used_ids, myId, numP, root, comm);
      }
      free(used_ids);
      break;
  }
}

void compute_max(results_data *results, double *computed_array, int myId, int root, MPI_Comm comm) {
68
  if(myId == root) {
69
70
71
72
73
74
75
76
77
    MPI_Reduce(MPI_IN_PLACE, computed_array, results->iter_index, MPI_DOUBLE, MPI_MAX, root, comm);
  } else {
    MPI_Reduce(computed_array, NULL, results->iter_index, MPI_DOUBLE, MPI_MAX, root, comm);
  }
}

void compute_mean(results_data *results, double *computed_array, int myId, int numP, int root, MPI_Comm comm) {
  if(myId == root) {
    MPI_Reduce(MPI_IN_PLACE, computed_array, results->iter_index, MPI_DOUBLE, MPI_SUM, root, comm);
78
    for(size_t i=0; i<results->iter_index; i++) {
79
80
      computed_array[i] = results->iters_time[i] / numP;
    }
81
  } else {
82
    MPI_Reduce(computed_array, NULL, results->iter_index, MPI_DOUBLE, MPI_SUM, root, comm);
83
  }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}



struct TimeWithIndex {
    double time;
    size_t index;
};

int compare(const void *a, const void *b) {
  return ((struct TimeWithIndex *)a)->time - ((struct TimeWithIndex *)b)->time;
}

/*
 * Calcula la mediana de un vector de tiempos replicado entre "numP" procesos.
 * Se calcula la mediana para cada elemento del vector final y se devuelve este.
 *
 * Además se devuelve en el vector "used_ids" de que proceso se ha obtenido la mediana de cada elemento.
 */
void compute_median(results_data *results, double *computed_array, size_t *used_ids, int myId, int numP, int root, MPI_Comm comm) {
  double *aux_all_iters, median;
  struct TimeWithIndex *aux_id_iters;
106
107
  if(myId == root) {
    aux_all_iters = malloc(numP *results->iter_index * sizeof(double));
108
    aux_id_iters = malloc(numP * sizeof(struct TimeWithIndex));
109
  }
110
  MPI_Gather(computed_array, results->iter_index, MPI_DOUBLE, aux_all_iters, results->iter_index, MPI_DOUBLE, root, comm);
111
112
113
  if(myId == root) {
    for(size_t i=0; i<results->iter_index; i++) {
      for(int j=0; j<numP; j++) {
114
115
        aux_id_iters[j].time = aux_all_iters[i+(results->iter_index*j)];
        aux_id_iters[j].index = (size_t) j;
116
117
      }
      // Get Median
118
119
120
121
122
      qsort(aux_id_iters, numP, sizeof(struct TimeWithIndex), &compare);
      median = aux_id_iters[numP/2].time;
      if (numP % 2 == 0) median = (aux_id_iters[numP/2 - 1].time + aux_id_iters[numP/2].time) / 2;
      computed_array[i] = median;
      used_ids[i] = aux_id_iters[numP/2].index; //FIXME What should be the index when numP is even?
123
124
125
    }
    free(aux_all_iters);
    free(aux_id_iters);
126
  }
127
128
}

129
/*
130
131
132
 * Obtiene las medianas de un vector de tiempos replicado entre "numP" procesos.
 * La mediana de cada elemento se obtiene consultando el vector "used_ids", que contiene
 * que proceso tiene la mediana.
133
 *
134
 * Como resultado devuelve un vector con la mediana calculada.
135
 */
136
137
138
void match_median(results_data *results, double *computed_array, size_t *used_ids, int myId, int numP, int root, MPI_Comm comm) {
  double *aux_all_iters;
  size_t matched_id;
139
  if(myId == root) {
140
    aux_all_iters = malloc(numP * results->iter_index * sizeof(double));
141
  }
142
143
144
145
146
  MPI_Gather(computed_array, results->iter_index, MPI_DOUBLE, aux_all_iters, results->iter_index, MPI_DOUBLE, root, comm);
  if(myId == root) {
    for(size_t i=0; i<results->iter_index; i++) {
      matched_id = used_ids[i];
      computed_array[i] = aux_all_iters[i+(results->iter_index*matched_id)];
147
    }
148
    free(aux_all_iters);
149
150
151
  }
}

152
153
154
155
156
//======================================================||
//======================================================||
//===============PRINT RESULTS FUNCTIONS================||
//======================================================||
//======================================================||
157
158
159
160

/*
 * Imprime por pantalla los resultados locales.
 * Estos son los relacionados con las iteraciones, que son el tiempo
161
 * por iteracion, el tipo (Normal o durante communicacion asincrona).
162
 */
163
void print_iter_results(results_data results) {
164
  size_t i;
165

166
  printf("Async_Iters: %ld\n", results.iters_async);
167
  printf("T_iter: ");
168
169
  for(i=0; i< results.iter_index; i++) {
    printf("%lf ", results.iters_time[i]);
170
  }
171
  printf("\n");
172
173
174
175
176
}

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

180
181
182
  for(i=0; i < n_stages; i++) {
    printf("T_stage %ld: ", i);
    for(j=0; j < results.iter_index; j++) {
183
184
185
186
      printf("%lf ", results.stage_times[i][j]);
    }
    printf("\n");
  }
187
188
}

189
190
191
192
193
/*
 * 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.
 */
194
195
void print_global_results(results_data results, size_t resizes) {
  size_t i;
196

197
  printf("T_spawn: ");
198
  for(i=0; i < resizes; i++) {
199
    printf("%lf ", results.spawn_time[i]);
200
201
  }

202
  printf("\nT_SR: ");
203
  for(i=0; i < resizes; i++) {
204
    printf("%lf ", results.sync_time[i]);
205
206
  }

207
  printf("\nT_AR: ");
208
  for(i=0; i < resizes; i++) {
209
    printf("%lf ", results.async_time[i]);
210
211
  }

212
213
214
215
216
  printf("\nT_Malleability: ");
  for(i=0; i < resizes; i++) {
    printf("%lf ", results.malleability_time[i]);
  }

217
  printf("\nT_total: %lf\n", results.exec_time);
218
219
}

220
221
222
223
224
225
//======================================================||
//======================================================||
//=============INIT/FREE RESULTS FUNCTIONS==============||
//======================================================||
//======================================================||

226
227
228
229
230
231
/*
 * 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.
 */
232
233
void init_results_data(results_data *results, size_t resizes, size_t stages, size_t iters_size) {
  size_t i;
234
235
236
237

  results->spawn_time = calloc(resizes, sizeof(double));
  results->sync_time = calloc(resizes, sizeof(double));
  results->async_time = calloc(resizes, sizeof(double));
238
  results->malleability_time = calloc(resizes, sizeof(double));
239
  results->wasted_time = 0;
240

241
242
243
  results->iters_size = iters_size + RESULTS_EXTRA_SIZE;
  results->iters_time = calloc(results->iters_size, sizeof(double));
  results->stage_times = malloc(stages * sizeof(double*));
244
  for(i=0; i<stages; i++) {
245
    results->stage_times[i] = calloc(results->iters_size, sizeof(double));
246
247
  }

248
  results->iters_async = 0;
249
  results->iter_index = 0;
250

251
252
}

253
254
void realloc_results_iters(results_data *results, size_t stages, size_t needed) {
  int error = 0;
255
  double *time_aux;
256
  size_t i;
257
258
259

  if(results->iters_size >= needed) return;

260
  time_aux = (double *) realloc(results->iters_time, needed * sizeof(double));
261
  if(time_aux == NULL) error = 1;
262

263
  for(i=0; i<stages; i++) { //TODO Comprobar que no da error el realloc
264
265
    results->stage_times[i] = (double *) realloc(results->stage_times[i], needed * sizeof(double));
    if(results->stage_times[i] == NULL) error = 1;
266
267
  }

268
  if(error) {
269
    fprintf(stderr, "Fatal error - No se ha podido realojar la memoria de resultados\n");
270
271
272
273
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  results->iters_time = time_aux;
274
  results->iters_size = needed;
275
276
}

277
278
279
/*
 * Libera toda la memoria asociada con una estructura de resultados.
 */
280
281
282
void free_results_data(results_data *results, size_t stages) {
  size_t i;
  if(results != NULL) {
283
284
285
286
287
288
289
290
291
292
293
294
    if(results->spawn_time != NULL) {
      free(results->spawn_time);
      results->spawn_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;
    }
295
296
297
298
    if(results->malleability_time != NULL) {
      free(results->malleability_time);
      results->malleability_time = NULL;
    }
299

300
301
302
303
    if(results->iters_time != NULL) {
      free(results->iters_time);
      results->iters_time = NULL;
    }
304
    for(i=0; i<stages; i++) {
305
306
307
308
309
310
311
312
      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;
313
    }
314
  }
315
}