Skip to content

Commit

Permalink
Refactor the python function symmetrizing the edgelist (#4649)
Browse files Browse the repository at this point in the history
This PR updates the python API to symmetrize the edge list through the
CAPI for PLC algorithms. This PR also deprecates the legacy python
function symmetrizing the edge list

closes #4588
  • Loading branch information
jnke2016 authored Oct 3, 2024
1 parent e6be367 commit 5fad435
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 71 deletions.
14 changes: 13 additions & 1 deletion cpp/include/cugraph_c/graph.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
* Copyright (c) 2021-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -105,6 +105,8 @@ cugraph_error_code_t cugraph_sg_graph_create(
weights,
* or take the maximum weight), the caller should remove specific edges themselves and not rely
* on this flag.
* @param [in] symmetrize If true, symmetrize the edgelist. The symmetrization of edges
* with edge_ids and/or edge_type_ids is currently not supported.
* @param [in] do_expensive_check If true, do expensive checks to validate the input data
* is consistent with software assumptions. If false bypass these checks.
* @param [out] graph A pointer to the graph object
Expand All @@ -126,6 +128,7 @@ cugraph_error_code_t cugraph_graph_create_sg(
bool_t renumber,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error);
Expand All @@ -150,6 +153,8 @@ cugraph_error_code_t cugraph_graph_create_sg(
* If false, do not renumber. Renumbering enables some significant optimizations within
* the graph primitives library, so it is strongly encouraged. Renumbering is required if
* the vertices are not sequential integer values from 0 to num_vertices.
* @param [in] symmetrize If true, symmetrize the edgelist. The symmetrization of edges
* with edge_ids and/or edge_type_ids is currently not supported.
* @param [in] do_expensive_check If true, do expensive checks to validate the input data
* is consistent with software assumptions. If false bypass these checks.
* @param [out] graph A pointer to the graph object
Expand All @@ -168,6 +173,7 @@ cugraph_error_code_t cugraph_sg_graph_create_from_csr(
const cugraph_type_erased_device_array_view_t* edge_type_ids,
bool_t store_transposed,
bool_t renumber,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error);
Expand All @@ -190,6 +196,8 @@ cugraph_error_code_t cugraph_sg_graph_create_from_csr(
* If false, do not renumber. Renumbering enables some significant optimizations within
* the graph primitives library, so it is strongly encouraged. Renumbering is required if
* the vertices are not sequential integer values from 0 to num_vertices.
* @param [in] symmetrize If true, symmetrize the edgelist. The symmetrization of edges
* with edge_ids and/or edge_type_ids is currently not supported.
* @param [in] do_expensive_check If true, do expensive checks to validate the input data
* is consistent with software assumptions. If false bypass these checks.
* @param [out] graph A pointer to the graph object
Expand All @@ -208,6 +216,7 @@ cugraph_error_code_t cugraph_graph_create_sg_from_csr(
const cugraph_type_erased_device_array_view_t* edge_type_ids,
bool_t store_transposed,
bool_t renumber,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error);
Expand Down Expand Up @@ -289,6 +298,8 @@ cugraph_error_code_t cugraph_mg_graph_create(
* Note that setting this flag will arbitrarily select one instance of a multi edge to be the
* edge that survives. If the edges have properties that should be honored (e.g. sum the
* weights, or take the maximum weight), the caller should do that on not rely on this flag.
* @param [in] symmetrize If true, symmetrize the edgelist. The symmetrization of edges
* with edge_ids and/or edge_type_ids is currently not supported.
* @param [in] do_expensive_check If true, do expensive checks to validate the input data
* is consistent with software assumptions. If false bypass these checks.
* @param [out] graph A pointer to the graph object
Expand All @@ -309,6 +320,7 @@ cugraph_error_code_t cugraph_graph_create_mg(
size_t num_arrays,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error);
Expand Down
30 changes: 30 additions & 0 deletions cpp/src/c_api/graph_mg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
bool_t renumber_;
bool_t drop_self_loops_;
bool_t drop_multi_edges_;
bool_t symmetrize_;
bool_t do_expensive_check_;
cugraph::c_api::cugraph_graph_t* result_{};

Expand All @@ -91,6 +92,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
bool_t renumber,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check)
: abstract_functor(),
properties_(properties),
Expand All @@ -109,6 +111,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
renumber_(renumber),
drop_self_loops_(drop_self_loops),
drop_multi_edges_(drop_multi_edges),
symmetrize_(symmetrize),
do_expensive_check_(do_expensive_check)
{
}
Expand Down Expand Up @@ -224,6 +227,22 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
: false);
}

if (symmetrize_) {
if (edgelist_edge_ids || edgelist_edge_types) {
// Currently doesn't support the symmetrization of edgelist with edge_ids and edge_types
unsupported();
}

// Symmetrize the edgelist
std::tie(edgelist_srcs, edgelist_dsts, edgelist_weights) =
cugraph::symmetrize_edgelist<vertex_t, weight_t, store_transposed, multi_gpu>(
handle_,
std::move(edgelist_srcs),
std::move(edgelist_dsts),
std::move(edgelist_weights),
false);
}

std::tie(*graph, new_edge_weights, new_edge_ids, new_edge_types, new_number_map) =
cugraph::create_graph_from_edgelist<vertex_t,
edge_t,
Expand Down Expand Up @@ -290,6 +309,7 @@ extern "C" cugraph_error_code_t cugraph_graph_create_mg(
size_t num_arrays,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error)
Expand Down Expand Up @@ -358,6 +378,14 @@ extern "C" cugraph_error_code_t cugraph_graph_create_mg(
if (weight_type == cugraph_data_type_id_t::NTYPES) weight_type = p_weights[i]->type_;
}

if (symmetrize == TRUE) {
CAPI_EXPECTS((properties->is_symmetric == TRUE),
CUGRAPH_INVALID_INPUT,
"Invalid input arguments: The graph property must be symmetric if 'symmetrize' "
"is set to True.",
*error);
}

CAPI_EXPECTS(p_src[i]->type_ == vertex_type,
CUGRAPH_INVALID_INPUT,
"Invalid input arguments: all vertex types must match",
Expand Down Expand Up @@ -488,6 +516,7 @@ extern "C" cugraph_error_code_t cugraph_graph_create_mg(
bool_t::TRUE,
drop_self_loops,
drop_multi_edges,
symmetrize,
do_expensive_check);

try {
Expand Down Expand Up @@ -534,6 +563,7 @@ extern "C" cugraph_error_code_t cugraph_mg_graph_create(
1,
FALSE,
FALSE,
FALSE,
do_expensive_check,
graph,
error);
Expand Down
61 changes: 61 additions & 0 deletions cpp/src/c_api/graph_sg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
bool_t renumber_;
bool_t drop_self_loops_;
bool_t drop_multi_edges_;
bool_t symmetrize_;
bool_t do_expensive_check_;
cugraph_data_type_id_t edge_type_;
cugraph::c_api::cugraph_graph_t* result_{};
Expand All @@ -58,6 +59,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
bool_t renumber,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_data_type_id_t edge_type)
: abstract_functor(),
Expand All @@ -72,6 +74,7 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
renumber_(renumber),
drop_self_loops_(drop_self_loops),
drop_multi_edges_(drop_multi_edges),
symmetrize_(symmetrize),
do_expensive_check_(do_expensive_check),
edge_type_(edge_type)
{
Expand Down Expand Up @@ -207,6 +210,22 @@ struct create_graph_functor : public cugraph::c_api::abstract_functor {
: false);
}

if (symmetrize_) {
if (edgelist_edge_ids || edgelist_edge_types) {
// Currently doesn't support the symmetrization with edge_ids and edge_types
unsupported();
}

// Symmetrize the edgelist
std::tie(edgelist_srcs, edgelist_dsts, edgelist_weights) =
cugraph::symmetrize_edgelist<vertex_t, weight_t, store_transposed, multi_gpu>(
handle_,
std::move(edgelist_srcs),
std::move(edgelist_dsts),
std::move(edgelist_weights),
false);
}

std::tie(*graph, new_edge_weights, new_edge_ids, new_edge_types, new_number_map) =
cugraph::create_graph_from_edgelist<vertex_t,
edge_t,
Expand Down Expand Up @@ -268,6 +287,7 @@ struct create_graph_csr_functor : public cugraph::c_api::abstract_functor {
cugraph::c_api::cugraph_type_erased_device_array_view_t const* edge_ids_;
cugraph::c_api::cugraph_type_erased_device_array_view_t const* edge_type_ids_;
bool_t renumber_;
bool_t symmetrize_;
bool_t do_expensive_check_;
cugraph::c_api::cugraph_graph_t* result_{};

Expand All @@ -280,6 +300,7 @@ struct create_graph_csr_functor : public cugraph::c_api::abstract_functor {
cugraph::c_api::cugraph_type_erased_device_array_view_t const* edge_ids,
cugraph::c_api::cugraph_type_erased_device_array_view_t const* edge_type_ids,
bool_t renumber,
bool_t symmetrize,
bool_t do_expensive_check)
: abstract_functor(),
properties_(properties),
Expand All @@ -290,6 +311,7 @@ struct create_graph_csr_functor : public cugraph::c_api::abstract_functor {
edge_ids_(edge_ids),
edge_type_ids_(edge_type_ids),
renumber_(renumber),
symmetrize_(symmetrize),
do_expensive_check_(do_expensive_check)
{
}
Expand Down Expand Up @@ -398,6 +420,22 @@ struct create_graph_csr_functor : public cugraph::c_api::abstract_functor {
cugraph::graph_view_t<vertex_t, edge_t, store_transposed, multi_gpu>,
edge_type_id_t>(handle_);

if (symmetrize_) {
if (edgelist_edge_ids || edgelist_edge_types) {
// Currently doesn't support the symmetrization with edge_ids and edge_types
unsupported();
}

// Symmetrize the edgelist
std::tie(edgelist_srcs, edgelist_dsts, edgelist_weights) =
cugraph::symmetrize_edgelist<vertex_t, weight_t, store_transposed, multi_gpu>(
handle_,
std::move(edgelist_srcs),
std::move(edgelist_dsts),
std::move(edgelist_weights),
false);
}

std::tie(*graph, new_edge_weights, new_edge_ids, new_edge_types, new_number_map) =
cugraph::create_graph_from_edgelist<vertex_t,
edge_t,
Expand Down Expand Up @@ -518,6 +556,7 @@ extern "C" cugraph_error_code_t cugraph_graph_create_sg(
bool_t renumber,
bool_t drop_self_loops,
bool_t drop_multi_edges,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error)
Expand All @@ -542,6 +581,14 @@ extern "C" cugraph_error_code_t cugraph_graph_create_sg(
auto p_edge_type_ids =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(edge_type_ids);

if (symmetrize == TRUE) {
CAPI_EXPECTS((properties->is_symmetric == TRUE),
CUGRAPH_INVALID_INPUT,
"Invalid input arguments: The graph property must be symmetric if 'symmetrize' is "
"set to True.",
*error);
}

CAPI_EXPECTS(p_src->size_ == p_dst->size_,
CUGRAPH_INVALID_INPUT,
"Invalid input arguments: src size != dst size.",
Expand Down Expand Up @@ -606,6 +653,7 @@ extern "C" cugraph_error_code_t cugraph_graph_create_sg(
renumber,
drop_self_loops,
drop_multi_edges,
symmetrize,
do_expensive_check,
edge_type);

Expand Down Expand Up @@ -658,6 +706,7 @@ extern "C" cugraph_error_code_t cugraph_sg_graph_create(
renumber,
FALSE,
FALSE,
FALSE,
do_expensive_check,
graph,
error);
Expand All @@ -673,6 +722,7 @@ cugraph_error_code_t cugraph_graph_create_sg_from_csr(
const cugraph_type_erased_device_array_view_t* edge_type_ids,
bool_t store_transposed,
bool_t renumber,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error)
Expand Down Expand Up @@ -707,6 +757,14 @@ cugraph_error_code_t cugraph_graph_create_sg_from_csr(
weight_type = cugraph_data_type_id_t::FLOAT32;
}

if (symmetrize == TRUE) {
CAPI_EXPECTS((properties->is_symmetric == TRUE),
CUGRAPH_INVALID_INPUT,
"Invalid input arguments: The graph property must be symmetric if 'symmetrize' is "
"set to True.",
*error);
}

CAPI_EXPECTS(
(edge_type_ids == nullptr && edge_ids == nullptr) ||
(edge_type_ids != nullptr && edge_ids != nullptr),
Expand Down Expand Up @@ -735,6 +793,7 @@ cugraph_error_code_t cugraph_graph_create_sg_from_csr(
p_edge_ids,
p_edge_type_ids,
renumber,
FALSE, // symmetrize
do_expensive_check);

try {
Expand Down Expand Up @@ -770,6 +829,7 @@ cugraph_error_code_t cugraph_sg_graph_create_from_csr(
const cugraph_type_erased_device_array_view_t* edge_type_ids,
bool_t store_transposed,
bool_t renumber,
bool_t symmetrize,
bool_t do_expensive_check,
cugraph_graph_t** graph,
cugraph_error_t** error)
Expand All @@ -783,6 +843,7 @@ cugraph_error_code_t cugraph_sg_graph_create_from_csr(
edge_type_ids,
store_transposed,
renumber,
symmetrize,
do_expensive_check,
graph,
error);
Expand Down
6 changes: 6 additions & 0 deletions cpp/tests/c_api/create_graph_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ int test_create_sg_graph_simple()
FALSE,
FALSE,
FALSE,
FALSE,
&graph,
&ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "graph creation failed.");
Expand Down Expand Up @@ -213,6 +214,7 @@ int test_create_sg_graph_csr()
FALSE,
FALSE,
FALSE,
FALSE,
&graph,
&ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "graph creation failed.");
Expand Down Expand Up @@ -408,6 +410,7 @@ int test_create_sg_graph_symmetric_error()
FALSE,
FALSE,
FALSE,
FALSE,
TRUE,
&graph,
&ret_error);
Expand Down Expand Up @@ -526,6 +529,7 @@ int test_create_sg_graph_with_isolated_vertices()
FALSE,
FALSE,
FALSE,
FALSE,
&graph,
&ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "graph creation failed.");
Expand Down Expand Up @@ -675,6 +679,7 @@ int test_create_sg_graph_csr_with_isolated()
FALSE,
FALSE,
FALSE,
FALSE,
&graph,
&ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "graph creation failed.");
Expand Down Expand Up @@ -840,6 +845,7 @@ int test_create_sg_graph_with_isolated_vertices_multi_input()
TRUE,
TRUE,
FALSE,
FALSE,
&graph,
&ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "graph creation failed.");
Expand Down
Loading

0 comments on commit 5fad435

Please sign in to comment.