调试并行Python程序(mpi4py)

tee*_*rna 5 python debugging parallel-processing trace mpi4py

我有一个mpi4py程序间歇性挂起。如何跟踪各个流程在做什么?

我可以在不同的终端上运行该程序,例如使用 pdb

mpiexec -n 4 xterm -e "python -m pdb my_program.py"
Run Code Online (Sandbox Code Playgroud)

但是,如果问题仅通过大量进程(在我的情况下为〜80)表现出来,则将变得很麻烦。另外,很容易捕获异常,pdb但是我需要查看跟踪以找出发生挂起的位置。

tee*_*rna 3

Python跟踪模块允许您跟踪程序执行情况。为了单独存储每个进程的跟踪,您需要将代码包装在一个函数中:

def my_program(*args, **kwargs):
    # insert your code here
    pass
Run Code Online (Sandbox Code Playgroud)

然后运行它trace.Trace.runfunc

import sys
import trace

# define Trace object: trace line numbers at runtime, exclude some modules
tracer = trace.Trace(
    ignoredirs=[sys.prefix, sys.exec_prefix],
    ignoremods=[
        'inspect', 'contextlib', '_bootstrap',
        '_weakrefset', 'abc', 'posixpath', 'genericpath', 'textwrap'
    ],
    trace=1,
    count=0)

# by default trace goes to stdout
# redirect to a different file for each processes
sys.stdout = open('trace_{:04d}.txt'.format(COMM_WORLD.rank), 'w')

tracer.runfunc(my_program)
Run Code Online (Sandbox Code Playgroud)

现在每个进程的跟踪将写入一个单独的文件trace_0001.txt等中。使用ignoredirsignoremods参数来省略低级调用。