Function: ptr_to_numpy

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

C Prototype

None

Python Function

output = acl.util.ptr_to_numpy(ptr, shape, type)

Function Usage

Converts pointer address data into a NumPy array, which can be directly accessed by Python code.

Input Description

ptr: int, pointer address in the C language. It is the start address of the data that can be accessed.

shape: tuple, shape of the NumPy to be constructed.

type: int, data type of the data in ptr.

The following lists some common types: (For details about the types that are not listed, data type APIs in NumPy C-API, and the following types, see the NumPy manual.)

  • 0: NPY_BOOL
  • 1: NPY_BYTE, NPY_INT8
  • 2: NPY_UINT8
  • 3: NPY_SHORT, NPY_INT16
  • 4: NPY_USHORT, NPY_UINT16
  • 5: NPY_INT, NPY_INT32
  • 6: NPY_UINT, NPY_UINT32
  • 7: NPY_INT64
  • 8: NPY_UINT64
  • 9: NPY_LONGLONG
  • 10: NPY_ULONGLONG
  • 11: NPY_FLOAT32
  • 12: NPY_DOUBLE
  • 23: NPY_HALF, NPY_FLOAT16

Return Value

output: NumPy type.

Restrictions

None

Precautions

  • Example modification:
    np_arr_out = acl.util.ptr_to_numpy(host_ptr, np_arr_in.shape, NPY_INT32)

    After modification:

    bytes_out = acl.util.ptr_to_bytes(ptr, size)
    np_arr_out = np.frombuffer(bytes_out, dtype=np.int32).reshape(np_arr_in.shape)
  • To continue to use this API, ensure that the operating environment uses Python 3.8 or later and NumPy 1.22.0 or later.