Main.c 21 KB
Newer Older
1
2
3
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
iker_martin's avatar
iker_martin committed
4
5
#include <fcntl.h>
#include <sys/stat.h>
6
#include <pthread.h>
7
#include "computing_func.h"
8
#include "../IOcodes/read_ini.h"
9
#include "../IOcodes/results.h"
iker_martin's avatar
iker_martin committed
10
11
#include "../malleability/ProcessDist.h"
#include "../malleability/CommDist.h"
12
13
14

#define ROOT 0

iker_martin's avatar
iker_martin committed
15
16
17
int work();
void Sons_init();

18
int checkpoint(int iter, int state, MPI_Request **comm_req);
19
int TC(int numS, int comm_type);
20
21
int start_redistribution(int numS, MPI_Request **comm_req);
int check_redistribution(int iter, MPI_Request **comm_req);
22
23
24
int end_redistribution(int iter);

int thread_creation();
25
int thread_check(int iter);
26
void* thread_async_work(void* void_arg);
iker_martin's avatar
iker_martin committed
27

28
void iterate(double *matrix, int n, int async_comm);
29

30
void init_group_struct(char *argv[], int argc, int myId, int numP);
31
void init_application();
32
void obtain_op_times();
33
34
void free_application_data();

35
void print_general_info(int myId, int grp, int numP);
36
int print_final_results();
iker_martin's avatar
iker_martin committed
37
int create_out_file(char *nombre, int *ptr, int newstdout);
38

iker_martin's avatar
iker_martin committed
39
40
41
42
typedef struct {
  int myId;
  int numP;
  int grp;
43
  int iter_start;
44
  int argc;
iker_martin's avatar
iker_martin committed
45

46
47
  int numS; // Cantidad de procesos hijos
  int commAsync;
iker_martin's avatar
iker_martin committed
48
  MPI_Comm children, parents;
49
50

  char *compute_comm_array;
iker_martin's avatar
iker_martin committed
51
  char **argv;
52
  char *sync_array, *async_array;
iker_martin's avatar
iker_martin committed
53
54
} group_data;

55
56
57
58
59
60
typedef struct {
  int myId, numP, numS, adr;
  MPI_Comm children;
  char *sync_array;
} thread_data;

iker_martin's avatar
iker_martin committed
61
62
configuration *config_file;
group_data *group;
63
results_data *results;
64
int run_id = 0; // Utilizado para diferenciar más fácilmente ejecuciones en el análisis
65

66
67
pthread_t async_thread; // TODO Cambiar de sitio?

68
int main(int argc, char *argv[]) {
69
    int numP, myId, res;
iker_martin's avatar
iker_martin committed
70
    int req;
71

72
    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &req);
73
    MPI_Comm_size(MPI_COMM_WORLD, &numP);
iker_martin's avatar
iker_martin committed
74
75
    MPI_Comm_rank(MPI_COMM_WORLD, &myId);

76
77
78
79
    if(req != MPI_THREAD_MULTIPLE) {
      printf("No se ha obtenido la configuración de hilos necesaria\nSolicitada %d -- Devuelta %d\n", req, MPI_THREAD_MULTIPLE);
    }

80
    init_group_struct(argv, argc, myId, numP);
81
82
83
84
85
86
87

    MPI_Comm_get_parent(&(group->parents));
    if(group->parents == MPI_COMM_NULL ) { // Si son el primer grupo de procesos, recogen la configuracion inicial
      init_application();
    } else { // Si son procesos hijos deben comunicarse con las padres
      Sons_init();
    }
iker_martin's avatar
iker_martin committed
88

89
90
91
    if(group->grp == 0) {
      MPI_Barrier(MPI_COMM_WORLD);
      results->exec_start = MPI_Wtime();
92
    }
93

94
    res = work();
95

96
97
98
    if(res) { // Se he llegado al final de la aplicacion
      MPI_Barrier(MPI_COMM_WORLD);
      results->exec_time = MPI_Wtime() - results->exec_start;
99
    }
100
101
102
    print_final_results();

    free_application_data();
103
104
105
106
107
    MPI_Finalize();
    return 0;
}

/*
108
109
110
111
112
113
114
115
116
 * Función de trabajo principal.
 *
 * Incializa los datos para realizar el computo y a continuacion
 * pasa a realizar "maxiter" iteraciones de computo.
 *
 * Terminadas las iteraciones realiza el redimensionado de procesos.
 * Si el redimensionado se realiza de forma asincrona se 
 * siguen realizando iteraciones de computo hasta que termine la 
 * comunicacion asincrona y realizar entonces la sincrona.
117
118
119
120
 *
 * Si el grupo de procesos es el ultimo que va a ejecutar, se devuelve
 * el valor 1 para indicar que no se va a seguir trabajando con nuevos grupos
 * de procesos. En caso contrario se devuelve 0.
121
 */
iker_martin's avatar
iker_martin committed
122
int work() {
123
  int iter, maxiter, state, res;
124
  double *matrix;
125
  MPI_Request *async_comm;
126

iker_martin's avatar
iker_martin committed
127
  maxiter = config_file->iters[group->grp];
128
  //initMatrix(&matrix, config_file->matrix_tam);
129
  state = MAL_COMM_UNINITIALIZED;
iker_martin's avatar
iker_martin committed
130

131
  res = 0;
132
  for(iter=group->iter_start; iter < maxiter; iter++) {
133
    iterate(matrix, config_file->matrix_tam, state);
134
  }
135
  state = checkpoint(iter, state, &async_comm);
136
  
137
  iter = 0;
138
  while(state == MAL_ASYNC_PENDING || state == COMM_IN_PROGRESS) {
139
    iterate(matrix, config_file->matrix_tam, state);
140
    iter++;
141
    state = checkpoint(iter, state, &async_comm);
142
  }
143
  
144
145
  if(config_file->resizes - 1 == group->grp) res=1;
  return res;
146
147
}

148
149
150
151
152
153
154
155
/*
 * Se realiza el redimensionado de procesos por parte de los padres.
 *
 * Se crean los nuevos procesos con la distribucion fisica elegida y
 * a continuacion se transmite la informacion a los mismos.
 *
 * Si hay datos asincronos a transmitir, primero se comienza a
 * transmitir estos y se termina la funcion. Se tiene que comprobar con
156
 * llamando a la función de nuevo que se han terminado de enviar
157
158
159
160
161
162
 *
 * Si hay ademas datos sincronos a enviar, no se envian aun.
 *
 * Si solo hay datos sincronos se envian tras la creacion de los procesos
 * y finalmente se desconectan los dos grupos de procesos.
 */
163
164
165
166
167
168
int checkpoint(int iter, int state, MPI_Request **comm_req) {
  
  if(state == MAL_COMM_UNINITIALIZED) {
    // Comprobar si se tiene que realizar un redimensionado
    if(config_file->iters[group->grp] > iter || config_file->resizes == group->grp + 1) {return MAL_COMM_UNINITIALIZED;}

169
    group->numS = config_file->procs[group->grp +1];
170
    int comm_type = COMM_SPAWN_PTHREAD; // TODO Pasar a CONFIG
171

172
173
174
175
176
177
178
179
    state = TC(group->numS, comm_type);

    if (state == COMM_FINISHED){
      state = start_redistribution(group->numS, comm_req);
    }

  } else if(state == COMM_IN_PROGRESS) { // Comprueba si el spawn ha terminado y comienza la redistribucion
    state = check_slurm_comm(group->myId, ROOT, &(group->children));
180

181
182
183
184
    if (state == COMM_FINISHED) {  
        results->spawn_time[group->grp] = MPI_Wtime() - results->spawn_start;
      state = start_redistribution(group->numS, comm_req);
    }
185

186
  } else if(state == MAL_ASYNC_PENDING) {
187
188
189
190
191
    if(config_file->aib == MAL_USE_THREAD) {
      state = thread_check(iter);
    } else {
      state = check_redistribution(iter, comm_req);
    }
192
  }
iker_martin's avatar
iker_martin committed
193

194
195
  return state;
}
iker_martin's avatar
iker_martin committed
196

197
198
/*
 * Se encarga de realizar la creacion de los procesos hijos.
199
 * Si se pide en segundo plano devuelve el estado actual.
200
 */
201
int TC(int numS, int comm_type){
202
203
  // Inicialización de la comunicación con SLURM
  int dist = config_file->phy_dist[group->grp +1];
204
  int comm_state;
iker_martin's avatar
iker_martin committed
205

206
207
208
209
210
      results->spawn_start = MPI_Wtime();
  comm_state = init_slurm_comm(group->argv, group->myId, numS, ROOT, dist, comm_type, MPI_COMM_WORLD, &(group->children));
  if(comm_type == COMM_SPAWN_SERIAL)
      results->spawn_time[group->grp] = MPI_Wtime() - results->spawn_start;
  return comm_state;
211
}
iker_martin's avatar
iker_martin committed
212

213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Comienza la redistribucion de los datos con el nuevo grupo de procesos.
 *
 * Primero se envia la configuracion a utilizar al nuevo grupo de procesos y a continuacion
 * se realiza el envio asincrono y/o sincrono si lo hay.
 *
 * En caso de que haya comunicacion asincrona, se comienza y se termina la funcion 
 * indicando que se ha comenzado un envio asincrono.
 *
 * Si no hay comunicacion asincrono se pasa a realizar la sincrona si la hubiese.
 *
 * Finalmente se envian datos sobre los resultados a los hijos y se desconectan ambos
 * grupos de procesos.
 */
227
int start_redistribution(int numS, MPI_Request **comm_req) {
iker_martin's avatar
iker_martin committed
228
229
230
231
232
  int rootBcast = MPI_PROC_NULL;
  if(group->myId == ROOT) rootBcast = MPI_ROOT;

  // Enviar a los hijos que grupo de procesos son
  MPI_Bcast(&(group->grp), 1, MPI_INT, rootBcast, group->children);
233
  MPI_Bcast(&run_id, 1, MPI_INT, rootBcast, group->children);
iker_martin's avatar
iker_martin committed
234
235
  send_config_file(config_file, rootBcast, group->children);

236
  if(config_file->adr > 0) {
237
    results->async_start = MPI_Wtime();
238
239
240
241
242
243
    if(config_file->aib == MAL_USE_THREAD) {
      return thread_creation();
    } else {
      send_async(group->async_array, config_file->adr, group->myId, group->numP, ROOT, group->children, group->numS, comm_req, config_file->aib);
      return MAL_ASYNC_PENDING;
    }
244
  } 
245
246
247
248
249
250
251
252
253
254
255
  return end_redistribution(0);
}

/*
 * Crea una hebra para ejecutar una comunicación en segundo plano.
 */
int thread_creation() {
  if(pthread_create(&async_thread, NULL, thread_async_work, NULL)) {
    printf("Error al crear el hilo\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
    return -1;
iker_martin's avatar
iker_martin committed
256
  }
257
258
  return MAL_ASYNC_PENDING;
}
259

260
261
262
263
264
265
/*
 * Comprobación por parte de una hebra maestra que indica
 * si una hebra esclava ha terminado su comunicación en segundo plano.
 *
 * El estado de la comunicación es devuelto al finalizar la función. 
 */
266
int thread_check(int iter) {
267
  int all_completed = 0;
iker_martin's avatar
iker_martin committed
268

269
270
271
272
273
274
275
276
277
278
  // Comprueba que todos los hilos han terminado la distribucion (Mismo valor en commAsync)
  MPI_Allreduce(&group->commAsync, &all_completed, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
  if(all_completed != MAL_COMM_COMPLETED) return MAL_ASYNC_PENDING; // Continue only if asynchronous send has ended 

  if(pthread_join(async_thread, NULL)) {
    printf("Error al esperar al hilo\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
    return -2;
  } 
  return end_redistribution(iter);
279
280
281
282
283
284
285
286
287
288
289
290
291
292
}

/*
 * Función ejecutada por una hebra.
 * Ejecuta una comunicación síncrona con los hijos que
 * para el usuario se puede considerar como en segundo plano.
 *
 * Cuando termina la comunicación la hebra maestra puede comprobarlo
 * por el valor "commAsync".
 */
void* thread_async_work(void* void_arg) {
  send_sync(group->async_array, config_file->adr, group->myId, group->numP, ROOT, group->children, group->numS);
  group->commAsync = MAL_COMM_COMPLETED;
  pthread_exit(NULL);
iker_martin's avatar
iker_martin committed
293
294
}

295
/*
296
 * @deprecated
297
298
299
300
301
302
303
 * Comprueba si la redistribucion asincrona ha terminado. 
 * Si no ha terminado la funcion termina indicandolo, en caso contrario,
 * se continua con la comunicacion sincrona, el envio de resultados y
 * se desconectan los grupos de procesos.
 *
 * Esta funcion permite dos modos de funcionamiento al comprobar si la
 * comunicacion asincrona ha terminado.
304
305
 * Si se utiliza el modo "MAL_USE_NORMAL" o "MAL_USE_POINT", se considera 
 * terminada cuando los padres terminan de enviar.
306
307
308
 * Si se utiliza el modo "MAL_USE_IBARRIER", se considera terminada cuando
 * los hijos han terminado de recibir.
 */
309
int check_redistribution(int iter, MPI_Request **comm_req) {
310
  int completed, all_completed, test_err;
311
  MPI_Request *req_completed;
iker_martin's avatar
iker_martin committed
312

313
314
315
316
317
318
319
320
321
322
  if (config_file->aib == MAL_USE_POINT) {
    test_err = MPI_Testall(group->numS, *comm_req, &completed, MPI_STATUSES_IGNORE);
  } else {
    if(config_file->aib == MAL_USE_NORMAL) {
      req_completed = &(*comm_req)[0];
    } else if (config_file->aib == MAL_USE_IBARRIER) {
      req_completed = &(*comm_req)[1];
    }
    test_err = MPI_Test(req_completed, &completed, MPI_STATUS_IGNORE);
  }
323
 
324
  if (test_err != MPI_SUCCESS && test_err != MPI_ERR_PENDING) {
325
    printf("P%d aborting -- Test Async\n", group->myId);
326
327
328
329
    MPI_Abort(MPI_COMM_WORLD, test_err);
  }

  MPI_Allreduce(&completed, &all_completed, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
330
  if(!all_completed) return MAL_ASYNC_PENDING; // Continue only if asynchronous send has ended 
331
  
332
333
334

  if(config_file->aib == MAL_USE_IBARRIER) {
    MPI_Wait(&(*comm_req)[0], MPI_STATUS_IGNORE); // Indicar como completado el envio asincrono
335
    //Para la desconexión de ambos grupos de procesos es necesario indicar a MPI que esta comm
336
    //ha terminado, aunque solo se pueda llegar a este punto cuando ha terminado
337
  }
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  free(*comm_req);
  return end_redistribution(iter);
}


/*
 * Termina la redistribución de los datos con los hijos, comprobando
 * si se han realizado iteraciones con comunicaciones en segundo plano
 * y enviando cuantas iteraciones se han realizado a los hijos.
 *
 * Además se realizan las comunicaciones síncronas se las hay.
 * Finalmente termina enviando los datos temporales a los hijos.
 */ 
int end_redistribution(int iter) {
  int rootBcast = MPI_PROC_NULL;
  if(group->myId == ROOT) rootBcast = MPI_ROOT;

  if(iter > 0) { // Mandar a los hijos iteracion en la que comenzar
    MPI_Bcast(&iter, 1, MPI_INT, rootBcast, group->children);
  }
358
  if(config_file->sdr > 0) { // Realizar envio sincrono
359
      results->sync_start = MPI_Wtime();
360
    send_sync(group->sync_array, config_file->sdr, group->myId, group->numP, ROOT, group->children, group->numS);
361
362
  }

363
  send_results(results, rootBcast, config_file->resizes, group->children);
364
365
366
  // Desconectar intercomunicador con los hijos
  MPI_Comm_disconnect(&(group->children));
  return MAL_COMM_COMPLETED;
iker_martin's avatar
iker_martin committed
367
368
}

369
370
371
372
373
374
/*
 * Inicializacion de los datos de los hijos.
 * En la misma se reciben datos de los padres: La configuracion
 * de la ejecucion a realizar; y los datos a recibir de los padres
 * ya sea de forma sincrona, asincrona o ambas.
 */
iker_martin's avatar
iker_martin committed
375
376
377
378
void Sons_init() {

  // Enviar a los hijos que grupo de procesos son
  MPI_Bcast(&(group->grp), 1, MPI_INT, ROOT, group->parents);
379
  MPI_Bcast(&run_id, 1, MPI_INT, ROOT, group->parents);
iker_martin's avatar
iker_martin committed
380
381
382
383
  group->grp++;

  config_file = recv_config_file(ROOT, group->parents);
  int numP_parents = config_file->procs[group->grp -1];
384
  init_results_data(&results, config_file->resizes - 1, config_file->iters[group->grp]);
iker_martin's avatar
iker_martin committed
385

386
387
388
389
  if(config_file->comm_tam) {
    group->compute_comm_array = malloc(config_file->comm_tam * sizeof(char));
  }
  if(config_file->adr) { // Recibir datos asincronos
390
391
392
393
394
395
    if(config_file->aib == MAL_USE_NORMAL || config_file->aib == MAL_USE_IBARRIER || config_file->aib == MAL_USE_POINT) {
      recv_async(&(group->async_array), config_file->adr, group->myId, group->numP, ROOT, group->parents, numP_parents, config_file->aib);
    } else if (config_file->aib == MAL_USE_THREAD) {
      recv_sync(&(group->async_array), config_file->adr, group->myId, group->numP, ROOT, group->parents, numP_parents);
    }

396
      results->async_time[group->grp] = MPI_Wtime();
397
398
    MPI_Bcast(&(group->iter_start), 1, MPI_INT, ROOT, group->parents);
  }
399
  if(config_file->sdr) { // Recibir datos sincronos
iker_martin's avatar
iker_martin committed
400
    recv_sync(&(group->sync_array), config_file->sdr, group->myId, group->numP, ROOT, group->parents, numP_parents);
401
    results->sync_time[group->grp] = MPI_Wtime();
iker_martin's avatar
iker_martin committed
402
  }
403
404
405

  // Guardar los resultados de esta transmision
  recv_results(results, ROOT, config_file->resizes, group->parents);
406
  if(config_file->sdr) { // Si no hay datos sincronos, el tiempo es 0
407
408
409
410
    results->sync_time[group->grp]  = MPI_Wtime() - results->sync_start;
  } else {
    results->sync_time[group->grp]  = 0;
  }
411
  if(config_file->adr) { // Si no hay datos asincronos, el tiempo es 0
412
413
414
415
    results->async_time[group->grp]  = MPI_Wtime() - results->async_start;
  } else {
    results->async_time[group->grp]  = 0;
  }
iker_martin's avatar
iker_martin committed
416
417
418
419
420
421
422
423
424
425
426
427
428

  // Desconectar intercomunicador con los hijos
  MPI_Comm_disconnect(&(group->parents));
}


/////////////////////////////////////////
/////////////////////////////////////////
//COMPUTE FUNCTIONS
/////////////////////////////////////////
/////////////////////////////////////////


429
430
/*
 * Simula la ejecucción de una iteración de computo en la aplicación
431
 * que dura al menos un tiempo de "time" segundos.
432
 */
433
void iterate(double *matrix, int n, int async_comm) {
434
  double start_time, actual_time;
iker_martin's avatar
iker_martin committed
435
  double time = config_file->general_time * config_file->factors[group->grp];
436
  double Top = config_file->Top;
437
  int i, operations = 0;
438
  double aux = 0;
439
440

  start_time = actual_time = MPI_Wtime();
441
442
443
444
445

  operations = time / Top;
  for(i=0; i < operations; i++) {
    aux += computePiSerial(n);
  }
446
447
448
449
450

  if(config_file->comm_tam) {
    MPI_Bcast(group->compute_comm_array, config_file->comm_tam, MPI_CHAR, ROOT, MPI_COMM_WORLD);
  }

451
452
453
454
455
  actual_time = MPI_Wtime(); // Guardar tiempos
  if(async_comm == MAL_ASYNC_PENDING) { // Se esta realizando una redistribucion de datos asincrona
    operations=0;
  }

456
457
458
459

  if(results->iter_index == results->iters_size) { // Aumentar tamaño de ambos vectores de resultados
    realloc_results_iters(results, results->iters_size + 100);
  }
460
461
462
  results->iters_time[results->iter_index] = actual_time - start_time;
  results->iters_type[results->iter_index] = operations;
  results->iter_index = results->iter_index + 1;
463

464
}
465

466
467
//======================================================||
//======================================================||
468
//=============INIT/FREE/PRINT FUNCTIONS================||
469
470
471
//======================================================||
//======================================================||

472
473
474
475
/*
 * Muestra datos generales sobre los procesos, su grupo,
 * en que nodo residen y la version de MPI utilizada.
 */
476
477
478
479
480
481
482
483
484
485
486
487
void print_general_info(int myId, int grp, int numP) {
  int len;
  char *name = malloc(MPI_MAX_PROCESSOR_NAME * sizeof(char));
  char *version = malloc(MPI_MAX_LIBRARY_VERSION_STRING * sizeof(char));
  MPI_Get_processor_name(name, &len);
  MPI_Get_library_version(version, &len);
  printf("P%d Nuevo GRUPO %d de %d procs en nodo %s con %s\n", myId, grp, numP, name, version);

  free(name);
  free(version);
}

488
489
490
491
492
493
/*
 * Pide al proceso raiz imprimir los datos sobre las iteraciones realizadas por el grupo de procesos.
 *
 * Si es el ultimo grupo de procesos, muestra los datos obtenidos de tiempo de ejecucion, creacion de procesos
 * y las comunicaciones.
 */
494
int print_final_results() {
iker_martin's avatar
iker_martin committed
495
496
497
  int ptr_local, ptr_global, err;
  char *file_name;

498
  if(group->myId == ROOT) {
iker_martin's avatar
iker_martin committed
499
500
    file_name = NULL;
    file_name = malloc(40 * sizeof(char));
501
    if(file_name == NULL) return -1; // No ha sido posible alojar la memoria
502
    err = snprintf(file_name, 40, "R%d_G%dNP%dID%d.out", run_id, group->grp, group->numP, group->myId);
503
    if(err < 0) return -2; // No ha sido posible obtener el nombre de fichero
iker_martin's avatar
iker_martin committed
504
505
    create_out_file(file_name, &ptr_local, 1);
  
506
507
    print_config_group(config_file, group->grp);
    print_iter_results(results, config_file->iters[group->grp] -1);
iker_martin's avatar
iker_martin committed
508
    free(file_name);
509
510

    if(group->grp == config_file->resizes -1) {
iker_martin's avatar
iker_martin committed
511
512
      file_name = NULL;
      file_name = malloc(20 * sizeof(char));
513
      if(file_name == NULL) return -1; // No ha sido posible alojar la memoria
514
      err = snprintf(file_name, 20, "R%d_Global.out", run_id);
515
      if(err < 0) return -2; // No ha sido posible obtener el nombre de fichero
iker_martin's avatar
iker_martin committed
516
517

      create_out_file(file_name, &ptr_global, 1);
518
519
      print_config(config_file, group->grp);
      print_global_results(results, config_file->resizes);
iker_martin's avatar
iker_martin committed
520
521
      free(file_name);
      
522
523
    }
  }
524
  return 0;
525
526
527
528
529
}

/*
 * Inicializa la estructura group
 */
530
void init_group_struct(char *argv[], int argc, int myId, int numP) {
531
532
533
534
535
  group = malloc(1 * sizeof(group_data));
  group->myId        = myId;
  group->numP        = numP;
  group->grp         = 0;
  group->iter_start  = 0;
536
  group->commAsync   = MAL_COMM_UNINITIALIZED;
537
  group->argc        = argc;
538
539
  group->argv        = argv;
}
540

541
542
543
544
545
546
547
548
549
550
/*
 * Inicializa los datos para este grupo de procesos.
 *
 * En caso de ser el primer grupo de procesos, lee el fichero de configuracion
 * e inicializa los vectores de comunicacion.
 *
 * En caso de ser otro grupo de procesos entra a la funcion "Sons_init()" donde
 * se comunican con los padres para inicializar sus datos.
 */
void init_application() {
551
552
553
554
555
556
557
  if(group->argc < 2) {
    printf("Falta el fichero de configuracion. Uso:\n./programa config.ini id\nEl argumento numerico id es opcional\n");
    exit(0);
  }
  if(group->argc > 2) {
    run_id = atoi(group->argv[2]);
  }
558

559
560
  config_file = read_ini_file(group->argv[1]);
  init_results_data(&results, config_file->resizes, config_file->iters[group->grp]);
561
562
563
564
  if(config_file->comm_tam) {
    group->compute_comm_array = malloc(config_file->comm_tam * sizeof(char));
  }
  if(config_file->sdr) {
565
566
    malloc_comm_array(&(group->sync_array), config_file->sdr , group->myId, group->numP);
  }
567
  if(config_file->adr) {
568
569
    malloc_comm_array(&(group->async_array), config_file->adr , group->myId, group->numP);
  }
570
571
572
   
  obtain_op_times();
}
573

574
575
576
577
/*
 * Obtiene cuanto tiempo es necesario para realizar una operacion de PI
 */
void obtain_op_times() {
578
  double result, start_time = MPI_Wtime();
579
  int i, qty = 20000;
580
  result = 0;
581
  for(i=0; i<qty; i++) {
582
    result += computePiSerial(config_file->matrix_tam);
583
  }
584
585
  //printf("Creado Top con valor %lf\n", result);
  //fflush(stdout);
586

587
  config_file->Top = (MPI_Wtime() - start_time) / qty; //Tiempo de una operacion
588
  MPI_Bcast(&(config_file->Top), 1, MPI_DOUBLE, ROOT, MPI_COMM_WORLD); 
589
590
591
592
593
594
}

/*
 * Libera toda la memoria asociada con la aplicacion
 */
void free_application_data() {
595
596
597
598
  if(config_file->comm_tam) {
    free(group->compute_comm_array);
  }
  if(config_file->sdr) {
599
600
    free(group->sync_array);
  }
601
  if(config_file->adr) {
602
603
    free(group->async_array);
  }
604
  
605
606
607
608
  free(group);
  free_config(config_file);
  free_results_data(&results);
}
iker_martin's avatar
iker_martin committed
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633


/* 
 * Función para crear un fichero con el nombre pasado como argumento.
 * Si el nombre ya existe, se escribe la informacion a continuacion.
 *
 * El proceso que llama a la función pasa a tener como salida estandar
 * dicho fichero si el valor "newstdout" es verdadero.
 *
 */
int create_out_file(char *nombre, int *ptr, int newstdout) {
  int err;

  *ptr = open(nombre, O_WRONLY | O_CREAT | O_APPEND, 0644);
  if(*ptr < 0) return -1; // No ha sido posible crear el fichero

  if(newstdout) {
    err = close(1);
    if(err < 0) return -2; // No es posible modificar la salida estandar
    err = dup(*ptr);
    if(err < 0) return -3; // No es posible modificar la salida estandar
  }

  return 0;
}