Skip to content

Commit

Permalink
Update SG PageRank C++ tests(#1307)
Browse files Browse the repository at this point in the history
- Add const to input pointers.
- Use a double type counter in std::accumulate as std::accumulate is inaccurate in adding a large number of a comparably sized values.
- Fix random number generator seed.
- Re-enable a temporarily disabled test case.
- Relax the thresholds to skip comparison for lowly ranked vertices (with low scores which are more susceptible to the limited floating-point resolution)

Authors:
  - Seunghwa Kang <[email protected]>

Approvers:
  - Andrei Schaffer (@aschaffer)
  - Brad Rees (@BradReesWork)

URL: #1307
  • Loading branch information
seunghwak authored Jan 6, 2021
1 parent 499d956 commit 896db87
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cpp/tests/experimental/bfs_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include <vector>

template <typename vertex_t, typename edge_t>
void bfs_reference(edge_t* offsets,
vertex_t* indices,
void bfs_reference(edge_t const* offsets,
vertex_t const* indices,
vertex_t* distances,
vertex_t* predecessors,
vertex_t num_vertices,
Expand Down
11 changes: 6 additions & 5 deletions cpp/tests/experimental/katz_centrality_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
#include <vector>

template <typename vertex_t, typename edge_t, typename weight_t, typename result_t>
void katz_centrality_reference(edge_t* offsets,
vertex_t* indices,
weight_t* weights,
result_t* betas,
void katz_centrality_reference(edge_t const* offsets,
vertex_t const* indices,
weight_t const* weights,
result_t const* betas,
result_t* katz_centralities,
vertex_t num_vertices,
result_t alpha,
Expand Down Expand Up @@ -195,7 +195,8 @@ class Tests_KatzCentrality : public ::testing::TestWithParam<KatzCentrality_Usec

auto threshold_ratio = 1e-3;
auto threshold_magnitude =
(epsilon / static_cast<result_t>(graph_view.get_number_of_vertices())) * threshold_ratio;
(1.0 / static_cast<result_t>(graph_view.get_number_of_vertices())) *
threshold_ratio; // skip comparison for low Katz Centrality verties (lowly ranked vertices)
auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) {
auto diff = std::abs(lhs - rhs);
return (diff < std::max(lhs, rhs) * threshold_ratio) || (diff < threshold_magnitude);
Expand Down
48 changes: 28 additions & 20 deletions cpp/tests/experimental/pagerank_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
#include <vector>

template <typename vertex_t, typename edge_t, typename weight_t, typename result_t>
void pagerank_reference(edge_t* offsets,
vertex_t* indices,
weight_t* weights,
vertex_t* personalization_vertices,
result_t* personalization_values,
void pagerank_reference(edge_t const* offsets,
vertex_t const* indices,
weight_t const* weights,
vertex_t const* personalization_vertices,
result_t const* personalization_values,
result_t* pageranks,
vertex_t num_vertices,
vertex_t personalization_vector_size,
Expand All @@ -52,7 +52,11 @@ void pagerank_reference(edge_t* offsets,
if (num_vertices == 0) { return; }

if (has_initial_guess) {
auto sum = std::accumulate(pageranks, pageranks + num_vertices, result_t{0.0});
// use a double type counter (instead of result_t) to accumulate as std::accumulate is
// inaccurate in adding a large number of comparably sized numbers. In C++17 or later,
// std::reduce may be a better option.
auto sum =
static_cast<result_t>(std::accumulate(pageranks, pageranks + num_vertices, double{0.0}));
ASSERT_TRUE(sum > 0.0);
std::for_each(pageranks, pageranks + num_vertices, [sum](auto& val) { val /= sum; });
} else {
Expand All @@ -61,13 +65,14 @@ void pagerank_reference(edge_t* offsets,
});
}

result_t personalization_sum{0.0};
if (personalization_vertices != nullptr) {
auto sum = std::accumulate(
personalization_values, personalization_values + personalization_vector_size, result_t{0.0});
ASSERT_TRUE(sum > 0.0);
std::for_each(personalization_values,
personalization_values + personalization_vector_size,
[sum](auto& val) { val /= sum; });
// use a double type counter (instead of result_t) to accumulate as std::accumulate is
// inaccurate in adding a large number of comparably sized numbers. In C++17 or later,
// std::reduce may be a better option.
personalization_sum = static_cast<result_t>(std::accumulate(
personalization_values, personalization_values + personalization_vector_size, double{0.0}));
ASSERT_TRUE(personalization_sum > 0.0);
}

std::vector<weight_t> out_weight_sums(num_vertices, result_t{0.0});
Expand Down Expand Up @@ -102,7 +107,8 @@ void pagerank_reference(edge_t* offsets,
if (personalization_vertices != nullptr) {
for (vertex_t i = 0; i < personalization_vector_size; ++i) {
auto v = personalization_vertices[i];
pageranks[v] += (dangling_sum * alpha + (1.0 - alpha)) * personalization_values[i];
pageranks[v] += (dangling_sum * alpha + (1.0 - alpha)) *
(personalization_values[i] / personalization_sum);
}
}
result_t diff_sum{0.0};
Expand Down Expand Up @@ -177,8 +183,7 @@ class Tests_PageRank : public ::testing::TestWithParam<PageRank_Usecase> {
std::vector<vertex_t> h_personalization_vertices{};
std::vector<result_t> h_personalization_values{};
if (configuration.personalization_ratio > 0.0) {
std::random_device r{};
std::default_random_engine generator{r()};
std::default_random_engine generator{};
std::uniform_real_distribution<double> distribution{0.0, 1.0};
h_personalization_vertices.resize(graph_view.get_number_of_local_vertices());
std::iota(h_personalization_vertices.begin(),
Expand All @@ -195,8 +200,11 @@ class Tests_PageRank : public ::testing::TestWithParam<PageRank_Usecase> {
std::for_each(h_personalization_values.begin(),
h_personalization_values.end(),
[&distribution, &generator](auto& val) { val = distribution(generator); });
auto sum = std::accumulate(
h_personalization_values.begin(), h_personalization_values.end(), result_t{0.0});
// use a double type counter (instead of result_t) to accumulate as std::accumulate is
// inaccurate in adding a large number of comparably sized numbers. In C++17 or later,
// std::reduce may be a better option.
auto sum = static_cast<result_t>(std::accumulate(
h_personalization_values.begin(), h_personalization_values.end(), double{0.0}));
std::for_each(h_personalization_values.begin(),
h_personalization_values.end(),
[sum](auto& val) { val /= sum; });
Expand Down Expand Up @@ -263,7 +271,8 @@ class Tests_PageRank : public ::testing::TestWithParam<PageRank_Usecase> {

auto threshold_ratio = 1e-3;
auto threshold_magnitude =
(epsilon / static_cast<result_t>(graph_view.get_number_of_vertices())) * threshold_ratio;
(1.0 / static_cast<result_t>(graph_view.get_number_of_vertices())) *
threshold_ratio; // skip comparison for low PageRank verties (lowly ranked vertices)
auto nearly_equal = [threshold_ratio, threshold_magnitude](auto lhs, auto rhs) {
auto diff = std::abs(lhs - rhs);
return (diff < std::max(lhs, rhs) * threshold_ratio) || (diff < threshold_magnitude);
Expand Down Expand Up @@ -299,8 +308,7 @@ INSTANTIATE_TEST_CASE_P(
PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.0, true),
PageRank_Usecase("test/datasets/ljournal-2008.mtx", 0.5, true),
PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.0, false),
// FIXME: Re-enable test after failures are addressed
// PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.5, false),
PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.5, false),
PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.0, true),
PageRank_Usecase("test/datasets/webbase-1M.mtx", 0.5, true)));

Expand Down
6 changes: 3 additions & 3 deletions cpp/tests/experimental/sssp_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

// Dijkstra's algorithm
template <typename vertex_t, typename edge_t, typename weight_t>
void sssp_reference(edge_t* offsets,
vertex_t* indices,
weight_t* weights,
void sssp_reference(edge_t const* offsets,
vertex_t const* indices,
weight_t const* weights,
weight_t* distances,
vertex_t* predecessors,
vertex_t num_vertices,
Expand Down

0 comments on commit 896db87

Please sign in to comment.