Skip to content

Instantly share code, notes, and snippets.

@xangma
Last active December 27, 2025 02:57
Show Gist options
  • Select an option

  • Save xangma/b2bc310e595f7abf61ff1e19f3fd63f5 to your computer and use it in GitHub Desktop.

Select an option

Save xangma/b2bc310e595f7abf61ff1e19f3fd63f5 to your computer and use it in GitHub Desktop.
debugging mpirun with vscode - launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
{
"name": "Python: mpirun launch",
"configurations": [
"mpirun script.py",
"Python Attach (local) proc 0",
"Python Attach (local) proc 1",
"Python Attach (local) proc 2",
"Python Attach (local) proc 3"
],
"stopAll": true
},
],
"configurations": [
{
"name": "Python Attach (local) proc 0",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"preLaunchTask": "sleep",
"justMyCode": false
},
{
"name": "Python Attach (local) proc 1",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5679
},
"preLaunchTask": "sleep",
"justMyCode": false
},
{
"name": "Python Attach (local) proc 2",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5680
},
"preLaunchTask": "sleep",
"justMyCode": false
},
{
"name": "Python Attach (local) proc 3",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5681
},
"preLaunchTask": "sleep",
"justMyCode": false
},
{
"name": "mpirun script.py",
"type": "cppdbg",
"request": "launch",
"program": "/home/xangma/miniconda3/envs/envname/bin/mpirun",
"cwd": "${workspaceFolder}",
"environment": [
{"name": "OMP_NUM_THREADS","value": "2"},
],
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"args": [
"-n",
"4",
"--bind-to",
"none",
"/home/xangma/miniconda3/envs/envname/bin/python",
"-m",
"mpi4py",
"/home/xangma/path/to/script.py",
],
},
]
}
import debugpy
from mpi4py import MPI as mpi
class SomeClass():
self.comm = mpi.COMM_WORLD
self.size = self.comm.Get_size()
self.rank = self.comm.Get_rank()
self.comm.Barrier()
debugpy.listen(('localhost', 5678 + self.rank))
debugpy.wait_for_client()
debugpy.breakpoint()
...
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "sleep",
"type": "shell",
"command": "sleep 2",
"windows": {
"command": "ping 127.0.0.1 -n 2 > nul"
},
"group": "none",
"presentation": {
"reveal": "silent",
"panel": "new"
}
}
]
}
@xangma
Copy link
Copy Markdown
Author

xangma commented Jul 9, 2024

I've been looking for a nice way using vscode to debug python code that's run with mpirun.

This requires a few vscode extensions (c++, python debuggers) and python packages (mpi4py, debugpy), but generally works well.

It uses a "compound debug config" (run all of these configs at once) to start the mpirun command using the c++ debugger, then starts 4 debugpy attach debuggers that each wait 2 seconds, then each connect to each mpi process

@FilLTP89
Copy link
Copy Markdown

Thanks for sharing! Any hint on how to bypass the problem with debugpy hanging when waiting for client? sometimes an error occurs

RuntimeError: Can't listen for client connections: [Errno 98] Address already in use

@mxkpp
Copy link
Copy Markdown

mxkpp commented Dec 27, 2025

This is great, thank you!

For those reading, if you get a VS Code popup error ECONNREFUSED, try a longer sleep in tasks.json.

Note that this approach also honors breakpoints set in the VS Code UI by clicking in the left margin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment