Skip to content

Commit

Permalink
C API code cleanup (#2077)
Browse files Browse the repository at this point in the history
Pull out common cascading logic/error handling into a template function.  This cleans up the existing C API implementations and will make new C API implementations a bit simpler.

Note: #2069 uses the old paradigm.  Depending on merge order we might want to modify the node2vec implementation to use this technique also.  Can decide whether to do that in 2069 if this PR merges first, or if 2069 merges first we can either add that to this PR or leave it as tech debt to address later.

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

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

URL: #2077
  • Loading branch information
ChuckHastings authored Feb 16, 2022
1 parent a6df45a commit 41c3b70
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 232 deletions.
62 changes: 16 additions & 46 deletions cpp/src/c_api/bfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include <c_api/abstract_functor.hpp>
#include <c_api/graph.hpp>
#include <c_api/paths_result.hpp>
#include <c_api/utils.hpp>

#include <cugraph/algorithms.hpp>
#include <cugraph/detail/utility_wrappers.hpp>
#include <cugraph/graph_functions.hpp>
#include <cugraph/visitors/generic_cascaded_dispatch.hpp>

#include <raft/handle.hpp>

Expand All @@ -40,17 +40,17 @@ struct bfs_functor : public abstract_functor {
bool do_expensive_check_;
cugraph_paths_result_t* result_{};

bfs_functor(raft::handle_t const& handle,
cugraph_graph_t* graph,
cugraph_type_erased_device_array_view_t* sources,
bfs_functor(cugraph_resource_handle_t const* handle,
::cugraph_graph_t* graph,
::cugraph_type_erased_device_array_view_t* sources,
bool direction_optimizing,
size_t depth_limit,
bool compute_predecessors,
bool do_expensive_check)
: abstract_functor(),
handle_(handle),
graph_(graph),
sources_(sources),
handle_(*reinterpret_cast<raft::handle_t const*>(handle)),
graph_(reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph)),
sources_(reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t*>(sources)),
direction_optimizing_(direction_optimizing),
depth_limit_(depth_limit),
compute_predecessors_(compute_predecessors),
Expand Down Expand Up @@ -184,43 +184,13 @@ extern "C" cugraph_error_code_t cugraph_bfs(const cugraph_resource_handle_t* han
cugraph_paths_result_t** result,
cugraph_error_t** error)
{
*result = nullptr;
*error = nullptr;

try {
auto p_handle = reinterpret_cast<raft::handle_t const*>(handle);
auto p_graph = reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph);
auto p_sources =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t*>(sources);

cugraph::c_api::bfs_functor functor(*p_handle,
p_graph,
p_sources,
direction_optimizing,
depth_limit,
compute_predecessors,
do_expensive_check);

// FIXME: This seems like a recurring pattern. Can I encapsulate
// The vertex_dispatcher and error handling calls into a reusable function?
// After all, we're in C++ here.
cugraph::dispatch::vertex_dispatcher(cugraph::c_api::dtypes_mapping[p_graph->vertex_type_],
cugraph::c_api::dtypes_mapping[p_graph->edge_type_],
cugraph::c_api::dtypes_mapping[p_graph->weight_type_],
p_graph->store_transposed_,
p_graph->multi_gpu_,
functor);

if (functor.error_code_ != CUGRAPH_SUCCESS) {
*error = reinterpret_cast<cugraph_error_t*>(functor.error_.release());
return functor.error_code_;
}

*result = reinterpret_cast<cugraph_paths_result_t*>(functor.result_);
} catch (std::exception const& ex) {
*error = reinterpret_cast<cugraph_error_t*>(new cugraph::c_api::cugraph_error_t{ex.what()});
return CUGRAPH_UNKNOWN_ERROR;
}

return CUGRAPH_SUCCESS;
cugraph::c_api::bfs_functor functor(handle,
graph,
sources,
direction_optimizing,
depth_limit,
compute_predecessors,
do_expensive_check);

return cugraph::c_api::run_algorithm(graph, functor, result, error);
}
67 changes: 16 additions & 51 deletions cpp/src/c_api/extract_paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#include <c_api/abstract_functor.hpp>
#include <c_api/graph.hpp>
#include <c_api/paths_result.hpp>
#include <c_api/utils.hpp>

#include <cugraph/algorithms.hpp>
#include <cugraph/detail/utility_wrappers.hpp>
#include <cugraph/graph_functions.hpp>
#include <cugraph/visitors/generic_cascaded_dispatch.hpp>

#include <raft/handle.hpp>

Expand All @@ -43,17 +43,20 @@ struct extract_paths_functor : public abstract_functor {
cugraph_type_erased_device_array_view_t const* destinations_;
cugraph_extract_paths_result_t* result_{};

extract_paths_functor(raft::handle_t const& handle,
cugraph_graph_t* graph,
cugraph_type_erased_device_array_view_t const* sources,
cugraph_paths_result_t const* paths_result,
cugraph_type_erased_device_array_view_t const* destinations)
extract_paths_functor(cugraph_resource_handle_t const* handle,
::cugraph_graph_t* graph,
::cugraph_type_erased_device_array_view_t const* sources,
::cugraph_paths_result_t const* paths_result,
::cugraph_type_erased_device_array_view_t const* destinations)
: abstract_functor(),
handle_(handle),
graph_(graph),
sources_(sources),
paths_result_(paths_result),
destinations_(destinations)
handle_(*reinterpret_cast<raft::handle_t const*>(handle)),
graph_(reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph)),
sources_(
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(sources)),
paths_result_(reinterpret_cast<cugraph::c_api::cugraph_paths_result_t const*>(paths_result)),
destinations_(
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
destinations))
{
}

Expand Down Expand Up @@ -171,45 +174,7 @@ extern "C" cugraph_error_code_t cugraph_extract_paths(
cugraph_extract_paths_result_t** result,
cugraph_error_t** error)
{
*result = nullptr;
*error = nullptr;

try {
auto p_handle = reinterpret_cast<raft::handle_t const*>(handle);
auto p_graph = reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph);
auto p_sources =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(sources);
auto p_paths_result =
reinterpret_cast<cugraph::c_api::cugraph_paths_result_t const*>(paths_result);
auto p_destinations =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
destinations);

cugraph::c_api::extract_paths_functor functor(
*p_handle, p_graph, p_sources, p_paths_result, p_destinations);

// FIXME: This seems like a recurring pattern. Can
// I encapsulate
// The vertex_dispatcher and error handling calls
// into a reusable function? After all, we're in
// C++ here.
cugraph::dispatch::vertex_dispatcher(cugraph::c_api::dtypes_mapping[p_graph->vertex_type_],
cugraph::c_api::dtypes_mapping[p_graph->edge_type_],
cugraph::c_api::dtypes_mapping[p_graph->weight_type_],
p_graph->store_transposed_,
p_graph->multi_gpu_,
functor);

if (functor.error_code_ != CUGRAPH_SUCCESS) {
*error = reinterpret_cast<cugraph_error_t*>(functor.error_.release());
return functor.error_code_;
}

*result = reinterpret_cast<cugraph_extract_paths_result_t*>(functor.result_);
} catch (std::exception const& ex) {
*error = reinterpret_cast<cugraph_error_t*>(new cugraph::c_api::cugraph_error_t{ex.what()});
return CUGRAPH_UNKNOWN_ERROR;
}
cugraph::c_api::extract_paths_functor functor(handle, graph, sources, paths_result, destinations);

return CUGRAPH_SUCCESS;
return cugraph::c_api::run_algorithm(graph, functor, result, error);
}
140 changes: 41 additions & 99 deletions cpp/src/c_api/pagerank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

#include <c_api/abstract_functor.hpp>
#include <c_api/graph.hpp>
#include <c_api/utils.hpp>

#include <cugraph/algorithms.hpp>
#include <cugraph/detail/utility_wrappers.hpp>
#include <cugraph/graph_functions.hpp>
#include <cugraph/visitors/generic_cascaded_dispatch.hpp>

#include <raft/handle.hpp>

Expand Down Expand Up @@ -50,22 +50,28 @@ struct pagerank_functor : public abstract_functor {
cugraph_pagerank_result_t* result_{};

pagerank_functor(
raft::handle_t const& handle,
cugraph_graph_t* graph,
cugraph_type_erased_device_array_view_t const* precomputed_vertex_out_weight_sums,
cugraph_type_erased_device_array_view_t* personalization_vertices,
cugraph_type_erased_device_array_view_t const* personalization_values,
cugraph_resource_handle_t const* handle,
::cugraph_graph_t* graph,
::cugraph_type_erased_device_array_view_t const* precomputed_vertex_out_weight_sums,
::cugraph_type_erased_device_array_view_t* personalization_vertices,
::cugraph_type_erased_device_array_view_t const* personalization_values,
double alpha,
double epsilon,
size_t max_iterations,
bool has_initial_guess,
bool do_expensive_check)
: abstract_functor(),
handle_(handle),
graph_(graph),
precomputed_vertex_out_weight_sums_(precomputed_vertex_out_weight_sums),
personalization_vertices_(personalization_vertices),
personalization_values_(personalization_values),
handle_(*reinterpret_cast<raft::handle_t const*>(handle)),
graph_(reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph)),
precomputed_vertex_out_weight_sums_(
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
precomputed_vertex_out_weight_sums)),
personalization_vertices_(
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t*>(
personalization_vertices)),
personalization_values_(
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
personalization_values)),
alpha_(alpha),
epsilon_(epsilon),
max_iterations_(max_iterations),
Expand Down Expand Up @@ -188,47 +194,18 @@ extern "C" cugraph_error_code_t cugraph_pagerank(
cugraph_pagerank_result_t** result,
cugraph_error_t** error)
{
*result = nullptr;
*error = nullptr;

try {
auto p_handle = reinterpret_cast<raft::handle_t const*>(handle);
auto p_graph = reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph);

auto p_precomputed_vertex_out_weight_sums =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
precomputed_vertex_out_weight_sums);

cugraph::c_api::pagerank_functor functor(*p_handle,
p_graph,
p_precomputed_vertex_out_weight_sums,
nullptr,
nullptr,
alpha,
epsilon,
max_iterations,
has_initial_guess,
do_expensive_check);

cugraph::dispatch::vertex_dispatcher(cugraph::c_api::dtypes_mapping[p_graph->vertex_type_],
cugraph::c_api::dtypes_mapping[p_graph->edge_type_],
cugraph::c_api::dtypes_mapping[p_graph->weight_type_],
p_graph->store_transposed_,
p_graph->multi_gpu_,
functor);

if (functor.error_code_ != CUGRAPH_SUCCESS) {
*error = reinterpret_cast<cugraph_error_t*>(functor.error_.release());
return functor.error_code_;
}

*result = reinterpret_cast<cugraph_pagerank_result_t*>(functor.result_);
} catch (std::exception const& ex) {
*error = reinterpret_cast<cugraph_error_t*>(new cugraph::c_api::cugraph_error_t{ex.what()});
return CUGRAPH_UNKNOWN_ERROR;
}

return CUGRAPH_SUCCESS;
cugraph::c_api::pagerank_functor functor(handle,
graph,
precomputed_vertex_out_weight_sums,
nullptr,
nullptr,
alpha,
epsilon,
max_iterations,
has_initial_guess,
do_expensive_check);

return cugraph::c_api::run_algorithm(graph, functor, result, error);
}

extern "C" cugraph_error_code_t cugraph_personalized_pagerank(
Expand All @@ -245,51 +222,16 @@ extern "C" cugraph_error_code_t cugraph_personalized_pagerank(
cugraph_pagerank_result_t** result,
cugraph_error_t** error)
{
*result = nullptr;
*error = nullptr;

try {
auto p_handle = reinterpret_cast<raft::handle_t const*>(handle);
auto p_graph = reinterpret_cast<cugraph::c_api::cugraph_graph_t*>(graph);

auto p_precomputed_vertex_out_weight_sums =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
precomputed_vertex_out_weight_sums);
auto p_personalization_vertices =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t*>(
personalization_vertices);
auto p_personalization_values =
reinterpret_cast<cugraph::c_api::cugraph_type_erased_device_array_view_t const*>(
personalization_vertices);

cugraph::c_api::pagerank_functor functor(*p_handle,
p_graph,
p_precomputed_vertex_out_weight_sums,
p_personalization_vertices,
p_personalization_values,
alpha,
epsilon,
max_iterations,
has_initial_guess,
do_expensive_check);

cugraph::dispatch::vertex_dispatcher(cugraph::c_api::dtypes_mapping[p_graph->vertex_type_],
cugraph::c_api::dtypes_mapping[p_graph->edge_type_],
cugraph::c_api::dtypes_mapping[p_graph->weight_type_],
p_graph->store_transposed_,
p_graph->multi_gpu_,
functor);

if (functor.error_code_ != CUGRAPH_SUCCESS) {
*error = reinterpret_cast<cugraph_error_t*>(functor.error_.release());
return functor.error_code_;
}

*result = reinterpret_cast<cugraph_pagerank_result_t*>(functor.result_);
} catch (std::exception const& ex) {
*error = reinterpret_cast<cugraph_error_t*>(new cugraph::c_api::cugraph_error_t{ex.what()});
return CUGRAPH_UNKNOWN_ERROR;
}

return CUGRAPH_SUCCESS;
cugraph::c_api::pagerank_functor functor(handle,
graph,
precomputed_vertex_out_weight_sums,
personalization_vertices,
personalization_values,
alpha,
epsilon,
max_iterations,
has_initial_guess,
do_expensive_check);

return cugraph::c_api::run_algorithm(graph, functor, result, error);
}
Loading

0 comments on commit 41c3b70

Please sign in to comment.