fpmas 1.6
location_manager.h
Go to the documentation of this file.
1#ifndef FPMAS_LOCATION_MANAGER_H
2#define FPMAS_LOCATION_MANAGER_H
3
9
11#include "fpmas/utils/log.h"
12
13namespace fpmas { namespace graph {
14
16
20 template<typename T>
22 : public api::graph::LocationManager<T> {
23 public:
25
34
35 private:
36 // MPI communicators are likely to be owned by the parent
37 // DistributedGraph. In any case, their lifetimes must span the
38 // LocationManager lifetime.
40 IdMpi* id_mpi;
41 LocationMpi* location_mpi;
42 std::unordered_map<DistributedId, int> managed_nodes_locations;
43
44 NodeMap local_nodes;
45 NodeMap distant_nodes;
46 NodeMap new_local_nodes;
47
48 public:
57 : comm(&comm), id_mpi(&id_mpi), location_mpi(&location_mpi) {}
58
59 void addManagedNode(api::graph::DistributedNode<T>* node, int initial_location) override {
60 this->managed_nodes_locations[node->getId()] = initial_location;
61 }
62 void addManagedNode(DistributedId id, int initial_location) override {
63 this->managed_nodes_locations[id] = initial_location;
64 }
65
67 this->managed_nodes_locations.erase(node->getId());
68 }
69
70 void removeManagedNode(DistributedId id) override {
71 this->managed_nodes_locations.erase(id);
72 }
73
77
78 const NodeMap& getLocalNodes() const override {return local_nodes;}
79 const NodeMap& getDistantNodes() const override {return distant_nodes;}
80
81 void updateLocations() override;
82
83
84 std::unordered_map<DistributedId, int> getCurrentLocations() const override {
85 return managed_nodes_locations;
86 }
87 };
88
89 template<typename T>
91 node->setLocation(comm->getRank());
93 this->local_nodes.insert({node->getId(), node});
94 this->distant_nodes.erase(node->getId());
95 new_local_nodes.insert({node->getId(), node});
96 }
97
98 template<typename T>
101 this->local_nodes.erase(node->getId());
102 this->distant_nodes.insert({node->getId(), node});
103 for(auto edge : node->getOutgoingEdges()) {
104 edge->setState(LocationState::DISTANT);
105 }
106 for(auto edge : node->getIncomingEdges()) {
107 edge->setState(LocationState::DISTANT);
108 }
109 }
110
111 template<typename T>
113 switch(node->state()) {
115 local_nodes.erase(node->getId());
116 break;
118 distant_nodes.erase(node->getId());
119 break;
120 }
121 }
122
123 template<typename T>
125 FPMAS_LOGD(comm->getRank(), "LOCATION_MANAGER", "Updating node locations...", "");
126 // Useful types
127 typedef
128 std::unordered_map<int, std::vector<DistributedId>>
129 DistributedIdMap;
130 typedef
131 std::unordered_map<int, std::vector<std::pair<DistributedId, int>>>
132 LocationMap;
133
134 /*
135 * Step 1 : send updated locations to origins, and receive
136 * locations of this origin's nodes
137 */
138 DistributedIdMap exported_updated_locations;
139 for(auto node : new_local_nodes) {
140 if(node.first.rank() != comm->getRank()) {
141 // Notify node origin that node is currently on this proc
142 exported_updated_locations[node.first.rank()]
143 .push_back(node.first);
144 } else {
145 // No need to export the new node location to itself :
146 // just locally set this node location to this proc
147 managed_nodes_locations[node.first]
148 = comm->getRank();
149 }
150 }
151
152 // Export / import location updates of managed_nodes_locations
153 DistributedIdMap imported_updated_locations =
154 id_mpi->migrate(exported_updated_locations);
155 // Updates the managed_nodes_locations data
156 for(auto list : imported_updated_locations) {
157 for(auto node_id : list.second) {
158 managed_nodes_locations[node_id]
159 = list.first;
160 }
161 }
162
163 // If some distant node has this proc has origin, the current
164 // location has already been updated and won't be requested to this
165 // proc himself
166 for(auto node : distant_nodes) {
167 if(node.first.rank() == comm->getRank()) {
168 node.second->setLocation(
169 managed_nodes_locations[node.first]
170 );
171 }
172 }
173
174 /*
175 * Step 2 : ask for current locations of distant_nodes
176 */
177 DistributedIdMap location_requests;
178 for(auto node : distant_nodes) {
179 if(node.first.rank() != comm->getRank()) {
180 // For distant_nodes that has an origin different from this
181 // proc, asks for the current location of node to its
182 // origin proc
183 location_requests[node.first.rank()]
184 .push_back(node.first);
185 }
186 }
187 // Export / import requests
188 DistributedIdMap imported_location_requests
189 = id_mpi->migrate(location_requests);
190
191 // Builds requests response
192 LocationMap exported_locations;
193 for(auto list : imported_location_requests) {
194 for(auto node_id : list.second) {
195 // for each requested node location, respond a tuple
196 // {node_id, currentLocation} to the requesting proc
197 exported_locations[list.first]
198 .push_back({node_id, managed_nodes_locations[node_id]});
199 }
200 }
201
202 // Import / export responses
203 LocationMap imported_locations
204 = location_mpi->migrate(exported_locations);
205
206 // Finally, updates the distant_nodes locations from the requests
207 // responses
208 for(auto list : imported_locations) {
209 for(auto location : list.second) {
210 distant_nodes.find(location.first)->second->setLocation(location.second);
211 }
212 }
213 new_local_nodes.clear();
214 FPMAS_LOGD(comm->getRank(), "LOCATION_MANAGER", "Node locations updated.", "");
215 }
216}}
217
218namespace nlohmann {
232 template<typename T>
233 struct adl_serializer<fpmas::api::graph::LocationManager<T>> {
234
241 static void to_json(json& j, const fpmas::api::graph::LocationManager<T>& location_manager) {
242 for(auto item : location_manager.getCurrentLocations())
243 j.push_back({item.first, item.second});
244 }
245
257 static void from_json(const json& j, fpmas::api::graph::LocationManager<T>& location_manager) {
258 for(auto item : j) {
259 location_manager.addManagedNode(
260 item[0].get<DistributedId>(),
261 item[1].get<int>()
262 );
263 }
264 }
265
266 };
267}
268#endif
Definition: communication.h:251
Definition: communication.h:637
Definition: distributed_id.h:89
Definition: distributed_node.h:28
virtual LocationState state() const =0
virtual void setLocation(int location)=0
virtual void setState(LocationState state)=0
Definition: location_manager.h:34
virtual void addManagedNode(DistributedNode< T > *node, int initial_location)=0
virtual std::unordered_map< DistributedId, int > getCurrentLocations() const =0
fpmas::api::graph::Graph< DistributedNode< T >, DistributedEdge< T > >::NodeMap NodeMap
Definition: location_manager.h:39
virtual const std::vector< EdgeType * > getIncomingEdges() const =0
virtual IdType getId() const =0
virtual const std::vector< EdgeType * > getOutgoingEdges() const =0
Definition: location_manager.h:22
api::communication::TypedMpi< std::pair< DistributedId, int > > LocationMpi
Definition: location_manager.h:33
void addManagedNode(api::graph::DistributedNode< T > *node, int initial_location) override
Definition: location_manager.h:59
const NodeMap & getLocalNodes() const override
Definition: location_manager.h:78
void removeManagedNode(api::graph::DistributedNode< T > *node) override
Definition: location_manager.h:66
void addManagedNode(DistributedId id, int initial_location) override
Definition: location_manager.h:62
api::communication::TypedMpi< DistributedId > IdMpi
Definition: location_manager.h:29
const NodeMap & getDistantNodes() const override
Definition: location_manager.h:79
void remove(api::graph::DistributedNode< T > *) override
Definition: location_manager.h:112
void setDistant(api::graph::DistributedNode< T > *) override
Definition: location_manager.h:99
void removeManagedNode(DistributedId id) override
Definition: location_manager.h:70
void setLocal(api::graph::DistributedNode< T > *) override
Definition: location_manager.h:90
std::unordered_map< DistributedId, int > getCurrentLocations() const override
Definition: location_manager.h:84
void updateLocations() override
Definition: location_manager.h:124
LocationManager(api::communication::MpiCommunicator &comm, IdMpi &id_mpi, LocationMpi &location_mpi)
Definition: location_manager.h:56
LocationState
Definition: location_state.h:15
@ DISTANT
Definition: location_state.h:28
@ LOCAL
Definition: location_state.h:21
Definition: fpmas.cpp:3
static void to_json(json &j, const fpmas::api::graph::LocationManager< T > &location_manager)
Definition: location_manager.h:241
static void from_json(const json &j, fpmas::api::graph::LocationManager< T > &location_manager)
Definition: location_manager.h:257