Merge.c 2.07 KB
Newer Older
1
2
3
4
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include "../malleabilityStates.h"
iker_martin's avatar
iker_martin committed
5
#include "../malleabilityDataStructures.h"
6
7
8
9
10
11
12
13
14
#include "Merge.h"
#include "Baseline.h"

//--------------PRIVATE DECLARATIONS---------------//
void merge_adapt_expand(MPI_Comm *child, int is_children_group);
void merge_adapt_shrink(int numC, MPI_Comm *child, MPI_Comm comm, int myId);

//--------------PUBLIC FUNCTIONS---------------//
int merge(Spawn_data spawn_data, MPI_Comm *child, int data_state) {
15
  MPI_Comm intercomm;
16
  int local_state;
17
  int is_children_group = 1;
18
19

  if(spawn_data.initial_qty > spawn_data.target_qty) { //Shrink
20
    if(data_state == MALL_DIST_COMPLETED) {
iker_martin's avatar
iker_martin committed
21
      merge_adapt_shrink(spawn_data.target_qty, child, spawn_data.comm, mall->myId);
22
      local_state = MALL_SPAWN_ADAPTED;
23
    } else {
24
25
26
      local_state = MALL_SPAWN_ADAPT_POSTPONE;
    }
  } else { //Expand
27
28
    MPI_Comm_get_parent(&intercomm);
    is_children_group = intercomm == MPI_COMM_NULL ? 0:1;
29

30

31
32
    baseline(spawn_data, child);
    merge_adapt_expand(child, is_children_group);
33
    local_state = MALL_SPAWN_COMPLETED;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  }

  return local_state;
}

//--------------PRIVATE MERGE TYPE FUNCTIONS---------------//

/*
 * Se encarga de que el grupo de procesos resultante se 
 * encuentren todos en un intra comunicador, uniendo a
 * padres e hijos en un solo comunicador.
 *
 * Se llama antes de la redistribución de datos.
 *
 * TODO REFACTOR
 */
void merge_adapt_expand(MPI_Comm *child, int is_children_group) {
  MPI_Comm new_comm = MPI_COMM_NULL;

53
  MPI_Intercomm_merge(*child, is_children_group, &new_comm); //El que pone 0 va primero
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

  MPI_Comm_free(child); //POSIBLE ERROR?
  *child = new_comm;
}


/*
 * Se encarga de que el grupo de procesos resultante se
 * eliminen aquellos procesos que ya no son necesarios.
 * Los procesos eliminados se quedaran como zombies.
 *
 * Se llama una vez ha terminado la redistribución de datos.
 */
void merge_adapt_shrink(int numC, MPI_Comm *child, MPI_Comm comm, int myId) {
  int color = MPI_UNDEFINED;

  if(myId < numC) {
      color = 1;  
  }
  MPI_Comm_split(comm, color, myId, child);
}