Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SMPI over SIMIX_network in a two days rush.
[simgrid.git] / src / smpi / smpi_group.c
1 #include "private.h"
2
3 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
4                                 "Logging specific to SMPI (group)");
5
6 typedef struct s_smpi_mpi_group {
7   int size;
8   int* rank_to_index_map;
9   int* index_to_rank_map;
10   int refcount;
11 } s_smpi_mpi_group_t;
12
13 static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = {
14   0,    /* size */
15   NULL, /* rank_to_index_map */
16   NULL, /* index_to_rank_map */
17   1,    /* refcount: start > 0 so that this group never gets freed */
18 };
19 MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
20
21 MPI_Group smpi_group_new(int size) {
22   MPI_Group group;
23   int i, count;
24
25   count = smpi_process_count();
26   group = xbt_new(s_smpi_mpi_group_t, 1);
27   group->size = size;
28   group->rank_to_index_map = xbt_new(int, size);
29   group->index_to_rank_map = xbt_new(int, count);
30   group->refcount = 0;
31   for(i = 0; i < size; i++) {
32     group->rank_to_index_map[i] = MPI_UNDEFINED;
33   }
34   for(i = 0; i < count; i++) {
35     group->index_to_rank_map[i] = MPI_UNDEFINED;
36   }
37   return group;
38 }
39
40 void smpi_group_destroy(MPI_Group group) {
41   if(smpi_group_unuse(group) <= 0) {
42     xbt_free(group->rank_to_index_map);
43     xbt_free(group->index_to_rank_map);
44     xbt_free(group);
45   }
46 }
47
48 void smpi_group_set_mapping(MPI_Group group, int index, int rank) {
49   if(rank < group->size && index < smpi_process_count()) {
50     group->rank_to_index_map[rank] = index;
51     group->index_to_rank_map[index] = rank;
52   }
53 }
54
55 int smpi_group_index(MPI_Group group, int rank) {
56   int index = MPI_UNDEFINED;
57
58   if(rank < group->size) {
59     index = group->rank_to_index_map[rank];
60   }
61   return index;
62 }
63
64 int smpi_group_rank(MPI_Group group, int index) {
65   int rank = MPI_UNDEFINED;
66
67   if(index < smpi_process_count()) {
68     rank = group->index_to_rank_map[index];
69   }
70   return rank;
71 }
72
73 int smpi_group_use(MPI_Group group) {
74   group->refcount++;
75   return group->refcount;
76 }
77
78 int smpi_group_unuse(MPI_Group group) {
79   group->refcount--;
80   return group->refcount;
81 }
82
83 int smpi_group_size(MPI_Group group) {
84   return group->size;
85 }
86
87 int smpi_group_compare(MPI_Group group1, MPI_Group group2) {
88   int result;
89   int i, index, rank, size;
90
91   result = MPI_IDENT;
92   if(smpi_group_size(group1) != smpi_group_size(group2)) {
93     result = MPI_UNEQUAL;
94   } else {
95     size = smpi_group_size(group2);
96     for(i = 0; i < size; i++) {
97       index = smpi_group_index(group1, i);
98       rank = smpi_group_rank(group2, index);
99       if(rank == MPI_UNDEFINED) {
100         result = MPI_UNEQUAL;
101         break;
102       }
103       if(rank != i) {
104         result = MPI_SIMILAR;
105       }
106     }
107   }
108   return result;
109 }