Skip to content

Commit

Permalink
Sync some changes from KvikIO's Array (#290)
Browse files Browse the repository at this point in the history
Pulls in a few changes from KvikIO's `Array`. Namely...

* `asarray` for easy conversion to `Array` (no-op if already an `Array`)
* Include types for a few more properties

Authors:
  - https://github.com/jakirkham

Approvers:
  - Peter Andreas Entschev (https://github.com/pentschev)
  - Lawrence Mitchell (https://github.com/wence-)

URL: #290
  • Loading branch information
jakirkham authored Oct 7, 2024
1 parent a7d36f5 commit 7cdaa6f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
7 changes: 6 additions & 1 deletion python/ucxx/ucxx/_lib/arr.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2020-2024, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: BSD-3-Clause

# cython: language_level=3


from libc.stdint cimport uintptr_t

Expand All @@ -22,3 +24,6 @@ cdef class Array:
cpdef bint _f_contiguous(self)
cpdef bint _contiguous(self)
cpdef Py_ssize_t _nbytes(self)


cpdef Array asarray(obj)
16 changes: 12 additions & 4 deletions python/ucxx/ucxx/_lib/arr.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2020-2024, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: BSD-3-Clause

from typing import Tuple
from typing import Generic, Tuple, TypeVar

class Array:
def __init__(self, obj: object): ...
T = TypeVar("T")

class Array(Generic[T]):
def __init__(self, obj: T): ...
@property
def c_contiguous(self) -> bool: ...
@property
Expand All @@ -17,3 +19,9 @@ class Array:
def shape(self) -> Tuple[int]: ...
@property
def strides(self) -> Tuple[int]: ...
@property
def cuda(self) -> bool: ...
@property
def obj(self) -> T: ...

def asarray(obj) -> Array: ...
23 changes: 22 additions & 1 deletion python/ucxx/ucxx/_lib/arr.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# SPDX-FileCopyrightText: Copyright (c) 2022-2023, NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: Copyright (c) 2020-2024, NVIDIA CORPORATION & AFFILIATES.
# SPDX-License-Identifier: BSD-3-Clause

# cython: language_level=3


from cpython.array cimport array, newarrayobject
from cpython.buffer cimport PyBuffer_IsContiguous
Expand Down Expand Up @@ -295,3 +297,22 @@ cdef inline Py_ssize_t _nbytes(Py_ssize_t itemsize,
for i in range(ndim):
nbytes *= shape_mv[i]
return nbytes


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

0 comments on commit 7cdaa6f

Please sign in to comment.