Skip to content

Commit f26ad66

Browse files
author
Franco Sansone
committed
use PWMapAF to simplify interfaces
1 parent 98dd29b commit f26ad66

4 files changed

Lines changed: 274 additions & 306 deletions

File tree

algorithms/partitioner/build_sb_graph.cpp

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ struct Node {
111111
return os;
112112
}
113113

114-
115114
/// This funcion takes a json array and returns a list of parsed variable objects (Var)
116115
vector<Var> read_var_object(const rapidjson::Value& var_array)
117116
{
@@ -284,8 +283,7 @@ vector<pair<Var, Exp>> read_left_vars(const Node& node, const string& var_id = "
284283
/// @param pre_image Subset of the domain of the expression we want to connect
285284
/// @param edge_domain Domain of the map
286285
/// @param var_exp Original expression of the variable
287-
Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const Exp& var_exps, int set_offset,
288-
const PWMapAF& pw_fact)
286+
Map create_set_edge_map(const Set& pre_image, const Set& edge_domain, const Exp& var_exps, int set_offset, const PWMapAF& pw_fact)
289287
{
290288
SBG::LIB::Map map = pw_fact.createMap();
291289

@@ -371,7 +369,7 @@ Set get_edge_domain(Set image_intersection_set, Set& edge_set, int& max_value, S
371369
tuple<Set, PWMap, PWMap, EdgeCost> create_graph_edges(const std::map<int, Node>& nodes, const map<int, int>& node_offsets, int& max_value,
372370
SBG::LIB::PWMapAF& pw_fact)
373371
{
374-
Set edge_set = pw_fact.createSet(); // Our set of edges
372+
Set edge_set = pw_fact.createSet(); // Our set of edges
375373
PWMap rhs_maps = pw_fact.createPWMap(); // Map object of one of the sides
376374
PWMap lhs_maps = pw_fact.createPWMap(); // Map object of one of the other side
377375
EdgeCost costs; // Weight of edges
@@ -506,8 +504,8 @@ tuple<Set, PWMap, PWMap, EdgeCost> create_graph_edges(const std::map<int, Node>&
506504
Exp(LExp(RATIONAL(node_candidate.lhs[0].exps[0].first, 1), RATIONAL(node_candidate.lhs[0].exps[0].second, 1)));
507505
auto pre_ima_candidate = node_candidate_CanonMap.dom();
508506
logging::sbg_log << "pre_ima_candidate " << pre_ima_candidate << " from " << node_candidate_CanonMap << endl;
509-
auto node_candidate_map = create_set_edge_map(pre_ima_candidate, edge_domain_set, first_lhs_node_candidate,
510-
node_offsets.at(i), pw_fact);
507+
auto node_candidate_map =
508+
create_set_edge_map(pre_ima_candidate, edge_domain_set, first_lhs_node_candidate, node_offsets.at(i), pw_fact);
511509
auto node_candidate_map_image = node_candidate_map.image();
512510
logging::sbg_log << "map is " << node_candidate_map << endl;
513511
logging::sbg_log << "image: " << node_candidate_map_image << endl;
@@ -547,14 +545,13 @@ tuple<Set, PWMap, PWMap, EdgeCost> create_graph_edges(const std::map<int, Node>&
547545
/// @brief Add documentation
548546
/// @param nodes
549547
/// @return
550-
SBG::LIB::WeightedSBGraph create_sb_graph(const std::map<int, Node>& nodes, SBG::LIB::SetAF& set_fact, SBG::LIB::MapAF& map_fact,
551-
SBG::LIB::PWMapAF& pw_fact)
548+
SBG::LIB::WeightedSBGraph create_sb_graph(const std::map<int, Node>& nodes, SBG::LIB::PWMapAF& pw_fact)
552549
{
553550
int max_value = 0; // We track the max value, so we avoid domain collision between edges and nodes
554551
map<int, int> node_offsets;
555552

556553
// Now, we create our set of nodes.
557-
auto [node_set, weights] = create_set_of_nodes(nodes, node_offsets, max_value, set_fact);
554+
auto [node_set, weights] = create_set_of_nodes(nodes, node_offsets, max_value, pw_fact);
558555
logging::sbg_log << "node_set " << node_set << endl;
559556

560557
// Create edges and maps.
@@ -615,9 +612,7 @@ size_t get_set_size(const Set& set)
615612
return size;
616613
}
617614

618-
619-
SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, // create needed factories
620-
SBG::LIB::SetAF& set_fact, SBG::LIB::MapAF& map_fact, SBG::LIB::PWMapAF& pw_fact)
615+
SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, SBG::LIB::PWMapAF& pw_fact)
621616
{
622617
logging::sbg_log << "Reading " << filename << "..." << endl;
623618

@@ -631,7 +626,7 @@ SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, // create need
631626
auto nodes = create_node_objects_from_json(document);
632627

633628
// Now, let's get our graph
634-
auto graph = create_sb_graph(nodes, set_fact, map_fact, pw_fact);
629+
auto graph = create_sb_graph(nodes, pw_fact);
635630

636631
SBG_LOG << graph;
637632

@@ -640,25 +635,25 @@ SBG::LIB::WeightedSBGraph build_sb_graph(const string& filename, // create need
640635

641636
Set get_adjacents(const SBG::LIB::SBG& graph, const Set& node, SetAF& set_fact, MapAF& map_fact)
642637
{
643-
Set adjacents = set_fact.createSet();
638+
Set adjacents = set_fact.createSet();
644639

645-
// Fill adjacents
646-
unsigned acc = 0;
647-
for (auto it1 = graph.map1().begin(), it2 = graph.map2().begin(); it1 != graph.map1().end() and it2 != graph.map2().end(); ++it1, ++it2) {
648-
const auto map1 = *it1;
649-
const auto map2 = *it2;
640+
// Fill adjacents
641+
unsigned acc = 0;
642+
for (auto it1 = graph.map1().begin(), it2 = graph.map2().begin(); it1 != graph.map1().end() and it2 != graph.map2().end(); ++it1, ++it2) {
643+
const auto map1 = *it1;
644+
const auto map2 = *it2;
650645

651-
acc += add_adjacent_nodes(map1, map2, node, adjacents, set_fact);
646+
acc += add_adjacent_nodes(map1, map2, node, adjacents, set_fact);
652647

653-
auto map2_minus_map1_dom = map2.dom().difference(map1.dom());
654-
if (not map2_minus_map1_dom.isEmpty()) {
655-
Map map2_ = map_fact.createMap(map2_minus_map1_dom, map2.exp());
648+
auto map2_minus_map1_dom = map2.dom().difference(map1.dom());
649+
if (not map2_minus_map1_dom.isEmpty()) {
650+
Map map2_ = map_fact.createMap(map2_minus_map1_dom, map2.exp());
656651

657-
acc += add_adjacent_nodes(map2_, map1, node, adjacents, set_fact);
658-
}
652+
acc += add_adjacent_nodes(map2_, map1, node, adjacents, set_fact);
659653
}
654+
}
660655

661-
return adjacents;
656+
return adjacents;
662657
}
663658

664659
pair<Set, Set> cut_bidimensional_interval(const SetPiece& set_piece, size_t s, SetAF& set_fact)
@@ -800,54 +795,50 @@ unsigned get_edge_set_cost(const SBG::LIB::Set& edges, const EdgeCost& edge_cost
800795

801796
void flatten_set(Set& set, const WeightedSBGraph& graph)
802797
{
803-
if ((not set.isEmpty()) and set.arity() > 1) {
804-
logging::sbg_log << "flatten_set for sets with " << set.arity() << " is not implemented" << endl;
805-
return;
806-
}
807-
808-
auto canonize = [](vector<SetPiece>& set_vector)
809-
{
810-
if (set_vector.empty()) {
811-
return set_vector;
812-
}
813-
814-
sort_partition_intervals(set_vector);
815-
vector<SetPiece> new_set_vector = {*set_vector.begin()};
816-
for (size_t new_set_vector_idx = 0, set_vector_idx = 1; set_vector_idx < set_vector.size(); set_vector_idx++) {
817-
if (new_set_vector[new_set_vector_idx][0].end() + 1 == set_vector[set_vector_idx][0].begin()) {
818-
new_set_vector[new_set_vector_idx] =
819-
Interval(new_set_vector[new_set_vector_idx][0].begin(), 1, set_vector[set_vector_idx][0].end());
820-
} else {
821-
new_set_vector.emplace_back(set_vector[set_vector_idx]);
822-
new_set_vector_idx++;
823-
}
824-
}
798+
if ((not set.isEmpty()) and set.arity() > 1) {
799+
logging::sbg_log << "flatten_set for sets with " << set.arity() << " is not implemented" << endl;
800+
return;
801+
}
825802

826-
return new_set_vector;
827-
};
803+
auto canonize = [](vector<SetPiece>& set_vector) {
804+
if (set_vector.empty()) {
805+
return set_vector;
806+
}
828807

829-
Set new_partition = graph.fact().createSet();
830-
for (const auto& v : graph.V()) {
831-
vector<SetPiece> set_piece_this_node_vector;
832-
for (auto set_piece : set) {
808+
sort_partition_intervals(set_vector);
809+
vector<SetPiece> new_set_vector = {*set_vector.begin()};
810+
for (size_t new_set_vector_idx = 0, set_vector_idx = 1; set_vector_idx < set_vector.size(); set_vector_idx++) {
811+
if (new_set_vector[new_set_vector_idx][0].end() + 1 == set_vector[set_vector_idx][0].begin()) {
812+
new_set_vector[new_set_vector_idx] =
813+
Interval(new_set_vector[new_set_vector_idx][0].begin(), 1, set_vector[set_vector_idx][0].end());
814+
} else {
815+
new_set_vector.emplace_back(set_vector[set_vector_idx]);
816+
new_set_vector_idx++;
817+
}
818+
}
833819

834-
if (not v.intersection(set_piece).isEmpty()) {
835-
set_piece_this_node_vector.emplace_back(set_piece);
836-
}
837-
}
820+
return new_set_vector;
821+
};
838822

839-
set_piece_this_node_vector = canonize(set_piece_this_node_vector);
840-
new_partition = new_partition.cup(from_vector(set_piece_this_node_vector, graph.fact()));
823+
Set new_partition = graph.fact().createSet();
824+
for (const auto& v : graph.V()) {
825+
vector<SetPiece> set_piece_this_node_vector;
826+
for (auto set_piece : set) {
827+
if (not v.intersection(set_piece).isEmpty()) {
828+
set_piece_this_node_vector.emplace_back(set_piece);
829+
}
841830
}
842831

843-
auto diff = set.difference(new_partition);
844-
assert(diff.isEmpty());
832+
set_piece_this_node_vector = canonize(set_piece_this_node_vector);
833+
new_partition = new_partition.cup(from_vector(set_piece_this_node_vector, graph.fact()));
834+
}
845835

846-
set = new_partition;
836+
auto diff = set.difference(new_partition);
837+
assert(diff.isEmpty());
847838

839+
set = new_partition;
848840
}
849841

850-
851842
SBG::LIB::WeightedSBGraph create_air_conditioners_graph()
852843
{
853844
SBG::LIB::UnordAF set_fact;
@@ -926,5 +917,4 @@ SBG::LIB::WeightedSBGraph create_air_conditioners_graph()
926917
return graph;
927918
}
928919

929-
930920
} // namespace sbg_partitioner

algorithms/partitioner/build_sb_graph.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ namespace sbg_partitioner {
3030
/// a node for each access to a variable and an edge for each connection
3131
/// between variables.
3232
/// If a variable appears on the left and on the right side, an edge is created.
33-
SBG::LIB::WeightedSBGraph build_sb_graph(const std::string& filename, SBG::LIB::SetAF& set_fact,
34-
SBG::LIB::MapAF& map_fact, SBG::LIB::PWMapAF& pw_map_fact);
33+
SBG::LIB::WeightedSBGraph build_sb_graph(const std::string& filename, SBG::LIB::PWMapAF& pw_map_fact);
3534

3635

3736
/// Ad hoc function to get pre image of an expression from its image.

0 commit comments

Comments
 (0)