1 /* Copyright (c) 2010, 2013-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
8 #include "smpi_group.hpp"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
11 simgrid::SMPI::Group mpi_MPI_GROUP_EMPTY;
12 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
20 m_rank_to_index_map=nullptr; /* m_rank_to_index_map */
21 m_index_to_rank_map=nullptr; /* m_index_to_rank_map */
22 m_refcount=1; /* m_refcount: start > 0 so that this group never gets freed */
30 m_rank_to_index_map = xbt_new(int, m_size);
31 m_index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
33 for (i = 0; i < m_size; i++) {
34 m_rank_to_index_map[i] = MPI_UNDEFINED;
38 Group::Group(MPI_Group origin)
42 xbt_dict_cursor_t cursor = nullptr;
45 if(origin != MPI_GROUP_NULL
46 && origin != MPI_GROUP_EMPTY)
48 m_size = origin->size();
49 m_rank_to_index_map = xbt_new(int, m_size);
50 m_index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
52 for (i = 0; i < m_size; i++) {
53 m_rank_to_index_map[i] = origin->m_rank_to_index_map[i];
56 xbt_dict_foreach(origin->m_index_to_rank_map, cursor, key, ptr_rank) {
57 int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
58 *cp=*reinterpret_cast<int*>(ptr_rank);
59 xbt_dict_set(m_index_to_rank_map, key, cp, nullptr);
66 xbt_free(m_rank_to_index_map);
67 xbt_dict_free(&m_index_to_rank_map);
72 if(this != MPI_COMM_WORLD->group()
73 && this != MPI_GROUP_NULL
74 && this != MPI_GROUP_EMPTY)
78 void Group::set_mapping(int index, int rank)
83 m_rank_to_index_map[rank] = index;
84 if (index!=MPI_UNDEFINED ) {
85 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
88 char * key = bprintf("%d", index);
89 xbt_dict_set(m_index_to_rank_map, key, val_rank, nullptr);
95 int Group::index(int rank)
97 int index = MPI_UNDEFINED;
99 if (0 <= rank && rank < m_size) {
100 index = m_rank_to_index_map[rank];
105 int Group::rank(int index)
107 int * ptr_rank = nullptr;
108 if (this==MPI_GROUP_EMPTY)
109 return MPI_UNDEFINED;
110 char * key = bprintf("%d", index);
111 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(m_index_to_rank_map, key));
114 if (ptr_rank==nullptr)
115 return MPI_UNDEFINED;
128 if (m_refcount <= 0) {
140 int Group::compare(MPI_Group group2)
143 int i, index, rank, sz;
146 if (m_size != group2->size()) {
147 result = MPI_UNEQUAL;
150 for (i = 0; i < sz; i++) {
151 index = this->index(i);
152 rank = group2->rank(index);
153 if (rank == MPI_UNDEFINED) {
154 result = MPI_UNEQUAL;
158 result = MPI_SIMILAR;
165 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
169 *newgroup = MPI_GROUP_EMPTY;
170 } else if (n == m_size) {
172 if(this!= MPI_COMM_WORLD->group()
173 && this != MPI_GROUP_NULL
174 && this != MPI_COMM_SELF->group()
175 && this != MPI_GROUP_EMPTY)
178 *newgroup = new Group(n);
179 for (i = 0; i < n; i++) {
180 index = this->index(ranks[i]);
181 (*newgroup)->set_mapping(index, i);
187 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
190 int size2 = group2->size();
191 for (int i = 0; i < size2; i++) {
192 int proc2 = group2->index(i);
193 int proc1 = this->rank(proc2);
194 if (proc1 == MPI_UNDEFINED) {
199 *newgroup = MPI_GROUP_EMPTY;
201 *newgroup = new simgrid::SMPI::Group(size1);
202 size2 = this->size();
203 for (int i = 0; i < size2; i++) {
204 int proc1 = this->index(i);
205 (*newgroup)->set_mapping(proc1, i);
207 for (int i = size2; i < size1; i++) {
208 int proc2 = group2->index(i - size2);
209 (*newgroup)->set_mapping(proc2, i);
215 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
217 int size2 = group2->size();
218 for (int i = 0; i < size2; i++) {
219 int proc2 = group2->index(i);
220 int proc1 = this->rank(proc2);
221 if (proc1 == MPI_UNDEFINED) {
226 *newgroup = MPI_GROUP_EMPTY;
228 *newgroup = new simgrid::SMPI::Group(size2);
230 for (int i = 0; i < group2->size(); i++) {
231 int proc2 = group2->index(i);
232 int proc1 = this->rank(proc2);
233 if (proc1 != MPI_UNDEFINED) {
234 (*newgroup)->set_mapping(proc2, j);
242 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
244 int newsize = m_size;
246 for (int i = 0; i < size2; i++) {
247 int proc1 = this->index(i);
248 int proc2 = group2->rank(proc1);
249 if (proc2 != MPI_UNDEFINED) {
254 *newgroup = MPI_GROUP_EMPTY;
256 *newgroup = new simgrid::SMPI::Group(newsize);
257 for (int i = 0; i < size2; i++) {
258 int proc1 = this->index(i);
259 int proc2 = group2->rank(proc1);
260 if (proc2 == MPI_UNDEFINED) {
261 (*newgroup)->set_mapping(proc1, i);
268 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
269 int oldsize = m_size;
270 int newsize = oldsize - n;
271 *newgroup = new simgrid::SMPI::Group(newsize);
272 int* to_exclude=xbt_new0(int, m_size);
273 for (int i = 0; i < oldsize; i++)
275 for (int i = 0; i < n; i++)
276 to_exclude[ranks[i]]=1;
278 for (int i = 0; i < oldsize; i++) {
279 if(to_exclude[i]==0){
280 int index = this->index(i);
281 (*newgroup)->set_mapping(index, j);
285 xbt_free(to_exclude);
290 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
292 for (int i = 0; i < n; i++) {
293 for (int rank = ranges[i][0]; /* First */
294 rank >= 0 && rank < m_size; /* Last */
297 if(rank == ranges[i][1]){/*already last ?*/
300 rank += ranges[i][2]; /* Stride */
301 if (ranges[i][0] < ranges[i][1]) {
302 if (rank > ranges[i][1])
305 if (rank < ranges[i][1])
310 *newgroup = new simgrid::SMPI::Group(newsize);
312 for (int i = 0; i < n; i++) {
313 for (int rank = ranges[i][0]; /* First */
314 rank >= 0 && rank < m_size; /* Last */
316 int index = this->index(rank);
317 (*newgroup)->set_mapping(index, j);
319 if(rank == ranges[i][1]){/*already last ?*/
322 rank += ranges[i][2]; /* Stride */
323 if (ranges[i][0] < ranges[i][1]) {
324 if (rank > ranges[i][1])
327 if (rank < ranges[i][1])
335 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
336 int newsize = m_size;
337 for (int i = 0; i < n; i++) {
338 for (int rank = ranges[i][0]; /* First */
339 rank >= 0 && rank < m_size; /* Last */
342 if(rank == ranges[i][1]){/*already last ?*/
345 rank += ranges[i][2]; /* Stride */
346 if (ranges[i][0] < ranges[i][1]) {
347 if (rank > ranges[i][1])
350 if (rank < ranges[i][1])
356 *newgroup = MPI_GROUP_EMPTY;
358 *newgroup = new simgrid::SMPI::Group(newsize);
361 while (newrank < newsize) {
363 for (int i = 0; i < n; i++) {
364 for (int rank = ranges[i][0]; rank >= 0 && rank < m_size;) {
369 if(rank == ranges[i][1]){/*already last ?*/
372 rank += ranges[i][2]; /* Stride */
373 if (ranges[i][0]<ranges[i][1]){
374 if (rank > ranges[i][1])
377 if (rank < ranges[i][1])
383 int index = this->index(oldrank);
384 (*newgroup)->set_mapping(index, newrank);