Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
13d0d00fedee407b20249c9ff4c845e06a4c1408
[simgrid.git] / src / smpi / mpi / smpi_group.cpp
1 /* Copyright (c) 2010-2021. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "simgrid/s4u/Actor.hpp"
7 #include "smpi_group.hpp"
8 #include "smpi_comm.hpp"
9 #include <string>
10
11 simgrid::smpi::Group smpi_MPI_GROUP_EMPTY;
12
13 namespace simgrid{
14 namespace smpi{
15
16 Group::Group(const Group* origin)
17 {
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_;
21   }
22 }
23
24 void Group::set_mapping(aid_t pid, int rank)
25 {
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;
31   }
32 }
33
34 int Group::rank(aid_t pid) const
35 {
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;
42     }
43   }
44   return res;
45 }
46
47 aid_t Group::actor_pid(int rank) const
48 {
49   return (0 <= rank && rank < size()) ? rank_to_pid_map_[rank] : -1;
50 }
51
52 void Group::ref()
53 {
54   refcount_++;
55 }
56
57 void Group::unref(Group* group)
58 {
59   group->refcount_--;
60   if (group->refcount_ <= 0) {
61     if (simgrid::smpi::F2C::lookup() != nullptr)
62       F2C::free_f(group->f2c_id());
63     delete group;
64   }
65 }
66
67 int Group::compare(const Group* group2) const
68 {
69   int result;
70
71   result = MPI_IDENT;
72   if (size() != group2->size()) {
73     result = MPI_UNEQUAL;
74   } else {
75     for (int i = 0; i < size(); i++) {
76       int rank = group2->rank(actor_pid(i));
77       if (rank == MPI_UNDEFINED) {
78         result = MPI_UNEQUAL;
79         break;
80       }
81       if (rank != i) {
82         result = MPI_SIMILAR;
83       }
84     }
85   }
86   return result;
87 }
88
89 int Group::incl(int n, const int* ranks, MPI_Group* newgroup) const
90 {
91   if (n == 0) {
92     *newgroup = MPI_GROUP_EMPTY;
93     return MPI_SUCCESS;
94   }
95
96   *newgroup = new Group(n);
97   for (int i = 0; i < n; i++) {
98     aid_t actor = this->actor_pid(ranks[i]);
99     (*newgroup)->set_mapping(actor, i);
100   }
101   (*newgroup)->add_f();
102   return MPI_SUCCESS;
103 }
104
105 int Group::incl(const std::vector<int>& ranks, MPI_Group* newgroup) const
106 {
107   return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
108 }
109
110 int Group::excl(const std::vector<bool>& excl_map, MPI_Group* newgroup) const
111 {
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++)
115     if (not excl_map[i])
116       ranks.push_back(i);
117   return incl(static_cast<int>(ranks.size()), ranks.data(), newgroup);
118 }
119
120 int Group::group_union(const Group* group2, MPI_Group* newgroup) const
121 {
122   std::vector<int> ranks2;
123   for (int i = 0; i < group2->size(); i++) {
124     aid_t actor = group2->actor_pid(i);
125     if (rank(actor) == MPI_UNDEFINED)
126       ranks2.push_back(i);
127   }
128
129   int newsize = size() + static_cast<int>(ranks2.size());
130   if (newsize == 0) {
131     *newgroup = MPI_GROUP_EMPTY;
132     return MPI_SUCCESS;
133   }
134
135   *newgroup = new Group(newsize);
136   int i;
137   for (i = 0; i < size(); i++) {
138     aid_t actor1 = actor_pid(i);
139     (*newgroup)->set_mapping(actor1, i);
140   }
141   for (int j : ranks2) {
142     aid_t actor2 = group2->actor_pid(j);
143     (*newgroup)->set_mapping(actor2, i);
144     i++;
145   }
146   (*newgroup)->add_f();
147   return MPI_SUCCESS;
148 }
149
150 int Group::intersection(const Group* group2, MPI_Group* newgroup) const
151 {
152   std::vector<int> ranks2;
153   for (int i = 0; i < group2->size(); i++) {
154     aid_t actor = group2->actor_pid(i);
155     if (rank(actor) != MPI_UNDEFINED)
156       ranks2.push_back(i);
157   }
158   return group2->incl(ranks2, newgroup);
159 }
160
161 int Group::difference(const Group* group2, MPI_Group* newgroup) const
162 {
163   std::vector<int> ranks;
164   for (int i = 0; i < size(); i++) {
165     aid_t actor = this->actor_pid(i);
166     if (group2->rank(actor) == MPI_UNDEFINED)
167       ranks.push_back(i);
168   }
169   return this->incl(ranks, newgroup);
170 }
171
172 int Group::excl(int n, const int* ranks, MPI_Group* newgroup) const
173 {
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);
178 }
179
180 static bool is_rank_in_range(int rank, int first, int last)
181 {
182   return (first <= rank && rank <= last) || (first >= rank && rank >= last);
183 }
184
185 int Group::range_incl(int n, const int ranges[][3], MPI_Group* newgroup) const
186 {
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]);
190          j += ranges[i][2])
191       ranks.push_back(j);
192   }
193   return this->incl(ranks, newgroup);
194 }
195
196 int Group::range_excl(int n, const int ranges[][3], MPI_Group* newgroup) const
197 {
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]);
201          j += ranges[i][2])
202       to_excl[j] = true;
203   }
204   return this->excl(to_excl, newgroup);
205 }
206
207 MPI_Group Group::f2c(int id) {
208   if(id == -2) {
209     return MPI_GROUP_EMPTY;
210   } else if (F2C::lookup() != nullptr && id >= 0) {
211     return static_cast<MPI_Group>(F2C::lookup()->at(id));
212   } else {
213     return MPI_GROUP_NULL;
214   }
215 }
216
217 }
218 }