Graybat  1.1
Graph Approach for Highly Generic Communication Schemes Based on Adaptive Topologies
GraphPartition.hpp
1 #pragma once
2 
3 #include <vector> /* std::vector */
4 #include <metis.h> /* idx_t, METIS_PartGraphKway */
5 
6 namespace graybat {
7 
8  namespace mapping {
9 
20  struct GraphPartition {
21 
23 
24  }
25 
26  GraphPartition(unsigned nParts)
27  : nParts(nParts){
28 
29  }
30 
38  template<typename T_Graph>
39  std::pair<std::vector<idx_t>, std::vector<idx_t> > toCompressedRowStorage(T_Graph &graph) {
40 
41  typedef typename T_Graph::Vertex Vertex;
42  typedef typename T_Graph::Edge Edge;
43 
44  unsigned i = 0;
45 
46  std::vector<idx_t> xadj(1,i);
47  std::vector<idx_t> adjncy;
48 
49 
50  for(Vertex v : graph.getVertices()){
51  for(Edge link : graph.getOutEdges(v)){
52  adjncy.push_back(link.target.id);
53  i++;
54 
55  }
56  xadj.push_back(i);
57 
58  }
59 
60  return std::make_pair(xadj, adjncy);
61  }
62 
63  template<typename T_Graph>
64  std::vector<typename T_Graph::Vertex> operator()(const unsigned processID, const unsigned processCount, T_Graph &graph){
65 
66  typedef typename T_Graph::Vertex Vertex;
67  std::vector<Vertex> myVertices;
68  auto csr = toCompressedRowStorage(graph);
69 
70  if(nParts == 0){
71  nParts = processCount;
72  }
73 
74  if(nParts == 1){
75  return graph.getVertices();
76  }
77 
78  idx_t nVertices = graph.getVertices().size();
79  idx_t nWeights = 1;
80  idx_t objval;
81  std::vector<idx_t> part(nVertices, 0);
82 
83  METIS_PartGraphKway(&nVertices, &nWeights,
84  csr.first.data(), csr.second.data(),
85  NULL, NULL, NULL, &nParts, NULL,
86  NULL, NULL,
87  &objval,
88  part.data());
89 
90 
91  for(unsigned part_i = 0; part_i < part.size(); part_i++){
92  if(part[part_i] == (int)processID){
93  myVertices.push_back(graph.getVertex(part_i));
94  }
95 
96  }
97 
98  return myVertices;
99  }
100 
101  private:
102  idx_t nParts;
103 
104  };
105 
106  } /* mapping */
107 
108 } /* graybat */
Definition: GraphPartition.hpp:20
Definition: BiStar.hpp:8
std::pair< std::vector< idx_t >, std::vector< idx_t > > toCompressedRowStorage(T_Graph &graph)
Definition: GraphPartition.hpp:39