Skip to content

Commit 76d4b89

Browse files
committed
connecting graph partition with sbg
1 parent b3cd191 commit 76d4b89

5 files changed

Lines changed: 112 additions & 61 deletions

File tree

algorithms/partitioner/weighted_sb_graph.cpp

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,69 @@
1616
1717
******************************************************************************/
1818

19-
2019
#include "weighted_sb_graph.hpp"
2120

22-
2321
using namespace std;
2422

25-
2623
namespace SBG {
2724

2825
namespace LIB {
2926

27+
WeightedSBGraph::WeightedSBGraph(WeightedSBGraph& graph)
28+
: SBG(graph), _node_weights(graph.get_node_weights()), _edge_costs(graph.get_edge_costs())
29+
{
30+
}
31+
32+
WeightedSBGraph::WeightedSBGraph(WeightedSBGraph&& graph)
33+
: SBG(graph), _node_weights(graph.get_node_weights()), _edge_costs(graph.get_edge_costs())
34+
{
35+
}
36+
37+
WeightedSBGraph::WeightedSBGraph(const Set& V, const PWMap& Vmap, const PWMap& map1, const PWMap& map2, const PWMap& Emap,
38+
const PWMap& subEmap)
39+
: SBG(V, Vmap, map1, map2, Emap, subEmap)
40+
{
41+
}
42+
43+
WeightedSBGraph::~WeightedSBGraph() = default;
44+
45+
void WeightedSBGraph::set_node_weights(NodeWeight& node_weights) { _node_weights = std::move(node_weights); }
46+
47+
NodeWeight WeightedSBGraph::get_node_weights() const { return _node_weights; }
48+
49+
void WeightedSBGraph::set_node_weight(const Set& node_set, int weight) { _node_weights[node_set] = weight; }
50+
51+
int WeightedSBGraph::get_node_weight(const Set& node_set) const { return _node_weights.at(node_set); }
52+
53+
void WeightedSBGraph::set_edge_costs(EdgeCost& edge_costs) { _edge_costs = std::move(edge_costs); }
54+
55+
EdgeCost WeightedSBGraph::get_edge_costs() const { return _edge_costs; }
56+
57+
void WeightedSBGraph::set_edge_cost(const Set& edge_set, unsigned cost) { _edge_costs[edge_set] = cost; }
58+
59+
unsigned WeightedSBGraph::get_edge_cost(const Set& edge_set) const { return _edge_costs.at(edge_set); }
60+
3061
ostream& operator<<(ostream& os, const WeightedSBGraph& graph)
3162
{
32-
os << SBG(graph);
63+
os << SBG(graph);
3364

34-
os << "node weight = << ";
35-
for (const auto& [set, weight] : graph.get_node_weights()) {
36-
os << set << "" << weight << ", ";
37-
}
65+
os << "node weight = << ";
66+
for (const auto& [set, weight] : graph.get_node_weights()) {
67+
os << set << "" << weight << ", ";
68+
}
3869

39-
os << "\b >>" << endl;
70+
os << "\b >>" << endl;
4071

41-
os << "edge costs = << ";
42-
for (const auto& [set, weight] : graph.get_edge_costs()) {
43-
os << set << "" << weight << ", ";
44-
}
72+
os << "edge costs = << ";
73+
for (const auto& [set, weight] : graph.get_edge_costs()) {
74+
os << set << "" << weight << ", ";
75+
}
4576

46-
os << "\b >>" << endl;
77+
os << "\b >>" << endl;
4778

48-
return os;
79+
return os;
4980
}
5081

51-
}
82+
} // namespace LIB
5283

53-
}
84+
} // namespace SBG

algorithms/partitioner/weighted_sb_graph.hpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include <map>
2222
#include <iostream>
2323

24-
#include "sbg/sbg.hpp"
25-
#include "sbg/set.hpp"
24+
#include <sbg/sbg.hpp>
25+
#include <sbg/set.hpp>
2626

2727
namespace SBG {
2828

@@ -41,39 +41,36 @@ using NodeWeight = std::map<Set, int, setCompare>;
4141
class WeightedSBGraph : public SBG
4242
{
4343
public:
44-
WeightedSBGraph(WeightedSBGraph& graph)
45-
: SBG(graph),
46-
_node_weights(graph.get_node_weights()),
47-
_edge_costs(graph.get_edge_costs())
48-
{}
49-
WeightedSBGraph(WeightedSBGraph&& graph)
50-
: SBG(graph),
51-
_node_weights(graph.get_node_weights()),
52-
_edge_costs(graph.get_edge_costs())
53-
{}
44+
WeightedSBGraph(WeightedSBGraph& graph);
45+
46+
WeightedSBGraph(WeightedSBGraph&& graph);
5447

5548
WeightedSBGraph(const Set &V, const PWMap &Vmap
5649
, const PWMap &map1, const PWMap &map2
57-
, const PWMap &Emap, const PWMap &subEmap)
58-
: SBG(V, Vmap, map1, map2, Emap, subEmap)
59-
{}
50+
, const PWMap &Emap, const PWMap &subEmap);
51+
52+
53+
WeightedSBGraph(const WeightedSBGraph&) = delete;
54+
WeightedSBGraph operator=(WeightedSBGraph&& other) = delete;
55+
56+
~WeightedSBGraph();
6057

61-
void set_node_weights(NodeWeight& node_weights) { _node_weights = std::move(node_weights); }
58+
void set_node_weights(NodeWeight& node_weights);
6259

63-
NodeWeight get_node_weights() const { return _node_weights; }
60+
NodeWeight get_node_weights() const;
6461

65-
void set_node_weight(const Set& node_set, int weight) { _node_weights[node_set] = weight; }
62+
void set_node_weight(const Set& node_set, int weight);
6663

67-
int get_node_weight(const Set& node_set) const { return _node_weights.at(node_set); }
64+
int get_node_weight(const Set& node_set) const;
6865

6966

70-
void set_edge_costs(EdgeCost& edge_costs) { _edge_costs = std::move(edge_costs); }
67+
void set_edge_costs(EdgeCost& edge_costs);
7168

72-
EdgeCost get_edge_costs() const { return _edge_costs; }
69+
EdgeCost get_edge_costs() const;
7370

74-
void set_edge_cost(const Set& edge_set, unsigned cost) { _edge_costs[edge_set] = cost; }
71+
void set_edge_cost(const Set& edge_set, unsigned cost);
7572

76-
unsigned get_edge_cost(const Set& edge_set) const { return _edge_costs.at(edge_set); }
73+
unsigned get_edge_cost(const Set& edge_set) const;
7774

7875
private:
7976
NodeWeight _node_weights;

test/partitioner/external_tools/graph_partitioner.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@
3737

3838
#include "graph_partitioner.hpp"
3939
#include <algorithms/partitioner/build_sb_graph.hpp>
40+
#include <algorithms/partitioner/kernighan_lin_partitioner.hpp>
41+
#include <algorithms/partitioner/partition_graph.hpp>
4042
#include <algorithms/partitioner/weighted_sb_graph.hpp>
4143

42-
constexpr const char *VALID_PARTITION_METHODS = "{ Scotch, Metis, HMetis, Kahip}";
44+
constexpr const char *VALID_PARTITION_METHODS = "{ Scotch, Metis, HMetis, Kahip, SBG }";
4345

4446
// Initialize the map outside the function
4547
static const std::unordered_map<std::string, PartitionMethod> PARTITION_METHOD_MAP = {{"Metis", PartitionMethod::Metis},
4648
{"HMetis", PartitionMethod::HMetis},
4749
{"Scotch", PartitionMethod::Scotch},
48-
{"Kahip", PartitionMethod::Kahip}};
50+
{"Kahip", PartitionMethod::Kahip},
51+
{"SBG", PartitionMethod::SBG}};
52+
53+
static std::unique_ptr<SBG::LIB::WeightedSBGraph> sbg_graph;
4954

5055
GraphPartitioner::GraphPartitioner(const std::string &name) : _name(name) { generateInputGraph(); }
5156

@@ -95,6 +100,9 @@ Partition GraphPartitioner::createPartition(const std::string &partition_method_
95100
case PartitionMethod::Kahip:
96101
partitionUsingKaHip(partition);
97102
break;
103+
case PartitionMethod::SBG:
104+
partitionUsingSBG();
105+
break;
98106
default:
99107
break;
100108
}
@@ -122,8 +130,9 @@ void GraphPartitioner::generateInputGraph()
122130

123131
void GraphPartitioner::readGraphFromJson()
124132
{
125-
auto sb_graph = sbg_partitioner::build_sb_graph(_name);
126-
readGraphFromSBG(sb_graph);
133+
auto temp_sbg_graph = sbg_partitioner::build_sb_graph(_name);
134+
sbg_graph.reset(new SBG::LIB::WeightedSBGraph(temp_sbg_graph));
135+
readGraphFromSBG();
127136
}
128137

129138
void GraphPartitioner::readGraph()
@@ -314,13 +323,23 @@ void GraphPartitioner::partitionUsingKaHip(Partition &partition)
314323
partition.values.data());
315324
}
316325

317-
void GraphPartitioner::readGraphFromSBG(const SBG::LIB::WeightedSBGraph &sbg_graph)
326+
void GraphPartitioner::partitionUsingSBG()
327+
{
328+
std::cout << "partitionUsingSBG" << std::endl;
329+
330+
auto partitions = sbg_partitioner::best_initial_partition(*sbg_graph, _nbr_parts, sbg_partitioner::InitialPartitionStrategy::ALL, false);
331+
// std::cout << "chosen partition " << partitions << std::endl;
332+
333+
sbg_partitioner::kl_sbg_imbalance_partitioner(*sbg_graph, partitions, _imbalance, false);
334+
}
335+
336+
void GraphPartitioner::readGraphFromSBG()
318337
{
319-
_nbr_vtxs = sbg_graph.V().cardinal();
320-
_edges = sbg_graph.E().cardinal();
338+
_nbr_vtxs = sbg_graph->V().cardinal();
339+
_edges = sbg_graph->E().cardinal();
321340
// asumming all setpiece are unidimensional and have step 1
322341
int max_node = -1;
323-
for (const auto &v : sbg_graph.V()) {
342+
for (const auto &v : sbg_graph->V()) {
324343
if (int(v.begin()[0].end()) > max_node) {
325344
max_node = int(v.begin()[0].end());
326345
}
@@ -330,11 +349,11 @@ void GraphPartitioner::readGraphFromSBG(const SBG::LIB::WeightedSBGraph &sbg_gra
330349

331350
for (int i = 0; i <= max_node; i++) {
332351
auto s = SBG::LIB::SET_FACT.createSet(SBG::LIB::Interval(i, 1, i));
333-
auto edges1 = sbg_graph.map1().preImage(s);
334-
auto nodes1 = sbg_graph.map2().image(edges1);
352+
auto edges1 = sbg_graph->map1().preImage(s);
353+
auto nodes1 = sbg_graph->map2().image(edges1);
335354

336-
auto edges2 = sbg_graph.map2().preImage(s);
337-
auto nodes2 = sbg_graph.map1().image(edges2);
355+
auto edges2 = sbg_graph->map2().preImage(s);
356+
auto nodes2 = sbg_graph->map1().image(edges2);
338357

339358
auto nodes = nodes1.cup(nodes2);
340359
auto nodes_vector = std::vector<int>();

test/partitioner/external_tools/graph_partitioner.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
1818
*****************************************************************************/
1919

20+
#pragma once
21+
2022
#include <vector>
2123
#include <string>
24+
#include <memory>
2225
#include <metis.h>
2326
#include <scotch/scotch.h>
2427

25-
2628
namespace SBG {
2729
namespace LIB {
2830
class WeightedSBGraph;
@@ -37,6 +39,7 @@ enum class PartitionMethod {
3739
HMetis,
3840
Scotch,
3941
Kahip,
42+
SBG,
4043
Unknown
4144
};
4245

@@ -70,14 +73,15 @@ class GraphPartitioner {
7073

7174
void partitionUsingMetis(Partition &partition);
7275
void partitionUsingHMetis(Partition &partition);
76+
void partitionUsingSBG();
7377
void createHMetisGraphFile(const std::string &file_name);
7478
void executeKhmetis(const std::string &h_graph_name) const;
7579

7680
void partitionUsingScotch(Partition &partition);
7781

7882
void partitionUsingKaHip(Partition &partition);
7983

80-
void readGraphFromSBG(const SBG::LIB::WeightedSBGraph& sbg_graph);
84+
void readGraphFromSBG();
8185

8286
std::string _name;
8387
grp_t _edges;

test/partitioner/external_tools/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*****************************************************************************/
1919

2020
#include <algorithm>
21-
#include <filesystem>
21+
// #include <filesystem>
2222
#include <fstream>
2323
#include <getopt.h>
2424
#include <iostream>
@@ -80,12 +80,12 @@ int main(int argc, char* argv[])
8080
Partition partition = partitioner.createPartition(partition_method, partitions);
8181

8282
// write results to a file
83-
std::filesystem::path json_filesystem_path(json_file_name);
84-
json_filesystem_path = json_filesystem_path.replace_extension();
85-
std::ofstream output_file(json_filesystem_path.filename().string() + "_" + partition_method + "_" + std::to_string(partitions) +
86-
"_output.txt");
87-
std::for_each(partition.values.cbegin(), partition.values.cend(),
88-
[&output_file](const auto& val) { output_file << std::to_string(val) << "\n"; });
83+
// std::filesystem::path json_filesystem_path(json_file_name);
84+
// json_filesystem_path = json_filesystem_path.replace_extension();
85+
// std::ofstream output_file(json_filesystem_path.filename().string() + "_" + partition_method + "_" + std::to_string(partitions) +
86+
// "_output.txt");
87+
// std::for_each(partition.values.cbegin(), partition.values.cend(),
88+
// [&output_file](const auto& val) { output_file << std::to_string(val) << "\n"; });
8989

9090
return 0;
9191
}

0 commit comments

Comments
 (0)