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
11 changes: 5 additions & 6 deletions numba_cuda/numba/cuda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ def as_cuda_array(obj, sync=True):
If ``sync`` is ``True``, then the imported stream (if present) will be
synchronized.
"""
if not is_cuda_array(obj):
raise TypeError("*obj* doesn't implement the cuda array interface.")
else:
return from_cuda_array_interface(
obj.__cuda_array_interface__, owner=obj, sync=sync
)
if (
interface := getattr(obj, "__cuda_array_interface__", None)
) is not None:
return from_cuda_array_interface(interface, owner=obj, sync=sync)
raise TypeError("*obj* doesn't implement the cuda array interface.")


def is_cuda_array(obj):
Expand Down
9 changes: 6 additions & 3 deletions numba_cuda/numba/cuda/cudadrv/devicearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import numpy as np

import numba
from numba.cuda.cext import _devicearray
from numba.cuda.cudadrv import devices, dummyarray
from numba.cuda.cudadrv import driver as _driver
Expand Down Expand Up @@ -901,8 +900,12 @@ def auto_device(obj, stream=0, copy=True, user_explicit=False):
"""
if _driver.is_device_memory(obj):
return obj, False
elif hasattr(obj, "__cuda_array_interface__"):
return numba.cuda.as_cuda_array(obj), False
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, there were total of 3 (!) accesses of __cuda_array_interface__ per call to auto_device.

elif (
interface := getattr(obj, "__cuda_array_interface__", None)
) is not None:
from numba.cuda.api import from_cuda_array_interface

return from_cuda_array_interface(interface, owner=obj), False
else:
if isinstance(obj, np.void):
devobj = from_record_like(obj, stream=stream)
Expand Down