Skip to content

Commit

Permalink
Touch ups to Array (#475)
Browse files Browse the repository at this point in the history
* Add return type to `asarray` (makes easy to use with other typed values)
* Include `asarray` in `pxd` for faster access within other Cython modules

Authors:
  - https://github.com/jakirkham
  - Mads R. B. Kristensen (https://github.com/madsbk)

Approvers:
  - Mads R. B. Kristensen (https://github.com/madsbk)

URL: #475
  • Loading branch information
jakirkham authored Oct 1, 2024
1 parent 3e0aabe commit 9643fab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 3 additions & 0 deletions python/kvikio/kvikio/_lib/arr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ cdef class Array:
cpdef Py_ssize_t _nbytes(self)


cpdef Array asarray(obj)


cdef pair[uintptr_t, size_t] parse_buffer_argument(
buf, size, bint accept_host_buffer
) except *
13 changes: 11 additions & 2 deletions python/kvikio/kvikio/_lib/arr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,18 @@ cdef inline Py_ssize_t _nbytes(Py_ssize_t itemsize,
nbytes *= shape_mv[i]
return nbytes

cpdef asarray(obj):

cpdef Array asarray(obj):
"""Coerce other objects to ``Array``. No-op for existing ``Array``s.
Args:
obj: Object exposing the Python buffer protocol or ``__cuda_array_interface__``
Returns:
Array: An instance of the ``Array`` class
"""
if isinstance(obj, Array):
return obj
return <Array>obj
else:
return Array(obj)

Expand Down

0 comments on commit 9643fab

Please sign in to comment.