Skip to content

Commit

Permalink
Performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Apr 22, 2024
1 parent 5f966b7 commit 2afb5d6
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 63 deletions.
28 changes: 9 additions & 19 deletions sbg/sbg_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,10 @@ MatchInfo<Set> SBGMatching<Set>::calculate(unsigned int k)
auto total = std::chrono::duration_cast<std::chrono::microseconds>(
duration1 + duration2
);
Util::SBG_LOG << "Total exec time: " << total.count() << " [μs]\n\n";
Util::SBG_LOG << "Total match exec time: " << total.count() << " [μs]\n\n";
}
else
Util::SBG_LOG << "Total exec time: " << duration1.count() << " [μs]\n\n";
Util::SBG_LOG << "Total match exec time: " << duration1.count() << " [μs]\n\n";

return MatchInfo(matched_E(), fullyMatchedU());
}
Expand Down Expand Up @@ -790,36 +790,26 @@ PWMap<Set> SBGSCC<Set>::calculate()
if (debug())
Util::SBG_LOG << "SCC dsbg: \n" << dsbg() << "\n\n";

auto begin = std::chrono::high_resolution_clock::now();
do {
sccStep();
sccStep();
} while (Ediff() != Set());
PW rmap = sccStep();
set_rmap(rmap);
auto end = std::chrono::high_resolution_clock::now();

auto total = std::chrono::duration_cast<std::chrono::microseconds>(
end - begin
);
Util::SBG_LOG << "Total SCC exec time: " << total.count() << " [μs]\n\n";

if (debug())
Util::SBG_LOG << "SCC result: " << rmap << "\n\n";

return rmap;
}

template<typename Set>
std::set<Set> SBGSCC<Set>::transformResult(PWMap<Set> scc)
{
Components res;

Set reps = scc.filterMap([](const SBGMap<Set> &sbgmap) {
return eqId(sbgmap);
}).image();

for (const SetPiece &mdi : reps) {
Set represented = scc.preImage(Set(mdi));
res.emplace(represented);
}

return res;
}

// -----------------------------------------------------------------------------
// Topological sort ------------------------------------------------------------
// -----------------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions sbg/sbg_algorithms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ template<typename Set>
struct SBGSCC {
using Map = SBGMap<Set>;
using PW = PWMap<Set>;
using Components = std::set<Set>;

//*** SBG info, constant
member_class(DSBGraph<Set>, dsbg);
Expand All @@ -200,7 +199,6 @@ struct SBGSCC {
SBGSCC(DSBGraph<Set> dsbg, bool debug);

PW calculate();
Components transformResult(PW scc);

private:
PW sccStep();
Expand Down
110 changes: 81 additions & 29 deletions test/performance/boost/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This file is meant to test the three examples presented in the paper
"Efficient Matching of Large Systems ...". When executed it will ten times
the Boost Edmonds matching algorithm and report the average execiton time
the Boost Edmonds matching algorithm and report the average execution time
for each of them.
<hr>
Expand Down Expand Up @@ -33,23 +33,19 @@

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/max_cardinality_matching.hpp>
#include <boost/graph/strong_components.hpp>
#include <gtest/gtest.h>

#include <eval/visitors/program_visitor.hpp>
#include <parser/sbg_program.hpp>
#include <test/performance/boost/ordinary_graph_builder.hpp>
#include <util/defs.hpp>

template<typename Set>
void computeMaxCardinalityMatching(SBG::LIB::SBGraph<Set> sb_graph)
void computeMaxCardinalityMatching(OG::Graph graph)
{
OG::OrdinaryGraphBuilder ordinary_graph_builder(sb_graph);

OG::Graph graph = ordinary_graph_builder.build();

int n_vertices = num_vertices(graph);

std::vector<boost::graph_traits<OG::Graph>::vertex_descriptor>
mate(n_vertices);
std::vector<OG::VertexDesc>mate(n_vertices);
auto begin = std::chrono::high_resolution_clock::now();
checked_edmonds_maximum_cardinality_matching(graph, &mate[0]);
auto end = std::chrono::high_resolution_clock::now();
Expand All @@ -59,17 +55,70 @@ void computeMaxCardinalityMatching(SBG::LIB::SBGraph<Set> sb_graph)

SBG::Util::SBG_LOG << "Boost Edmonds Maximum cardinality matching time: "
<< total.count() << " [μs]" << std::endl;
SBG::Util::SBG_LOG << "Mathcing sz: " << matching_size(graph, &mate[0]) << "\n";
SBG::Util::SBG_LOG << "Matching sz: " << matching_size(graph, &mate[0]) << "\n";
}

template void computeMaxCardinalityMatching<SBG::LIB::UnordSet>(
SBG::LIB::BaseSBG sbg
);
template void computeMaxCardinalityMatching<SBG::LIB::OrdSet>(
SBG::LIB::CanonSBG sbg
);
void computeSCC(OG::DGraph graph)
{
int n_vertices = num_vertices(graph);

std::vector<int> component(n_vertices);
auto begin = std::chrono::high_resolution_clock::now();
int num = strong_components(graph, &component[0]);
auto end = std::chrono::high_resolution_clock::now();
auto total = std::chrono::duration_cast<std::chrono::microseconds>(
end - begin
);

void parseEvalProgramFromFile(std::string fname, int copies)
SBG::Util::SBG_LOG << "Tarjan SCC time: "
<< total.count() << " [μs]" << std::endl;
SBG::Util::SBG_LOG << "SCC sz: " << num << "\n";
}

auto graph_visitor_ = SBG::Util::Overload {
[](int a, SBG::LIB::BaseSBG b) {
SBG::LIB::BaseMatch match(b, false);
match.calculate(6).matched_edges();

if (a == 0) {
OG::OrdinaryGraphBuilder ordinary_graph_builder(b);
OG::Graph graph = ordinary_graph_builder.build();
computeMaxCardinalityMatching(graph);
}

SBG::LIB::BaseSCC scc(buildSCCFromMatching(match), false);

if (a == 1) {
OG::OrdinaryDGraphBuilder ordinary_dgraph_builder(scc.dsbg());
OG::DGraph dgraph = ordinary_dgraph_builder.build();
computeSCC(dgraph);
}
},
[](int a, SBG::LIB::CanonSBG b) {
SBG::LIB::CanonMatch match(b, false);
match.calculate(6).matched_edges();

if (a == 0) {
OG::OrdinaryGraphBuilder ordinary_graph_builder(b);
OG::Graph graph = ordinary_graph_builder.build();
computeMaxCardinalityMatching(graph);
}

SBG::LIB::CanonSCC scc(buildSCCFromMatching(match), false);

if (a == 1) {
OG::OrdinaryDGraphBuilder ordinary_dgraph_builder(scc.dsbg());
OG::DGraph dgraph = ordinary_dgraph_builder.build();
computeSCC(dgraph);
}
},
[](auto a, auto b) {
SBG::Util::ERROR("Wrong arguments for algorithm");
return;
}
};

void parseEvalProgramFromFile(int alg, std::string fname, int copies)
{
std::ifstream in(fname.c_str());
if (in.fail())
Expand Down Expand Up @@ -98,10 +147,7 @@ void parseEvalProgramFromFile(std::string fname, int copies)
auto e = std::get<1>(ev);
if (std::holds_alternative<SBG::Eval::SBGBaseType>(e)) {
auto g_variant = std::get<SBG::Eval::SBGBaseType>(e);
if (std::holds_alternative<SBG::LIB::CanonSBG>(g_variant)) {
auto g = std::get<SBG::LIB::CanonSBG>(g_variant);
computeMaxCardinalityMatching(g.copy(copies));
}
std::visit(graph_visitor_, std::variant<int>(alg), g_variant);
}
}
}
Expand All @@ -120,6 +166,7 @@ void usage()
std::cout << "Usage parser [options] file" << std::endl;
std::cout << "Parses a SBG program." << std::endl;
std::cout << std::endl;
std::cout << "-a, --file Algorithm selection: [0] matching, [1] scc, [2] topological sort" << std::endl;
std::cout << "-f, --file SBG program file used as input " << std::endl;
std::cout << "-h, --help Display this information and exit" << std::endl;
std::cout << "-v, --version Display version information and exit"
Expand All @@ -143,19 +190,24 @@ void version()
int main(int argc, char**argv)
{
std::string filename;
int opt, copies = 1;
int opt, copies = 1, alg = 0;
extern char* optarg;

while (true) {
static struct option long_options[] = {{"file", required_argument, 0, 'f'}
, {"help", no_argument, 0, 'h'}
, {"version", no_argument, 0, 'v'}
, {"copies", required_argument, 0, 'c'}
, {0, 0, 0, 0}};
opt = getopt_long(argc, argv, "f:hvc:", long_options, nullptr);
static struct option long_options[] = {
{"algorithm", required_argument, 0, 'a'}
, {"file", required_argument, 0, 'f'}
, {"help", no_argument, 0, 'h'}
, {"version", no_argument, 0, 'v'}
, {"copies", required_argument, 0, 'c'}
, {0, 0, 0, 0}};
opt = getopt_long(argc, argv, "a:f:hvc:", long_options, nullptr);
if (opt == EOF)
break;
switch (opt) {
case 'a':
alg = atoi(optarg);
break;
case 'f':
filename = optarg;
break;
Expand All @@ -178,7 +230,7 @@ int main(int argc, char**argv)
}

if (!filename.empty())
parseEvalProgramFromFile(filename, copies);
parseEvalProgramFromFile(alg, filename, copies);
else
SBG::Util::ERROR("A filename should be provided");

Expand Down
11 changes: 11 additions & 0 deletions test/performance/boost/ordinary_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ typedef Graph::edge_descriptor EdgeDesc;
typedef boost::graph_traits<Graph>::edge_iterator EdgeIt;
typedef boost::graph_traits<Graph>::out_edge_iterator OutEdgeIt;

// Ordinary directed Graph definition ------------------------------------------

typedef boost::adjacency_list<
boost::vecS, boost::vecS, boost::bidirectionalS, Vertex, Edge
> DGraph;
typedef DGraph::vertex_descriptor DVertexDesc;
typedef boost::graph_traits<DGraph>::vertex_iterator DVertexIt;
typedef DGraph::edge_descriptor DEdgeDesc;
typedef boost::graph_traits<DGraph>::edge_iterator DEdgeIt;
typedef boost::graph_traits<DGraph>::out_edge_iterator OutDEdgeIt;

} // namespace OG

#endif
66 changes: 66 additions & 0 deletions test/performance/boost/ordinary_graph_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,70 @@ template class OrdinaryGraphBuilder<SBG::LIB::UnordSet>;

template class OrdinaryGraphBuilder<SBG::LIB::OrdSet>;

// Directed graph builder ------------------------------------------------------

template<typename Set>
OrdinaryDGraphBuilder<Set>::OrdinaryDGraphBuilder(
OrdinaryDGraphBuilder::DSBGraph sb_graph
)
: _sb_graph(sb_graph), _vertex_map() {}

template<typename Set>
OG::DGraph OrdinaryDGraphBuilder<Set>::build()
{
OG::DGraph graph;

for (const SBG::LIB::SetPiece &v : _sb_graph.V()) {
assert(!v.isEmpty());
SBG::Util::MD_NAT beg = v.minElem(), end = v.maxElem();
for (auto it = beg; it != end; it = nextElem(it, v))
addVertex(it, graph);
addVertex(end, graph);
}

for (const SBG::LIB::SetPiece &e : _sb_graph.E()) {
assert(!e.isEmpty());
SBG::Util::MD_NAT beg = e.minElem(), end = e.maxElem();
for (auto it = beg; it != end; it = nextElem(it, e))
addEdge(it, graph);
addEdge(end, graph);
}

return graph;
}

template<typename Set>
OG::DVertexDesc OrdinaryDGraphBuilder<Set>::addVertex(
SBG::Util::MD_NAT id, OG::DGraph &g
)
{
Vertex V(id);
OG::DVertexDesc v = boost::add_vertex(g);
_vertex_map[id] = v;
g[v] = V;
return v;
}

template<typename Set>
OG::DEdgeDesc OrdinaryDGraphBuilder<Set>::addEdge(
SBG::Util::MD_NAT id, OG::DGraph &g
)
{
SBG::LIB::SetPiece mdi(id);
PW mapb = _sb_graph.mapB(), mapd = _sb_graph.mapD();
SBG::Util::MD_NAT v1 = mapb.image(Set(mdi)).minElem();
SBG::Util::MD_NAT v2 = mapd.image(Set(mdi)).minElem();

Edge E(id);
OG::DEdgeDesc e;
bool b;
boost::tie(e, b) = boost::add_edge(_vertex_map[v1], _vertex_map[v2], g);
g[e] = E;
return e;
}

template class OrdinaryDGraphBuilder<SBG::LIB::UnordSet>;

template class OrdinaryDGraphBuilder<SBG::LIB::OrdSet>;

} // namespace OG
25 changes: 23 additions & 2 deletions test/performance/boost/ordinary_graph_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,29 @@ class OrdinaryGraphBuilder {
};

typedef OrdinaryGraphBuilder<SBG::LIB::UnordSet> BaseBuilder;
typedef OrdinaryGraphBuilder<SBG::LIB::OrdSet> CanonBuilder;

typedef OrdinaryGraphBuilder<SBG::LIB::OrdSet> CanonBuilder;

template<typename Set>
class OrdinaryDGraphBuilder {
using PW = SBG::LIB::PWMap<Set>;
using DSBGraph = SBG::LIB::DSBGraph<Set>;

public:
OrdinaryDGraphBuilder(DSBGraph graph);
~OrdinaryDGraphBuilder() = default;

virtual OG::DGraph build();

protected:
OG::DVertexDesc addVertex(SBG::Util::MD_NAT id, OG::DGraph &g);
OG::DEdgeDesc addEdge(SBG::Util::MD_NAT id, OG::DGraph &g);

DSBGraph _sb_graph;
std::map<SBG::Util::MD_NAT, OG::DVertexDesc> _vertex_map;
};

typedef OrdinaryDGraphBuilder<SBG::LIB::UnordSet> BaseDBuilder;
typedef OrdinaryDGraphBuilder<SBG::LIB::OrdSet> CanonDBuilder;

} // namespace OG

Expand Down
Loading

0 comments on commit 2afb5d6

Please sign in to comment.