20 #ifndef __CXXGRAPH_PARTITIONING_GREEDYVERTEXCUT_H__
21 #define __CXXGRAPH_PARTITIONING_GREEDYVERTEXCUT_H__
25 #include "Partitioning/Utility/Globals.hpp"
26 #include "Edge/Edge.hpp"
27 #include "PartitionStrategy.hpp"
32 namespace PARTITIONING
56 GreedyVertexCut<T>::~GreedyVertexCut()
60 void GreedyVertexCut<T>::performStep(
const Edge<T> &e, PartitionState<T> &state)
62 int P = GLOBALS.numberOfPartition;
63 auto nodePair = e.getNodePair();
64 int u = nodePair.first->getId();
65 int v = nodePair.second->getId();
67 std::shared_ptr<Record<T>> u_record = state.getRecord(u);
68 std::shared_ptr<Record<T>> v_record = state.getRecord(v);
71 bool locks_taken =
false;
74 srand((
unsigned)time(NULL));
76 while (!u_record->getLock())
78 std::this_thread::sleep_for(std::chrono::microseconds(usleep_time));
79 usleep_time = (int)pow(usleep_time, 2);
84 while (!v_record->getLock())
86 std::this_thread::sleep_for(std::chrono::microseconds(usleep_time));
87 usleep_time = (int)pow(usleep_time, 2);
89 if (usleep_time > GLOBALS.SLEEP_LIMIT)
91 u_record->releaseLock();
92 performStep(e, state);
103 std::vector<int> candidates;
105 if (u_record->getPartitions().empty() && v_record->getPartitions().empty())
108 int min_load = INT_MAX;
110 for (
int i = 0; i < P; i++)
112 if (state.getMachineLoad(i) < min_load)
114 min_load = state.getMachineLoad(i);
118 candidates.push_back(machine_id);
120 else if (!u_record->getPartitions().empty() && v_record->getPartitions().empty())
123 int min_load = INT_MAX;
125 for (
auto &partition : u_record->getPartitions())
127 if (state.getMachineLoad(partition) < min_load)
129 min_load = state.getMachineLoad(partition);
130 machine_id = partition;
133 candidates.push_back(machine_id);
135 else if (u_record->getPartitions().empty() && !v_record->getPartitions().empty())
138 int min_load = INT_MAX;
140 for (
auto &partition : v_record->getPartitions())
142 if (state.getMachineLoad(partition) < min_load)
144 min_load = state.getMachineLoad(partition);
145 machine_id = partition;
148 candidates.push_back(machine_id);
150 else if (!u_record->getPartitions().empty() && !v_record->getPartitions().empty())
153 std::set<int> intersection;
154 std::set_intersection(u_record->getPartitions().begin(), u_record->getPartitions().end(),
155 v_record->getPartitions().begin(), v_record->getPartitions().end(),
156 std::inserter(intersection, intersection.begin()));
157 if (!intersection.empty())
160 int min_load = INT_MAX;
162 for (
auto &partition : intersection)
164 if (state.getMachineLoad(partition) < min_load)
166 min_load = state.getMachineLoad(partition);
167 machine_id = partition;
170 candidates.push_back(machine_id);
175 std::set<int> part_union;
176 std::set_union(u_record->getPartitions().begin(), u_record->getPartitions().end(),
177 v_record->getPartitions().begin(), v_record->getPartitions().end(),
178 std::inserter(part_union, part_union.begin()));
179 int min_load = INT_MAX;
181 for (
auto &partition : part_union)
183 if (state.getMachineLoad(partition) < min_load)
185 min_load = state.getMachineLoad(partition);
186 machine_id = partition;
189 candidates.push_back(machine_id);
194 if (candidates.empty())
196 std::cout <<
"ERROR: GreedyObjectiveFunction.performStep -> candidates.isEmpty()" << std::endl;
202 unsigned int seed = (
unsigned int)time(NULL);
204 int choice = rand_r(&seed) % candidates.size();
205 machine_id = candidates.at(choice);
208 CoordinatedPartitionState<T> &cord_state =
dynamic_cast<CoordinatedPartitionState<T> &
>(state);
210 if (!u_record->hasReplicaInPartition(machine_id))
212 u_record->addPartition(machine_id);
213 cord_state.incrementMachineLoadVertices(machine_id);
215 if (!v_record->hasReplicaInPartition(machine_id))
217 v_record->addPartition(machine_id);
218 cord_state.incrementMachineLoadVertices(machine_id);
221 catch (
const std::bad_cast &e)
225 if (!u_record->hasReplicaInPartition(machine_id))
227 u_record->addPartition(machine_id);
229 if (!v_record->hasReplicaInPartition(machine_id))
231 v_record->addPartition(machine_id);
236 state.incrementMachineLoad(machine_id, &e);
239 u_record->incrementDegree();
240 v_record->incrementDegree();
243 u_record->releaseLock();
244 v_record->releaseLock();
Definition: Globals.hpp:32
A Greedy Vertex Cut Partioning Algorithm.
Definition: GreedyVertexCut.hpp:40
Definition: PartitionState.hpp:33
Definition: PartitionStrategy.hpp:34