1+ # Copyright (c) 2021, NVIDIA CORPORATION.
2+ # Licensed under the Apache License, Version 2.0 (the "License");
3+ # you may not use this file except in compliance with the License.
4+ # You may obtain a copy of the License at
5+ #
6+ # http://www.apache.org/licenses/LICENSE-2.0
7+ #
8+ # Unless required by applicable law or agreed to in writing, software
9+ # distributed under the License is distributed on an "AS IS" BASIS,
10+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ # See the License for the specific language governing permissions and
12+ # limitations under the License.
13+ from cugraph.sampling.random_walks cimport call_random_walks
14+ # from cugraph.structure.graph_primtypes cimport *
15+ from cugraph.structure.graph_utilities cimport *
16+ from libcpp cimport bool
17+ from libcpp.utility cimport move
18+ from libc.stdint cimport uintptr_t
19+ from cugraph.structure import graph_primtypes_wrapper
20+ import cudf
21+ import rmm
22+ import numpy as np
23+ import numpy.ctypeslib as ctypeslib
24+ from rmm._lib.device_buffer cimport DeviceBuffer
25+ from cudf.core.buffer import Buffer
26+ from cython.operator cimport dereference as deref
27+ def random_walks (input_graph , start_vertices , max_depth ):
28+ """
29+ Call random_walks
30+ """
31+ # FIXME: Offsets and indices are currently hardcoded to int, but this may
32+ # not be acceptable in the future.
33+ numberTypeMap = {np.dtype(" int32" ) : < int > numberTypeEnum.int32Type,
34+ np.dtype(" int64" ) : < int > numberTypeEnum.int64Type,
35+ np.dtype(" float32" ) : < int > numberTypeEnum.floatType,
36+ np.dtype(" double" ) : < int > numberTypeEnum.doubleType}
37+ [src, dst] = [input_graph.edgelist.edgelist_df[' src' ], input_graph.edgelist.edgelist_df[' dst' ]]
38+ vertex_t = src.dtype
39+ edge_t = np.dtype(" int32" )
40+ weights = None
41+ if input_graph.edgelist.weights:
42+ weights = input_graph.edgelist.edgelist_df[' weights' ]
43+ num_verts = input_graph.number_of_vertices()
44+ num_edges = input_graph.number_of_edges(directed_edges = True )
45+ num_partition_edges = num_edges
46+
47+ if num_edges > (2 ** 31 - 1 ):
48+ edge_t = np.dtype(" int64" )
49+ cdef unique_ptr[random_walk_ret_t] rw_ret_ptr
50+
51+ cdef uintptr_t c_src_vertices = src.__cuda_array_interface__[' data' ][0 ]
52+ cdef uintptr_t c_dst_vertices = dst.__cuda_array_interface__[' data' ][0 ]
53+ cdef uintptr_t c_edge_weights = < uintptr_t> NULL
54+ if weights is not None :
55+ c_edge_weights = weights.__cuda_array_interface__[' data' ][0 ]
56+ weight_t = weights.dtype
57+ is_weighted = True
58+ else :
59+ weight_t = np.dtype(" float32" )
60+ is_weighted = False
61+ # Pointers for random_walks
62+ start_vertices = start_vertices.astype(' int32' )
63+ cdef uintptr_t c_start_vertex_ptr = start_vertices.__cuda_array_interface__[' data' ][0 ]
64+ num_paths = start_vertices.size
65+ cdef unique_ptr[handle_t] handle_ptr
66+ handle_ptr.reset(new handle_t())
67+ handle_ = handle_ptr.get()
68+ cdef graph_container_t graph_container
69+ populate_graph_container(graph_container,
70+ handle_[0 ],
71+ < void * > c_src_vertices, < void * > c_dst_vertices, < void * > c_edge_weights,
72+ < void * > NULL ,
73+ < numberTypeEnum> (< int > (numberTypeMap[vertex_t])),
74+ < numberTypeEnum> (< int > (numberTypeMap[edge_t])),
75+ < numberTypeEnum> (< int > (numberTypeMap[weight_t])),
76+ num_partition_edges,
77+ num_verts,
78+ num_edges,
79+ False ,
80+ is_weighted,
81+ False , False )
82+ if (vertex_t == np.dtype(" int32" )):
83+ if (edge_t == np.dtype(" int32" )):
84+ rw_ret_ptr = move(call_random_walks[int , int ]( deref(handle_),
85+ graph_container,
86+ < int * > c_start_vertex_ptr,
87+ < int > num_paths,
88+ < int > max_depth))
89+ else : # (edge_t == np.dtype("int64")):
90+ rw_ret_ptr = move(call_random_walks[int , long ]( deref(handle_),
91+ graph_container,
92+ < int * > c_start_vertex_ptr,
93+ < long > num_paths,
94+ < long > max_depth))
95+ else : # (vertex_t == edge_t == np.dtype("int64")):
96+ rw_ret_ptr = move(call_random_walks[long , long ]( deref(handle_),
97+ graph_container,
98+ < long * > c_start_vertex_ptr,
99+ < long > num_paths,
100+ < long > max_depth))
101+
102+
103+ rw_ret= move(rw_ret_ptr.get()[0 ])
104+ vertex_set = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_coalesced_v_))
105+ edge_set = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_coalesced_w_))
106+ sizes = DeviceBuffer.c_from_unique_ptr(move(rw_ret.d_sizes_))
107+ vertex_set = Buffer(vertex_set)
108+ edge_set = Buffer(edge_set)
109+ sizes = Buffer(sizes)
110+
111+ set_vertex = cudf.Series(data = vertex_set, dtype = vertex_t)
112+ set_edge = cudf.Series(data = edge_set, dtype = weight_t)
113+ set_sizes = cudf.Series(data = sizes, dtype = edge_t)
114+
115+ return set_vertex, set_edge, set_sizes
116+
0 commit comments