我有一个名为main_parallel.py的Python脚本,使用了mpi4py。我可以通过cli使用time来测量时间,但是如何像cProfile一样进行分析呢?我想要看到代码的每个部分调用的次数。由于cProfile只适用于串行代码,所以我不能使用它。
谢谢!
谢谢!
正如Rob Latham所说,您可以使用cProfile。您可以将每个进程的输出保存在不同的文件中。如果您想对一个函数进行分析,可以使用以下装饰器:
from mpi4py import MPI
import cProfile
def profile(filename=None, comm=MPI.COMM_WORLD):
def prof_decorator(f):
def wrap_f(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
result = f(*args, **kwargs)
pr.disable()
if filename is None:
pr.print_stats()
else:
filename_r = filename + ".{}".format(comm.rank)
pr.dump_stats(filename_r)
return result
return wrap_f
return prof_decorator
@profile(filename="profile_out")
def my_function():
# do something
为什么您不能使用cprofile?您尝试过了吗?
对于MPICH,我是这样运行的:
$ mpiexec -l -np 4 python -m cProfile ./simple-io.py doodad
--tag-output
。其他实现可能使用其他内容。% python
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pstats
>>> pstats.Stats("simple-io.cprofile").sort_stats('cumulative').print_stats()
提供了大量的 cprofile 信息... 但我的小程序太微不足道,无法提供有用信息。
mpiexec
(来自 Ubuntu 16.04 包管理器的 1.10.2 版本)似乎没有 -l
开关。没有它,我只得到一个输出文件,显然是针对排名为 0 的进程。考虑到我的安装比你的回答更新,也许你的回答已经过时了? - NichtJensmpiexec
以如此不一致的方式执行这个任务有点奇怪。我原以为它的目的是取代mpirun
,因为后者由于缺乏标准化而存在这些不一致性问题... - NichtJens
import cProfile
有什么原因吗?这样每次都会重新导入... - NichtJens