Ubuntu终端运行C语言MPI代码报错:无法连接本地mpd的解决咨询
The error you're seeing points to a problem with the MPI Process Daemon (MPD)—a component used in older MPI implementations like MPICH2—either not running at all or missing the required console mode flag. Here's how to resolve this:
Option 1: Properly Configure and Start MPD (For MPICH2)
If you're sticking with MPICH2, follow these steps to get the daemon working correctly:
- Create the MPD secret file
MPD requires a password file to operate. Create a.mpd.confin your home directory with a simple secret string (replaceyour_secretwith any value you like):
echo "secretword=your_secret" > ~/.mpd.conf chmod 600 ~/.mpd.conf
The chmod 600 ensures only your user can access this sensitive file.
- Start MPD in console mode
Use the-nflag (console mode) mentioned in the error message to launch the daemon:
mpd -n
This will create the required socket at /tmp/mpd2.console_karim that mpirun needs to connect to.
- Verify MPD is running
Check if the daemon is active with:
ps aux | grep mpd
You should see an mpd process listed in the output.
- Run your MPI program again
Now execute your compiled program as before:
mpirun -np 3 test
Option 2: Switch to OpenMPI (Modern, Daemon-Free Alternative)
MPD is deprecated in newer MPI ecosystems. OpenMPI is a more user-friendly, modern alternative that doesn't require a background daemon. Here's how to set it up:
- Install OpenMPI
If you don't have it installed already, run:
sudo apt update sudo apt install openmpi-bin libopenmpi-dev
- Recompile your code
Thempiccwrapper will now point to OpenMPI's compiler. Recompile your code:
mpicc main.c -o test
- Run the program directly
No daemon setup is needed—just execute your program:
mpirun -np 3 test
This should produce your expected output:
Greetings from process 1 ! Greetings from process 2 ! Greetings from process 3 !
Quick Note on Your Code
Your MPI code is correctly structured—it follows standard MPI initialization, rank/size checks, and message passing logic. The issue is purely with the runtime environment setup, not the code itself.
内容的提问来源于stack exchange,提问作者Kareem Ali

