1 /* Copyright (c) 2010-2017. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "smpi_group.hpp"
7 #include "smpi_comm.hpp"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
13 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
14 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
22 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
25 Group::Group(int n) : size_(n), rank_to_index_map_(size_, MPI_UNDEFINED)
30 Group::Group(MPI_Group origin)
32 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
33 size_ = origin->size();
35 rank_to_index_map_ = origin->rank_to_index_map_;
36 index_to_rank_map_ = origin->index_to_rank_map_;
40 void Group::set_mapping(int index, int rank)
42 if (0 <= rank && rank < size_) {
43 rank_to_index_map_[rank] = index;
44 if (index != MPI_UNDEFINED) {
45 if ((unsigned)index >= index_to_rank_map_.size())
46 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
47 index_to_rank_map_[index] = rank;
52 int Group::index(int rank)
55 if (0 <= rank && rank < size_)
56 index = rank_to_index_map_[rank];
58 index = MPI_UNDEFINED;
62 int Group::rank(int index)
65 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
66 rank = index_to_rank_map_[index];
77 void Group::unref(Group* group)
80 if (group->refcount_ <= 0) {
90 int Group::compare(MPI_Group group2)
95 if (size_ != group2->size()) {
98 for (int i = 0; i < size_; i++) {
99 int index = this->index(i);
100 int rank = group2->rank(index);
101 if (rank == MPI_UNDEFINED) {
102 result = MPI_UNEQUAL;
106 result = MPI_SIMILAR;
113 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
118 *newgroup = MPI_GROUP_EMPTY;
119 } else if (n == size_) {
121 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
124 *newgroup = new Group(n);
125 for (i = 0; i < n; i++) {
126 index = this->index(ranks[i]);
127 (*newgroup)->set_mapping(index, i);
133 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
136 int size2 = group2->size();
137 for (int i = 0; i < size2; i++) {
138 int proc2 = group2->index(i);
139 int proc1 = this->rank(proc2);
140 if (proc1 == MPI_UNDEFINED) {
145 *newgroup = MPI_GROUP_EMPTY;
147 *newgroup = new Group(size1);
148 size2 = this->size();
149 for (int i = 0; i < size2; i++) {
150 int proc1 = this->index(i);
151 (*newgroup)->set_mapping(proc1, i);
153 for (int i = size2; i < size1; i++) {
154 int proc2 = group2->index(i - size2);
155 (*newgroup)->set_mapping(proc2, i);
161 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
163 int size2 = group2->size();
164 for (int i = 0; i < size2; i++) {
165 int proc2 = group2->index(i);
166 int proc1 = this->rank(proc2);
167 if (proc1 == MPI_UNDEFINED) {
172 *newgroup = MPI_GROUP_EMPTY;
174 *newgroup = new Group(size2);
176 for (int i = 0; i < group2->size(); i++) {
177 int proc2 = group2->index(i);
178 int proc1 = this->rank(proc2);
179 if (proc1 != MPI_UNDEFINED) {
180 (*newgroup)->set_mapping(proc2, j);
188 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
192 for (int i = 0; i < size2; i++) {
193 int proc1 = this->index(i);
194 int proc2 = group2->rank(proc1);
195 if (proc2 != MPI_UNDEFINED) {
200 *newgroup = MPI_GROUP_EMPTY;
202 *newgroup = new Group(newsize);
203 for (int i = 0; i < size2; i++) {
204 int proc1 = this->index(i);
205 int proc2 = group2->rank(proc1);
206 if (proc2 == MPI_UNDEFINED) {
207 (*newgroup)->set_mapping(proc1, i);
214 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
216 int newsize = oldsize - n;
217 *newgroup = new Group(newsize);
218 int* to_exclude = new int[size_];
219 for (int i = 0; i < oldsize; i++)
221 for (int i = 0; i < n; i++)
222 to_exclude[ranks[i]]=1;
224 for (int i = 0; i < oldsize; i++) {
225 if(to_exclude[i]==0){
226 int index = this->index(i);
227 (*newgroup)->set_mapping(index, j);
236 static bool is_rank_in_range(int rank, int first, int last)
244 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
246 for (int i = 0; i < n; i++) {
247 for (int rank = ranges[i][0]; /* First */
248 rank >= 0 && rank < size_; /* Last */
251 if(rank == ranges[i][1]){/*already last ?*/
254 rank += ranges[i][2]; /* Stride */
255 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
259 *newgroup = new Group(newsize);
261 for (int i = 0; i < n; i++) {
262 for (int rank = ranges[i][0]; /* First */
263 rank >= 0 && rank < size_; /* Last */
265 int index = this->index(rank);
266 (*newgroup)->set_mapping(index, j);
268 if(rank == ranges[i][1]){/*already last ?*/
271 rank += ranges[i][2]; /* Stride */
272 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
279 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
281 for (int i = 0; i < n; i++) {
282 for (int rank = ranges[i][0]; /* First */
283 rank >= 0 && rank < size_; /* Last */
286 if(rank == ranges[i][1]){/*already last ?*/
289 rank += ranges[i][2]; /* Stride */
290 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
295 *newgroup = MPI_GROUP_EMPTY;
297 *newgroup = new Group(newsize);
300 while (newrank < newsize) {
302 for (int i = 0; i < n; i++) {
303 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
308 if(rank == ranges[i][1]){/*already last ?*/
311 rank += ranges[i][2]; /* Stride */
312 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
317 int index = this->index(oldrank);
318 (*newgroup)->set_mapping(index, newrank);
327 MPI_Group Group::f2c(int id) {
329 return MPI_GROUP_EMPTY;
330 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
332 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
334 return static_cast<MPI_Group>(MPI_GROUP_NULL);