Skip to content

Commit

Permalink
CSR implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean De Leo committed Sep 19, 2020
1 parent 1f6ca55 commit 6d80a23
Show file tree
Hide file tree
Showing 10 changed files with 1,650 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ sources := \
graph/vertex_list.cpp \
library/interface.cpp \
library/baseline/adjacency_list.cpp \
library/baseline/csr.cpp \
library/baseline/dummy.cpp \
network/client.cpp \
network/internal.cpp \
Expand Down
78 changes: 74 additions & 4 deletions graph/edge_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,18 @@ void WeightedEdgeStream::permute(uint64_t seed){
common::permute(permutation, m_num_edges, seed);

// Permute the elements in m_sources, m_destinations and m_weights
do_permute_edges(permutation);

timer.stop();

LOG("Permutation completed in " << timer);
}

void WeightedEdgeStream::do_permute_edges(uint64_t* permutation){
auto bytes_per_vertex_id = CByteArray::compute_bytes_per_elements(m_max_vertex_id);
auto new_sources = make_unique<CByteArray>(/* bytes per element */ bytes_per_vertex_id, m_num_edges);
auto new_destinations = make_unique<CByteArray>(/* bytes per element */ bytes_per_vertex_id, m_num_edges);
vector<double> new_weights; new_weights.resize(m_num_edges, 0.0);
// auto new_weights = make_unique<CByteArray>(/* bytes per element */ CByteArray::compute_bytes_per_elements(m_max_weight), m_num_edges);

auto permute = [&](uint64_t start, uint64_t length){
for(size_t i = start, end = start + length; i < end; i++){
Expand All @@ -155,11 +162,9 @@ void WeightedEdgeStream::permute(uint64_t seed){
delete m_destinations; m_destinations = new_destinations.release();
m_weights = std::move(new_weights);

timer.stop();

LOG("Permutation completed in " << timer);
}


WeightedEdge WeightedEdgeStream::get(uint64_t index) const {
if(index >= num_edges()){ INVALID_ARGUMENT("Index out of bound: " << index << " >= " << num_edges()); }
return WeightedEdge { m_sources->get_value_at(index), m_destinations->get_value_at(index), m_weights[index] };
Expand Down Expand Up @@ -228,6 +233,71 @@ unique_ptr<cuckoohash_map<uint64_t, uint64_t>> WeightedEdgeStream::vertex_table(
return ptr_vertex_table;
}

void WeightedEdgeStream::sort() {
sort_by_src_dst();
}

void WeightedEdgeStream::sort_by_src_dst(){
if(m_num_edges <= 0) return; // there is nothing to sort

LOG("Sorting the edge list by <source, destination> ...");

Timer timer;
timer.start();

// Create the permutation array
auto ptr_permutation = make_unique<uint64_t[]>(m_num_edges);
uint64_t* __restrict permutation = ptr_permutation.get();
for(size_t i = 0; i < m_num_edges; i++){ permutation[i] = i; }

std::sort(permutation, permutation + m_num_edges, [this](uint64_t i, uint64_t j){
assert(i != j);
uint64_t source_i = m_sources->get_value_at(i);
uint64_t source_j = m_sources->get_value_at(j);
if(source_i < source_j) {
return true;
} else if (source_i == source_j){
uint64_t dest_i = m_destinations->get_value_at(i);
uint64_t dest_j = m_destinations->get_value_at(j);
return dest_i < dest_j;
} else { // source_i > source_j
return false;
}
});

do_permute_edges(permutation);

timer.stop();

LOG("Sorting completed in " << timer);
}

void WeightedEdgeStream::sort_by_dst_src(){
if(m_num_edges <= 0) return; // there is nothing to sort

LOG("Sorting the edge list by <destination, source> ...");

Timer timer;
timer.start();

// Create the permutation array
auto ptr_permutation = make_unique<uint64_t[]>(m_num_edges);
uint64_t* __restrict permutation = ptr_permutation.get();
for(size_t i = 0; i < m_num_edges; i++){ permutation[i] = i; }

std::sort(permutation, permutation + m_num_edges, [this](uint64_t i, uint64_t j){
assert(i != j);
return m_destinations->get_value_at(i) < m_destinations->get_value_at(j) ||
(m_destinations->get_value_at(i) == m_destinations->get_value_at(j) && m_sources->get_value_at(i) < m_sources->get_value_at(j));
});

do_permute_edges(permutation);

timer.stop();

LOG("Sorting completed in " << timer);
}

} // namespace


10 changes: 10 additions & 0 deletions graph/edge_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class WeightedEdgeStream {
uint64_t m_max_vertex_id { 0 };
double m_max_weight { 0 };

// Permute the edges according to the given permutation vector, with indices in 0, ..., num_edges -1
void do_permute_edges(uint64_t* permutation);

public:
/**
* Load the list of edges from the given file
Expand Down Expand Up @@ -89,6 +92,13 @@ class WeightedEdgeStream {

// Get the max weight for an edge in the graph
double max_weight() const { return m_max_weight; }

// Sort the edge list by <src, dst>
void sort();
void sort_by_src_dst();

// Sort the edge list by <dst, src>
void sort_by_dst_src();
};

} // namespace
Expand Down
4 changes: 4 additions & 0 deletions graph/vertex_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ void VertexList::permute(uint64_t seed){
delete m_vertices; m_vertices = new_vertices.release();
}

void VertexList::sort(){
m_vertices->sort();
}

} // namespace
4 changes: 4 additions & 0 deletions graph/vertex_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class VertexList {
*/
void permute(uint64_t seed);

/**
* Sort the list of vertices
*/
void sort();
};

} // namespace
Loading

0 comments on commit 6d80a23

Please sign in to comment.