CXXGraph  0.1.6
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 <vector>
31 #include <set>
32 
33 namespace CXXGRAPH
34 {
35  namespace PARTITIONING
36  {
37  template <typename T>
39  {
40  private:
41  std::map<int, CoordinatedRecord<T>> record_map;
42  std::vector<std::atomic<int>> machines_load_edges;
43  std::vector<std::atomic<int>> machines_load_vertices;
44  Globals GLOBALS;
45  int MAX_LOAD;
46  //DatWriter out; //to print the final partition of each edge
47  public:
50 
51  Record<T> &getRecord(int x);
52  int getMachineLoad(int m);
53  void incrementMachineLoad(int m, Edge<T> &e);
54  int getMinLoad();
55  int getMaxLoad();
56  std::vector<int> getMachines_load();
57  int getTotalReplicas();
58  int getNumVertices();
59  std::set<int> getVertexIds();
60 
61  void incrementMachineLoadVertices(int m);
62  std::vector<int> getMachines_loadVertices();
63  };
64  template <typename T>
66  {
67  this->GLOBALS = G;
68  for (int i = 0; i < GLOBALS.numberOfPartition; i++)
69  {
70  machines_load_edges.push_back(std::atomic<int>(0));
71  machines_load_vertices.push_back(std::atomic<int>(0));
72  }
73  MAX_LOAD = 0;
74  }
75  template <typename T>
76  CoordinatedPartitionState<T>::~CoordinatedPartitionState()
77  {
78  }
79  template <typename T>
80  Record<T> &CoordinatedPartitionState<T>::getRecord(int x)
81  {
82  if (record_map.find(x) == record_map.end())
83  {
84  record_map[x] = CoordinatedRecord<T>();
85  }
86  return record_map.at(x);
87  }
88  template <typename T>
89  int CoordinatedPartitionState<T>::getMachineLoad(int m)
90  {
91  return machines_load_edges.at(m);
92  }
93  template <typename T>
94  void CoordinatedPartitionState<T>::incrementMachineLoad(int m, Edge<T> &e)
95  {
96  machines_load_edges[m] = machines_load_edges[m] + 1;
97  int new_value = machines_load_edges.at(m);
98  if (new_value > MAX_LOAD)
99  {
100  MAX_LOAD = new_value;
101  }
102  }
103  template <typename T>
104  int CoordinatedPartitionState<T>::getMinLoad()
105  {
106  int MIN_LOAD = std::numeric_limits<int>::max();
107  auto machines_load_edges_it = machines_load_edges.begin();
108  for (machines_load_edges_it; machines_load_edges_it != machines_load_edges.end(); ++machines_load_edges_it)
109  {
110  int loadi = *machines_load_edges_it;
111  if (loadi < MIN_LOAD)
112  {
113  MIN_LOAD = loadi;
114  }
115  }
116  return MIN_LOAD;
117  }
118  template <typename T>
119  int CoordinatedPartitionState<T>::getMaxLoad()
120  {
121  return MAX_LOAD;
122  }
123  template <typename T>
124  std::vector<int> CoordinatedPartitionState<T>::getMachines_load()
125  {
126  std::vector<int> result;
127  for (int i = 0; i < machines_load_edges.size(); i++)
128  {
129  result.push_back(machines_load_edges[i]);
130  }
131  return result;
132  }
133  template <typename T>
134  int CoordinatedPartitionState<T>::getTotalReplicas()
135  {
136  //TODO
137  int result = 0;
138  auto record_map_it = record_map.begin();
139  for (record_map_it; record_map_it != record_map.end(); ++record_map_it)
140  {
141  int r = record_map_it->second.getReplicas();
142  if (r > 0)
143  {
144  result += r;
145  }
146  else
147  {
148  result++;
149  }
150  }
151  return result;
152  }
153  template <typename T>
154  int CoordinatedPartitionState<T>::getNumVertices()
155  {
156  return record_map.size();
157  }
158  template <typename T>
159  std::set<int> CoordinatedPartitionState<T>::getVertexIds()
160  {
161  //if (GLOBALS.OUTPUT_FILE_NAME!=null){ out.close(); }
162  std::set<int> result;
163  auto record_map_it = record_map.begin();
164  for (record_map_it; record_map_it != record_map.end(); ++record_map_it)
165  {
166  result.insert(record_map_it->first);
167  }
168  return result;
169  }
170  template <typename T>
171  void CoordinatedPartitionState<T>::incrementMachineLoadVertices(int m)
172  {
173  machines_load_vertices[m] = machines_load_vertices[m] + 1;
174  }
175  template <typename T>
176  std::vector<int> CoordinatedPartitionState<T>::getMachines_loadVertices()
177  {
178  std::vector<int> result;
179  for (int i = 0; i < machines_load_vertices.size(); i++)
180  {
181  result.push_back(machines_load_vertices.at(i));
182  }
183  return result;
184  }
185  }
186 }
187 
188 #endif // __CXXGRAPH_PARTITIONING_COORDINATEDPARTITIONSTATE_H__
Definition: Edge.hpp:40
Definition: CoordinatedPartitionState.hpp:39
Definition: Globals.hpp:32
Definition: PartitionState.hpp:31
Definition: Record.hpp:29