Skip to content

Commit 1a4fad6

Browse files
committed
added hyperdag template arguments
1 parent 55200c9 commit 1a4fad6

12 files changed

+207
-128
lines changed

apps/ilp_hypergraph_partitioner.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ limitations under the License.
3939
using namespace osp;
4040

4141
using graph = computational_dag_vector_impl_def_int_t;
42+
using hypergraph = Hypergraph_def_t;
4243

4344
int main(int argc, char *argv[]) {
4445
if (argc < 4) {
@@ -80,16 +81,16 @@ int main(int argc, char *argv[]) {
8081
return 1;
8182
}
8283

83-
Hypergraph hgraph;
84+
PartitioningProblem<hypergraph> instance;
8485

8586
bool file_status = true;
8687
if (file_ending == "hdag") {
8788
graph dag;
8889
file_status = file_reader::readComputationalDagHyperdagFormatDB(filename_hgraph, dag);
8990
if(file_status)
90-
hgraph = convert_from_cdag_as_hyperdag<size_t, int, int, int, graph>(dag);
91+
instance.getHypergraph() = convert_from_cdag_as_hyperdag<hypergraph, graph>(dag);
9192
} else if (file_ending == "mtx") {
92-
file_status = file_reader::readHypergraphMartixMarketFormat(filename_hgraph, hgraph);
93+
file_status = file_reader::readHypergraphMartixMarketFormat(filename_hgraph, instance.getHypergraph());
9394
} else {
9495
std::cout << "Unknown file extension." << std::endl;
9596
return 1;
@@ -100,12 +101,12 @@ int main(int argc, char *argv[]) {
100101
return 1;
101102
}
102103

103-
PartitioningProblem instance(hgraph, static_cast<unsigned>(nr_parts));
104+
instance.setNumberOfPartitions(static_cast<unsigned>(nr_parts));
104105
instance.setMaxWorkWeightViaImbalanceFactor(imbalance);
105106

106-
Partitioning initial_partition(instance);
107-
GenericFM fm;
108-
for(size_t node = 0; node < hgraph.num_vertices(); ++node)
107+
Partitioning<hypergraph> initial_partition(instance);
108+
GenericFM<hypergraph> fm;
109+
for(size_t node = 0; node < instance.getHypergraph().num_vertices(); ++node)
109110
initial_partition.setAssignedPartition(node, static_cast<unsigned>(node % static_cast<size_t>(nr_parts)));
110111
if(nr_parts == 2)
111112
fm.ImprovePartitioning(initial_partition);
@@ -114,17 +115,17 @@ int main(int argc, char *argv[]) {
114115

115116
if (replicate > 0) {
116117

117-
PartitioningWithReplication partition(instance);
118-
HypergraphPartitioningILPWithReplication partitioner;
118+
PartitioningWithReplication<hypergraph> partition(instance);
119+
HypergraphPartitioningILPWithReplication<hypergraph> partitioner;
119120

120-
for(size_t node = 0; node < hgraph.num_vertices(); ++node)
121+
for(size_t node = 0; node < instance.getHypergraph().num_vertices(); ++node)
121122
partition.setAssignedPartitions(node, {initial_partition.assignedPartition(node)});
122123
if(partition.satisfiesBalanceConstraint())
123124
partitioner.setUseInitialSolution(true);
124125

125126
partitioner.setTimeLimitSeconds(600);
126127
if(replicate == 2)
127-
partitioner.setReplicationModel(HypergraphPartitioningILPWithReplication<>::REPLICATION_MODEL_IN_ILP::GENERAL);
128+
partitioner.setReplicationModel(HypergraphPartitioningILPWithReplication<hypergraph>::REPLICATION_MODEL_IN_ILP::GENERAL);
128129

129130
auto solve_status = partitioner.computePartitioning(partition);
130131

@@ -139,10 +140,10 @@ int main(int argc, char *argv[]) {
139140

140141
} else {
141142

142-
Partitioning partition(instance);
143-
HypergraphPartitioningILP partitioner;
143+
Partitioning<hypergraph> partition(instance);
144+
HypergraphPartitioningILP<hypergraph> partitioner;
144145

145-
for(size_t node = 0; node < hgraph.num_vertices(); ++node)
146+
for(size_t node = 0; node < instance.getHypergraph().num_vertices(); ++node)
146147
partition.setAssignedPartition(node, initial_partition.assignedPartition(node));
147148
if(partition.satisfiesBalanceConstraint())
148149
partitioner.setUseInitialSolution(true);

include/osp/auxiliary/io/partitioning_file_writer.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,27 @@ limitations under the License.
2525

2626
namespace osp { namespace file_writer {
2727

28-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
29-
void write_txt(std::ostream &os, const Partitioning<index_type, workw_type, memw_type, commw_type> &partition) {
28+
template<typename hypergraph_t>
29+
void write_txt(std::ostream &os, const Partitioning<hypergraph_t> &partition) {
30+
31+
using index_type = typename hypergraph_t::vertex_idx;
3032

3133
os << "\%\% Partitioning for " << partition.getInstance().getNumberOfPartitions() << " parts." << std::endl;
3234

3335
for(index_type node = 0; node < partition.getInstance().getHypergraph().num_vertices(); ++node)
3436
os << node << " " << partition.assignedPartition(node) << std::endl;
3537
}
3638

37-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
38-
void write_txt(const std::string &filename, const Partitioning<index_type, workw_type, memw_type, commw_type> &partition) {
39+
template<typename hypergraph_t>
40+
void write_txt(const std::string &filename, const Partitioning<hypergraph_t> &partition) {
3941
std::ofstream os(filename);
4042
write_txt(os, partition);
4143
}
4244

43-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
44-
void write_txt(std::ostream &os, const PartitioningWithReplication<index_type, workw_type, memw_type, commw_type> &partition) {
45+
template<typename hypergraph_t>
46+
void write_txt(std::ostream &os, const PartitioningWithReplication<hypergraph_t> &partition) {
47+
48+
using index_type = typename hypergraph_t::vertex_idx;
4549

4650
os << "\%\% Partitioning for " << partition.getInstance().getNumberOfPartitions() << " parts with replication." << std::endl;
4751

@@ -54,8 +58,8 @@ void write_txt(std::ostream &os, const PartitioningWithReplication<index_type, w
5458
}
5559
}
5660

57-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
58-
void write_txt(const std::string &filename, const PartitioningWithReplication<index_type, workw_type, memw_type, commw_type> &partition) {
61+
template<typename hypergraph_t>
62+
void write_txt(const std::string &filename, const PartitioningWithReplication<hypergraph_t> &partition) {
5963
std::ofstream os(filename);
6064
write_txt(os, partition);
6165
}

include/osp/partitioning/model/hypergraph.hpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,24 @@ namespace osp {
2727
template<typename index_type = size_t, typename workw_type = int, typename memw_type = int, typename commw_type = int>
2828
class Hypergraph {
2929

30+
using this_t = Hypergraph<index_type, workw_type, memw_type, commw_type>;
31+
3032
public:
3133

34+
using vertex_idx = index_type;
35+
using vertex_work_weight_type = workw_type;
36+
using vertex_mem_weight_type = memw_type;
37+
using vertex_comm_weight_type = commw_type;
38+
3239
Hypergraph() = default;
3340

3441
Hypergraph(index_type num_vertices_, index_type num_hyperedges_)
3542
: Num_vertices(num_vertices_), Num_hyperedges(num_hyperedges_), vertex_work_weights(num_vertices_, 1),
3643
vertex_memory_weights(num_vertices_, 1), hyperedge_weights(num_hyperedges_, 1),
3744
incident_hyperedges_to_vertex(num_vertices_), vertices_in_hyperedge(num_hyperedges_){}
3845

39-
Hypergraph(const Hypergraph<index_type, workw_type, memw_type, commw_type> &other) = default;
40-
Hypergraph &operator=(const Hypergraph<index_type, workw_type, memw_type, commw_type> &other) = default;
46+
Hypergraph(const this_t &other) = default;
47+
Hypergraph &operator=(const this_t &other) = default;
4148

4249
virtual ~Hypergraph() = default;
4350

@@ -75,6 +82,8 @@ class Hypergraph {
7582
std::vector<std::vector<index_type>> vertices_in_hyperedge;
7683
};
7784

85+
using Hypergraph_def_t = Hypergraph<size_t, int, int, int>;
86+
7887
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
7988
void Hypergraph<index_type, workw_type, memw_type, commw_type>::add_pin(index_type vertex_idx, index_type hyperedge_idx)
8089
{
@@ -179,4 +188,6 @@ void Hypergraph<index_type, workw_type, memw_type, commw_type>::reset(index_type
179188
}
180189

181190

191+
192+
182193
} // namespace osp

include/osp/partitioning/model/hypergraph_utility.hpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ namespace osp {
3636

3737
// summing up weights
3838

39-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
40-
workw_type compute_total_vertex_work_weight(const Hypergraph<index_type, workw_type, memw_type, commw_type>& hgraph)
39+
template<typename hypergraph_t>
40+
typename hypergraph_t::vertex_work_weight_type compute_total_vertex_work_weight(const hypergraph_t& hgraph)
4141
{
42+
using index_type = typename hypergraph_t::vertex_idx;
43+
using workw_type = typename hypergraph_t::vertex_work_weight_type;
44+
4245
workw_type total = 0;
4346
for(index_type node = 0; node < hgraph.num_vertices(); ++node)
4447
total += hgraph.get_vertex_work_weight(node);
4548
return total;
4649
}
4750

48-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
49-
memw_type compute_total_vertex_memory_weight(const Hypergraph<index_type, workw_type, memw_type, commw_type>& hgraph)
51+
template<typename hypergraph_t>
52+
typename hypergraph_t::vertex_mem_weight_type compute_total_vertex_memory_weight(const hypergraph_t& hgraph)
5053
{
54+
using index_type = typename hypergraph_t::vertex_idx;
55+
using memw_type = typename hypergraph_t::vertex_mem_weight_type;
56+
5157
memw_type total = 0;
5258
for(index_type node = 0; node < hgraph.num_vertices(); ++node)
5359
total += hgraph.get_vertex_memory_weight(node);
@@ -57,19 +63,22 @@ memw_type compute_total_vertex_memory_weight(const Hypergraph<index_type, workw_
5763

5864
// get induced subhypergraph
5965

60-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
61-
Hypergraph<index_type, workw_type, memw_type, commw_type> create_induced_hypergraph(const Hypergraph<index_type, workw_type, memw_type, commw_type>& hgraph, const std::vector<bool>& include)
66+
template<typename hypergraph_t>
67+
hypergraph_t create_induced_hypergraph(const hypergraph_t& hgraph, const std::vector<bool>& include)
6268
{
6369
if(include.size() != hgraph.num_vertices())
6470
throw std::invalid_argument("Invalid Argument while extracting induced hypergraph: input bool array has incorrect size.");
6571

72+
using index_type = typename hypergraph_t::vertex_idx;
73+
74+
6675
std::vector<index_type> new_index(hgraph.num_vertices());
6776
unsigned current_index = 0;
6877
for(index_type node = 0; node < hgraph.num_vertices(); ++node)
6978
if(include[node])
7079
new_index[node] = current_index++;
7180

72-
Hypergraph<index_type, workw_type, memw_type, commw_type> new_hgraph(current_index, 0);
81+
hypergraph_t new_hgraph(current_index, 0);
7382
for(index_type node = 0; node < hgraph.num_vertices(); ++node)
7483
if(include[node])
7584
{
@@ -97,15 +106,20 @@ Hypergraph<index_type, workw_type, memw_type, commw_type> create_induced_hypergr
97106

98107
// conversion
99108

100-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type, typename Graph_t>
101-
Hypergraph<index_type, workw_type, memw_type, commw_type> convert_from_cdag_as_dag(const Graph_t& dag)
109+
template<typename hypergraph_t, typename Graph_t>
110+
hypergraph_t convert_from_cdag_as_dag(const Graph_t& dag)
102111
{
112+
using index_type = typename hypergraph_t::vertex_idx;
113+
using workw_type = typename hypergraph_t::vertex_work_weight_type;
114+
using memw_type = typename hypergraph_t::vertex_mem_weight_type;
115+
using commw_type = typename hypergraph_t::vertex_comm_weight_type;
116+
103117
static_assert(std::is_same_v<vertex_idx_t<Graph_t>, index_type>, "Index type mismatch, cannot convert DAG to hypergraph.");
104118
static_assert(std::is_same_v<v_workw_t<Graph_t>, workw_type>, "Work weight type mismatch, cannot convert DAG to hypergraph.");
105119
static_assert(std::is_same_v<v_memw_t<Graph_t>, memw_type>, "Memory weight type mismatch, cannot convert DAG to hypergraph.");
106120
static_assert(!has_edge_weights_v<Graph_t> || std::is_same_v<e_commw_t<Graph_t>, commw_type>, "Communication weight type mismatch, cannot convert DAG to hypergraph.");
107121

108-
Hypergraph<index_type, workw_type, memw_type, commw_type> hgraph(dag.num_vertices(), 0);
122+
hypergraph_t hgraph(dag.num_vertices(), 0);
109123
for(const auto &node : dag.vertices())
110124
{
111125
hgraph.set_vertex_work_weight(node, dag.vertex_work_weight(node));
@@ -119,15 +133,20 @@ Hypergraph<index_type, workw_type, memw_type, commw_type> convert_from_cdag_as_d
119133
return hgraph;
120134
}
121135

122-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type, typename Graph_t>
123-
Hypergraph<index_type, workw_type, memw_type, commw_type> convert_from_cdag_as_hyperdag(const Graph_t& dag)
136+
template<typename hypergraph_t, typename Graph_t>
137+
hypergraph_t convert_from_cdag_as_hyperdag(const Graph_t& dag)
124138
{
139+
using index_type = typename hypergraph_t::vertex_idx;
140+
using workw_type = typename hypergraph_t::vertex_work_weight_type;
141+
using memw_type = typename hypergraph_t::vertex_mem_weight_type;
142+
using commw_type = typename hypergraph_t::vertex_comm_weight_type;
143+
125144
static_assert(std::is_same_v<vertex_idx_t<Graph_t>, index_type>, "Index type mismatch, cannot convert DAG to hypergraph.");
126145
static_assert(std::is_same_v<v_workw_t<Graph_t>, workw_type>, "Work weight type mismatch, cannot convert DAG to hypergraph.");
127146
static_assert(std::is_same_v<v_memw_t<Graph_t>, memw_type>, "Memory weight type mismatch, cannot convert DAG to hypergraph.");
128147
static_assert(std::is_same_v<v_commw_t<Graph_t>, commw_type>, "Communication weight type mismatch, cannot convert DAG to hypergraph.");
129148

130-
Hypergraph<index_type, workw_type, memw_type, commw_type> hgraph(dag.num_vertices(), 0);
149+
hypergraph_t hgraph(dag.num_vertices(), 0);
131150
for(const auto &node : dag.vertices())
132151
{
133152
hgraph.set_vertex_work_weight(node, dag.vertex_work_weight(node));

include/osp/partitioning/model/partitioning.hpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,41 @@ namespace osp {
2626

2727
// Represents a partitioning where each vertex of a hypergraph is assigned to a specifc partition
2828

29-
template<typename index_type = size_t, typename workw_type = int, typename memw_type = int, typename commw_type = int>
29+
template<typename hypergraph_t>
3030
class Partitioning {
3131

3232
private:
3333

34-
const PartitioningProblem<index_type, workw_type, memw_type, commw_type> *instance;
34+
using index_type = typename hypergraph_t::vertex_idx;
35+
using workw_type = typename hypergraph_t::vertex_work_weight_type;
36+
using memw_type = typename hypergraph_t::vertex_mem_weight_type;
37+
using commw_type = typename hypergraph_t::vertex_comm_weight_type;
38+
39+
const PartitioningProblem<hypergraph_t> *instance;
3540

3641
std::vector<unsigned> node_to_partition_assignment;
3742

3843
public:
3944

4045
Partitioning() = delete;
4146

42-
Partitioning(const PartitioningProblem<index_type, workw_type, memw_type, commw_type> &inst)
47+
Partitioning(const PartitioningProblem<hypergraph_t> &inst)
4348
: instance(&inst), node_to_partition_assignment(std::vector<unsigned>(inst.getHypergraph().num_vertices(), 0)) {}
4449

45-
Partitioning(const PartitioningProblem<index_type, workw_type, memw_type, commw_type> &inst, const std::vector<unsigned> &partition_assignment_)
50+
Partitioning(const PartitioningProblem<hypergraph_t> &inst, const std::vector<unsigned> &partition_assignment_)
4651
: instance(&inst), node_to_partition_assignment(partition_assignment_) {}
4752

48-
Partitioning(const Partitioning<index_type, workw_type, memw_type, commw_type> &partitioning_) = default;
49-
Partitioning(Partitioning<index_type, workw_type, memw_type, commw_type> &&partitioning_) = default;
53+
Partitioning(const Partitioning<hypergraph_t> &partitioning_) = default;
54+
Partitioning(Partitioning<hypergraph_t> &&partitioning_) = default;
5055

51-
Partitioning &operator=(const Partitioning<index_type, workw_type, memw_type, commw_type> &partitioning_) = default;
56+
Partitioning &operator=(const Partitioning<hypergraph_t> &partitioning_) = default;
5257

5358
virtual ~Partitioning() = default;
5459

5560

5661
// getters and setters
5762

58-
inline const PartitioningProblem<index_type, workw_type, memw_type, commw_type> &getInstance() const { return *instance; }
63+
inline const PartitioningProblem<hypergraph_t> &getInstance() const { return *instance; }
5964

6065
inline unsigned assignedPartition(index_type node) const { return node_to_partition_assignment[node]; }
6166
inline const std::vector<unsigned> &assignedPartitions() const { return node_to_partition_assignment; }
@@ -109,8 +114,8 @@ class Partitioning {
109114

110115
};
111116

112-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
113-
std::vector<unsigned> Partitioning<index_type, workw_type, memw_type, commw_type>::computeLambdaForHyperedges() const
117+
template<typename hypergraph_t>
118+
std::vector<unsigned> Partitioning<hypergraph_t>::computeLambdaForHyperedges() const
114119
{
115120
std::vector<unsigned> lambda(instance->getHypergraph().num_hyperedges(), 0);
116121
for(index_type edge_idx = 0; edge_idx < instance->getHypergraph().num_hyperedges(); ++edge_idx)
@@ -128,8 +133,8 @@ std::vector<unsigned> Partitioning<index_type, workw_type, memw_type, commw_type
128133
return lambda;
129134
}
130135

131-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
132-
commw_type Partitioning<index_type, workw_type, memw_type, commw_type>::computeConnectivityCost() const {
136+
template<typename hypergraph_t>
137+
typename hypergraph_t::vertex_comm_weight_type Partitioning<hypergraph_t>::computeConnectivityCost() const {
133138

134139
commw_type total = 0;
135140
std::vector<unsigned> lambda = computeLambdaForHyperedges();
@@ -141,8 +146,8 @@ commw_type Partitioning<index_type, workw_type, memw_type, commw_type>::computeC
141146
return total;
142147
}
143148

144-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
145-
commw_type Partitioning<index_type, workw_type, memw_type, commw_type>::computeCutNetCost() const {
149+
template<typename hypergraph_t>
150+
typename hypergraph_t::vertex_comm_weight_type Partitioning<hypergraph_t>::computeCutNetCost() const {
146151

147152
commw_type total = 0;
148153
std::vector<unsigned> lambda = computeLambdaForHyperedges();
@@ -153,8 +158,8 @@ commw_type Partitioning<index_type, workw_type, memw_type, commw_type>::computeC
153158
return total;
154159
}
155160

156-
template<typename index_type, typename workw_type, typename memw_type, typename commw_type>
157-
bool Partitioning<index_type, workw_type, memw_type, commw_type>::satisfiesBalanceConstraint() const {
161+
template<typename hypergraph_t>
162+
bool Partitioning<hypergraph_t>::satisfiesBalanceConstraint() const {
158163
std::vector<workw_type> work_weight(instance->getNumberOfPartitions(), 0);
159164
std::vector<memw_type> memory_weight(instance->getNumberOfPartitions(), 0);
160165
for (index_type node = 0; node < node_to_partition_assignment.size(); ++node) {

0 commit comments

Comments
 (0)