CXXGraph  0.4.0
CXXGraph is a header only, that manages the Graphs and it's algorithm in C++
Partitioner.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_PARTITIONER_H__
21 #define __CXXGRAPH_PARTITIONING_PARTITIONER_H__
22 
23 #pragma once
24 #include <vector>
25 #include "PartitionStrategy.hpp"
26 #include "Partitioning/Utility/Globals.hpp"
27 #include "Edge/Edge.hpp"
28 #include "CoordinatedPartitionState.hpp"
29 #include "Utility/Runnable.hpp"
30 #include "PartitionerThread.hpp"
31 #include "PartitionAlgorithm.hpp"
32 #include "HDRF.hpp"
33 #include "EdgeBalancedVertexCut.hpp"
34 #include "GreedyVertexCut.hpp"
35 #include "EBV.hpp"
36 
37 namespace CXXGRAPH
38 {
39  namespace PARTITIONING
40  {
41  template <typename T>
43  {
44  private:
45  const T_EdgeSet<T>* dataset = nullptr;
46  PartitionStrategy<T>* algorithm = nullptr;
47  Globals GLOBALS;
48 
49  CoordinatedPartitionState<T> startCoordinated();
50 
51 
52  public:
53  Partitioner(const T_EdgeSet<T> *dataset, Globals &G);
54  Partitioner(const Partitioner& other);
55  ~Partitioner();
56 
57  CoordinatedPartitionState<T> performCoordinatedPartition();
58  };
59  template <typename T>
60  Partitioner<T>::Partitioner(const T_EdgeSet<T> *dataset, Globals &G) : GLOBALS(G)
61  {
62  //this->GLOBALS = G;
63  this->dataset = dataset;
64  if (GLOBALS.partitionStategy == PartitionAlgorithm::HDRF_ALG)
65  {
66  algorithm = new HDRF<T>(GLOBALS);
67  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EDGEBALANCED_VC_ALG)
68  {
69  algorithm = new EdgeBalancedVertexCut<T>(GLOBALS);
70  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::GREEDY_VC_ALG)
71  {
72  algorithm = new GreedyVertexCut<T>(GLOBALS);
73  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EBV_ALG)
74  {
75  algorithm = new EBV<T>(GLOBALS);
76  }
77 
78  }
79 
80  template <typename T>
81  Partitioner<T>::Partitioner(const Partitioner& other){
82  this->dataset = other.dataset;
83  this->GLOBALS = other.GLOBALS;
84  if (GLOBALS.partitionStategy == PartitionAlgorithm::HDRF_ALG)
85  {
86  algorithm = new HDRF<T>(GLOBALS);
87  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EDGEBALANCED_VC_ALG)
88  {
89  algorithm = new EdgeBalancedVertexCut<T>(GLOBALS);
90  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::GREEDY_VC_ALG)
91  {
92  algorithm = new GreedyVertexCut<T>(GLOBALS);
93  } else if (GLOBALS.partitionStategy == PartitionAlgorithm::EBV_ALG)
94  {
95  algorithm = new EBV<T>(GLOBALS);
96  }
97  }
98 
99  template <typename T>
100  CoordinatedPartitionState<T> Partitioner<T>::startCoordinated()
101  {
102  CoordinatedPartitionState<T> state(GLOBALS);
103  int processors = GLOBALS.threads;
104 
105  std::thread myThreads[processors];
106  std::shared_ptr<Runnable> myRunnable[processors];
107  std::vector<const Edge<T>*> list_vector[processors];
108  int n = dataset->size();
109  int subSize = n / processors + 1;
110  for (int t = 0; t < processors; ++t)
111  {
112  int iStart = t * subSize;
113  int iEnd = std::min((t + 1) * subSize, n);
114  if (iEnd >= iStart)
115  {
116  list_vector[t] = std::vector<const Edge<T>*>(std::next(dataset->begin(), iStart), std::next(dataset->begin(), iEnd));
117  myRunnable[t] = std::make_shared<PartitionerThread<T>>(list_vector[t], &state, algorithm);
118  myThreads[t] = std::thread(&Runnable::run, myRunnable[t].get());
119  }
120  }
121  for (int t = 0; t < processors; ++t)
122  {
123  if (myThreads[t].joinable()){
124  myThreads[t].join();
125  }
126  //if(myRunnable[t] != nullptr){
127  // delete myRunnable[t];
128  //}
129  }
130  /*
131  for (int t = 0; t < processors; ++t){
132  if (runnableList[t] != nullptr){
133  delete runnableList[t];
134  }
135  }
136  */
137  return state;
138  }
139  template <typename T>
140  Partitioner<T>::~Partitioner()
141  {
142  if(algorithm != nullptr)
143  {
144  delete algorithm;
145  }
146  }
147  template <typename T>
148  CoordinatedPartitionState<T> Partitioner<T>::performCoordinatedPartition()
149  {
150  return startCoordinated();
151  }
152 
153  }
154 }
155 
156 #endif // __CXXGRAPH_PARTITIONING_PARTITIONER_H__
Definition: CoordinatedPartitionState.hpp:41
Definition: Globals.hpp:32
A Vertex Cut Partioning Algorithm ( as described by this paper https://www.fabiopetroni....
Definition: HDRF.hpp:40
Definition: PartitionStrategy.hpp:34
Definition: Partitioner.hpp:43