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 Description

Obtains the memory data pointer address of the numpy.ndarray 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 input numpy.ndarray object has not ended (not deleted or reclaimed by the GC of Python). Otherwise, undefined behavior will occur.
  • If the input numpy.ndarray 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.

    Use the acl.util.numpy_contiguous_to_ptr API to obtain the address object of the NumPy array. Otherwise, when acl.rt.memcpy is used to copy data, the copied data may be inconsistent with the target data because acl.rt.memcpy copies contiguous memory addresses.

  • 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.