Skip to content

Commit

Permalink
Triangle counting C API implementation (rapidsai#2302)
Browse files Browse the repository at this point in the history
Connect C API to new C++ implementation for triangle counting.

Authors:
  - Chuck Hastings (https://github.com/ChuckHastings)

Approvers:
  - Seunghwa Kang (https://github.com/seunghwak)

URL: rapidsai#2302
  • Loading branch information
ChuckHastings authored May 23, 2022
1 parent c837aaa commit 199bdd9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,10 @@ void per_v_transform_reduce_dst_nbr_intersection_of_e_endpoints(
d_vertex_partition_range_lasts_in_edge_partition_minor_range =
rmm::device_uvector<vertex_t>(row_comm_size, handle.get_stream());
auto h_vertex_partition_range_lasts = graph_view.vertex_partition_range_lasts();
raft::update_device(
(*d_vertex_partition_range_lasts_in_edge_partition_minor_range).data(),
h_vertex_partition_range_lasts.data() + row_comm_size * col_comm_rank,
h_vertex_partition_range_lasts.size() + row_comm_size * (col_comm_rank + int{1}),
handle.get_stream());
raft::update_device((*d_vertex_partition_range_lasts_in_edge_partition_minor_range).data(),
h_vertex_partition_range_lasts.data() + row_comm_size * col_comm_rank,
row_comm_size,
handle.get_stream());
}

for (size_t i = 0; i < graph_view.number_of_local_edge_partitions(); ++i) {
Expand Down
22 changes: 15 additions & 7 deletions cpp/src/c_api/triangle_count.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ struct triangle_count_functor : public cugraph::c_api::abstract_functor {
if constexpr (!cugraph::is_candidate<vertex_t, edge_t, weight_t>::value) {
unsupported();
} else {
#if 1
error_code_ = CUGRAPH_NOT_IMPLEMENTED;
error_->error_message_ = "Triangle Counting not implemented yet";
#else
// triangle counting expects store_transposed == false
if constexpr (store_transposed) {
error_code_ = cugraph::c_api::
Expand All @@ -101,6 +97,10 @@ struct triangle_count_functor : public cugraph::c_api::abstract_functor {
raft::copy(
vertices.data(), vertices_->as_type<vertex_t>(), vertices.size(), handle_.get_stream());

if constexpr (multi_gpu) {
vertices = cugraph::detail::shuffle_ext_vertices_by_gpu_id(handle_, std::move(vertices));
}

cugraph::renumber_ext_vertices<vertex_t, multi_gpu>(
handle_,
vertices.data(),
Expand All @@ -113,8 +113,7 @@ struct triangle_count_functor : public cugraph::c_api::abstract_functor {
counts.resize(graph_view.local_vertex_partition_range_size(), handle_.get_stream());
}

// cugraph::triangle_count<vertex_t, edge_t, weight_t, multi_gpu>(
cugraph::triangle_counts<vertex_t, edge_t, weight_t, multi_gpu>(
cugraph::triangle_count<vertex_t, edge_t, weight_t, multi_gpu>(
handle_,
graph_view,
vertices_ == nullptr
Expand All @@ -126,12 +125,21 @@ struct triangle_count_functor : public cugraph::c_api::abstract_functor {
if (vertices_ == nullptr) {
vertices.resize(graph_view.local_vertex_partition_range_size(), handle_.get_stream());
raft::copy(vertices.data(), number_map->data(), vertices.size(), handle_.get_stream());
} else {
std::vector<vertex_t> vertex_partition_range_lasts =
graph_view.vertex_partition_range_lasts();

cugraph::unrenumber_int_vertices<vertex_t, multi_gpu>(handle_,
vertices.data(),
vertices.size(),
number_map->data(),
vertex_partition_range_lasts,
do_expensive_check_);
}

result_ = new cugraph::c_api::cugraph_triangle_count_result_t{
new cugraph::c_api::cugraph_type_erased_device_array_t(vertices, graph_->vertex_type_),
new cugraph::c_api::cugraph_type_erased_device_array_t(counts, graph_->edge_type_)};
#endif
}
}
};
Expand Down
35 changes: 18 additions & 17 deletions cpp/tests/c_api/mg_triangle_count_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ int generic_triangle_count_test(const cugraph_resource_handle_t* handle,
cugraph_type_erased_device_array_view_t* p_start_view = NULL;

ret_code = create_mg_test_graph(
handle, h_src, h_dst, h_wgt, num_edges, store_transposed, FALSE, &p_graph, &ret_error);
handle, h_src, h_dst, h_wgt, num_edges, store_transposed, TRUE, &p_graph, &ret_error);

TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "create_mg_test_graph failed.");

Expand All @@ -64,10 +64,7 @@ int generic_triangle_count_test(const cugraph_resource_handle_t* handle,
}

ret_code = cugraph_triangle_count(handle, p_graph, p_start_view, FALSE, &p_result, &ret_error);
#if 1
TEST_ASSERT(test_ret_value, ret_code != CUGRAPH_SUCCESS, cugraph_error_message(ret_error));
TEST_ALWAYS_ASSERT(ret_code != CUGRAPH_SUCCESS, "cugraph_triangle_count expected to fail.");
#else

TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, cugraph_error_message(ret_error));
TEST_ALWAYS_ASSERT(ret_code == CUGRAPH_SUCCESS, "cugraph_triangle_count failed.");

Expand All @@ -82,8 +79,10 @@ int generic_triangle_count_test(const cugraph_resource_handle_t* handle,
cugraph_type_erased_device_array_view_size(vertices) == num_results,
"invalid number of results");

vertex_t h_vertices[num_results];
edge_t h_counts[num_results];
vertex_t num_local_results = cugraph_type_erased_device_array_view_size(vertices);

vertex_t h_vertices[num_local_results];
edge_t h_counts[num_local_results];

ret_code = cugraph_type_erased_device_array_view_copy_to_host(
handle, (byte_t*)h_vertices, vertices, &ret_error);
Expand All @@ -93,14 +92,13 @@ int generic_triangle_count_test(const cugraph_resource_handle_t* handle,
handle, (byte_t*)h_counts, counts, &ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "copy_to_host failed.");

for (int i = 0; (i < num_vertices) && (test_ret_value == 0); ++i) {
for (int i = 0; (i < num_local_results) && (test_ret_value == 0); ++i) {
TEST_ASSERT(
test_ret_value, h_result[h_vertices[i]] == h_counts[i], "counts results don't match");
}

cugraph_triangle_count_result_free(p_result);
}
#endif

cugraph_mg_graph_free(p_graph);
cugraph_error_free(ret_error);
Expand All @@ -110,15 +108,18 @@ int generic_triangle_count_test(const cugraph_resource_handle_t* handle,

int test_triangle_count(const cugraph_resource_handle_t* handle)
{
size_t num_edges = 8;
size_t num_edges = 16;
size_t num_vertices = 6;
size_t num_results = 3;

vertex_t h_src[] = {0, 1, 1, 2, 2, 2, 3, 4};
vertex_t h_dst[] = {1, 3, 4, 0, 1, 3, 5, 5};
weight_t h_wgt[] = {0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f};
vertex_t h_verts[] = {0, 1, 2};
edge_t h_result[] = {0, 0, 0};
size_t num_results = 4;

vertex_t h_src[] = {0, 1, 1, 2, 2, 2, 3, 4,
1, 3, 4, 0, 1, 3, 5, 5};
vertex_t h_dst[] = {1, 3, 4, 0, 1, 3, 5, 5,
0, 1, 1, 2, 2, 2, 3, 4};
weight_t h_wgt[] = {0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f,
0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f};
vertex_t h_verts[] = {0, 1, 2, 4};
edge_t h_result[] = {1, 2, 2, 0};

// Triangle Count wants store_transposed = FALSE
return generic_triangle_count_test(
Expand Down
34 changes: 17 additions & 17 deletions cpp/tests/c_api/triangle_count_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int generic_triangle_count_test(vertex_t* h_src,
TEST_ASSERT(test_ret_value, p_handle != NULL, "resource handle creation failed.");

ret_code = create_test_graph(
p_handle, h_src, h_dst, h_wgt, num_edges, store_transposed, FALSE, FALSE, &p_graph, &ret_error);
p_handle, h_src, h_dst, h_wgt, num_edges, store_transposed, FALSE, TRUE, &p_graph, &ret_error);

TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "create_test_graph failed.");
TEST_ALWAYS_ASSERT(ret_code == CUGRAPH_SUCCESS, cugraph_error_message(ret_error));
Expand All @@ -68,10 +68,6 @@ int generic_triangle_count_test(vertex_t* h_src,
}

ret_code = cugraph_triangle_count(p_handle, p_graph, p_start_view, FALSE, &p_result, &ret_error);
#if 1
TEST_ASSERT(test_ret_value, ret_code != CUGRAPH_SUCCESS, cugraph_error_message(ret_error));
TEST_ALWAYS_ASSERT(ret_code != CUGRAPH_SUCCESS, "cugraph_triangle_count expected to fail.");
#else
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, cugraph_error_message(ret_error));
TEST_ALWAYS_ASSERT(ret_code == CUGRAPH_SUCCESS, "cugraph_triangle_count failed.");

Expand All @@ -86,8 +82,10 @@ int generic_triangle_count_test(vertex_t* h_src,
cugraph_type_erased_device_array_view_size(vertices) == num_results,
"invalid number of results");

vertex_t h_vertices[num_results];
edge_t h_counts[num_results];
vertex_t num_local_results = cugraph_type_erased_device_array_view_size(vertices);

vertex_t h_vertices[num_local_results];
edge_t h_counts[num_local_results];

ret_code = cugraph_type_erased_device_array_view_copy_to_host(
p_handle, (byte_t*)h_vertices, vertices, &ret_error);
Expand All @@ -97,14 +95,13 @@ int generic_triangle_count_test(vertex_t* h_src,
p_handle, (byte_t*)h_counts, counts, &ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "copy_to_host failed.");

for (int i = 0; (i < num_vertices) && (test_ret_value == 0); ++i) {
for (int i = 0; (i < num_local_results) && (test_ret_value == 0); ++i) {
TEST_ASSERT(
test_ret_value, h_result[h_vertices[i]] == h_counts[i], "counts results don't match");
}

cugraph_triangle_count_result_free(p_result);
}
#endif

cugraph_sg_graph_free(p_graph);
cugraph_free_resource_handle(p_handle);
Expand All @@ -115,15 +112,18 @@ int generic_triangle_count_test(vertex_t* h_src,

int test_triangle_count()
{
size_t num_edges = 8;
size_t num_edges = 16;
size_t num_vertices = 6;
size_t num_results = 3;

vertex_t h_src[] = {0, 1, 1, 2, 2, 2, 3, 4};
vertex_t h_dst[] = {1, 3, 4, 0, 1, 3, 5, 5};
weight_t h_wgt[] = {0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f};
vertex_t h_verts[] = {0, 1, 2};
edge_t h_result[] = {0, 0, 0};
size_t num_results = 4;

vertex_t h_src[] = {0, 1, 1, 2, 2, 2, 3, 4,
1, 3, 4, 0, 1, 3, 5, 5};
vertex_t h_dst[] = {1, 3, 4, 0, 1, 3, 5, 5,
0, 1, 1, 2, 2, 2, 3, 4};
weight_t h_wgt[] = {0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f,
0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f};
vertex_t h_verts[] = {0, 1, 2, 4};
edge_t h_result[] = {1, 2, 2, 0};

// Triangle Count wants store_transposed = FALSE
return generic_triangle_count_test(
Expand Down

0 comments on commit 199bdd9

Please sign in to comment.