From 47437f5a2fd04fae13aba56cdf9ac1a21b322fd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Mon, 6 Nov 2023 12:41:35 +0100 Subject: [PATCH] further method splitup for profiling --- run_tests.sh | 2 +- src/edmonds.cpp | 57 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/run_tests.sh b/run_tests.sh index 3df3d1e..25efa8b 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -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 diff --git a/src/edmonds.cpp b/src/edmonds.cpp index 0e7712c..29ae7ea 100644 --- a/src/edmonds.cpp +++ b/src/edmonds.cpp @@ -1,6 +1,7 @@ #include "edmonds.h" #include #include +#include using namespace ED; @@ -123,11 +124,9 @@ void augment(Graph & graph, std::vector const & x_path, std::vector const & x_path, std::vector const & y_path, - std::stack & outer_unvisited_nodes) +__attribute__((noinline)) +std::tuple find_blossom_root_id(Graph const & graph, std::vector const & x_path, std::vector 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 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 const & x_path, std::vector const & y_path, + std::tuple 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 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 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 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 const & x_path, std::vector const & y_path, + std::tuple const & blossom_root_description, + std::stack & 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 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 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 const & x_path, std::vector const & y_path, + std::stack & outer_unvisited_nodes) +{ + //std::cout << "Contract blossom" << std::endl; + std::tuple 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); } }