Skip to content

Commit

Permalink
Apply flake8 to Cython code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdice committed Dec 8, 2022
1 parent cfe6395 commit adf0741
Show file tree
Hide file tree
Showing 77 changed files with 1,560 additions and 1,165 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
args: ["--config=setup.cfg"]
files: python/.*$
types: [file]
types_or: [python] # TODO: Enable [python, cython]
types_or: [python, cython]
additional_dependencies: ["flake8-force"]
- repo: https://github.com/asottile/yesqa
rev: v1.3.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# cython: embedsignature = True
# cython: language_level = 3

from cugraph.centrality.betweenness_centrality cimport betweenness_centrality as c_betweenness_centrality
from cugraph.centrality.betweenness_centrality cimport \
betweenness_centrality as c_betweenness_centrality
from cugraph.structure.graph_primtypes cimport *
from libc.stdint cimport uintptr_t
from libcpp cimport bool
Expand All @@ -37,7 +38,7 @@ def get_output_df(number_of_vertices, result_dtype):

def get_batch(sources, number_of_workers, current_worker):
batch_size = len(sources) // number_of_workers
begin = current_worker * batch_size
begin = current_worker * batch_size
end = (current_worker + 1) * batch_size
if current_worker == (number_of_workers - 1):
end = len(sources)
Expand All @@ -55,23 +56,25 @@ cdef void run_c_betweenness_centrality(uintptr_t c_handle,
uintptr_t c_batch,
result_dtype):
if result_dtype == np.float64:
c_betweenness_centrality[int, int, double, double]((<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, double] *> c_graph)[0],
<double *> c_betweenness,
normalized,
endpoints,
<double *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
c_betweenness_centrality[int, int, double, double](
(<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, double] *> c_graph)[0],
<double *> c_betweenness,
normalized,
endpoints,
<double *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
elif result_dtype == np.float32:
c_betweenness_centrality[int, int, float, float]((<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, float] *> c_graph)[0],
<float *> c_betweenness,
normalized,
endpoints,
<float *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
c_betweenness_centrality[int, int, float, float](
(<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, float] *> c_graph)[0],
<float *> c_betweenness,
normalized,
endpoints,
<float *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
else:
raise ValueError("result_dtype can only be np.float64 or np.float32")

Expand Down Expand Up @@ -127,7 +130,8 @@ def run_internal_work(handle, input_data, normalized, endpoints,
raise ValueError("result_dtype can only be np.float64 or np.float32")

c_identifier = result_df['vertex'].__cuda_array_interface__['data'][0]
c_betweenness = result_df['betweenness_centrality'].__cuda_array_interface__['data'][0]
c_betweenness = \
result_df['betweenness_centrality'].__cuda_array_interface__['data'][0]
if weights is not None:
c_weights = weights.__cuda_array_interface__['data'][0]

Expand All @@ -152,6 +156,7 @@ def run_internal_work(handle, input_data, normalized, endpoints,

return result_df


def run_mg_work(input_data, normalized, endpoints,
weights, sources,
result_dtype, session_id):
Expand All @@ -170,20 +175,20 @@ def run_mg_work(input_data, normalized, endpoints,


def batch_betweenness_centrality(input_graph, normalized, endpoints,
weights, vertices, result_dtype):
weights, vertices, result_dtype):
df = None
client = get_client()
comms = Comms.get_comms()
replicated_adjlists = input_graph.batch_adjlists
work_futures = [client.submit(run_mg_work,
(data, input_graph.is_directed()),
normalized,
endpoints,
weights,
vertices,
result_dtype,
comms.sessionId,
workers=[worker]) for
work_futures = [client.submit(run_mg_work,
(data, input_graph.is_directed()),
normalized,
endpoints,
weights,
vertices,
result_dtype,
comms.sessionId,
workers=[worker]) for
idx, (worker, data) in enumerate(replicated_adjlists.items())]
dask.distributed.wait(work_futures)
df = work_futures[0].result()
Expand Down Expand Up @@ -215,13 +220,13 @@ def betweenness_centrality(input_graph, normalized, endpoints, weights,
if not input_graph.adjlist:
input_graph.view_adj_list()

if Comms.is_initialized() and input_graph.batch_enabled == True:
if Comms.is_initialized() and input_graph.batch_enabled is True:
df = batch_betweenness_centrality(input_graph,
normalized,
endpoints,
weights,
vertices,
result_dtype)
normalized,
endpoints,
weights,
vertices,
result_dtype)
else:
df = sg_betweenness_centrality(input_graph,
normalized,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# cython: embedsignature = True
# cython: language_level = 3

from cugraph.centrality.betweenness_centrality cimport edge_betweenness_centrality as c_edge_betweenness_centrality
from cugraph.centrality.betweenness_centrality cimport \
edge_betweenness_centrality as c_edge_betweenness_centrality
from cugraph.structure import graph_primtypes_wrapper
from cugraph.structure.graph_primtypes cimport *
from libc.stdint cimport uintptr_t
Expand All @@ -40,7 +41,7 @@ def get_output_df(indices, result_dtype):

def get_batch(sources, number_of_workers, current_worker):
batch_size = len(sources) // number_of_workers
begin = current_worker * batch_size
begin = current_worker * batch_size
end = (current_worker + 1) * batch_size
if current_worker == (number_of_workers - 1):
end = len(sources)
Expand All @@ -49,7 +50,7 @@ def get_batch(sources, number_of_workers, current_worker):


def run_mg_work(input_data, normalized, weights, sources,
result_dtype, session_id):
result_dtype, session_id):
result = None

number_of_workers = Comms.get_n_workers(session_id)
Expand Down Expand Up @@ -80,7 +81,7 @@ def run_internal_work(handle, input_data, normalized, weights, batch,
cdef GraphCSRViewDouble graph_double
cdef GraphCSRViewFloat graph_float

(offsets, indices, graph_weights), is_directed = input_data
(offsets, indices, graph_weights), is_directed = input_data

if graph_weights is not None:
c_graph_weights = graph_weights.__cuda_array_interface__['data'][0]
Expand All @@ -93,7 +94,8 @@ def run_internal_work(handle, input_data, normalized, weights, batch,
result_df = get_output_df(indices, result_dtype)
c_src_identifier = result_df['src'].__cuda_array_interface__['data'][0]
c_dst_identifier = result_df['dst'].__cuda_array_interface__['data'][0]
c_betweenness = result_df['betweenness_centrality'].__cuda_array_interface__['data'][0]
c_betweenness = \
result_df['betweenness_centrality'].__cuda_array_interface__['data'][0]

number_of_sources_in_batch = len(batch)
if result_dtype == np.float64:
Expand Down Expand Up @@ -140,38 +142,41 @@ cdef void run_c_edge_betweenness_centrality(uintptr_t c_handle,
uintptr_t c_batch,
result_dtype):
if result_dtype == np.float64:
c_edge_betweenness_centrality[int, int, double, double]((<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, double] *> c_graph)[0],
<double *> c_betweenness,
normalized,
<double *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
c_edge_betweenness_centrality[int, int, double, double](
(<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, double] *> c_graph)[0],
<double *> c_betweenness,
normalized,
<double *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
elif result_dtype == np.float32:
c_edge_betweenness_centrality[int, int, float, float]((<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, float] *> c_graph)[0],
<float *> c_betweenness,
normalized,
<float *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
c_edge_betweenness_centrality[int, int, float, float](
(<handle_t *> c_handle)[0],
(<GraphCSRView[int, int, float] *> c_graph)[0],
<float *> c_betweenness,
normalized,
<float *> c_weights,
number_of_sources_in_batch,
<int *> c_batch)
else:
raise ValueError("result_dtype can only be np.float64 or np.float32")


def batch_edge_betweenness_centrality(input_graph,
normalized,
weights, vertices, result_dtype):
normalized,
weights, vertices, result_dtype):
client = get_client()
comms = Comms.get_comms()
replicated_adjlists = input_graph.batch_adjlists
work_futures = [client.submit(run_mg_work,
(data, input_graph.is_directed()),
normalized,
weights,
vertices,
result_dtype,
comms.sessionId,
workers=[worker]) for
work_futures = [client.submit(run_mg_work,
(data, input_graph.is_directed()),
normalized,
weights,
vertices,
result_dtype,
comms.sessionId,
workers=[worker]) for
(worker, data) in replicated_adjlists.items()]
dask.distributed.wait(work_futures)
df = work_futures[0].result()
Expand Down Expand Up @@ -200,25 +205,26 @@ def edge_betweenness_centrality(input_graph, normalized, weights,
cdef GraphCSRViewDouble graph_double
cdef GraphCSRViewFloat graph_float


df = None

if not input_graph.adjlist:
input_graph.view_adj_list()

if Comms.is_initialized() and input_graph.batch_enabled == True:
if Comms.is_initialized() and input_graph.batch_enabled is True:
df = batch_edge_betweenness_centrality(input_graph, normalized,
weights, vertices,
result_dtype)
weights, vertices,
result_dtype)
else:
df = sg_edge_betweenness_centrality(input_graph, normalized,
weights, vertices, result_dtype)

if result_dtype == np.float64:
graph_double = get_graph_view[GraphCSRViewDouble](input_graph)
graph_double.get_source_indices(<int*>(<uintptr_t>df['src'].__cuda_array_interface__['data'][0]))
graph_double.get_source_indices(
<int*>(<uintptr_t>df['src'].__cuda_array_interface__['data'][0]))
elif result_dtype == np.float32:
graph_float = get_graph_view[GraphCSRViewFloat](input_graph)
graph_float.get_source_indices(<int*>(<uintptr_t>df['src'].__cuda_array_interface__['data'][0]))
graph_float.get_source_indices(
<int*>(<uintptr_t>df['src'].__cuda_array_interface__['data'][0]))

return df
6 changes: 3 additions & 3 deletions python/cugraph/cugraph/community/ecg.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021, NVIDIA CORPORATION.
# Copyright (c) 2019-2022, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -21,9 +21,9 @@ from cugraph.structure.graph_primtypes cimport *

cdef extern from "cugraph/algorithms.hpp" namespace "cugraph":

cdef void ecg[VT,ET,WT](
cdef void ecg[VT, ET, WT](
const handle_t &handle,
const GraphCSRView[VT,ET,WT] &graph,
const GraphCSRView[VT, ET, WT] &graph,
WT min_weight,
VT ensemble_size,
VT* ecg_parts) except +
50 changes: 31 additions & 19 deletions python/cugraph/cugraph/community/ecg_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ def ecg(input_graph, min_weight=.05, ensemble_size=16):
cdef unique_ptr[handle_t] handle_ptr
handle_ptr.reset(new handle_t())

[offsets, indices] = graph_primtypes_wrapper.datatype_cast([input_graph.adjlist.offsets,
input_graph.adjlist.indices], [np.int32, np.int64])
[weights] = graph_primtypes_wrapper.datatype_cast([input_graph.adjlist.weights], [np.float32, np.float64])
[offsets, indices] = graph_primtypes_wrapper.datatype_cast(
[
input_graph.adjlist.offsets,
input_graph.adjlist.indices,
], [np.int32, np.int64])
[weights] = graph_primtypes_wrapper.datatype_cast(
[input_graph.adjlist.weights], [np.float32, np.float64])

num_verts = input_graph.number_of_vertices()
num_edges = input_graph.number_of_edges(directed_edges=True)
Expand All @@ -55,30 +59,38 @@ def ecg(input_graph, min_weight=.05, ensemble_size=16):
cdef uintptr_t c_partition = df['partition'].__cuda_array_interface__['data'][0]
cdef uintptr_t c_weights = weights.__cuda_array_interface__['data'][0]

cdef GraphCSRView[int,int,float] graph_float
cdef GraphCSRView[int,int,double] graph_double
cdef GraphCSRView[int, int, float] graph_float
cdef GraphCSRView[int, int, double] graph_double

if weights.dtype == np.float32:
graph_float = GraphCSRView[int,int,float](<int*>c_offsets, <int*>c_indices,
<float*>c_weights, num_verts, num_edges)
graph_float = GraphCSRView[int, int, float](
<int*> c_offsets,
<int*> c_indices,
<float*> c_weights,
num_verts,
num_edges)

graph_float.get_vertex_identifiers(<int*>c_identifier)

c_ecg[int,int,float](handle_ptr.get()[0],
graph_float,
min_weight,
ensemble_size,
<int*> c_partition)
c_ecg[int, int, float](handle_ptr.get()[0],
graph_float,
min_weight,
ensemble_size,
<int*> c_partition)
else:
graph_double = GraphCSRView[int,int,double](<int*>c_offsets, <int*>c_indices,
<double*>c_weights, num_verts, num_edges)
graph_double = GraphCSRView[int, int, double](
<int*> c_offsets,
<int*> c_indices,
<double*> c_weights,
num_verts,
num_edges)

graph_double.get_vertex_identifiers(<int*>c_identifier)

c_ecg[int,int,double](handle_ptr.get()[0],
graph_double,
min_weight,
ensemble_size,
<int*> c_partition)
c_ecg[int, int, double](handle_ptr.get()[0],
graph_double,
min_weight,
ensemble_size,
<int*> c_partition)

return df
6 changes: 3 additions & 3 deletions python/cugraph/cugraph/community/ktruss_subgraph.pxd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019-2021, NVIDIA CORPORATION.
# Copyright (c) 2019-2022, NVIDIA CORPORATION.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand All @@ -21,6 +21,6 @@ from cugraph.structure.graph_primtypes cimport *

cdef extern from "cugraph/algorithms.hpp" namespace "cugraph":

cdef unique_ptr[GraphCOO[VT,ET,WT]] k_truss_subgraph[VT,ET,WT](
const GraphCOOView[VT,ET,WT] &graph,
cdef unique_ptr[GraphCOO[VT, ET, WT]] k_truss_subgraph[VT, ET, WT](
const GraphCOOView[VT, ET, WT] &graph,
int k) except +
Loading

0 comments on commit adf0741

Please sign in to comment.