CXXGraph  0.1.6
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 
34 namespace CXXGRAPH
35 {
36  namespace PARTITIONING
37  {
38  template <typename T>
40  {
41  private:
42  std::vector<Edge<T>> dataset;
43  PartitionStrategy<T> algorithm;
44  Globals GLOBALS;
45 
46  CoordinatedPartitionState<T> startCoordinated();
47 
48  public:
49  Partitioner(std::vector<Edge<T>> &dataset, Globals &G);
50  ~Partitioner();
51 
52  CoordinatedPartitionState<T> performCoordinatedPartition();
53  };
54  template <typename T>
55  Partitioner<T>::Partitioner(std::vector<Edge<T>> &dataset, Globals &G)
56  {
57  this->GLOBALS = G;
58  this->dataset = dataset;
59  if (GLOBALS.partitionStategy == PartitionAlgorithm::HDRF_ALG)
60  {
61  algorithm = new HDRF<T>(GLOBALS);
62  }
63  }
64  template <typename T>
65  CoordinatedPartitionState<T> Partitioner<T>::startCoordinated()
66  {
67  CoordinatedPartitionState state(GLOBALS);
68  int processors = GLOBALS.threads;
69 
70  std::thread myThreads[processors];
71 
72  int n = dataset.size();
73  int subSize = n / processors + 1;
74  for (int t = 0; t < processors; t++)
75  {
76  int iStart = t * subSize;
77  int iEnd = std::min((t + 1) * subSize, n);
78  if (iEnd >= iStart)
79  {
80  std::vector<Edge<T>> list(dataset.begin() + iStart, dataset.begin() + iEnd);
81  Runnable x = PartitionerThread<T>(list, state, algorithm, new std::list<int>());
82  myThreads[t] = std::thread(&Runnable::run, &x);
83  }
84  }
85  for (int t = 0; t < processors; t++)
86  {
87  myThreads[t].join();
88  }
89  return state;
90  }
91  template <typename T>
92  Partitioner<T>::~Partitioner()
93  {
94  }
95  template <typename T>
96  CoordinatedPartitionState<T> Partitioner<T>::performCoordinatedPartition()
97  {
98  return startCoordinated();
99  }
100  }
101 }
102 
103 #endif // __CXXGRAPH_PARTITIONING_PARTITIONER_H__
Definition: Edge.hpp:40
Definition: CoordinatedPartitionState.hpp:39
Definition: Globals.hpp:32
Definition: HDRF.hpp:35
Definition: PartitionStrategy.hpp:34
Definition: Partitioner.hpp:40