1 /* Copyright (c) 2010-2021. 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"
11 simgrid::smpi::Group smpi_MPI_GROUP_EMPTY;
16 Group::Group(const Group* origin)
18 if (origin != MPI_GROUP_NULL && origin != MPI_GROUP_EMPTY) {
19 // FIXME: cheinrich: There is no such thing as an index any more; the two maps should be removed
20 index_to_rank_map_ = origin->index_to_rank_map_;
21 rank_to_actor_map_ = origin->rank_to_actor_map_;
22 actor_to_rank_map_ = origin->actor_to_rank_map_;
26 void Group::set_mapping(s4u::Actor* actor, int rank)
28 if (0 <= rank && rank < size()) {
29 int index = actor->get_pid();
30 if ((unsigned)index >= index_to_rank_map_.size())
31 index_to_rank_map_.resize(index + 1, MPI_UNDEFINED);
32 index_to_rank_map_[index] = rank;
33 rank_to_actor_map_[rank] = actor;
34 actor_to_rank_map_.insert({actor, rank});
38 int Group::rank(int index) const
41 if (0 <= index && (unsigned)index < index_to_rank_map_.size())
42 rank = index_to_rank_map_[index];
49 s4u::Actor* Group::actor(int rank) const
51 if (0 <= rank && rank < size())
52 return rank_to_actor_map_[rank];
57 int Group::rank(s4u::Actor* actor) const
59 auto iterator = actor_to_rank_map_.find(actor);
60 //I'm not in the communicator ... but maybe my parent is ?
61 if (iterator == actor_to_rank_map_.end())
62 iterator = actor_to_rank_map_.find(s4u::Actor::by_pid(actor->get_ppid()).get());
63 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
71 void Group::unref(Group* group)
74 if (group->refcount_ <= 0) {
75 if (simgrid::smpi::F2C::lookup() != nullptr)
76 F2C::free_f(group->f2c_id());
81 int Group::compare(const Group* group2) const
86 if (size() != group2->size()) {
89 for (int i = 0; i < size(); i++) {
90 int rank = group2->rank(actor(i));
91 if (rank == MPI_UNDEFINED) {
103 int Group::incl(int n, const int* ranks, MPI_Group* newgroup) const
106 *newgroup = MPI_GROUP_EMPTY;
110 *newgroup = new Group(n);
111 for (int i = 0; i < n; i++) {
112 s4u::Actor* actor = this->actor(ranks[i]);
113 (*newgroup)->set_mapping(actor, i);
115 (*newgroup)->add_f();
119 int Group::incl(const std::vector<int> ranks, MPI_Group* newgroup) const
121 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
124 int Group::excl(const std::vector<bool> excl_map, MPI_Group* newgroup) const
126 xbt_assert(static_cast<int>(excl_map.size()) == size());
127 std::vector<int> ranks;
128 for (int i = 0; i < static_cast<int>(excl_map.size()); i++)
131 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
134 int Group::group_union(const Group* group2, MPI_Group* newgroup) const
136 std::vector<int> ranks2;
137 for (int i = 0; i < group2->size(); i++) {
138 s4u::Actor* actor = group2->actor(i);
139 if (rank(actor) == MPI_UNDEFINED)
143 int newsize = size() + static_cast<int>(ranks2.size());
145 *newgroup = MPI_GROUP_EMPTY;
149 *newgroup = new Group(newsize);
151 for (i = 0; i < size(); i++) {
152 s4u::Actor* actor1 = actor(i);
153 (*newgroup)->set_mapping(actor1, i);
155 for (int j : ranks2) {
156 s4u::Actor* actor2 = group2->actor(j);
157 (*newgroup)->set_mapping(actor2, i);
160 (*newgroup)->add_f();
164 int Group::intersection(const Group* group2, MPI_Group* newgroup) const
166 std::vector<int> ranks2;
167 for (int i = 0; i < group2->size(); i++) {
168 s4u::Actor* actor = group2->actor(i);
169 if (rank(actor) != MPI_UNDEFINED)
172 return group2->incl(ranks2, newgroup);
175 int Group::difference(const Group* group2, MPI_Group* newgroup) const
177 std::vector<int> ranks;
178 for (int i = 0; i < size(); i++) {
179 s4u::Actor* actor = this->actor(i);
180 if (group2->rank(actor) == MPI_UNDEFINED)
183 return this->incl(ranks, newgroup);
186 int Group::excl(int n, const int* ranks, MPI_Group* newgroup) const
188 std::vector<bool> to_excl(size(), false);
189 for (int i = 0; i < n; i++)
190 to_excl[ranks[i]] = true;
191 return this->excl(to_excl, newgroup);
194 static bool is_rank_in_range(int rank, int first, int last)
196 return (first <= rank && rank <= last) || (first >= rank && rank >= last);
199 int Group::range_incl(int n, const int ranges[][3], MPI_Group* newgroup) const
201 std::vector<int> ranks;
202 for (int i = 0; i < n; i++) {
203 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
207 return this->incl(ranks, newgroup);
210 int Group::range_excl(int n, const int ranges[][3], MPI_Group* newgroup) const
212 std::vector<bool> to_excl(size(), false);
213 for (int i = 0; i < n; i++) {
214 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
218 return this->excl(to_excl, newgroup);
221 MPI_Group Group::f2c(int id) {
223 return MPI_GROUP_EMPTY;
224 } else if (F2C::lookup() != nullptr && id >= 0) {
225 return static_cast<MPI_Group>(F2C::lookup()->at(id));
227 return MPI_GROUP_NULL;