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 int sz = group2->size();
99 for (int i = 0; i < sz; i++) {
100 int index = this->index(i);
101 int rank = group2->rank(index);
102 if (rank == MPI_UNDEFINED) {
103 result = MPI_UNEQUAL;
107 result = MPI_SIMILAR;
114 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
119 *newgroup = MPI_GROUP_EMPTY;
120 } else if (n == size_) {
122 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
125 *newgroup = new Group(n);
126 for (i = 0; i < n; i++) {
127 index = this->index(ranks[i]);
128 (*newgroup)->set_mapping(index, i);
134 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
137 int size2 = group2->size();
138 for (int i = 0; i < size2; i++) {
139 int proc2 = group2->index(i);
140 int proc1 = this->rank(proc2);
141 if (proc1 == MPI_UNDEFINED) {
146 *newgroup = MPI_GROUP_EMPTY;
148 *newgroup = new Group(size1);
149 size2 = this->size();
150 for (int i = 0; i < size2; i++) {
151 int proc1 = this->index(i);
152 (*newgroup)->set_mapping(proc1, i);
154 for (int i = size2; i < size1; i++) {
155 int proc2 = group2->index(i - size2);
156 (*newgroup)->set_mapping(proc2, i);
162 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
164 int size2 = group2->size();
165 for (int i = 0; i < size2; i++) {
166 int proc2 = group2->index(i);
167 int proc1 = this->rank(proc2);
168 if (proc1 == MPI_UNDEFINED) {
173 *newgroup = MPI_GROUP_EMPTY;
175 *newgroup = new Group(size2);
177 for (int i = 0; i < group2->size(); i++) {
178 int proc2 = group2->index(i);
179 int proc1 = this->rank(proc2);
180 if (proc1 != MPI_UNDEFINED) {
181 (*newgroup)->set_mapping(proc2, j);
189 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
193 for (int i = 0; i < size2; i++) {
194 int proc1 = this->index(i);
195 int proc2 = group2->rank(proc1);
196 if (proc2 != MPI_UNDEFINED) {
201 *newgroup = MPI_GROUP_EMPTY;
203 *newgroup = new Group(newsize);
204 for (int i = 0; i < size2; i++) {
205 int proc1 = this->index(i);
206 int proc2 = group2->rank(proc1);
207 if (proc2 == MPI_UNDEFINED) {
208 (*newgroup)->set_mapping(proc1, i);
215 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
217 int newsize = oldsize - n;
218 *newgroup = new Group(newsize);
219 int* to_exclude = new int[size_];
220 for (int i = 0; i < oldsize; i++)
222 for (int i = 0; i < n; i++)
223 to_exclude[ranks[i]]=1;
225 for (int i = 0; i < oldsize; i++) {
226 if(to_exclude[i]==0){
227 int index = this->index(i);
228 (*newgroup)->set_mapping(index, j);
237 static bool is_rank_in_range(int rank, int first, int last)
245 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
247 for (int i = 0; i < n; i++) {
248 for (int rank = ranges[i][0]; /* First */
249 rank >= 0 && rank < size_; /* Last */
252 if(rank == ranges[i][1]){/*already last ?*/
255 rank += ranges[i][2]; /* Stride */
256 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
260 *newgroup = new Group(newsize);
262 for (int i = 0; i < n; i++) {
263 for (int rank = ranges[i][0]; /* First */
264 rank >= 0 && rank < size_; /* Last */
266 int index = this->index(rank);
267 (*newgroup)->set_mapping(index, j);
269 if(rank == ranges[i][1]){/*already last ?*/
272 rank += ranges[i][2]; /* Stride */
273 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
280 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
282 for (int i = 0; i < n; i++) {
283 for (int rank = ranges[i][0]; /* First */
284 rank >= 0 && rank < size_; /* Last */
287 if(rank == ranges[i][1]){/*already last ?*/
290 rank += ranges[i][2]; /* Stride */
291 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
296 *newgroup = MPI_GROUP_EMPTY;
298 *newgroup = new Group(newsize);
301 while (newrank < newsize) {
303 for (int i = 0; i < n; i++) {
304 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
309 if(rank == ranges[i][1]){/*already last ?*/
312 rank += ranges[i][2]; /* Stride */
313 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
318 int index = this->index(oldrank);
319 (*newgroup)->set_mapping(index, newrank);
328 MPI_Group Group::f2c(int id) {
330 return MPI_GROUP_EMPTY;
331 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
333 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
335 return static_cast<MPI_Group>(MPI_GROUP_NULL);