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. */
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
10 simgrid::SMPI::Group mpi_MPI_GROUP_EMPTY;
11 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
19 m_rank_to_index_map=nullptr; /* m_rank_to_index_map */
20 m_index_to_rank_map=nullptr; /* m_index_to_rank_map */
21 m_refcount=1; /* m_refcount: start > 0 so that this group never gets freed */
29 m_rank_to_index_map = xbt_new(int, m_size);
30 m_index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
32 for (i = 0; i < m_size; i++) {
33 m_rank_to_index_map[i] = MPI_UNDEFINED;
37 Group::Group(MPI_Group origin)
41 xbt_dict_cursor_t cursor = nullptr;
44 if(origin != MPI_GROUP_NULL
45 && origin != MPI_GROUP_EMPTY)
47 m_size = origin->size();
48 m_rank_to_index_map = xbt_new(int, m_size);
49 m_index_to_rank_map = xbt_dict_new_homogeneous(xbt_free_f);
51 for (i = 0; i < m_size; i++) {
52 m_rank_to_index_map[i] = origin->m_rank_to_index_map[i];
55 xbt_dict_foreach(origin->m_index_to_rank_map, cursor, key, ptr_rank) {
56 int * cp = static_cast<int*>(xbt_malloc(sizeof(int)));
57 *cp=*reinterpret_cast<int*>(ptr_rank);
58 xbt_dict_set(m_index_to_rank_map, key, cp, nullptr);
65 xbt_free(m_rank_to_index_map);
66 xbt_dict_free(&m_index_to_rank_map);
71 if(this != MPI_COMM_WORLD->group()
72 && this != MPI_GROUP_NULL
73 && this != MPI_GROUP_EMPTY)
77 void Group::set_mapping(int index, int rank)
82 m_rank_to_index_map[rank] = index;
83 if (index!=MPI_UNDEFINED ) {
84 val_rank = static_cast<int *>(xbt_malloc(sizeof(int)));
87 char * key = bprintf("%d", index);
88 xbt_dict_set(m_index_to_rank_map, key, val_rank, nullptr);
94 int Group::index(int rank)
96 int index = MPI_UNDEFINED;
98 if (0 <= rank && rank < m_size) {
99 index = m_rank_to_index_map[rank];
104 int Group::rank(int index)
106 int * ptr_rank = nullptr;
107 if (this==MPI_GROUP_EMPTY)
108 return MPI_UNDEFINED;
109 char * key = bprintf("%d", index);
110 ptr_rank = static_cast<int*>(xbt_dict_get_or_null(m_index_to_rank_map, key));
113 if (ptr_rank==nullptr)
114 return MPI_UNDEFINED;
127 if (m_refcount <= 0) {
139 int Group::compare(MPI_Group group2)
142 int i, index, rank, sz;
145 if (m_size != group2->size()) {
146 result = MPI_UNEQUAL;
149 for (i = 0; i < sz; i++) {
150 index = this->index(i);
151 rank = group2->rank(index);
152 if (rank == MPI_UNDEFINED) {
153 result = MPI_UNEQUAL;
157 result = MPI_SIMILAR;
164 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
168 *newgroup = MPI_GROUP_EMPTY;
169 } else if (n == m_size) {
171 if(this!= MPI_COMM_WORLD->group()
172 && this != MPI_GROUP_NULL
173 && this != MPI_COMM_SELF->group()
174 && this != MPI_GROUP_EMPTY)
177 *newgroup = new Group(n);
178 for (i = 0; i < n; i++) {
179 index = this->index(ranks[i]);
180 (*newgroup)->set_mapping(index, i);
186 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
189 int size2 = group2->size();
190 for (int i = 0; i < size2; i++) {
191 int proc2 = group2->index(i);
192 int proc1 = this->rank(proc2);
193 if (proc1 == MPI_UNDEFINED) {
198 *newgroup = MPI_GROUP_EMPTY;
200 *newgroup = new simgrid::SMPI::Group(size1);
201 size2 = this->size();
202 for (int i = 0; i < size2; i++) {
203 int proc1 = this->index(i);
204 (*newgroup)->set_mapping(proc1, i);
206 for (int i = size2; i < size1; i++) {
207 int proc2 = group2->index(i - size2);
208 (*newgroup)->set_mapping(proc2, i);
214 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
216 int size2 = group2->size();
217 for (int i = 0; i < size2; i++) {
218 int proc2 = group2->index(i);
219 int proc1 = this->rank(proc2);
220 if (proc1 == MPI_UNDEFINED) {
225 *newgroup = MPI_GROUP_EMPTY;
227 *newgroup = new simgrid::SMPI::Group(size2);
229 for (int i = 0; i < group2->size(); i++) {
230 int proc2 = group2->index(i);
231 int proc1 = this->rank(proc2);
232 if (proc1 != MPI_UNDEFINED) {
233 (*newgroup)->set_mapping(proc2, j);
241 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
243 int newsize = m_size;
245 for (int i = 0; i < size2; i++) {
246 int proc1 = this->index(i);
247 int proc2 = group2->rank(proc1);
248 if (proc2 != MPI_UNDEFINED) {
253 *newgroup = MPI_GROUP_EMPTY;
255 *newgroup = new simgrid::SMPI::Group(newsize);
256 for (int i = 0; i < size2; i++) {
257 int proc1 = this->index(i);
258 int proc2 = group2->rank(proc1);
259 if (proc2 == MPI_UNDEFINED) {
260 (*newgroup)->set_mapping(proc1, i);
267 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
268 int oldsize = m_size;
269 int newsize = oldsize - n;
270 *newgroup = new simgrid::SMPI::Group(newsize);
271 int* to_exclude=xbt_new0(int, m_size);
272 for (int i = 0; i < oldsize; i++)
274 for (int i = 0; i < n; i++)
275 to_exclude[ranks[i]]=1;
277 for (int i = 0; i < oldsize; i++) {
278 if(to_exclude[i]==0){
279 int index = this->index(i);
280 (*newgroup)->set_mapping(index, j);
284 xbt_free(to_exclude);
289 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
291 for (int i = 0; i < n; i++) {
292 for (int rank = ranges[i][0]; /* First */
293 rank >= 0 && rank < m_size; /* Last */
296 if(rank == ranges[i][1]){/*already last ?*/
299 rank += ranges[i][2]; /* Stride */
300 if (ranges[i][0] < ranges[i][1]) {
301 if (rank > ranges[i][1])
304 if (rank < ranges[i][1])
309 *newgroup = new simgrid::SMPI::Group(newsize);
311 for (int i = 0; i < n; i++) {
312 for (int rank = ranges[i][0]; /* First */
313 rank >= 0 && rank < m_size; /* Last */
315 int index = this->index(rank);
316 (*newgroup)->set_mapping(index, j);
318 if(rank == ranges[i][1]){/*already last ?*/
321 rank += ranges[i][2]; /* Stride */
322 if (ranges[i][0] < ranges[i][1]) {
323 if (rank > ranges[i][1])
326 if (rank < ranges[i][1])
334 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
335 int newsize = m_size;
336 for (int i = 0; i < n; i++) {
337 for (int rank = ranges[i][0]; /* First */
338 rank >= 0 && rank < m_size; /* Last */
341 if(rank == ranges[i][1]){/*already last ?*/
344 rank += ranges[i][2]; /* Stride */
345 if (ranges[i][0] < ranges[i][1]) {
346 if (rank > ranges[i][1])
349 if (rank < ranges[i][1])
355 *newgroup = MPI_GROUP_EMPTY;
357 *newgroup = new simgrid::SMPI::Group(newsize);
360 while (newrank < newsize) {
362 for (int i = 0; i < n; i++) {
363 for (int rank = ranges[i][0]; rank >= 0 && rank < m_size;) {
368 if(rank == ranges[i][1]){/*already last ?*/
371 rank += ranges[i][2]; /* Stride */
372 if (ranges[i][0]<ranges[i][1]){
373 if (rank > ranges[i][1])
376 if (rank < ranges[i][1])
382 int index = this->index(oldrank);
383 (*newgroup)->set_mapping(index, newrank);