python - 调试并行 Python 程序 (mpi4py)

标签 python debugging parallel-processing trace mpi4py

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

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

mpiexec -n 4 xterm -e "python -m pdb my_program.py"

但如果问题仅在大量进程(在我的情况下为 80 个)时出现,这会变得很麻烦。此外,使用 pdb 很容易捕获异常,但我需要查看跟踪以找出发生挂起的位置。

最佳答案

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

def my_program(*args, **kwargs):
    # insert your code here
    pass

然后用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)

现在每个进程的trace会被写入一个单独的文件trace_0001.txt等。使用ignoredirsignoremods参数来省略low水平调用。

关于python - 调试并行 Python 程序 (mpi4py),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856327/

相关文章:

.net - 为什么有时用十六进制字符填充进程镜像名称?

python - "Operation not permitted"使用 setuid() 函数删除权限时

python - 使用python发送自动生成的邮件问题

python - 尝试在 python 子进程中运行 rsync 时出现意外的远程参数错误

c++ - 调试时无效句柄异常

eclipse - 调试 Eclipse 核心表达式

matlab - MATLAB 并行计算术语之间的差异

c++ - 退出 TBB 应用程序(任务调度程序)

c - 并行程序中的 GCC 段错误

Python 2.7 Unicode 字典