cuvec.swig#
Thin wrappers around cuvec_swig
C++/CUDA module
A SWIG-driven equivalent of the CPython Extension API-driven cpython.py
SWIGVector Objects#
class SWIGVector(CVector)
__init__#
def __init__(typechar: str, shape: Shape, cuvec=None)
Args: typechar(char) shape(tuple(int)) cuvec(SwigPyObjecttypechar
and shape
are ignored
CuVec Objects#
class CuVec(np.ndarray)
A numpy.ndarray
compatible view with a cuvec
member containing the underlying SWIGVector
object (for use in CPython API function calls).
__array_interface__#
https://numpy.org/doc/stable/reference/arrays.interface.html
__cuda_array_interface__#
@property
def __cuda_array_interface__() -> Dict[str, Any]
https://numba.readthedocs.io/en/stable/cuda/cuda_array_interface.html
resize#
def resize(new_shape: Shape)
Change shape (but not size) of array in-place.
zeros#
def zeros(shape: Shape, dtype="float32") -> CuVec
Returns a cuvec.swig.CuVec
view of a new numpy.ndarray
of the specified shape and data type (cuvec
equivalent of numpy.zeros
).
copy#
def copy(arr) -> CuVec
Returns a cuvec.swig.CuVec
view of a new numpy.ndarray
with data copied from the specified arr
(cuvec
equivalent of numpy.copy
).
asarray#
def asarray(arr, dtype=None, order=None, ownership: str = 'warning') -> CuVec
Returns a cuvec.swig.CuVec
view of arr
, avoiding memory copies if possible. (cuvec
equivalent of numpy.asarray
).
Args: ownership: logging level if is_raw_cuvec(arr)
. WARNING: asarray()
should not be used on an existing reference, e.g.: >>> res = asarray(some_swig_api_func(..., output=getattr(out, 'cuvec', None))) res.cuvec
and out.cuvec
are now the same yet garbage collected separately (dangling ptr). Instead, use the retarray
helper: >>> raw = some_swig_api_func(..., output=getattr(out, 'cuvec', None)) >>> res = retarray(raw, out) NB: asarray()
/retarray()
are safe if the raw cuvec was created in C++/SWIG, e.g.: >>> res = retarray(some_swig_api_func(..., output=None))
retarray#
def retarray(raw, out: Optional[CuVec] = None)
Returns out if hasattr(out, 'cuvec') else asarray(raw, ownership='debug')
. See asarray
for explanation. Args: raw: a raw CuVec (returned by C++/SWIG function). out: preallocated output array.