我有一个使用 mpi 的程序。要调试它,我可以使用
mpirun -np 2 xterm -e gdb myprog
。
但是,xterm 在我的机器上有问题。我想尝试 gnome-terminal 但我不知道该输入什么。我试过了:
1)
mpirun -np 2 gnome-terminal -- gdb myprog
2)
mpirun -np 2 gnome-terminal -- "gdb myprog"
3)
mpirun -np 2 gnome-terminal -- bash -c "gdb myprog"
4)
mpirun -np 2 gnome-terminal -- bash -c "gdb myprog; exec bash"
但这些似乎都不起作用; 1),3),4) 在 gdb 中的
run
之后说:
看起来 MPI_INIT 由于某种原因失败了;你的并行进程是 可能会中止。并行进程可以有很多原因 MPI_INIT 期间失败;其中一些是由于配置或环境造成的 问题。此故障似乎是内部故障;这是一些 附加信息(可能仅与开放 MPI 相关) 开发商):
ompi_mpi_init:ompi_rte_init 失败
--> 返回“(null)”(-43) 而不是“成功”(0)
------------------------------------------------- ------------------------
*** MPI_Init 发生错误
*** 在 NULL 通信器上
*** MPI_ERRORS_ARE_FATAL(此通信器中的进程现在将中止,
*** 可能还有您的 MPI 工作)
[oleg-VirtualBox:4169] MPI_INIT 完成之前本地中止已成功完成,但无法聚合错误消息,并且无法保证所有其他进程都被终止!
[下级 1(进程 4169)退出,代码为 01]
2)终端显示:
为此终端创建子进程时出错
无法执行子进程“gdb app”(没有这样的文件或目录)
顺便说一句,我使用 Ubuntu 18.04.02 LTS。
我做错了什么?
编辑:事实证明,这不是有问题的 xterm,而是带有 --tui 选项的 gdb。如果您的程序打印某些内容,无论在哪个终端,gdb 窗口都将开始错误地显示内容。
问题是gnome-terminal将请求的程序交给终端服务器,然后立即退出。 mpirun 然后发现启动的程序已退出,并销毁 MPI 运行时环境。当MPI程序真正启动时,mpirun已经退出。据我所知,没有办法让 gnome-terminal 等待给定的命令结束。
有一个解决方法:不要直接使用 mpirun 启动 gnome-terminal,而是使用两个包装脚本。第一个由 mpirun 启动。它创建一个临时文件,告诉 gnome-terminal 启动第二个包装器脚本,然后等待直到临时文件消失。第二个包装器脚本运行您实际想要运行的命令,例如
gdb myprog
,等待结束,然后删除临时文件。此时,第一个包装器注意到临时文件消失并退出。然后mpirun就可以安全地破坏MPI环境了。
从脚本本身来看,这可能更容易理解。
调试.sh:
#!/bin/bash
# This is run outside gnome-terminal by mpirun.
# Create a tmp file that we can wait on.
export MY_MPIRUN_TMP_FILE="$(mktemp)"
# Start the gnome-terminal. It will exit immediately.
# Call the wrapper script which removes the tmp file
# after the actual command has ended.
gnome-terminal -- ./helper.sh "$@"
# Wait for the file to disappear.
while [ -f "${MY_MPIRUN_TMP_FILE}" ] ; do
sleep 1
done
# Now exit, so mpirun can destroy the MPI environment
# and exit itself.
helper.sh
#!/bin/bash
# This is run by gnome-terminal.
# The command you actually want to run.
"$@"
# Remove the tmp file to show that the command has exited.
rm "${MY_MPIRUN_TMP_FILE}"
运行它作为
mpirun debug.sh gdb myproc
。
回复相当晚,但对于仍然发现此问题的人来说,一个相当简洁的解决方案是:
mpirun -np N gnome-terminal --wait -- gdb -ex run --args myprog arg1 arg2
关键是
gnome-terminal --wait
,正如它所暗示的,在终止终端之前等待子进程终止。
如果可能的话,我建议减少进程数,因为进程数很多,这种方法变得非常麻烦。