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_comm.hpp"
7 #include "smpi_group.hpp"
9 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 rank_to_index_map_ = nullptr; /* rank_to_index_map_ */
21 refcount_ = 1; /* refcount_: start > 0 so that this group never gets freed */
24 Group::Group(int n) : size_(n)
26 rank_to_index_map_ = new int[size_];
28 for (int i = 0; i < size_; i++)
29 rank_to_index_map_[i] = MPI_UNDEFINED;
32 Group::Group(MPI_Group origin)
34 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
35 size_ = origin->size();
36 rank_to_index_map_ = new int[size_];
38 for (int i = 0; i < size_; i++) {
39 rank_to_index_map_[i] = origin->rank_to_index_map_[i];
42 for (auto elm : origin->index_to_rank_map_) {
43 index_to_rank_map_.insert({elm.first, elm.second});
50 delete[] rank_to_index_map_;
53 void Group::set_mapping(int index, int rank)
56 rank_to_index_map_[rank] = index;
57 if (index != MPI_UNDEFINED)
58 index_to_rank_map_.insert({index, rank});
62 int Group::index(int rank)
64 int index = MPI_UNDEFINED;
66 if (0 <= rank && rank < size_) {
67 index = rank_to_index_map_[rank];
72 int Group::rank(int index)
74 if (this == MPI_GROUP_EMPTY)
76 if (index_to_rank_map_.find(index) == index_to_rank_map_.end())
79 return index_to_rank_map_.at(index);
87 void Group::unref(Group* group)
90 if (group->refcount_ <= 0) {
100 int Group::compare(MPI_Group group2)
105 if (size_ != group2->size()) {
106 result = MPI_UNEQUAL;
108 int sz = group2->size();
109 for (int i = 0; i < sz; i++) {
110 int index = this->index(i);
111 int rank = group2->rank(index);
112 if (rank == MPI_UNDEFINED) {
113 result = MPI_UNEQUAL;
117 result = MPI_SIMILAR;
124 int Group::incl(int n, int* ranks, MPI_Group* newgroup)
129 *newgroup = MPI_GROUP_EMPTY;
130 } else if (n == size_) {
132 if(this!= MPI_COMM_WORLD->group()
133 && this != MPI_COMM_SELF->group()
134 && this != MPI_GROUP_EMPTY)
137 *newgroup = new Group(n);
138 for (i = 0; i < n; i++) {
139 index = this->index(ranks[i]);
140 (*newgroup)->set_mapping(index, i);
146 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
149 int size2 = group2->size();
150 for (int i = 0; i < size2; i++) {
151 int proc2 = group2->index(i);
152 int proc1 = this->rank(proc2);
153 if (proc1 == MPI_UNDEFINED) {
158 *newgroup = MPI_GROUP_EMPTY;
160 *newgroup = new Group(size1);
161 size2 = this->size();
162 for (int i = 0; i < size2; i++) {
163 int proc1 = this->index(i);
164 (*newgroup)->set_mapping(proc1, i);
166 for (int i = size2; i < size1; i++) {
167 int proc2 = group2->index(i - size2);
168 (*newgroup)->set_mapping(proc2, i);
174 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
176 int size2 = group2->size();
177 for (int i = 0; i < size2; i++) {
178 int proc2 = group2->index(i);
179 int proc1 = this->rank(proc2);
180 if (proc1 == MPI_UNDEFINED) {
185 *newgroup = MPI_GROUP_EMPTY;
187 *newgroup = new Group(size2);
189 for (int i = 0; i < group2->size(); i++) {
190 int proc2 = group2->index(i);
191 int proc1 = this->rank(proc2);
192 if (proc1 != MPI_UNDEFINED) {
193 (*newgroup)->set_mapping(proc2, j);
201 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
205 for (int i = 0; i < size2; i++) {
206 int proc1 = this->index(i);
207 int proc2 = group2->rank(proc1);
208 if (proc2 != MPI_UNDEFINED) {
213 *newgroup = MPI_GROUP_EMPTY;
215 *newgroup = new Group(newsize);
216 for (int i = 0; i < size2; i++) {
217 int proc1 = this->index(i);
218 int proc2 = group2->rank(proc1);
219 if (proc2 == MPI_UNDEFINED) {
220 (*newgroup)->set_mapping(proc1, i);
227 int Group::excl(int n, int *ranks, MPI_Group * newgroup){
229 int newsize = oldsize - n;
230 *newgroup = new Group(newsize);
231 int* to_exclude=xbt_new0(int, size_);
232 for (int i = 0; i < oldsize; i++)
234 for (int i = 0; i < n; i++)
235 to_exclude[ranks[i]]=1;
237 for (int i = 0; i < oldsize; i++) {
238 if(to_exclude[i]==0){
239 int index = this->index(i);
240 (*newgroup)->set_mapping(index, j);
244 xbt_free(to_exclude);
249 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
251 for (int i = 0; i < n; i++) {
252 for (int rank = ranges[i][0]; /* First */
253 rank >= 0 && rank < size_; /* Last */
256 if(rank == ranges[i][1]){/*already last ?*/
259 rank += ranges[i][2]; /* Stride */
260 if (ranges[i][0] < ranges[i][1]) {
261 if (rank > ranges[i][1])
264 if (rank < ranges[i][1])
269 *newgroup = new Group(newsize);
271 for (int i = 0; i < n; i++) {
272 for (int rank = ranges[i][0]; /* First */
273 rank >= 0 && rank < size_; /* Last */
275 int index = this->index(rank);
276 (*newgroup)->set_mapping(index, j);
278 if(rank == ranges[i][1]){/*already last ?*/
281 rank += ranges[i][2]; /* Stride */
282 if (ranges[i][0] < ranges[i][1]) {
283 if (rank > ranges[i][1])
286 if (rank < ranges[i][1])
294 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
296 for (int i = 0; i < n; i++) {
297 for (int rank = ranges[i][0]; /* First */
298 rank >= 0 && rank < size_; /* Last */
301 if(rank == ranges[i][1]){/*already last ?*/
304 rank += ranges[i][2]; /* Stride */
305 if (ranges[i][0] < ranges[i][1]) {
306 if (rank > ranges[i][1])
309 if (rank < ranges[i][1])
315 *newgroup = MPI_GROUP_EMPTY;
317 *newgroup = new Group(newsize);
320 while (newrank < newsize) {
322 for (int i = 0; i < n; i++) {
323 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
328 if(rank == ranges[i][1]){/*already last ?*/
331 rank += ranges[i][2]; /* Stride */
332 if (ranges[i][0]<ranges[i][1]){
333 if (rank > ranges[i][1])
336 if (rank < ranges[i][1])
342 int index = this->index(oldrank);
343 (*newgroup)->set_mapping(index, newrank);
352 MPI_Group Group::f2c(int id) {
354 return MPI_GROUP_EMPTY;
355 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
357 return static_cast<MPI_Group>(xbt_dict_get_or_null(F2C::f2c_lookup(), get_key(key, id)));
359 return static_cast<MPI_Group>(MPI_GROUP_NULL);