mstxRangeStartA

Function Description

Marks the start position of the mstx range capability.

Function Prototype

C/C++:

mstxRangeId mstxRangeStartA(const char *message, aclrtStream stream)

Python:

mstx.range_start(message, stream)

Parameters

Table 1 Parameters

Parameter

Input/Output

Description

message

Input

message indicates the marked text, which carries the dotting information.

Data type in C/C++: const char *.

In Python, message is a character string.

The default value is None.

Length of the input message string:

  • In MSPTI scenarios, the length cannot exceed 255 bytes.
  • In scenarios other than MSPTI (such as msProf command line and Ascend PyTorch Profiler), the length cannot exceed 156 bytes.
NOTE:

message cannot be a null pointer.

stream

Input

stream indicates the thread that uses the mark.

Data type in C/C++: aclrtStream.

In Python, stream is an aclrtStream object.

The default value is None.

  • Marks only instantaneous events on the host when nullptr is configured.
  • Identifies instantaneous events on the host and device when a valid stream is configured.

Returns

If 0 is returned, the operation fails.

Calling Example

  • C/C++ calling:
    ...
    bool RunOp()
    {
    // create op desc
    ...
    const char *message = "h1";
    mstxRangeId id = mstxRangeStartA(message, NULL);
    ...
    // Run op
    if
    (!opRunner.RunOp()) {
    ERROR_LOG("Run
    op failed");
    return false;
    }
    mstxRangeEnd(id);
    ...
    }
  • Python calling method 1:

    The Python API is used to implement related API content in the C/C++ language and compile and generate .so files. The .so files can be directly referenced by Python in PYTHONPATH.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    import mstx
    mstx.range_start("aaa")
    print(1)
    mstx.range_end(1)
    import torch
    import torch_npu
    a = torch.Tensor([1,2,3,4]).npu()
    b = torch.Tensor([1,2,3,4]).npu()
    hi_str = "hi"
    hello_str = "hello"
    hi_id = mstx.range_start(hi_str, None)
    c = a + b
    hello_id = mstx.range_start(hello_str, stream=None)
    d = a - b
    mstx.range_end(hi_id)
    e = a * b
    mstx.range_end(hello_id)
    
  • Python calling method 2:

    Use Python for development, use ctypes.CDLL("libms_tools_ext.so") to reference the original .so file of mstx, and use the APIs provided in the file.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import mstx
    import torch
    import torch_npu
    import acl
    import sys
    import ctypes
    lib = ctypes.CDLL("libms_tools_ext.so")
    # Define the parameter type and return type of the function.
    lib.mstxRangeStartA.argtypes = [ctypes.c_char_p, ctypes.c_void_p]
    lib.mstxRangeStartA.restype = ctypes.c_uint64
    lib.mstxRangeEnd.argtypes = [ctypes.c_uint64]
    lib.mstxRangeEnd.restype = None
    a = torch.Tensor([1,2,3,4]).npu()
    b = torch.Tensor([1,2,3,4]).npu()
    # Create a ctypes.c_char_p pointer.
    hi_str = b"hi"
    hi_ptr = ctypes.c_char_p(hi_str)
    hi_id = ctypes.c_uint64()
    # Create a ctypes.c_char_p pointer.
    hello_str = b"hello"
    hello_ptr = ctypes.c_char_p(hello_str)
    hello_id = ctypes.c_uint64()
    # Call functions.
    hi_id.value = lib.mstxRangeStartA(hi_ptr, None)
    c = a + b
    hello_id.value = lib.mstxRangeStartA(hello_ptr, None)
    d = a - b
    lib.mstxRangeEnd(hi_id)
    e = a * b
    lib.mstxRangeEnd(hello_id)