16
16
# cython: embedsignature = True
17
17
# cython: language_level = 3
18
18
19
- import numpy as np
20
19
from libc.stdint cimport uintptr_t
21
20
from libcpp.utility cimport move
22
21
23
22
from rmm.pylibrmm.device_buffer cimport DeviceBuffer
24
- from cudf.core.buffer import as_buffer
23
+ import pylibcudf
25
24
import cudf
26
25
27
26
28
27
cdef move_device_buffer_to_column(
29
- unique_ptr[device_buffer] device_buffer_unique_ptr, dtype):
28
+ unique_ptr[device_buffer] device_buffer_unique_ptr,
29
+ dtype,
30
+ size_t itemsize,
31
+ ):
30
32
"""
31
33
Transfers ownership of device_buffer_unique_ptr to a cuDF buffer which is
32
34
used to construct a cudf column object, which is then returned. If the
33
35
intermediate buffer is empty, the device_buffer_unique_ptr is still
34
36
transfered but None is returned.
35
37
"""
36
- buff = DeviceBuffer.c_from_unique_ptr(move(device_buffer_unique_ptr))
37
- buff = as_buffer(buff)
38
- if buff.nbytes != 0 :
39
- column = cudf.core.column.build_column(buff, dtype = cudf.dtype(dtype))
40
- return column
38
+ cdef size_t buff_size = device_buffer_unique_ptr.get().size()
39
+ cdef DeviceBuffer buff = DeviceBuffer.c_from_unique_ptr(move(device_buffer_unique_ptr))
40
+ cdef size_t col_size = buff_size // itemsize
41
+ result_column = pylibcudf.Column.from_rmm_buffer(
42
+ buff,
43
+ dtype,
44
+ col_size,
45
+ [],
46
+ )
47
+ if buff_size != 0 :
48
+ return result_column
41
49
return None
42
50
43
51
44
52
cdef move_device_buffer_to_series(
45
- unique_ptr[device_buffer] device_buffer_unique_ptr, dtype, series_name):
53
+ unique_ptr[device_buffer] device_buffer_unique_ptr,
54
+ dtype,
55
+ size_t itemsize,
56
+ series_name
57
+ ):
46
58
"""
47
59
Transfers ownership of device_buffer_unique_ptr to a cuDF buffer which is
48
60
used to construct a cudf.Series object with name series_name, which is then
49
61
returned. If the intermediate buffer is empty, the device_buffer_unique_ptr
50
62
is still transfered but None is returned.
51
63
"""
52
- column = move_device_buffer_to_column(move(device_buffer_unique_ptr), dtype)
64
+ column = move_device_buffer_to_column(
65
+ move(device_buffer_unique_ptr),
66
+ dtype,
67
+ itemsize,
68
+ )
53
69
if column is not None :
54
- series = cudf.Series._from_data({series_name: column})
55
- return series
70
+ return cudf.Series.from_pylibcudf(column, metadata = {" name" : series_name})
56
71
return None
57
72
58
73
59
74
cdef coo_to_df(GraphCOOPtrType graph):
60
75
# FIXME: this function assumes columns named "src" and "dst" and can only
61
76
# be used for SG graphs due to that assumption.
62
77
contents = move(graph.get()[0 ].release())
63
- src = move_device_buffer_to_column(move(contents.src_indices), " int32" )
64
- dst = move_device_buffer_to_column(move(contents.dst_indices), " int32" )
78
+ src = move_device_buffer_to_series(
79
+ move(contents.src_indices),
80
+ pylibcudf.DataType(pylibcudf.TypeId.INT32),
81
+ 4 ,
82
+ None ,
83
+ )
84
+ dst = move_device_buffer_to_series(
85
+ move(contents.dst_indices),
86
+ pylibcudf.DataType(pylibcudf.TypeId.INT32),
87
+ 4 ,
88
+ None ,
89
+ )
65
90
66
91
if GraphCOOPtrType is GraphCOOPtrFloat:
67
- weight_type = " float32"
92
+ weight_type = pylibcudf.DataType(pylibcudf.TypeId.FLOAT32)
93
+ itemsize = 4
68
94
elif GraphCOOPtrType is GraphCOOPtrDouble:
69
- weight_type = " float64"
95
+ weight_type = pylibcudf.DataType(pylibcudf.TypeId.FLOAT64)
96
+ itemsize = 8
70
97
else :
71
98
raise TypeError (" Invalid GraphCOOPtrType" )
72
99
73
- wgt = move_device_buffer_to_column(move(contents.edge_data), weight_type)
100
+ wgt = move_device_buffer_to_series(
101
+ move(contents.edge_data),
102
+ weight_type,
103
+ itemsize,
104
+ None ,
105
+ )
74
106
75
107
df = cudf.DataFrame()
76
108
df[' src' ] = src
@@ -83,20 +115,34 @@ cdef coo_to_df(GraphCOOPtrType graph):
83
115
84
116
cdef csr_to_series(GraphCSRPtrType graph):
85
117
contents = move(graph.get()[0 ].release())
86
- csr_offsets = move_device_buffer_to_series(move(contents.offsets),
87
- " int32" , " csr_offsets" )
88
- csr_indices = move_device_buffer_to_series(move(contents.indices),
89
- " int32" , " csr_indices" )
118
+ csr_offsets = move_device_buffer_to_series(
119
+ move(contents.offsets),
120
+ pylibcudf.DataType(pylibcudf.TypeId.INT32),
121
+ 4 ,
122
+ " csr_offsets"
123
+ )
124
+ csr_indices = move_device_buffer_to_series(
125
+ move(contents.indices),
126
+ pylibcudf.DataType(pylibcudf.TypeId.INT32),
127
+ 4 ,
128
+ " csr_indices"
129
+ )
90
130
91
131
if GraphCSRPtrType is GraphCSRPtrFloat:
92
- weight_type = " float32"
132
+ weight_type = pylibcudf.DataType(pylibcudf.TypeId.FLOAT32)
133
+ itemsize = 4
93
134
elif GraphCSRPtrType is GraphCSRPtrDouble:
94
- weight_type = " float64"
135
+ weight_type = pylibcudf.DataType(pylibcudf.TypeId.FLOAT64)
136
+ itemsize = 8
95
137
else :
96
138
raise TypeError (" Invalid GraphCSRPtrType" )
97
139
98
- csr_weights = move_device_buffer_to_series(move(contents.edge_data),
99
- weight_type, " csr_weights" )
140
+ csr_weights = move_device_buffer_to_series(
141
+ move(contents.edge_data),
142
+ weight_type,
143
+ itemsize,
144
+ " csr_weights"
145
+ )
100
146
101
147
return (csr_offsets, csr_indices, csr_weights)
102
148
0 commit comments