1 /* Copyright (c) 2010-2019. 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 "simgrid/s4u/Actor.hpp"
7 #include "smpi_group.hpp"
8 #include "smpi_comm.hpp"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_group, smpi, "Logging specific to SMPI (group)");
14 simgrid::smpi::Group mpi_MPI_GROUP_EMPTY;
15 MPI_Group MPI_GROUP_EMPTY=&mpi_MPI_GROUP_EMPTY;
20 Group::Group(Group* origin)
22 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
23 size_ = origin->size();
24 // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
25 index_to_rank_map_ = origin->index_to_rank_map_;
26 rank_to_actor_map_ = origin->rank_to_actor_map_;
27 actor_to_rank_map_ = origin->actor_to_rank_map_;
31 void Group::set_mapping(s4u::Actor* actor, int rank)
33 if (0 <= rank && rank < size_) {
34 int index = actor->get_pid();
35 if (index != MPI_UNDEFINED) {
36 if ((unsigned)index >= index_to_rank_map_.size())
37 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
38 index_to_rank_map_[index] = rank;
41 rank_to_actor_map_[rank] = actor;
42 actor_to_rank_map_.insert({actor, rank});
46 int Group::rank(int index)
49 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
50 rank = index_to_rank_map_[index];
57 s4u::Actor* Group::actor(int rank)
59 if (0 <= rank && rank < size_)
60 return rank_to_actor_map_[rank];
65 int Group::rank(s4u::Actor* actor)
67 auto iterator = actor_to_rank_map_.find(actor);
68 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
76 void Group::unref(Group* group)
79 if (group->refcount_ <= 0) {
84 int Group::compare(MPI_Group group2)
89 if (size_ != group2->size()) {
92 for (int i = 0; i < size_; i++) {
93 s4u::Actor* actor = this->actor(i);
94 int rank = group2->rank(actor);
95 if (rank == MPI_UNDEFINED) {
100 result = MPI_SIMILAR;
107 int Group::incl(int n, const int* ranks, MPI_Group* newgroup)
110 *newgroup = MPI_GROUP_EMPTY;
111 } else if (n == size_) {
113 if (this != MPI_COMM_WORLD->group() && this != MPI_COMM_SELF->group() && this != MPI_GROUP_EMPTY)
116 *newgroup = new Group(n);
117 for (int i = 0; i < n; i++) {
118 s4u::Actor* actor = this->actor(ranks[i]); // ranks[] was passed as a param!
119 (*newgroup)->set_mapping(actor, i);
125 int Group::group_union(MPI_Group group2, MPI_Group* newgroup)
128 int size2 = group2->size();
129 for (int i = 0; i < size2; i++) {
130 s4u::Actor* actor = group2->actor(i);
131 int proc1 = this->rank(actor);
132 if (proc1 == MPI_UNDEFINED) {
137 *newgroup = MPI_GROUP_EMPTY;
139 *newgroup = new Group(size1);
140 size2 = this->size();
141 for (int i = 0; i < size2; i++) {
142 s4u::Actor* actor1 = this->actor(i);
143 (*newgroup)->set_mapping(actor1, i);
145 for (int i = size2; i < size1; i++) {
146 s4u::Actor* actor = group2->actor(i - size2);
147 (*newgroup)->set_mapping(actor, i);
153 int Group::intersection(MPI_Group group2, MPI_Group* newgroup)
155 int size2 = group2->size();
156 for (int i = 0; i < size2; i++) {
157 s4u::Actor* actor = group2->actor(i);
158 int proc1 = this->rank(actor);
159 if (proc1 == MPI_UNDEFINED) {
164 *newgroup = MPI_GROUP_EMPTY;
166 *newgroup = new Group(size2);
168 for (int i = 0; i < group2->size(); i++) {
169 s4u::Actor* actor = group2->actor(i);
170 int proc1 = this->rank(actor);
171 if (proc1 != MPI_UNDEFINED) {
172 (*newgroup)->set_mapping(actor, j);
180 int Group::difference(MPI_Group group2, MPI_Group* newgroup)
184 for (int i = 0; i < size2; i++) {
185 s4u::Actor* actor = this->actor(i);
186 int proc2 = group2->rank(actor);
187 if (proc2 != MPI_UNDEFINED) {
192 *newgroup = MPI_GROUP_EMPTY;
194 *newgroup = new Group(newsize);
195 for (int i = 0; i < size2; i++) {
196 s4u::Actor* actor = this->actor(i);
197 int proc2 = group2->rank(actor);
198 if (proc2 == MPI_UNDEFINED) {
199 (*newgroup)->set_mapping(actor, i);
206 int Group::excl(int n, const int *ranks, MPI_Group * newgroup){
208 int newsize = oldsize - n;
209 *newgroup = new Group(newsize);
210 int* to_exclude = new int[size_];
211 for (int i = 0; i < oldsize; i++)
213 for (int i = 0; i < n; i++)
214 to_exclude[ranks[i]]=1;
216 for (int i = 0; i < oldsize; i++) {
217 if(to_exclude[i]==0){
218 s4u::Actor* actor = this->actor(i);
219 (*newgroup)->set_mapping(actor, j);
228 static bool is_rank_in_range(int rank, int first, int last)
236 int Group::range_incl(int n, int ranges[][3], MPI_Group * newgroup){
238 for (int i = 0; i < n; i++) {
239 for (int rank = ranges[i][0]; /* First */
240 rank >= 0 && rank < size_; /* Last */
243 if(rank == ranges[i][1]){/*already last ?*/
246 rank += ranges[i][2]; /* Stride */
247 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
251 *newgroup = new Group(newsize);
253 for (int i = 0; i < n; i++) {
254 for (int rank = ranges[i][0]; /* First */
255 rank >= 0 && rank < size_; /* Last */
257 s4u::Actor* actor = this->actor(rank);
258 (*newgroup)->set_mapping(actor, j);
260 if(rank == ranges[i][1]){/*already last ?*/
263 rank += ranges[i][2]; /* Stride */
264 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
271 int Group::range_excl(int n, int ranges[][3], MPI_Group * newgroup){
273 for (int i = 0; i < n; i++) {
274 for (int rank = ranges[i][0]; /* First */
275 rank >= 0 && rank < size_; /* Last */
278 if(rank == ranges[i][1]){/*already last ?*/
281 rank += ranges[i][2]; /* Stride */
282 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
287 *newgroup = MPI_GROUP_EMPTY;
289 *newgroup = new Group(newsize);
292 while (newrank < newsize) {
294 for (int i = 0; i < n; i++) {
295 for (int rank = ranges[i][0]; rank >= 0 && rank < size_;) {
300 if(rank == ranges[i][1]){/*already last ?*/
303 rank += ranges[i][2]; /* Stride */
304 if (not is_rank_in_range(rank, ranges[i][0], ranges[i][1]))
309 s4u::Actor* actor = this->actor(oldrank);
310 (*newgroup)->set_mapping(actor, newrank);
319 MPI_Group Group::f2c(int id) {
321 return MPI_GROUP_EMPTY;
322 } else if(F2C::f2c_lookup() != nullptr && id >= 0) {
324 return static_cast<MPI_Group>(F2C::f2c_lookup()->at(get_key(key, id)));
326 return static_cast<MPI_Group>(MPI_GROUP_NULL);