| CXXGraph 0.4.0 CXXGraph is a header only, that manages the Graphs and it's algorithm in C++

CoordinatedPartitionState.hpp
1 /***********************************************************/
2 /*** ______ ____ ______ _ ***/
3 /*** / ___\ \/ /\ \/ / ___|_ __ __ _ _ __ | |__ ***/
4 /*** | | \ / \ / | _| '__/ _` | '_ \| '_ \ ***/
5 /*** | |___ / \ / \ |_| | | | (_| | |_) | | | | ***/
6 /*** \____/_/\_\/_/\_\____|_| \__,_| .__/|_| |_| ***/
7 /*** |_| ***/
8 /***********************************************************/
9 /*** Header-Only C++ Library for Graph ***/
10 /*** Representation and Algorithms ***/
11 /***********************************************************/
12 /*** Author: ZigRazor ***/
13 /*** E-Mail: zigrazor@gmail.com ***/
14 /***********************************************************/
15 /*** Collaboration: ----------- ***/
16 /***********************************************************/
17 /*** License: MPL v2.0 ***/
18 /***********************************************************/
19
20 #ifndef __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__
21 #define __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__
22
23 #pragma once
24
25 #include "Record.hpp"
26 #include "Edge/Edge.hpp"
27 #include "Partitioning/Utility/Globals.hpp"
28 #include "PartitionState.hpp"
29 #include "CoordinatedRecord.hpp"
30 #include <memory>
31 #include <mutex>
32 #include <vector>
33 #include <set>
34
35 namespace CXXGRAPH
36 {
37 namespace PARTITIONING
38 {
39 template <typename T>
40 class CoordinatedPartitionState : public PartitionState<T>
41 {
42 private:
43 std::map<int, std::shared_ptr<CoordinatedRecord<T>>> record_map;
44 std::vector<int> machines_load_edges;
45 std::vector<int> machines_load_vertices;
46 PartitionMap<T> partition_map;
47 Globals GLOBALS;
48 int MAX_LOAD;
49 std::shared_ptr<std::mutex> machines_load_edges_mutex = nullptr;
50 std::shared_ptr<std::mutex> machines_load_vertices_mutex = nullptr;
51 std::shared_ptr<std::mutex> record_map_mutex = nullptr;
52 //DatWriter out; //to print the final partition of each edge
53 public:
54 CoordinatedPartitionState(Globals &G);
55 ~CoordinatedPartitionState();
56
57 std::shared_ptr<Record<T>> getRecord(int x);
58 int getMachineLoad(int m);
59 int getMachineLoadVertices(int m);
60 void incrementMachineLoad(int m, const Edge<T> *e);
61 int getMinLoad();
62 int getMaxLoad();
63 std::vector<int> getMachines_load();
64 int getTotalReplicas();
65 int getNumVertices();
66 std::set<int> getVertexIds();
67
68 void incrementMachineLoadVertices(int m);
69 std::vector<int> getMachines_loadVertices();
70 const PartitionMap<T> &getPartitionMap();
71 };
72 template <typename T>
73 CoordinatedPartitionState<T>::CoordinatedPartitionState(Globals &G) : record_map(), GLOBALS(G), machines_load_edges_mutex(std::make_shared<std::mutex>()), machines_load_vertices_mutex(std::make_shared<std::mutex>()), record_map_mutex(std::make_shared<std::mutex>())
74 {
75 for (int i = 0; i < GLOBALS.numberOfPartition; ++i)
76 {
77 machines_load_edges.push_back(0);
78 machines_load_vertices.push_back(0);
79 partition_map[i] = std::make_shared<PARTITIONING::Partition<T>>(i);
80 }
81 MAX_LOAD = 0;
82 }
83 template <typename T>
84 CoordinatedPartitionState<T>::~CoordinatedPartitionState()
85 {
86 }
87 template <typename T>
88 std::shared_ptr<Record<T>> CoordinatedPartitionState<T>::getRecord(int x)
89 {
90 std::lock_guard<std::mutex> lock(*record_map_mutex);
91 if (record_map.find(x) == record_map.end())
92 {
93 record_map[x] = std::make_shared<CoordinatedRecord<T>>();
94 }
95 return record_map.at(x);
96 }
97
98 template <typename T>
99 int CoordinatedPartitionState<T>::getMachineLoad(int m)
100 {
101 std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
102 return machines_load_edges.at(m);
103 }
104
105 template <typename T>
106 int CoordinatedPartitionState<T>::getMachineLoadVertices(int m)
107 {
108 std::lock_guard<std::mutex> lock(*machines_load_vertices_mutex);
109 return machines_load_vertices.at(m);
110 }
111 template <typename T>
112 void CoordinatedPartitionState<T>::incrementMachineLoad(int m, const Edge<T> *e)
113 {
114 std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
115 machines_load_edges[m] = machines_load_edges[m] + 1;
116 int new_value = machines_load_edges.at(m);
117 if (new_value > MAX_LOAD)
118 {
119 MAX_LOAD = new_value;
120 }
121 partition_map[m]->addEdge(e);
122 }
123 template <typename T>
124 int CoordinatedPartitionState<T>::getMinLoad()
125 {
126 std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
127 int MIN_LOAD = std::numeric_limits<int>::max();
128 for (const auto &machines_load_edges_it : machines_load_edges)
129 {
130 int loadi = machines_load_edges_it;
131 if (loadi < MIN_LOAD)
132 {
133 MIN_LOAD = loadi;
134 }
135 }
136 return MIN_LOAD;
137 }
138 template <typename T>
139 int CoordinatedPartitionState<T>::getMaxLoad()
140 {
141 return MAX_LOAD;
142 }
143 template <typename T>
144 std::vector<int> CoordinatedPartitionState<T>::getMachines_load()
145 {
146 std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
147 std::vector<int> result;
148 for (const auto &machines_load_edges_it : machines_load_edges)
149 {
150 result.push_back(machines_load_edges_it);
151 }
152 return result;
153 }
154 template <typename T>
155 int CoordinatedPartitionState<T>::getTotalReplicas()
156 {
157 //TODO
158 std::lock_guard<std::mutex> lock(*record_map_mutex);
159 int result = 0;
160 for (const auto &record_map_it : record_map)
161 {
162 int r = record_map_it.second->getReplicas();
163 if (r > 0)
164 {
165 result += r;
166 }
167 else
168 {
169 result++;
170 }
171 }
172 return result;
173 }
174 template <typename T>
175 int CoordinatedPartitionState<T>::getNumVertices()
176 {
177 std::lock_guard<std::mutex> lock(*record_map_mutex);
178 return record_map.size();
179 }
180 template <typename T>
181 std::set<int> CoordinatedPartitionState<T>::getVertexIds()
182 {
183 std::lock_guard<std::mutex> lock(*record_map_mutex);
184 //if (GLOBALS.OUTPUT_FILE_NAME!=null){ out.close(); }
185 std::set<int> result;
186 for (const auto &record_map_it : record_map)
187 {
188 result.insert(record_map_it.first);
189 }
190 return result;
191 }
192 template <typename T>
193 void CoordinatedPartitionState<T>::incrementMachineLoadVertices(int m)
194 {
195 std::lock_guard<std::mutex> lock(*machines_load_vertices_mutex);
196 machines_load_vertices[m] = machines_load_vertices[m] + 1;
197 }
198 template <typename T>
199 std::vector<int> CoordinatedPartitionState<T>::getMachines_loadVertices()
200 {
201 std::lock_guard<std::mutex> lock(*machines_load_vertices_mutex);
202 std::vector<int> result;
203 for (const auto &machines_load_vertices_it : machines_load_vertices)
204 {
205 result.push_back(machines_load_vertices_it);
206 }
207 return result;
208 }
209
210 template <typename T>
211 const PartitionMap<T> &CoordinatedPartitionState<T>::getPartitionMap()
212 {
213 return partition_map;
214 }
215 }
216 }
217
218 #endif // __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__
CXXGRAPH::Edge
Definition: Edge.hpp:39
CXXGRAPH::PARTITIONING::CoordinatedPartitionState
Definition: CoordinatedPartitionState.hpp:41
CXXGRAPH::PARTITIONING::Globals
Definition: Globals.hpp:32
CXXGRAPH::PARTITIONING::PartitionState
Definition: PartitionState.hpp:33