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 pid_to_rank_map_ = origin->pid_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 aid_t pid = actor->get_pid();
30 if (static_cast<size_t>(pid) >= pid_to_rank_map_.size())
31 pid_to_rank_map_.resize(pid + 1, MPI_UNDEFINED);
32 pid_to_rank_map_[pid] = rank;
33 rank_to_actor_map_[rank] = actor;
34 actor_to_rank_map_.insert({actor, rank});
38 int Group::rank(aid_t pid) const
40 return static_cast<size_t>(pid) < pid_to_rank_map_.size() ? pid_to_rank_map_[pid] : MPI_UNDEFINED;
43 s4u::Actor* Group::actor(int rank) const
45 if (0 <= rank && rank < size())
46 return rank_to_actor_map_[rank];
51 int Group::rank(s4u::Actor* actor) const
53 auto iterator = actor_to_rank_map_.find(actor);
54 //I'm not in the communicator ... but maybe my parent is ?
55 if (iterator == actor_to_rank_map_.end())
56 iterator = actor_to_rank_map_.find(s4u::Actor::by_pid(actor->get_ppid()).get());
57 return (iterator == actor_to_rank_map_.end()) ? MPI_UNDEFINED : (*iterator).second;
65 void Group::unref(Group* group)
68 if (group->refcount_ <= 0) {
69 if (simgrid::smpi::F2C::lookup() != nullptr)
70 F2C::free_f(group->f2c_id());
75 int Group::compare(const Group* group2) const
80 if (size() != group2->size()) {
83 for (int i = 0; i < size(); i++) {
84 int rank = group2->rank(actor(i));
85 if (rank == MPI_UNDEFINED) {
97 int Group::incl(int n, const int* ranks, MPI_Group* newgroup) const
100 *newgroup = MPI_GROUP_EMPTY;
104 *newgroup = new Group(n);
105 for (int i = 0; i < n; i++) {
106 s4u::Actor* actor = this->actor(ranks[i]);
107 (*newgroup)->set_mapping(actor, i);
109 (*newgroup)->add_f();
113 int Group::incl(const std::vector<int>& ranks, MPI_Group* newgroup) const
115 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
118 int Group::excl(const std::vector<bool>& excl_map, MPI_Group* newgroup) const
120 xbt_assert(static_cast<int>(excl_map.size()) == size());
121 std::vector<int> ranks;
122 for (int i = 0; i < static_cast<int>(excl_map.size()); i++)
125 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
128 int Group::group_union(const Group* group2, MPI_Group* newgroup) const
130 std::vector<int> ranks2;
131 for (int i = 0; i < group2->size(); i++) {
132 s4u::Actor* actor = group2->actor(i);
133 if (rank(actor) == MPI_UNDEFINED)
137 int newsize = size() + static_cast<int>(ranks2.size());
139 *newgroup = MPI_GROUP_EMPTY;
143 *newgroup = new Group(newsize);
145 for (i = 0; i < size(); i++) {
146 s4u::Actor* actor1 = actor(i);
147 (*newgroup)->set_mapping(actor1, i);
149 for (int j : ranks2) {
150 s4u::Actor* actor2 = group2->actor(j);
151 (*newgroup)->set_mapping(actor2, i);
154 (*newgroup)->add_f();
158 int Group::intersection(const Group* group2, MPI_Group* newgroup) const
160 std::vector<int> ranks2;
161 for (int i = 0; i < group2->size(); i++) {
162 s4u::Actor* actor = group2->actor(i);
163 if (rank(actor) != MPI_UNDEFINED)
166 return group2->incl(ranks2, newgroup);
169 int Group::difference(const Group* group2, MPI_Group* newgroup) const
171 std::vector<int> ranks;
172 for (int i = 0; i < size(); i++) {
173 s4u::Actor* actor = this->actor(i);
174 if (group2->rank(actor) == MPI_UNDEFINED)
177 return this->incl(ranks, newgroup);
180 int Group::excl(int n, const int* ranks, MPI_Group* newgroup) const
182 std::vector<bool> to_excl(size(), false);
183 for (int i = 0; i < n; i++)
184 to_excl[ranks[i]] = true;
185 return this->excl(to_excl, newgroup);
188 static bool is_rank_in_range(int rank, int first, int last)
190 return (first <= rank && rank <= last) || (first >= rank && rank >= last);
193 int Group::range_incl(int n, const int ranges[][3], MPI_Group* newgroup) const
195 std::vector<int> ranks;
196 for (int i = 0; i < n; i++) {
197 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
201 return this->incl(ranks, newgroup);
204 int Group::range_excl(int n, const int ranges[][3], MPI_Group* newgroup) const
206 std::vector<bool> to_excl(size(), false);
207 for (int i = 0; i < n; i++) {
208 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
212 return this->excl(to_excl, newgroup);
215 MPI_Group Group::f2c(int id) {
217 return MPI_GROUP_EMPTY;
218 } else if (F2C::lookup() != nullptr && id >= 0) {
219 return static_cast<MPI_Group>(F2C::lookup()->at(id));
221 return MPI_GROUP_NULL;