Skip to content

Commit

Permalink
_from_host_memory(): use numpy.array()
Browse files Browse the repository at this point in the history
  • Loading branch information
madsbk committed Nov 2, 2022
1 parent 93f4164 commit ff17377
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 7 additions & 3 deletions python/cudf/cudf/core/buffer/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ def _from_host_memory(cls: Type[T], data: Any) -> T:
"""Create a Buffer from a buffer or array like object
Data must implement `__array_interface__`, the buffer protocol, and/or
be convertible to a buffer object using `numpy.asarray()`
be convertible to a buffer object using `numpy.array()`
The host memory is copied to a new device allocation.
Raises ValueError if array isn't C-contiguous.
Parameters
----------
data : array-like or buffer-like
data : Any
An object that represens host memory.
Returns
Expand All @@ -137,8 +139,10 @@ def _from_host_memory(cls: Type[T], data: Any) -> T:
Buffer representing a copy of `data`.
"""

# Convert to numpy array, this will not copy data in most cases.
ary = numpy.array(data, copy=False, subok=True)
# Extract pointer and size
ptr, size = get_ptr_and_size(numpy.asarray(data).__array_interface__)
ptr, size = get_ptr_and_size(ary.__array_interface__)
# Copy to device memory
buf = rmm.DeviceBuffer(ptr=ptr, size=size)
# Create from device memory
Expand Down
4 changes: 4 additions & 0 deletions python/cudf/cudf/core/buffer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ def as_buffer(
**not** copied, instead the new buffer keeps a reference to `data` in order
to retain its lifetime.
If `data` is an integer, it is assumed to point to device memory.
Raises ValueError if data isn't C-contiguous.
Parameters
----------
data : int or buffer-like or array-like
Expand Down

0 comments on commit ff17377

Please sign in to comment.