MSPTI Usage (Python APIs)

Python APIs of MSPTI

The following types of APIs are available:

  • HcclMonitor: API for communication operator profiling.
  • KernelMonitor: API for kernel operator profiling.
  • MstxMonitor: API for mstx profiling.

Prerequisites

  • Ensure that operations in Before You Start have been completed.
  • Set the following environment variable:
    export LD_PRELOAD=${INSTALL_DIR}/cann/lib64/libmspti.so

    Replace ${INSTALL_DIR} with the CANN component directory. For example, if the installation is performed by the root user, the default file storage path is /usr/local/Ascend/cann.

Examples

The following are only examples of MSPTI APIs. For details about sample projects, see MSPTI Samples.

For details about the following APIs, see MSPTI Python API Reference.

  • HcclMonitor
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    from mspti import (
        HcclData,
        HcclMonitor
    )
    
    def HcclParser(data : HcclData):
        print(f"[Hccl Info] kind:{data.kind}, start:{data.start}, end:{data.end}, device_id:{data.device_id}, stream_id:{data.stream_id}, bandwidth:{data.bandwidth}, name:{data.name}, comm_name:{data.comm_name}")
    
    monitor = HcclMonitor()
    monitor.start(HcclParser)
    # User service logic
    ...
    monitor.stop()
    
  • KernelMonitor
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    from mspti import (
        KernelData,
        KernelMonitor
    )
    
    def KernelParser(data: KernelData):
        print(f"[Kernel Info] kernel name:{data.name}, start:{data.start}, end:{data.end}, deviceId:{data.device_id}, streamId:{data.stream_id}, type:{data.type}, correlationId:{data.correlation_id}")
    
    monitor = KernelMonitor()
    monitor.start(KernelParser)
    ...
    # Service logic
    monitor.stop()
    
  • MstxMonitor
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    from mspti import (
        MarkerData,
        MstxMonitor,
        RangeMarkerData			
    )
    
    def MarkerParser(data: MarkerData):
        print(f" [Mark Data Info] flag:{data.flag}, source_kind:{data.source_kind}, timestamp:{data.timestamp}, device_id:{data.object_id.device_id}, stream_id:{ data.object_id.stream_id}, process_id:{data.object_id.process_id}, thread_id:{data.object_id.thread_id}" )
    
    def RangeParser(data: RangeMarkerData):
    	print(f"[RangeMark Data Info] kind:{data.kind}, source_kind:{data.source_kind}, id:{data.id}, name:{data.name}, device_id:{data.object_id.device_id}, stream_id:{data.object_id.stream_id}, process_id:{data.object_id.process_id}, thread_id:{data.object_id.thread_id}, start:{data.start}, end:{data.end}")
    
    monitor = MstxMonitor()
    monitor.start(MarkerParser,RangeParser)
    ...
    # Service logic
    monitor.stop()