Function: ptr_to_numpy

NOTE

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

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Supported
  • Atlas inference products: Supported
  • Atlas training products: Supported

Function

Obtains the memory data pointer address of the numpy.ndarray array.

If the input numpy.ndarray array is non-contiguous in the memory, convert the non-contiguous memory into contiguous one and then obtain the memory data pointer address of the converted numpy.ndarray array.

Prototype

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

Parameters

Parameter 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.
Some common types are provided below:
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 Values

Return Value Description
output NumPy type.

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.