Skip to content

Commit

Permalink
removed some duplicate sorts
Browse files Browse the repository at this point in the history
  • Loading branch information
etwest committed Jan 2, 2025
1 parent d6d71d1 commit afa83c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
5 changes: 4 additions & 1 deletion include/return_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class ConnectedComponents {
// This class defines a spanning forest of a graph
class SpanningForest {
private:
std::vector<Edge> edges;
node_id_t num_vertices;
std::vector<Edge> edges;
std::vector<Edge> sorted_adjacency;
bool has_adjacency = false;
public:
SpanningForest(node_id_t num_vertices, const std::unordered_set<node_id_t> *spanning_forest);

const std::vector<Edge>& get_edges() const { return edges; }
const std::vector<Edge>& get_sorted_adjacency();
};
29 changes: 4 additions & 25 deletions src/cc_sketch_alg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ SpanningForest CCSketchAlg::calc_spanning_forest() {
connected_components();

SpanningForest ret(num_vertices, spanning_forest);

#ifdef VERIFY_SAMPLES_F
verifier->verify_spanning_forests(std::vector<SpanningForest>{ret});
#endif
Expand All @@ -573,24 +574,7 @@ void CCSketchAlg::filter_sf_edges(SpanningForest &sf) {
dsu_valid = false;
shared_dsu_valid = false;

auto edges = sf.get_edges();
size_t num = edges.size();
edges.resize(2 * edges.size());

#pragma omp parallel for
for (size_t i = 0; i < num; i++) {
edges[i + num] = edges[i];
std::swap(edges[i + num].src, edges[i + num].dst);
}

auto setup = std::chrono::steady_clock::now();
std::cout << "Setup time = " << std::chrono::duration<double>(setup - start).count() << std::endl;

// sort the edges
std::sort(edges.begin(), edges.end());

auto sort = std::chrono::steady_clock::now();
std::cout << "Sort time = " << std::chrono::duration<double>(sort - setup).count() << std::endl;
const std::vector<Edge> &edges = sf.get_sorted_adjacency();

#pragma omp parallel
{
Expand Down Expand Up @@ -632,8 +616,6 @@ void CCSketchAlg::filter_sf_edges(SpanningForest &sf) {
sketches[edge.src]->update(static_cast<vec_t>(concat_pairing_fn(edge.src, edge.dst)));
}
}
auto del = std::chrono::steady_clock::now();
std::cout << "Delete time = " << std::chrono::duration<double>(del - sort).count() << std::endl;

delete_time += std::chrono::steady_clock::now() - start;
}
Expand All @@ -644,20 +626,17 @@ std::vector<SpanningForest> CCSketchAlg::calc_disjoint_spanning_forests(size_t k

for (size_t i = 0; i < k; i++) {
start = std::chrono::steady_clock::now();
SpanningForest sf = calc_spanning_forest();
SFs.push_back(sf);
SFs.push_back(calc_spanning_forest());
query_time += std::chrono::steady_clock::now() - start;

filter_sf_edges(sf);
filter_sf_edges(SFs[SFs.size() - 1]);
}

// revert the state of the sketches to remove all deletions
for (auto &sf : SFs) {
filter_sf_edges(sf);
}

std::cout << "Number of SFs: " << SFs.size() << std::endl;

#ifdef VERIFY_SAMPLES_F
verifier->verify_spanning_forests(SFs);
#endif
Expand Down
21 changes: 21 additions & 0 deletions src/return_types.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "return_types.h"

#include <map>
#include <algorithm>

ConnectedComponents::ConnectedComponents(node_id_t num_vertices,
DisjointSetUnion_MT<node_id_t> &dsu)
Expand Down Expand Up @@ -39,3 +40,23 @@ SpanningForest::SpanningForest(node_id_t num_vertices,
}
}
}

const std::vector<Edge> &SpanningForest::get_sorted_adjacency() {
if (has_adjacency) return sorted_adjacency;

size_t num = edges.size();
sorted_adjacency.resize(edges.size() * 2);

#pragma omp parallel for
for (size_t i = 0; i < num; i++) {
sorted_adjacency[i] = edges[i];
sorted_adjacency[i + num] = sorted_adjacency[i];
std::swap(sorted_adjacency[i + num].src, sorted_adjacency[i + num].dst);
}

// sort the edges
std::sort(sorted_adjacency.begin(), sorted_adjacency.end());

has_adjacency = true;
return sorted_adjacency;
}

0 comments on commit afa83c0

Please sign in to comment.