调试并行Python程序(mpi4py)
问题描述:
我有一个mpi4py
程序挂起间歇。我如何追踪个别流程正在做什么?调试并行Python程序(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_0001.txt
等使用和ignoremods
论点忽略低水平电话。