Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/awkward/_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _cast(cls, x, t):
if numpy.is_own_array(x):
assert numpy.is_c_contiguous(x), "kernel expects contiguous array"
if x.ndim > 0:
return ctypes.cast(x.ctypes.data, t)
return ctypes.cast(numpy.memory_ptr(x), t)
else:
return x
# Or, do we have a ctypes type
Expand Down Expand Up @@ -116,7 +116,7 @@ def _cast(self, x, t):
raise ValueError(msg)
assert self._jax.is_c_contiguous(x), "kernel expects contiguous array"
if x.ndim > 0:
return ctypes.cast(x.unsafe_buffer_pointer(), t)
return ctypes.cast(self._jax.memory_ptr(x), t)
else:
return x
# Or, do we have a ctypes type
Expand Down
5 changes: 1 addition & 4 deletions src/awkward/_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ def __init__(self, layout, generator=None):
def arrayptr(x):
if isinstance(x, int):
return x
elif isinstance(self.nplike, ak._nplikes.cupy.Cupy):
return x.data.ptr
else:
return x.ctypes.data
return self.nplike.memory_ptr(x)

self.nplike = layout.backend.nplike
self.generator = generator
Expand Down
3 changes: 3 additions & 0 deletions src/awkward/_nplikes/array_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,6 @@ def is_own_array(cls, obj) -> bool:
if isinstance(obj, VirtualArray):
return cls.is_own_array_type(obj.nplike.ndarray)
return cls.is_own_array_type(type(obj))

def memory_ptr(self, x: ArrayLikeT) -> int:
raise NotImplementedError()
5 changes: 5 additions & 0 deletions src/awkward/_nplikes/cupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,8 @@ def is_c_contiguous(self, x: ArrayLike) -> bool:
else:
(x,) = materialize_if_virtual(x)
return x.flags["C_CONTIGUOUS"] # type: ignore[attr-defined]

def memory_ptr(self, x: ArrayLike) -> int:
(x,) = materialize_if_virtual(x)
assert not isinstance(x, PlaceholderArray)
return x.data.ptr # type: ignore[attr-defined]
5 changes: 5 additions & 0 deletions src/awkward/_nplikes/jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ def divide(
assert not isinstance(x2, PlaceholderArray)
x1, x2 = materialize_if_virtual(x1, x2)
return self._module.divide(x1, x2)

def memory_ptr(self, x: ArrayLike) -> int:
(x,) = materialize_if_virtual(x)
assert not isinstance(x, PlaceholderArray)
return x.unsafe_buffer_pointer() # type: ignore[attr-defined]
5 changes: 5 additions & 0 deletions src/awkward/_nplikes/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ def unpackbits(
):
assert not isinstance(x, PlaceholderArray)
return numpy.unpackbits(x, axis=axis, count=count, bitorder=bitorder) # type: ignore[arg-type]

def memory_ptr(self, x: NDArray) -> int:
(x,) = materialize_if_virtual(x)
assert not isinstance(x, PlaceholderArray)
return x.ctypes.data
3 changes: 3 additions & 0 deletions src/awkward/_nplikes/numpy_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,6 @@ def is_own_array(cls, obj) -> bool: ...
@classmethod
@abstractmethod
def is_own_array_type(cls, type_: type) -> bool: ...

@abstractmethod
def memory_ptr(self, x: ArrayLikeT) -> int: ...
4 changes: 4 additions & 0 deletions src/awkward/_nplikes/typetracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1674,6 +1674,10 @@ def is_c_contiguous(self, x: TypeTracerArray | PlaceholderArray) -> bool:
assert isinstance(x, TypeTracerArray)
return True

def memory_ptr(self, x: TypeTracerArray | PlaceholderArray) -> int:
assert isinstance(x, TypeTracerArray)
return 0

def __dlpack_device__(self) -> tuple[int, int]:
raise NotImplementedError

Expand Down
14 changes: 0 additions & 14 deletions src/awkward/_nplikes/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,6 @@ def copy(self) -> VirtualArray:
def tolist(self) -> list:
return self.materialize().tolist() # type: ignore[attr-defined]

@property
def ctypes(self):
if isinstance((self._nplike), ak._nplikes.cupy.Cupy):
raise AttributeError("Cupy ndarrays do not have a ctypes attribute")
return self.materialize().ctypes

@property
def data(self):
return self.materialize().data

def unsafe_buffer_pointer(self):
assert isinstance(self._nplike, ak._nplikes.jax.Jax)
return self.materialize().unsafe_buffer_pointer()

def byteswap(self, inplace=False):
if self._array is not UNMATERIALIZED:
return self._array.byteswap(inplace=inplace)
Expand Down
13 changes: 1 addition & 12 deletions src/awkward/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,7 @@ def metadata(self) -> dict:

@property
def ptr(self):
if isinstance(self._nplike, Numpy):
return self._data.ctypes.data
elif isinstance(self._nplike, Cupy):
return self._data.data.ptr
elif isinstance(self._nplike, Jax):
return self._data.unsafe_buffer_pointer()
elif isinstance(self._nplike, TypeTracer):
return 0
else:
raise NotImplementedError(
f"this function hasn't been implemented for the {type(self._nplike).__name__} backend"
)
return self._nplike.memory_ptr(self._data)

@property
def length(self) -> ShapeItem:
Expand Down
12 changes: 0 additions & 12 deletions tests/test_3364_virtualarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,18 +454,6 @@ def test_tobytes(virtual_array):
assert virtual_array.is_materialized


# Test ctypes
def test_ctypes(virtual_array):
ctypes_data = virtual_array.ctypes
assert ctypes_data is not None


# Test data
def test_data(virtual_array):
data = virtual_array.data
assert data is not None


# Test __repr__ and __str__
def test_repr(virtual_array):
repr_str = repr(virtual_array)
Expand Down
16 changes: 0 additions & 16 deletions tests/test_3475_virtualarray_unknown_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,6 @@ def test_tobytes(virtual_array):
assert virtual_array.is_materialized


# Test ctypes
def test_ctypes(virtual_array):
ctypes_data = virtual_array.ctypes
assert ctypes_data is not None
# Should be materialized after this
assert virtual_array.is_materialized


# Test data
def test_data(virtual_array):
data = virtual_array.data
assert data is not None
# Should be materialized after this
assert virtual_array.is_materialized


# Test __repr__ and __str__
def test_repr(virtual_array, shape_generator_param):
repr_str = repr(virtual_array)
Expand Down
Loading