Skip to content

Commit

Permalink
Use LAGraph_dense_relabel during loading. Closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
marci543 committed May 25, 2020
1 parent d573b95 commit 96a402f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
30 changes: 18 additions & 12 deletions cpp/Query1.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@ class Query1 : public Query<uint64_t, uint64_t, int> {
int comment_lower_limit;

std::tuple<std::string, std::string> initial_calculation() override {

GrB_Matrix filtered_matrix_ptr;

GBxx_Object<GrB_Matrix> personAToComment2 = GB(GrB_Matrix_new, GrB_UINT64, input.person_size(), input.comment_size());
GBxx_Object<GrB_Matrix> personAToComment2 = GB(GrB_Matrix_new, GrB_UINT64, input.person_size(),
input.comment_size());

ok(GrB_mxm(personAToComment2.get(), GrB_NULL, GrB_NULL, GxB_PLUS_TIMES_INT64, input.hasCreatorTran.matrix.get(), input.replyOf.matrix.get(), GrB_NULL));
// ok(GxB_Matrix_fprint(personAToComment2.get(), "personAToComment2", GxB_SUMMARY, stdout));
ok(GrB_mxm(personAToComment2.get(), GrB_NULL, GrB_NULL, GxB_PLUS_TIMES_INT64, input.hasCreatorTran.matrix.get(),
input.replyOf.matrix.get(), GrB_NULL));
// ok(GxB_Matrix_fprint(personAToComment2.get(), "personAToComment2", GxB_SUMMARY, stdout));

GBxx_Object<GrB_Matrix> personToPerson = GB(GrB_Matrix_new, GrB_UINT64, input.person_size(), input.person_size());
GBxx_Object<GrB_Matrix> personToPerson = GB(GrB_Matrix_new, GrB_UINT64, input.person_size(),
input.person_size());

ok(GrB_mxm(personToPerson.get(), input.knows.matrix.get(), GrB_NULL, GxB_PLUS_TIMES_INT64, personAToComment2.get(), input.hasCreator.matrix.get(), GrB_NULL));
// ok(GxB_Matrix_fprint(personToPerson.get(), "personToPerson", GxB_SUMMARY, stdout));
ok(GrB_mxm(personToPerson.get(), input.knows.matrix.get(), GrB_NULL, GxB_PLUS_TIMES_INT64,
personAToComment2.get(), input.hasCreator.matrix.get(), GrB_NULL));
// ok(GxB_Matrix_fprint(personToPerson.get(), "personToPerson", GxB_SUMMARY, stdout));

if (comment_lower_limit == -1) {
filtered_matrix_ptr = input.knows.matrix.get();

} else {
auto limit = GB(GxB_Scalar_new, GrB_INT32);
ok(GxB_Scalar_setElement_INT32(limit.get(), comment_lower_limit));
ok(GxB_Matrix_select(personToPerson.get(), GrB_NULL, GrB_NULL, GxB_GT_THUNK, personToPerson.get(), limit.get(), GrB_NULL));
ok(GxB_Matrix_select(personToPerson.get(), GrB_NULL, GrB_NULL, GxB_GT_THUNK, personToPerson.get(),
limit.get(), GrB_NULL));
filtered_matrix_ptr = personToPerson.get();
}

Expand All @@ -47,7 +52,8 @@ class Query1 : public Query<uint64_t, uint64_t, int> {
#endif

//Person to person is symmtetric, so no need to transpose
GBxx_Object<GrB_Vector> v_output = GB(LAGraph_bfs_pushpull, nullptr, filtered_matrix_ptr, filtered_matrix_ptr, p1, GrB_NULL, false);
GBxx_Object<GrB_Vector> v_output = GB(LAGraph_bfs_pushpull, nullptr, filtered_matrix_ptr, filtered_matrix_ptr,
p1, GrB_NULL, false);
#ifndef NDEBUG
ok(GxB_Vector_fprint(v_output.get(), "output_vec", GxB_SUMMARY, stdout));
#endif
Expand All @@ -57,7 +63,7 @@ class Query1 : public Query<uint64_t, uint64_t, int> {
//Decrementing result, because levels in BFS start at 1
result--;
std::string result_str, comment_str;

result_str = std::to_string(result);
return {result_str, comment_str};
}
Expand All @@ -69,7 +75,7 @@ class Query1 : public Query<uint64_t, uint64_t, int> {

std::tie(p1_id, p2_id, comment_lower_limit) = queryParams;

p1 = input.persons.id_to_index.find(p1_id)->second;
p2 = input.persons.id_to_index.find(p2_id)->second;
ok(GrB_Vector_extractElement_UINT64(&p1, input.persons.idToIndex.get(), p1_id));
ok(GrB_Vector_extractElement_UINT64(&p2, input.persons.idToIndex.get(), p2_id));
}
};
4 changes: 2 additions & 2 deletions cpp/Query2.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Query2 : public Query<int, std::string> {
std::vector<time_t> birthdays;
birthdays.resize(input.persons.size());
all_indices.resize(input.persons.size());
std::transform(input.persons.vertices.begin(), input.persons.vertices.end(), birthdays.begin(),
std::transform(input.persons.vertexExtraValues.begin(), input.persons.vertexExtraValues.end(), birthdays.begin(),
[](auto &p) { return p.birthday; });
std::iota(all_indices.begin(), all_indices.end(), 0);
ok(GrB_Vector_build_INT64(birthday_person_mask.get(), all_indices.data(), birthdays.data(), birthdays.size(),
Expand Down Expand Up @@ -103,7 +103,7 @@ class Query2 : public Query<int, std::string> {

score = *std::max_element(component_sizes.begin(), component_sizes.end());
}
tag_scores_local.add({score, input.tags.vertices[tag_index].name});
tag_scores_local.add({score, input.tags.vertexExtraValues[tag_index].name});
}

#pragma omp critical(Q2_merge_thread_local_toplists)
Expand Down
10 changes: 4 additions & 6 deletions cpp/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
#include "load.h"

struct Vertex {
uint64_t id;

static auto extraColumns() {
return array_of<char const *>();
}

template<typename Reader>
bool parseLine(Reader &csv_reader) {
bool parseLine(Reader &csv_reader, GrB_Index &id) {
return csv_reader.read_row(id);
}
};
Expand All @@ -26,7 +24,7 @@ struct Place : public Vertex {
}

template<typename Reader>
bool parseLine(Reader &csv_reader) {
bool parseLine(Reader &csv_reader, GrB_Index &id) {
return csv_reader.read_row(id, name);
}
};
Expand All @@ -39,7 +37,7 @@ struct Tag : public Vertex {
}

template<typename Reader>
bool parseLine(Reader &csv_reader) {
bool parseLine(Reader &csv_reader, GrB_Index &id) {
return csv_reader.read_row(id, name);
}
};
Expand All @@ -52,7 +50,7 @@ struct Person : public Vertex {
}

template<typename Reader>
bool parseLine(Reader &csv_reader) {
bool parseLine(Reader &csv_reader, GrB_Index &id) {
const char *birthday_str = nullptr;
if (!csv_reader.read_row(id, birthday_str))
return false;
Expand Down
2 changes: 1 addition & 1 deletion cpp/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const BaseVertexCollection &EdgeCollection::findVertexCollection(const std::stri
const std::vector<std::reference_wrapper<BaseVertexCollection>> &vertex_collection) {
auto iterator = std::find_if(vertex_collection.begin(), vertex_collection.end(),
[&](const BaseVertexCollection &vertex) {
return vertex.vertex_name == vertex_name;
return vertex.vertexName == vertex_name;
});

if (iterator == vertex_collection.end())
Expand Down
40 changes: 22 additions & 18 deletions cpp/load.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ std::tuple<std::ifstream, std::vector<std::string>, std::string> openFileWithHea
std::string parseHeaderField(std::string const &field, char const *prefix, char const *postfix);

struct BaseVertexCollection {
std::string vertex_name;
std::map<uint64_t, GrB_Index> id_to_index;
std::string vertexName;
std::vector<GrB_Index> vertexIds;
GBxx_Object<GrB_Vector> idToIndex;

GrB_Index size() const {
return id_to_index.size();
return vertexIds.size();
}
};

template<typename T>
struct VertexCollection : public BaseVertexCollection {
std::vector<T> vertices;
std::vector<T> vertexExtraValues;

VertexCollection(const std::string &file_path) {
importFile(file_path);
Expand Down Expand Up @@ -60,28 +61,30 @@ struct VertexCollection : public BaseVertexCollection {
}

std::string id_column = selected_column_names[0];
vertex_name = parseHeaderField(id_column, "id:ID(", ")");
vertexName = parseHeaderField(id_column, "id:ID(", ")");

io::CSVReader<selected_columns_count, io::trim_chars<>, io::no_quote_escape<'|'>> csv_reader(file_path,
csv_file);

std::apply([&](auto... col) { csv_reader.read_header(io::ignore_extra_column, col...); },
selected_column_names);

T vertex;
while (vertex.parseLine(csv_reader)) {
GrB_Index current_index = vertices.size();

vertices.emplace_back(std::move(vertex));
bool newly_inserted = id_to_index.insert({vertex.id, current_index}).second;
assert(newly_inserted);
T vertex_extra;
GrB_Index id;
while (vertex_extra.parseLine(csv_reader, id)) {
vertexIds.push_back(id);
vertexExtraValues.emplace_back(std::move(vertex_extra));
}

GrB_Vector id_to_index_ptr = nullptr;
ok(LAGraph_dense_relabel(GrB_NULL, GrB_NULL, &id_to_index_ptr, vertexIds.data(), size(), GrB_NULL));
idToIndex.reset(id_to_index_ptr);
}
};

struct EdgeCollection {
const BaseVertexCollection *src, *trg;
GrB_Index edge_number;
GrB_Index edgeNumber;
GBxx_Object<GrB_Matrix> matrix;

EdgeCollection(const std::string &file_path,
Expand Down Expand Up @@ -118,19 +121,20 @@ struct EdgeCollection {
std::vector<GrB_Index> src_indices, trg_indices;
uint64_t src_id, trg_id;
while (csv_reader.read_row(src_id, trg_id)) {
GrB_Index src_index = src->id_to_index.find(src_id)->second;
GrB_Index trg_index = trg->id_to_index.find(trg_id)->second;
GrB_Index src_index, trg_index;
ok(GrB_Vector_extractElement_UINT64(&src_index, src->idToIndex.get(), src_id));
ok(GrB_Vector_extractElement_UINT64(&trg_index, trg->idToIndex.get(), trg_id));

src_indices.push_back(src_index);
trg_indices.push_back(trg_index);
}
edge_number = src_indices.size();
edgeNumber = src_indices.size();

matrix = GB(GrB_Matrix_new, GrB_BOOL, src->size(), trg->size());
ok(GrB_Matrix_build_BOOL(matrix.get(),
src_indices.data(), trg_indices.data(),
array_of_true(edge_number).get(),
edge_number, GrB_LOR));
array_of_true(edgeNumber).get(),
edgeNumber, GrB_LOR));
}
};

Expand Down

0 comments on commit 96a402f

Please sign in to comment.