Skip to content

Commit

Permalink
Solved a bug where the mutex are locked twice
Browse files Browse the repository at this point in the history
Fix #49
  • Loading branch information
ZigRazor committed Aug 26, 2021
1 parent 9a7c555 commit 7daeb9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions benchmark/Graph_BM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ static auto nodes = generateRandomNodes(100000, 2);
static auto edges = generateRandomEdges(100000, nodes);

static CXXGRAPH::Graph<int> *graph;
static CXXGRAPH::Graph_TS<int> *graph_ts;

static void GraphCreation(benchmark::State &state)
{
Expand Down Expand Up @@ -45,7 +46,7 @@ static void AddEdgeX(benchmark::State &state)
}
}
}
BENCHMARK(AddEdgeX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 18);
BENCHMARK(AddEdgeX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void GraphCreation_TS(benchmark::State &state)
{
Expand Down Expand Up @@ -85,13 +86,13 @@ static void AddEdgeX_TS(benchmark::State &state)
}
}
}
BENCHMARK(AddEdgeX_TS)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 18);
BENCHMARK(AddEdgeX_TS)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void BM_AddEdgeX_MT_TS(benchmark::State &state)
{
if (state.thread_index == 0)
{
graph = new CXXGRAPH::Graph_TS<int>();
graph_ts = new CXXGRAPH::Graph_TS<int>();
}
auto subrange = state.range(0) / state.threads;
auto range_start = edges.find(subrange * state.thread_index);
Expand All @@ -102,15 +103,15 @@ static void BM_AddEdgeX_MT_TS(benchmark::State &state)
{
for (auto e : edgesX)
{
graph->addEdge(&(*e.second));
graph_ts->addEdge(&(*e.second));
}
}
if (state.thread_index == 0)
{
delete graph;
delete graph_ts;
}
}
BENCHMARK(BM_AddEdgeX_MT_TS)->RangeMultiplier(16)->Range((unsigned long)1 << 4, (unsigned long)1 << 18)->ThreadRange(1, 4);
BENCHMARK(BM_AddEdgeX_MT_TS)->RangeMultiplier(16)->Range((unsigned long)1 << 4, (unsigned long)1 << 16)->ThreadRange(1, 4);

static void RemoveEdge(benchmark::State &state)
{
Expand Down Expand Up @@ -145,7 +146,7 @@ static void RemoveEdgeX(benchmark::State &state)
}
}
}
BENCHMARK(RemoveEdgeX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 18);
BENCHMARK(RemoveEdgeX)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void RemoveEdge_TS(benchmark::State &state)
{
Expand Down Expand Up @@ -180,35 +181,36 @@ static void RemoveEdgeX_TS(benchmark::State &state)
}
}
}
BENCHMARK(RemoveEdgeX_TS)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 18);
BENCHMARK(RemoveEdgeX_TS)->RangeMultiplier(16)->Range((unsigned long)1, (unsigned long)1 << 16);

static void RemoveEdgeX_MT_TS(benchmark::State &state)
{
if (state.thread_index == 0)
{
graph = new CXXGRAPH::Graph_TS<int>();
graph_ts = new CXXGRAPH::Graph_TS<int>();
}
sleep(1); //let the possibility to create the Graph and avoid segmentation fault
auto subrange = state.range(0) / state.threads;
auto range_start = edges.find(subrange * state.thread_index);
auto range_end = edges.find(subrange * (state.thread_index + 1));
std::map<unsigned long, CXXGRAPH::Edge<int> *> edgesX;
edgesX.insert(range_start, range_end);
for (auto e : edgesX)
{
graph->addEdge(&(*e.second));
graph_ts->addEdge(&(*e.second));
}
for (auto _ : state)
{

for (auto e : edgesX)
{
graph->removeEdge(e.second->getId());
graph_ts->removeEdge(e.second->getId());
}
}

if (state.thread_index == 0)
{
delete graph;
delete graph_ts;
}
}
BENCHMARK(RemoveEdgeX_MT_TS)->RangeMultiplier(16)->Range((unsigned long)1 << 4, (unsigned long)1 << 18)->ThreadRange(1, 4);
BENCHMARK(RemoveEdgeX_MT_TS)->RangeMultiplier(16)->Range((unsigned long)1 << 4, (unsigned long)1 << 16)->ThreadRange(1, 4);
2 changes: 1 addition & 1 deletion include/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ namespace CXXGRAPH
template <typename T>
void Graph<T>::removeEdge(unsigned long edgeId)
{
auto edgeOpt = getEdge(edgeId);
auto edgeOpt = Graph<T>::getEdge(edgeId);
if (edgeOpt.has_value())
{
edgeSet.erase(std::find_if(this->edgeSet.begin(), this->edgeSet.end(), [edgeOpt](const Edge<T> *edge)
Expand Down

0 comments on commit 7daeb9b

Please sign in to comment.