further method splitup for profiling

This commit is contained in:
Maximilian Keßler 2023-11-06 12:41:35 +01:00
parent f3240b7f6b
commit 47437f5a2f
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 43 additions and 16 deletions

View file

@ -4,7 +4,7 @@ SUCC=()
FAILED=()
while read -r instance opt; do
linecount=$(./build/main $GRAPHS/$instance | wc -l)
linecount=$(./cmake-build-release/matching $GRAPHS/$instance | wc -l)
result=$(($linecount - 1))
if [ "$result" -eq "$opt" ];
then

View file

@ -1,6 +1,7 @@
#include "edmonds.h"
#include <iostream>
#include <stack>
#include <tuple>
using namespace ED;
@ -123,11 +124,9 @@ void augment(Graph & graph, std::vector<NodeId> const & x_path, std::vector<Node
collect_exposed_vertices(graph, outer_unvisited_nodes);
}
void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::vector<NodeId> const & y_path,
std::stack<NodeId> & outer_unvisited_nodes)
__attribute__((noinline))
std::tuple<NodeId, size_type, size_type> find_blossom_root_id(Graph const & graph, std::vector<NodeId> const & x_path, std::vector<NodeId> const & y_path)
{
//std::cout << "Contract blossom" << std::endl;
// Paths are not disjoint -> shrink blossom
size_t distance_from_x = x_path.size() - 1;
size_t distance_from_y = y_path.size() - 1;
while (distance_from_x > 0 and distance_from_y > 0 and \
@ -143,9 +142,16 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
++distance_from_y;
};
// found first vertex fixed by rho
NodeId blossom_root_id = x_path[distance_from_x];
return { x_path[distance_from_x], distance_from_x, distance_from_y };
}
// Update φ along the paths to encode the ear decomposition
__attribute__((noinline))
void update_phi_along_blossom_paths(Graph & graph, std::vector<NodeId> const & x_path, std::vector<NodeId> const & y_path,
std::tuple<NodeId, size_type, size_type> const & blossom_root)
{
auto const [blossom_root_id, distance_from_x, distance_from_y] = blossom_root;
// Update φ along the paths to encode the ear decomposition
for (size_t i = 1; i <= distance_from_x; i += 2)
{
if (graph.rho(graph.phi(x_path[i])) != blossom_root_id)
@ -153,6 +159,7 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
graph.node(graph.phi(x_path[i])).phi = x_path[i];
}
}
for (size_t i = 1; i <= distance_from_y; i += 2)
{
if (graph.rho(graph.phi(y_path[i])) != blossom_root_id)
@ -161,7 +168,7 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
}
}
// Link x and y
// Link x and y
if (graph.rho(x_path.front()) != blossom_root_id)
{
graph.node(x_path.front()).phi = y_path.front();
@ -170,13 +177,20 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
{
graph.node(y_path.front()).phi = x_path.front();
}
}
// Update root indices. We have to do this for all vertices v with
// ρ(v) in the paths from x or y to r
// We update ρ(v) first for the paths themselves, and then 'contract' ρ
// by updating ρ(v) to r for all vertices where ρ(ρ(v)) = r
// Also, while walking along the paths, we can add all vertices (which are now outer)
// to the stack.
__attribute__((noinline))
void update_rho(Graph & graph, std::vector<NodeId> const & x_path, std::vector<NodeId> const & y_path,
std::tuple<NodeId, size_type, size_type> const & blossom_root_description,
std::stack<NodeId> & outer_unvisited_nodes)
{
// Update root indices. We have to do this for all vertices v with
// ρ(v) in the paths from x or y to r
// We update ρ(v) first for the paths themselves, and then 'contract' ρ
// by updating ρ(v) to r for all vertices where ρ(ρ(v)) = r
// Also, while walking along the paths, we can add all vertices (which are now outer)
// to the stack.
auto const [blossom_root_id, distance_from_x, distance_from_y] = blossom_root_description;
for (size_t i = 0; i <= distance_from_x; ++i)
{
graph.node(x_path[i]).rho = blossom_root_id;
@ -194,7 +208,7 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
}
}
// Iterating over whole graph.
// Iterating over whole graph.
for (NodeId node_id = 0; node_id < graph.num_nodes(); ++node_id)
{
if (graph.rho(graph.rho(node_id)) == blossom_root_id)
@ -202,7 +216,18 @@ void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::ve
graph.node(node_id).rho = blossom_root_id;
}
}
//check_integrity(graph);
}
__attribute__((noinline))
void contract_blossom(Graph & graph, std::vector<NodeId> const & x_path, std::vector<NodeId> const & y_path,
std::stack<NodeId> & outer_unvisited_nodes)
{
//std::cout << "Contract blossom" << std::endl;
std::tuple<NodeId, size_type, size_type> const blossom_root_description = find_blossom_root_id(graph, x_path, y_path);
update_phi_along_blossom_paths(graph, x_path, y_path, blossom_root_description);
//check_integrity(graph);
update_rho(graph, x_path, y_path, blossom_root_description, outer_unvisited_nodes);
}
void maximum_matching_from_initial_matching(Graph & graph)
@ -240,11 +265,13 @@ void maximum_matching_from_initial_matching(Graph & graph)
if (x_path.back() != y_path.back())
{
// paths are disjoint -> can augment
augment(graph, x_path, y_path, outer_unvisited_nodes);
break;
}
else
{
// Paths are not disjoint -> contract the new blossom
contract_blossom(graph, x_path, y_path, outer_unvisited_nodes);
}
}