Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
these test work on windows, actually
[simgrid.git] / src / smpi / smpi_group.cpp
1 /* Copyright (c) 2010, 2013-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi,
10                                 "Logging specific to SMPI (group)");
11
12 typedef struct s_smpi_mpi_group {
13   int size;
14   int *rank_to_index_map;
15   xbt_dict_t index_to_rank_map;
16   int refcount;
17 } s_smpi_mpi_group_t;
18
19 static s_smpi_mpi_group_t mpi_MPI_GROUP_EMPTY = {
20   0,                            /* size */
21   NULL,                         /* rank_to_index_map */
22   NULL,                         /* index_to_rank_map */
23   1,                            /* refcount: start > 0 so that this group never gets freed */
24 };
25
26 MPI_Group MPI_GROUP_EMPTY = &mpi_MPI_GROUP_EMPTY;
27
28 MPI_Group smpi_group_new(int size)
29 {
30   MPI_Group group;
31   int i;
32
33   group = xbt_new(s_smpi_mpi_group_t, 1);
34   group->size = size;
35   group->rank_to_index_map = xbt_new(int, size);
36   group->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
37   group->refcount = 1;
38   for (i = 0; i < size; i++) {
39     group->rank_to_index_map[i] = MPI_UNDEFINED;
40   }
41
42   return group;
43 }
44
45 MPI_Group smpi_group_copy(MPI_Group origin)
46 {
47   MPI_Group group=origin;
48   char *key;
49   char *ptr_rank;
50   xbt_dict_cursor_t cursor = NULL;
51   
52   int i;
53   if(origin!= smpi_comm_group(MPI_COMM_WORLD)
54             && origin != MPI_GROUP_NULL
55             && origin != smpi_comm_group(MPI_COMM_SELF)
56             && origin != MPI_GROUP_EMPTY)
57     {
58       group = xbt_new(s_smpi_mpi_group_t, 1);
59       group->size = origin->size;
60       group->rank_to_index_map = xbt_new(int, group->size);
61       group->index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
62       group->refcount = 1;
63       for (i = 0; i < group->size; i++) {
64         group->rank_to_index_map[i] = origin->rank_to_index_map[i];
65       }
66
67       xbt_dict_foreach(origin->index_to_rank_map, cursor, key, ptr_rank) {
68         xbt_dict_set(group->index_to_rank_map, key, ptr_rank, NULL);
69       }
70     }
71
72   return group;
73 }
74
75
76 void smpi_group_destroy(MPI_Group group)
77 {
78   if(group!= smpi_comm_group(MPI_COMM_WORLD)
79           && group != MPI_GROUP_NULL
80           && group != smpi_comm_group(MPI_COMM_SELF)
81           && group != MPI_GROUP_EMPTY)
82   smpi_group_unuse(group);
83 }
84
85 void smpi_group_set_mapping(MPI_Group group, int index, int rank)
86 {
87   int * val_rank;
88
89   if (rank < group->size) {
90     group->rank_to_index_map[rank] = index;
91     if (index!=MPI_UNDEFINED ) {
92       val_rank = (int *) malloc(sizeof(int));
93       *val_rank = rank;
94
95       char * key = bprintf("%d", index);
96       xbt_dict_set(group->index_to_rank_map, key, val_rank, NULL);
97       free(key);
98     }
99   }
100 }
101
102 int smpi_group_index(MPI_Group group, int rank)
103 {
104   int index = MPI_UNDEFINED;
105
106   if (0 <= rank && rank < group->size) {
107     index = group->rank_to_index_map[rank];
108   }
109   return index;
110 }
111
112 int smpi_group_rank(MPI_Group group, int index)
113 {
114   int * ptr_rank = NULL;
115   char * key = bprintf("%d", index);
116   ptr_rank = static_cast<int*>(xbt_dict_get_or_null(group->index_to_rank_map, key));
117   xbt_free(key);
118
119   if (!ptr_rank)
120     return MPI_UNDEFINED;
121   return *ptr_rank;
122 }
123
124 int smpi_group_use(MPI_Group group)
125 {
126   group->refcount++;
127   return group->refcount;
128 }
129
130 int smpi_group_unuse(MPI_Group group)
131 {
132   group->refcount--;
133   if (group->refcount <= 0) {
134     xbt_free(group->rank_to_index_map);
135     xbt_dict_free(&group->index_to_rank_map);
136     xbt_free(group);
137     return 0;
138   }
139   return group->refcount;
140
141 }
142
143 int smpi_group_size(MPI_Group group)
144 {
145   return group->size;
146 }
147
148 int smpi_group_compare(MPI_Group group1, MPI_Group group2)
149 {
150   int result;
151   int i, index, rank, size;
152
153   result = MPI_IDENT;
154   if (smpi_group_size(group1) != smpi_group_size(group2)) {
155     result = MPI_UNEQUAL;
156   } else {
157     size = smpi_group_size(group2);
158     for (i = 0; i < size; i++) {
159       index = smpi_group_index(group1, i);
160       rank = smpi_group_rank(group2, index);
161       if (rank == MPI_UNDEFINED) {
162         result = MPI_UNEQUAL;
163         break;
164       }
165       if (rank != i) {
166         result = MPI_SIMILAR;
167       }
168     }
169   }
170   return result;
171 }
172
173 int smpi_group_incl(MPI_Group group, int n, int* ranks, MPI_Group* newgroup)
174 {
175   int i=0, index=0;
176   if (n == 0) {
177     *newgroup = MPI_GROUP_EMPTY;
178   } else if (n == smpi_group_size(group)) {
179     *newgroup = group;
180     if(group!= smpi_comm_group(MPI_COMM_WORLD)
181               && group != MPI_GROUP_NULL
182               && group != smpi_comm_group(MPI_COMM_SELF)
183               && group != MPI_GROUP_EMPTY)
184     smpi_group_use(group);
185   } else {
186     *newgroup = smpi_group_new(n);
187     for (i = 0; i < n; i++) {
188       index = smpi_group_index(group, ranks[i]);
189       smpi_group_set_mapping(*newgroup, index, i);
190     }
191   }
192   return MPI_SUCCESS;
193 }