CXXGraph  0.2.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: AGPL v3.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 <mutex>
31 #include <vector>
32 #include <set>
33 
34 namespace CXXGRAPH
35 {
36  namespace PARTITIONING
37  {
38  template <typename T>
40  {
41  private:
42  std::map<int, CoordinatedRecord<T>> record_map;
43  std::vector<int> machines_load_edges;
44  std::vector<int> machines_load_vertices;
45  PartitionMap<T> partition_map;
46  Globals GLOBALS;
47  int MAX_LOAD;
48  std::mutex* machines_load_edges_mutex;
49  std::mutex* machines_load_vertices_mutex;
50  //DatWriter out; //to print the final partition of each edge
51  public:
54 
55  Record<T> &getRecord(int x);
56  int getMachineLoad(int m);
57  void incrementMachineLoad(int m, const Edge<T> *e);
58  int getMinLoad();
59  int getMaxLoad();
60  std::vector<int> getMachines_load();
61  int getTotalReplicas();
62  int getNumVertices();
63  std::set<int> getVertexIds();
64 
65  void incrementMachineLoadVertices(int m);
66  std::vector<int> getMachines_loadVertices();
67  PartitionMap<T> getPartitionMap();
68  };
69  template <typename T>
71  {
72  machines_load_edges_mutex = new std::mutex();
73  machines_load_vertices_mutex = new std::mutex();
74  //this->GLOBALS = G;
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] = new PARTITIONING::Partition<T>(i);
80  }
81  MAX_LOAD = 0;
82  }
83  template <typename T>
84  CoordinatedPartitionState<T>::~CoordinatedPartitionState()
85  {
86  //TODOOOOOOOOOOOO
87  //if (machines_load_edges_mutex != NULL){
88  // delete machines_load_edges_mutex;
89  //}
90  //if (machines_load_vertices_mutex != NULL){
91  // delete machines_load_vertices_mutex;
92  //}
93  }
94  template <typename T>
95  Record<T> &CoordinatedPartitionState<T>::getRecord(int x)
96  {
97  if (record_map.find(x) == record_map.end())
98  {
99  std::cout << "Record " << x << " not found" << std::endl;
100  record_map[x] = CoordinatedRecord<T>();
101  }
102  std::cout << "Return Record " << x << std::endl;
103  return record_map.at(x);
104  }
105  template <typename T>
106  int CoordinatedPartitionState<T>::getMachineLoad(int m)
107  {
108  std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
109  return machines_load_edges.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  }
124  template <typename T>
125  int CoordinatedPartitionState<T>::getMinLoad()
126  {
127  std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
128  int MIN_LOAD = std::numeric_limits<int>::max();
129  auto machines_load_edges_it = machines_load_edges.begin();
130  for (machines_load_edges_it; machines_load_edges_it != machines_load_edges.end(); ++machines_load_edges_it)
131  {
132  int loadi = *machines_load_edges_it;
133  if (loadi < MIN_LOAD)
134  {
135  MIN_LOAD = loadi;
136  }
137  }
138  return MIN_LOAD;
139  }
140  template <typename T>
141  int CoordinatedPartitionState<T>::getMaxLoad()
142  {
143  return MAX_LOAD;
144  }
145  template <typename T>
146  std::vector<int> CoordinatedPartitionState<T>::getMachines_load()
147  {
148  std::lock_guard<std::mutex> lock(*machines_load_edges_mutex);
149  std::vector<int> result;
150  for (int i = 0; i < machines_load_edges.size(); i++)
151  {
152  result.push_back(machines_load_edges[i]);
153  }
154  return result;
155  }
156  template <typename T>
157  int CoordinatedPartitionState<T>::getTotalReplicas()
158  {
159  //TODO
160  int result = 0;
161  auto record_map_it = record_map.begin();
162  for (record_map_it; record_map_it != record_map.end(); ++record_map_it)
163  {
164  int r = record_map_it->second.getReplicas();
165  if (r > 0)
166  {
167  result += r;
168  }
169  else
170  {
171  result++;
172  }
173  }
174  return result;
175  }
176  template <typename T>
177  int CoordinatedPartitionState<T>::getNumVertices()
178  {
179  return record_map.size();
180  }
181  template <typename T>
182  std::set<int> CoordinatedPartitionState<T>::getVertexIds()
183  {
184  //if (GLOBALS.OUTPUT_FILE_NAME!=null){ out.close(); }
185  std::set<int> result;
186  auto record_map_it = record_map.begin();
187  for (record_map_it; record_map_it != record_map.end(); ++record_map_it)
188  {
189  result.insert(record_map_it->first);
190  }
191  return result;
192  }
193  template <typename T>
194  void CoordinatedPartitionState<T>::incrementMachineLoadVertices(int m)
195  {
196  std::lock_guard<std::mutex> lock(*machines_load_vertices_mutex);
197  machines_load_vertices[m] = machines_load_vertices[m] + 1;
198  }
199  template <typename T>
200  std::vector<int> CoordinatedPartitionState<T>::getMachines_loadVertices()
201  {
202  std::lock_guard<std::mutex> lock(*machines_load_vertices_mutex);
203  std::vector<int> result;
204  for (int i = 0; i < machines_load_vertices.size(); i++)
205  {
206  result.push_back(machines_load_vertices.at(i));
207  }
208  return result;
209  }
210 
211  template<typename T>
212  PartitionMap<T> CoordinatedPartitionState<T>::getPartitionMap()
213  {
214  return partition_map;
215  }
216  }
217 }
218 
219 #endif // __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__
Definition: Edge.hpp:39
Definition: CoordinatedPartitionState.hpp:40
Definition: Globals.hpp:32
Definition: PartitionState.hpp:33
Definition: Partition.hpp:41
Definition: Record.hpp:31