Function: numpy_to_ptr

This API will be deprecated. Use acl.util.bytes_to_ptr instead.

C Prototype

None

Python Function

ptr = acl.util.numpy_to_ptr(data)

Function Usage

Obtains the memory data pointer address of a numpy.ndarry array.

Input Description

data: data of the numpy.ndarray type.

Return Value

ptr: int, memory data pointer address of the NumPy array.

Restrictions

None

Precautions

  • To continue to use this API, ensure that the operating environment uses Python 3.8 or later and NumPy 1.22.0 or later.
  • Example modification:
    np_ptr = acl.util.numpy_to_ptr(np_arr_in)

    After modification:

    bytes_in = np_arr_in.tobytes()
    bytes_ptr = acl.util.bytes_to_ptr(bytes_in)
  • Before using the pointer address returned by this function, ensure that the lifecycle of the passed numpy.ndarray object has not ended (has not been deleted or collected by Python's GC), otherwise undefined behavior may occur.
  • If the input numpy.ndarry array is non-contiguous in memory, the following information is displayed when the API is called:
    Warning: The input ndarray is discontiguous. Please use acl.util.numpy_contiguous_to_ptr instead.

    Call acl.util.numpy_contiguous_to_ptr to obtain the address object of the NumPy data. Otherwise, when acl.rt.memcpy is used to copy memory, the copied data is inconsistent with the target data as the copied memory is contiguous.

  • You can use the flags attribute of the NumPy array to check whether the NumPy array is contiguous in memory.
    print(data.flags)

    The results are as follows:

    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False
    UPDATEIFCOPY : False

    C_CONTIGUOUS : True indicates rows in the NumPy array are contiguous in memory. F_CONTIGUOUS : False indicates columns in the NumPy array are non-contiguous in memory.