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 rank_to_pid_map_ = origin->rank_to_pid_map_;
20 pid_to_rank_map_ = origin->pid_to_rank_map_;
24 void Group::set_mapping(aid_t pid, int rank)
26 if (0 <= rank && rank < size()) {
27 if (static_cast<size_t>(pid) >= pid_to_rank_map_.size())
28 pid_to_rank_map_.resize(pid + 1, MPI_UNDEFINED);
29 rank_to_pid_map_[rank] = pid;
30 pid_to_rank_map_[pid] = rank;
34 int Group::rank(aid_t pid) const
36 int res = static_cast<size_t>(pid) < pid_to_rank_map_.size() ? pid_to_rank_map_[pid] : MPI_UNDEFINED;
37 if (res == MPI_UNDEFINED) {
38 // I'm not in the communicator ... but maybe my parent is?
39 if (auto parent = s4u::Actor::by_pid(pid)) {
40 aid_t ppid = parent->get_ppid();
41 res = static_cast<size_t>(ppid) < pid_to_rank_map_.size() ? pid_to_rank_map_[ppid] : MPI_UNDEFINED;
47 aid_t Group::actor(int rank) const
49 return (0 <= rank && rank < size()) ? rank_to_pid_map_[rank] : -1;
57 void Group::unref(Group* group)
60 if (group->refcount_ <= 0) {
61 if (simgrid::smpi::F2C::lookup() != nullptr)
62 F2C::free_f(group->f2c_id());
67 int Group::compare(const Group* group2) const
72 if (size() != group2->size()) {
75 for (int i = 0; i < size(); i++) {
76 int rank = group2->rank(actor(i));
77 if (rank == MPI_UNDEFINED) {
89 int Group::incl(int n, const int* ranks, MPI_Group* newgroup) const
92 *newgroup = MPI_GROUP_EMPTY;
96 *newgroup = new Group(n);
97 for (int i = 0; i < n; i++) {
98 aid_t actor = this->actor(ranks[i]);
99 (*newgroup)->set_mapping(actor, i);
101 (*newgroup)->add_f();
105 int Group::incl(const std::vector<int>& ranks, MPI_Group* newgroup) const
107 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
110 int Group::excl(const std::vector<bool>& excl_map, MPI_Group* newgroup) const
112 xbt_assert(static_cast<int>(excl_map.size()) == size());
113 std::vector<int> ranks;
114 for (int i = 0; i < static_cast<int>(excl_map.size()); i++)
117 return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
120 int Group::group_union(const Group* group2, MPI_Group* newgroup) const
122 std::vector<int> ranks2;
123 for (int i = 0; i < group2->size(); i++) {
124 aid_t actor = group2->actor(i);
125 if (rank(actor) == MPI_UNDEFINED)
129 int newsize = size() + static_cast<int>(ranks2.size());
131 *newgroup = MPI_GROUP_EMPTY;
135 *newgroup = new Group(newsize);
137 for (i = 0; i < size(); i++) {
138 aid_t actor1 = actor(i);
139 (*newgroup)->set_mapping(actor1, i);
141 for (int j : ranks2) {
142 aid_t actor2 = group2->actor(j);
143 (*newgroup)->set_mapping(actor2, i);
146 (*newgroup)->add_f();
150 int Group::intersection(const Group* group2, MPI_Group* newgroup) const
152 std::vector<int> ranks2;
153 for (int i = 0; i < group2->size(); i++) {
154 aid_t actor = group2->actor(i);
155 if (rank(actor) != MPI_UNDEFINED)
158 return group2->incl(ranks2, newgroup);
161 int Group::difference(const Group* group2, MPI_Group* newgroup) const
163 std::vector<int> ranks;
164 for (int i = 0; i < size(); i++) {
165 aid_t actor = this->actor(i);
166 if (group2->rank(actor) == MPI_UNDEFINED)
169 return this->incl(ranks, newgroup);
172 int Group::excl(int n, const int* ranks, MPI_Group* newgroup) const
174 std::vector<bool> to_excl(size(), false);
175 for (int i = 0; i < n; i++)
176 to_excl[ranks[i]] = true;
177 return this->excl(to_excl, newgroup);
180 static bool is_rank_in_range(int rank, int first, int last)
182 return (first <= rank && rank <= last) || (first >= rank && rank >= last);
185 int Group::range_incl(int n, const int ranges[][3], MPI_Group* newgroup) const
187 std::vector<int> ranks;
188 for (int i = 0; i < n; i++) {
189 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
193 return this->incl(ranks, newgroup);
196 int Group::range_excl(int n, const int ranges[][3], MPI_Group* newgroup) const
198 std::vector<bool> to_excl(size(), false);
199 for (int i = 0; i < n; i++) {
200 for (int j = ranges[i][0]; j >= 0 && j < size() && is_rank_in_range(j, ranges[i][0], ranges[i][1]);
204 return this->excl(to_excl, newgroup);
207 MPI_Group Group::f2c(int id) {
209 return MPI_GROUP_EMPTY;
210 } else if (F2C::lookup() != nullptr && id >= 0) {
211 return static_cast<MPI_Group>(F2C::lookup()->at(id));
213 return MPI_GROUP_NULL;