GenericSpawn.c 13.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <mpi.h>
#include <string.h>
#include "../malleabilityStates.h"
#include "ProcessDist.h"
#include "GenericSpawn.h"
#include "Baseline.h"
#include "Merge.h"
13
#include "Spawn_state.h"
14
15
16
17

// This code is a Singleton object -- Only one instance can be used at a given time and
// no multiple calls to perform diferent resizes can be performed at the same time.

18
Spawn_data *spawn_data = NULL;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pthread_t spawn_thread;
MPI_Comm *returned_comm;

double end_time; //FIXME REFACTOR

//--------------PRIVATE CONFIGURATION DECLARATIONS---------------//
void set_spawn_configuration(char *cmd, int num_cpus, int num_nodes, char *nodelist, int myId, int root, int initial_qty, int target_qty, int type_dist, int spawn_method, int spawn_strategies, MPI_Comm comm);
void set_basic_spawn_dtype();
void deallocate_spawn_data();

//--------------PRIVATE DECLARATIONS---------------//
void generic_spawn(MPI_Comm *child, int data_stage);

int check_single_state(MPI_Comm comm, int global_state);
int check_generic_state(MPI_Comm comm, MPI_Comm *child, int local_state, double *real_time);

//--------------PRIVATE THREADS DECLARATIONS---------------//
int allocate_thread_spawn();
37
void* thread_work();
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62


//--------------PUBLIC FUNCTIONS---------------//

/*
 * Se solicita la creacion de un nuevo grupo de "numP" procesos con una distribucion
 * fisica "type_dist".
 *
 * Se puede solicitar en primer plano, encargandose por tanto el proceso que llama a esta funcion,
 * o en segundo plano, donde un hilo se encarga de configurar esta creacion.
 *
 * Si se pide en primer plano, al terminarla es posible llamar a "check_spawn_state()" para crear
 * los procesos.
 *
 * Si se pide en segundo plano, llamar a "check_spawn_state()" comprobara si la configuracion para
 * crearlos esta lista, y si es asi, los crea.
 *
 * Devuelve el estado de el procedimiento. Si no devuelve "MALL_SPAWN_COMPLETED", es necesario llamar a
 * "check_spawn_state()".
 */
int init_spawn(char *argv, int num_cpus, int num_nodes, char *nodelist, int myId, int initial_qty, int target_qty, int root, int type_dist, int spawn_method, int spawn_strategies, MPI_Comm comm, MPI_Comm *child) {
  int local_state;
  set_spawn_configuration(argv, num_cpus, num_nodes, nodelist, myId, root, initial_qty, target_qty, type_dist, spawn_method, spawn_strategies, comm);

  if(!spawn_data->spawn_is_async) {
63
    generic_spawn(child, MALL_NOT_STARTED);
64
65
66
67
68
    local_state = get_spawn_state(spawn_data->spawn_is_async);
    if (local_state == MALL_SPAWN_COMPLETED)
      deallocate_spawn_data();

  } else {
69
70
71
72
    local_state = spawn_data->spawn_is_single ? 
	    MALL_SPAWN_SINGLE_PENDING : MALL_SPAWN_PENDING;
    local_state = spawn_data->spawn_method == MALL_SPAWN_MERGE && spawn_data->initial_qty > spawn_data->target_qty ?
	    MALL_SPAWN_ADAPT_POSTPONE : local_state;
73
74
75
76
77
78
    set_spawn_state(local_state, 0);
    if((spawn_data->spawn_is_single && myId == root) || !spawn_data->spawn_is_single) {
      allocate_thread_spawn();
    }
  }
    
79
  return local_state;
80
81
82
83
84
85
}

/*
 * Comprueba si una configuracion para crear un nuevo grupo de procesos esta lista,
 * y en caso de que lo este, se devuelve el communicador a estos nuevos procesos.
 */
86
int check_spawn_state(MPI_Comm *child, MPI_Comm comm, double *real_time) { 
87
88
89
  int local_state;
  int global_state=MALL_NOT_STARTED;

90
  if(spawn_data->spawn_is_async) { // Async
91
92
    local_state = get_spawn_state(spawn_data->spawn_is_async);

93
    if(local_state == MALL_SPAWN_SINGLE_PENDING || local_state == MALL_SPAWN_SINGLE_COMPLETED) { // Single
94
95
      global_state = check_single_state(comm, local_state);

96
    } else if(local_state == MALL_SPAWN_PENDING || local_state == MALL_SPAWN_COMPLETED || local_state == MALL_SPAWN_ADAPTED) { // Baseline
97
98
      global_state = check_generic_state(comm, child, local_state, real_time);

99
100
101
    } else if(local_state == MALL_SPAWN_ADAPT_POSTPONE) {
      global_state = local_state;
      
102
    } else {
103
      printf("Error Check spawn: Configuracion invalida State = %d\n", local_state);
104
105
106
      MPI_Abort(MPI_COMM_WORLD, -1);
      return -10;
    }
107
  } else if(spawn_data->spawn_method == MALL_SPAWN_MERGE){ // Start Merge shrink Sync
108
109
110
    generic_spawn(child, MALL_DIST_COMPLETED);
    global_state = get_spawn_state(spawn_data->spawn_is_async);
  }
111
  if(global_state == MALL_SPAWN_COMPLETED || global_state == MALL_SPAWN_ADAPTED)
112
    deallocate_spawn_data();
113

114
115
116
  return global_state;
}

117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Elimina la bandera bloqueante MALL_SPAWN_ADAPT_POSTPONE para los hilos 
 * auxiliares. Esta bandera los bloquea para que el metodo Merge shrink no 
 * avance hasta que se complete la redistribucion de datos. Por tanto,
 * al modificar la bandera los hilos pueden continuar.
 *
 * Por seguridad se comprueba que no se realice el cambio a la bandera a 
 * no ser que se cumplan las 3 condiciones.
 */
void unset_spawn_postpone_flag(int outside_state) {
  int local_state = get_spawn_state(spawn_data->spawn_is_async);
  if(local_state == MALL_SPAWN_ADAPT_POSTPONE && outside_state == MALL_SPAWN_ADAPT_PENDING && spawn_data->spawn_is_async) { 
    set_spawn_state(MALL_SPAWN_PENDING, MALL_SPAWN_PTHREAD);
130
    wakeup();
131
132
133
  }
}

134
135
136
137
138
139
140
141
/*
 * Funcion bloqueante de los hijos para asegurar que todas las tareas del paso
 * de creacion de los hijos se terminan correctamente.
 *
 * Ademas los hijos obtienen informacion basica de los padres
 * para el paso de redistribucion de datos (Numeros de procesos y Id del Root).
 *
 */
142
void malleability_connect_children(int myId, int numP, int root, MPI_Comm comm, int *numP_parents, int *root_parents, MPI_Comm *parents) {
143
144
145
146
147
148
149
150
151
152
153
154
  spawn_data = (Spawn_data *) malloc(sizeof(Spawn_data));
  spawn_data->root = root;
  spawn_data->myId = myId;
  spawn_data->spawn_qty = numP;
  spawn_data->target_qty = numP;
  spawn_data->comm = comm;

  set_basic_spawn_dtype();
  MPI_Bcast(spawn_data, 1, spawn_data->dtype, MALLEABILITY_ROOT, *parents);

  switch(spawn_data->spawn_method) {
    case MALL_SPAWN_BASELINE:
155
      baseline(*spawn_data, parents);
156
157
      break;
    case MALL_SPAWN_MERGE:
158
      spawn_data->target_qty += spawn_data->initial_qty;
159
      merge(*spawn_data, parents, MALL_NOT_STARTED);
160
161
162
      break;
  }

163
164
165
  *root_parents = spawn_data->root_parents;
  *numP_parents = spawn_data->initial_qty;

166
167
168
169
  MPI_Type_free(&(spawn_data->dtype));
  free(spawn_data);
}

170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Función para obtener si entre las estrategias elegidas, se utiliza
 * la estrategia pasada como segundo argumento.
 *
 * Devuelve en "result" 1(Verdadero) si utiliza la estrategia, 0(Falso) en caso
 * contrario.
 */
int malleability_spawn_contains_strat(int spawn_strategies, int strategy, int *result) {
  int value = spawn_strategies % strategy ? 0 : 1;
  if(result != NULL) *result = value;
  return value;
}

183
184
185
186
187
188
189
190
191
192
193
194
195
196
//--------------PRIVATE CONFIGURATION FUNCTIONS---------------//
/*
 * Agrupa en una sola estructura todos los datos de configuración necesarios
 * e inicializa las estructuras necesarias.
 */
void set_spawn_configuration(char *cmd, int num_cpus, int num_nodes, char *nodelist, int myId, int root, int initial_qty, int target_qty, int type_dist, int spawn_method, int spawn_strategies, MPI_Comm comm) {
  spawn_data = (Spawn_data *) malloc(sizeof(Spawn_data));

  spawn_data->myId = myId;
  spawn_data->root = root;
  spawn_data->root_parents = root;
  spawn_data->initial_qty = initial_qty;
  spawn_data->target_qty = target_qty;
  spawn_data->spawn_method = spawn_method;
197
198
  malleability_spawn_contains_strat(spawn_strategies, MALL_SPAWN_SINGLE, &(spawn_data->spawn_is_single));
  malleability_spawn_contains_strat(spawn_strategies, MALL_SPAWN_PTHREAD, &(spawn_data->spawn_is_async));
199
200
201
202
203
204
205
206
  spawn_data->comm = comm;

  set_basic_spawn_dtype();

  switch(spawn_data->spawn_method) {
    case MALL_SPAWN_BASELINE:
      spawn_data->spawn_qty = spawn_data->target_qty;
      spawn_data->already_created = 0;
207
      break;
208
209
210
    case MALL_SPAWN_MERGE:
      spawn_data->spawn_qty = spawn_data->target_qty - spawn_data->initial_qty;
      spawn_data->already_created = spawn_data->initial_qty;
211
      break;
212
213
214
  }

  if(spawn_data->spawn_is_async) {
215
    init_spawn_state();
216
217
  }

218
  spawn_data->mapping = MPI_INFO_NULL;
219
220
221
222
  if(spawn_data->myId == spawn_data->root) {
    physical_struct_create(target_qty, spawn_data->already_created, num_cpus, num_nodes, nodelist, type_dist, MALL_DIST_STRING, &(spawn_data->dist));

    //COPY PROGRAM NAME
223
    spawn_data->cmd = malloc((strlen(cmd)+1) * sizeof(char));
224
    strcpy(spawn_data->cmd, cmd);
225
    spawn_data->cmd[strlen(cmd)]='\0';
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

  } else {
    spawn_data->cmd = malloc(1 * sizeof(char));
  }
}

/*
 * Crea un tipo derivado para mandar 4 enteros con informacion
 * basica a los hijos. Son datos necesarios para que terminen
 * la creacion de procesos.
 */
void set_basic_spawn_dtype() {
  int i, counts = 4;
  int blocklengths[] = {1, 1, 1, 1};
  MPI_Aint displs[counts], dir;
  MPI_Datatype types[counts];

  // Rellenar vector types
  types[0] = types[1] = types[2] = types[3] = MPI_INT;

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

  MPI_Get_address(&(spawn_data->root_parents), &displs[0]);
  MPI_Get_address(&(spawn_data->initial_qty), &displs[1]);
  MPI_Get_address(&(spawn_data->spawn_is_single), &displs[2]);
  MPI_Get_address(&(spawn_data->spawn_method), &displs[3]);

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

  MPI_Type_create_struct(counts, blocklengths, displs, types, &(spawn_data->dtype));
  MPI_Type_commit(&(spawn_data->dtype));
}

/*
 * Libera una estructura de datos spawn_data
 * junto a la destrucion de aquellas estructuras que utiliza.
 */
void deallocate_spawn_data() {
265
266
267
268
269
270
271
272
  if(spawn_data == NULL) return;

  if(spawn_data->cmd != NULL) {
    free(spawn_data->cmd);
  }
  if(spawn_data->dtype != MPI_DATATYPE_NULL) {
    MPI_Type_free(&(spawn_data->dtype));
  }
273
274
275
276
  if(spawn_data->mapping != MPI_INFO_NULL) {
    MPI_Info_free(&(spawn_data->mapping));
  }
  if(spawn_data->spawn_is_async) {
277
    free_spawn_state();
278
279
  }
  free(spawn_data); 
280
  spawn_data = NULL;
281
282
283
284
285
286
287
288
289
290
291
292
}


//--------------PRIVATE SPAWN CREATION FUNCTIONS---------------//

/*
 * Funcion generica para la creacion de procesos. Obtiene la configuracion
 * y segun esta, elige como deberian crearse los procesos.
 *
 * Cuando termina, modifica la variable global para indicar este cambio
 */
void generic_spawn(MPI_Comm *child, int data_stage) {
293
  int local_state, aux_state;
294
295

  // WORK
296
  if(spawn_data->myId == spawn_data->root && spawn_data->spawn_qty > 0) { //SET MAPPING FOR NEW PROCESSES
297
298
299
300
301
302
303
304
305
306
307
308
    processes_dist(spawn_data->dist, &(spawn_data->mapping));
  }
  switch(spawn_data->spawn_method) {
    case MALL_SPAWN_BASELINE:
      local_state = baseline(*spawn_data, child);
      break;
    case MALL_SPAWN_MERGE:
      local_state = merge(*spawn_data, child, data_stage);
      break;
  }
  // END WORK
  end_time = MPI_Wtime();
309
310
311
312
  aux_state = get_spawn_state(spawn_data->spawn_is_async);
  if(!(aux_state == MALL_SPAWN_PENDING && local_state == MALL_SPAWN_ADAPT_POSTPONE)) {
    set_spawn_state(local_state, spawn_data->spawn_is_async);
  }
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
}


//--------------PRIVATE THREAD FUNCTIONS---------------//

/*
 * Aloja la memoria para un hilo auxiliar dedicado a la creacion de procesos.
 * No se puede realizar un "join" sobre el hilo y el mismo libera su memoria
 * asociado al terminar.
 */
int allocate_thread_spawn() {
  if(pthread_create(&spawn_thread, NULL, thread_work, NULL)) {
    printf("Error al crear el hilo de SPAWN\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
    return -1;
  }
  if(pthread_detach(spawn_thread)) {
    printf("Error when detaching spawning thread\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
    return -1;
  }
  return 0;
}

/*
 * Funcion llamada por un hilo para que este se encarge
 * de configurar la creacion de un nuevo grupo de procesos.
 *
 * Una vez esta lista la configuracion y es posible crear los procesos
 * se avisa al hilo maestro.
 */
344
void* thread_work() {
345
  int local_state;
346
347
  returned_comm = (MPI_Comm *) malloc(sizeof(MPI_Comm));
 
348
349
350
  generic_spawn(returned_comm, MALL_NOT_STARTED);

  local_state = get_spawn_state(MALL_SPAWN_PTHREAD);
351
  if(local_state == MALL_SPAWN_ADAPT_POSTPONE || local_state == MALL_SPAWN_PENDING) {
352
    // El grupo de procesos se terminara de juntar tras la redistribucion de datos
353

354
    local_state = wait_wakeup();
355
    generic_spawn(returned_comm, MALL_DIST_COMPLETED);
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  }

  pthread_exit(NULL);
}

/*
 * Comprueba si una creacion de procesos asincrona en el
 * paso "single" ha terminado. 
 * Si no ha terminado se mantiene el estado 
 * "MALL_SPAWN_SINGLE_PENDING".
 *
 * Si ha terminado se crean los hilos auxiliares para 
 * los procesos no root y se devuelve el estado
 * "MALL_SPAWN_PENDING".
 */
int check_single_state(MPI_Comm comm, int global_state) {
  MPI_Bcast(&global_state, 1, MPI_INT, spawn_data->root, comm);

  // Non-root processes join root to finalize the spawn
  // They also must join if the application has ended its work
  if(global_state == MALL_SPAWN_SINGLE_COMPLETED) { 
    global_state = MALL_SPAWN_PENDING;
378
    set_spawn_state(global_state, MALL_SPAWN_PTHREAD);
379

380
    if(spawn_data->myId != spawn_data->root) {
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
      allocate_thread_spawn(spawn_data);
    }
  }
  return global_state;
}

/*
 * Comprueba si una creación de procesos asincrona en el
 * paso "generic" ha terminado.
 * Si no ha terminado devuelve el estado 
 * "MALL_SPAWN_PENDING".
 *
 * Si ha terminado libera la memoria asociada a spawn_data
 * y devuelve el estado "MALL_SPAWN_COMPLETED".
 */
int check_generic_state(MPI_Comm comm, MPI_Comm *child, int local_state, double *real_time) {
  int global_state;

  MPI_Allreduce(&local_state, &global_state, 1, MPI_INT, MPI_MIN, comm);
400
  if(global_state == MALL_SPAWN_COMPLETED || global_state == MALL_SPAWN_ADAPTED) {
401
    set_spawn_state(global_state, MALL_SPAWN_PTHREAD);
402
403
404
405
406
407
    *child = *returned_comm;
    deallocate_spawn_data(spawn_data);
    *real_time=end_time;
  }
  return global_state;
}